
    j                        U d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
  ej                  e      ZdZi Zeedf   ed<    ej&                         Z ed	
       G d d             Zdeeef   defdZdededefdZdedefdZdededeeef   dz  fdZdedeeef   dz  fdZdededeeef   ddfdZy)a5  On-disk cache for repository tree listings.

A tree listing is the set of files (with their download metadata) contained in a repo at a given commit. Because a
commit hash is immutable, its tree listing never changes and can be cached forever without any invalidation logic.

The listing is stored as a human-readable JSON file under `<tree_cache_folder>/trees/<commit_hash>.json`. The
folder depends on the download target: the per-repo `storage_folder` for `cache_dir` downloads, or
`local_dir/.cache/huggingface/` for `local_dir` downloads (see `tree_cache_folder_for_local_dir`).

```json
{
  "format_version": 1,
  "files": {
    "config.json": {"size": 519, "blob_id": "<git sha1>"},
    "model.safetensors": {"size": 1234, "blob_id": "...", "lfs_sha256": "<sha256>", "lfs_size": 1234, "xet_hash": "..."}
  }
}
```
    N)	dataclass   )logging)is_valid_xet_hashzdict[str, TreeCacheEntry]_IN_MEMORY_TREE_CACHET)frozenc                       e Zd ZU dZeed<   eed<   dZedz  ed<   dZedz  ed<   dZ	edz  ed<   de
fd	Zed
e
dd fd       Zy)TreeCacheEntryz^Raw metadata of a single file in a cached tree listing, mirroring the `/tree` endpoint fields.sizeblob_idN
lfs_sha256lfs_sizexet_hashreturnc                     | j                   | j                  d}| j                  | j                  |d<   | j                  |d<   | j                  | j                  |d<   |S )N)r   r   r   r   r   r   r   r   r   r   )selfinfos     /home/vela/workspace/domain/tn-silver/web/tn-site/.runtime/depth-eval/venv/lib/python3.12/site-packages/huggingface_hub/_tree_cache.pyto_jsonzTreeCacheEntry.to_json?   sW    "iiDLLA??&!%D#}}D==$#}}D    r   c           	           | |d   |d   |j                  d      |j                  d      |j                  d            S )Nr   r   r   r   r   r   )get)clsr   s     r   	from_jsonzTreeCacheEntry.from_jsonH   sA    fOxx-XXj)XXj)
 	
r   )__name__
__module____qualname____doc__int__annotations__strr   r   r   dictr   classmethodr    r   r   r
   r
   5   sh    h
IL!Jd
!HcDjHcDj  
T 
&6 
 
r   r
   entriesr   c                 B    t        d | j                         D              S )z<Return whether all Xet hashes in the tree listing are valid.c              3   f   K   | ])  }|j                   d u xs t        |j                          + y w)N)r   r   ).0entrys     r   	<genexpr>z(is_valid_tree_entries.<locals>.<genexpr>U   s.     iXhuu~~%J):5>>)JJXhs   /1)allvalues)r&   s    r   is_valid_tree_entriesr.   S   s    iX_XfXfXhiiir   tree_cache_foldercommit_hashc                 J    t         j                  j                  | d| d      S )Ntreesz.jsonospathjoin)r/   r0   s     r   _tree_cache_pathr7   X   s!    77<<)7{m54IJJr   	local_dirc                 D    t         j                  j                  | dd      S )zGFolder under which the `trees/` cache lives for a `local_dir` download.z.cachehuggingfacer3   )r8   s    r   tree_cache_folder_for_local_dirr;   \   s    77<<	8];;r   c                 $   t        | |      }t        5  |t        v r!t        |   }t        |      r|ndcddd       S 	 ddd       t	        |      }|t        |      syt        5  |t        |<   ddd       |S # 1 sw Y   <xY w# 1 sw Y   |S xY w)zbReturn the cached tree listing for a commit hash, or `None` if not cached, invalid, or unreadable.N)r7   _IN_MEMORY_TREE_CACHE_LOCKr   r.   _read_tree_cache_from_disk)r/   r0   r5   cached_entriesr&   s        r   read_tree_cacher@   a   s    -{;D	#((248N%:>%J>PT 
$	#( 
$ ).G3G<	#&-d# 
$N 
$	# 
$Ns    A9%
B9BBr5   c                    	 t        | d      5 }t        j                  |      }d d d        j                  d      t        k7  ry |d   j                         D ci c]  \  }}|t        j                  |       c}}S # 1 sw Y   \xY wc c}}w # t        $ r Y y t        t        t        t        f$ r%}t        j                  d|  d|        Y d }~y d }~ww xY w)Nutf-8encodingformat_versionfilesz#Ignoring corrupted tree cache file : )openjsonloadr   TREE_CACHE_FORMAT_VERSIONitemsr
   r   FileNotFoundErrorOSError
ValueErrorKeyError	TypeErrorloggerwarning)r5   fdata	file_pathr   es         r   r>   r>   p   s    $)Q99Q<D *88$%)BBQUV]Q^QdQdQfgQfoi	>33D99Qfgg *)
 h Z95 <TF"QCHIsE   B B  B B  B<B  B	B 	CC5CCc                    t        |      syt        | |      }t        t        |      D ci c]  }|||   j	                          c}d}	 t        j                  t
        j                  j                  |      d       t        j                  t
        j                  j                  |      d      \  }}t        j                  |dd	      5 }t        j                  ||d
       ddd       t        j                  ||       t$        5  t'        |      t(        |<   ddd       yc c}w # 1 sw Y   FxY w# t        $ r%}	t         j#                  d| d|	        Y d}	~	yd}	~	ww xY w# 1 sw Y   yxY w)zTWrite a valid tree listing to the cache (ignoring invalid entries and any failures).N)rE   rF   T)exist_okz.tmp)dirsuffixwrB   rC   r   )indentz,Ignored error while writing tree cache file rG   )r.   r7   rK   sortedr   r4   makedirsr5   dirnametempfilemkstempfdopenrI   dumpreplacerN   rR   rS   r=   r#   r   )
r/   r0   r&   r5   rV   rU   tmp_fdtmp_pathrT   rW   s
             r   write_tree_cacherh      s)    )-{;D3KQRY?[?i)WY/7799?[D
BGGOOD)D9#++0EfUYYvsW5IIdAa( 6


8T" 
$&*7md# 
$	# \
 65  EdV2aSQR
 
$	#sB   D(BD9 D-(D9 E*-D62D9 9	E'E""E'*E3)r   rI   r4   ra   	threadingdataclassesr   utilsr   
utils._xetr   
get_loggerr   rR   rK   r   r#   r"   r!   Lockr=   r
   boolr.   r7   r;   r@   r>   rh   r%   r   r   <module>rp      sL  (  	   !  ) 
		H	%  AC tC!<<= B+Y^^-  $
 
 
:j4^(;#< j j
K K# K# K<s <s <
s  c>FYAZ]aAa S T#~2E-F-M 4 4# 4SR`M`Ha 4fj 4r   