
    *"hj                    2   d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	m
Z
mZ dZdhZh dZd	d
hZ G d de      ZddZddZddZd dZd!dZd"dZd#dZd$dZ	 	 	 	 d%dZef	 	 	 	 	 d&dZef	 	 	 	 	 d'dZdd	 	 	 	 	 	 	 d(dZef	 	 	 	 	 d)dZd*dZy)+zCHelpers for reading, hashing, and writing multi-file skill bundles.    )annotationsN)PathPurePosixPath)IterableMappingzSKILL.mdz	.DS_Store>   .git.mypy_cache.ruff_cache.pytest_cache__pycache__z.pycz.pyoc                      e Zd ZdZy)SkillBundleErrorz=Raised when a bundle is malformed or a bundle path is unsafe.N)__name__
__module____qualname____doc__     1/root/.hermes/SkillClaw/skillclaw/skill_bundle.pyr   r      s    Gr   r   c                    t        | t        t        f      rt        |       S t        | t              r| j	                  d      S t        dt        |       j                         )Nutf-8z!Unsupported bundle payload type: )
isinstancebytes	bytearraystrencode	TypeErrortyper   )datas    r   _coerce_bytesr       sR    $	*+T{${{7##
7T
8K8K7LM
NNr   c                   t        | xs d      j                         j                  dd      }|st        d      t	        |      j
                  }|st        d      t        d |D              rt        d|       dj                  |      S )N \/zBundle path must not be emptyc              3  $   K   | ]  }|d v  
 yw)>   ..r"   .Nr   .0parts     r   	<genexpr>z,normalize_bundle_rel_path.<locals>.<genexpr>$   s     
5t4?"
5s   zUnsafe bundle path: )r   stripreplacer   r   partsanyjoin)rel_pathvaluer.   s      r   normalize_bundle_rel_pathr3      s    B%%'//c:E>??% &&E>??

5u
55!5h\BCC88E?r   c                    t        t        |             j                  }t        d |d d D              ry|d   t        v ryt        fdt
        D              S )Nc              3  ,   K   | ]  }|t         v   y wN)_IGNORED_DIR_NAMESr(   s     r   r+   z-is_ignored_bundle_rel_path.<locals>.<genexpr>+   s     
=$4%%
=s   Tc              3  @   K   | ]  }j                  |        y wr6   )endswith)r)   suffixleafs     r   r+   z-is_ignored_bundle_rel_path.<locals>.<genexpr>0   s     Et}}V$Es   )r   r3   r.   r/   _IGNORED_NAMES_IGNORED_SUFFIXES)r1   r.   r<   s     @r   is_ignored_bundle_rel_pathr?   )   sV    3H=>DDE

=%*
==9D~E3DEEEr   c                    t        |       }|j                         si S i }t        |      D ]#  }|t        |      z  }|j                         ||<   % |S r6   )r   is_dirlist_skill_bundle_paths
read_bytes)	skill_dirrootbundler1   paths        r   read_skill_bundlerH   3   sX    	?D;;=	!F+D1 -d8n$??,x- Mr   c                   t        |       }|j                         sg S g }t        |j                  d            D ]O  }|j	                         s|j                  |      j                         }t        |      r?|j                  |       Q |S )N*)	r   rA   sortedrglobis_filerelative_toas_posixr?   append)rD   rE   pathsrG   r1   s        r   rB   rB   ?   s}    	?D;;=	Etzz#' ||~##D)224%h/X Lr   c                    i }| j                         D ]*  \  }}t        |      }t        |      rt        |      ||<   , |S r6   )itemsr3   r?   r    )bundle_filesrF   raw_rel_pathraw_datar1   s        r   coerce_skill_bundlerW   O   sO    !F"."4"4"6 3h,\:%h/(2x	3
 Mr   c                    g }t        t        |       j                               D ]O  \  }}t        |      }|j	                  |t        j                  |      j                         t        |      d       Q |S )N)rG   sha256size)	rK   rW   rS   r    rP   hashlibrY   	hexdigestlen)rT   recordsr1   rV   r   s        r   bundle_file_recordsr_   Y   sp    *,G$%8%F%L%L%NO 
(X& !...88:D		

 Nr   c                   t        j                         }t        |       D ]  }|j                  t	        |d         j                  d             |j                  d       |j                  t	        |d         j                  d             |j                  d       |j                  t	        |d         j                  d             |j                  d        |j                         S )NrG   r       rY   asciirZ      
)r[   rY   r_   updater   r   r\   )rT   digestrecords      r   bundle_tree_sha256rg   g   s    ^^F%l3 c&.)009:ec&*+227;<ec&.)009:e r   c                N    t        |       }t        |      }t        |      }|||fS r6   )rH   r_   rg   )rD   rF   r^   tree_shas       r   read_skill_bundle_with_metarj   s   s/     y)F!&)G!&)H7H$$r   c                \    t        |       }t        |      }||vrt        d|       ||   S )Nz,Skill bundle is missing required entrypoint )rW   r3   r   )rT   
entrypointrF   keys       r   bundle_entrypoint_bytesrn   |   s=     !.F
#J
/C
&!McUSTT#;r   c                8    t        | |      j                  d      S )Nr   )rn   decode)rT   rl   s     r   bundle_entrypoint_textrq      s     #<<CCGLLr   F)cleanc               Z   t        |       }|r%|j                         rt        j                  |       |j	                  dd       t        t        |      j                               D ]A  \  }}|t        |      z  }|j                  j	                  dd       |j                  |       C y )NT)parentsexist_ok)
r   existsshutilrmtreemkdirrK   rW   rS   parentwrite_bytes)rD   rT   rr   rE   r1   r   rG   s          r   write_skill_bundler|      s     	?DdJJtdJ+ !4\!B!H!H!JK $d8n$$6r   c                d    t        |       }t        |j                               t        |      hk(  S r6   )rW   setkeysr3   )rT   rl   rF   s      r   bundle_has_only_entrypointr      s-     !.Fv{{}";J"G!HHHr   c                    t        | t              r| j                         }n| }g }|D ]3  }t        t	        |            }t        |      r#|j                  |       5 t        t        |            S r6   )	r   r   r   r3   r   r?   rP   rK   r~   )rT   rQ   outr1   rr   s        r   bundle_pathsr      si    ,(!!#C )#h-8%e,

5	
 #c(r   )r   zbytes | bytearray | strreturnr   )r1   r   r   r   )r1   r   r   bool)rD   str | os.PathLike[str]r   dict[str, bytes])rD   r   r   	list[str])rT   %Mapping[str, bytes | bytearray | str]r   r   )rT   r   r   zlist[dict[str, int | str]])rT   r   r   r   )rD   r   r   z8tuple[dict[str, bytes], list[dict[str, int | str]], str])rT   r   rl   r   r   r   )rT   r   rl   r   r   r   )rD   r   rT   r   rr   r   r   None)rT   r   rl   r   r   r   )rT   z5Mapping[str, bytes | bytearray | str] | Iterable[str]r   r   ) r   
__future__r   r[   osrw   pathlibr   r   typingr   r   _BUNDLE_ENTRYPOINTr=   r7   r>   
ValueErrorr   r    r3   r?   rH   rB   rW   r_   rg   rj   rn   rq   r|   r   r   r   r   r   <module>r      s,   I "  	  ' $ [ V$ Hz HO	F	 	%%%=% )7  )M7MM 	M 	%7 	
 
& )I7II 
Ir   