
    \j                     Z   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  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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   )loggingz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     N/root/.hermes/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_jsonG   sA    fOxx-XXj)XXj)
 	
r   )__name__
__module____qualname____doc__int__annotations__strr   r   r   dictr   classmethodr    r   r   r	   r	   4   sh    h
IL!Jd
!HcDjHcDj  
T 
&6 
 
r   r	   tree_cache_foldercommit_hashr   c                 J    t         j                  j                  | d| d      S )Ntreesz.jsonospathjoin)r%   r&   s     r   _tree_cache_pathr-   R   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huggingfacer)   )r.   s    r   tree_cache_folder_for_local_dirr1   V   s    77<<	8];;r   c                     t        | |      }t        5  |t        v rt        |   cddd       S 	 ddd       t        |      }|t        5  |t        |<   ddd       |S |S # 1 sw Y   2xY w# 1 sw Y   |S xY w)zZReturn the cached tree listing for a commit hash, or `None` if not cached (or unreadable).N)r-   _IN_MEMORY_TREE_CACHE_LOCKr   _read_tree_cache_from_disk)r%   r&   r+   entriess       r   read_tree_cacher6   [   s    -{;D	# /(((./ /(/ ).G' 	2*1!$'	2N7N/ /
	2Ns   A 

A, A),A6r+   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)r+   fdata	file_pathr   es         r   r4   r4   h   s    $) 	 Q99Q<D	 88$%)BBQUV]Q^QdQdQfgoi	>33D99gg	  	 
 h Z95 <TF"QCHIsE   B B  B B  B<B  B	B 	CC5CCr5   c                    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)zMWrite the tree listing of a commit hash to the cache (ignoring any failures).)r;   r<   T)exist_okz.tmp)dirsuffixwr8   r9   r   )indentNz,Ignored error while writing tree cache file r=   )r-   rA   sortedr   r*   makedirsr+   dirnametempfilemkstempfdopenr?   dumpreplacerD   rH   rI   r3   r"   r   )
r%   r&   r5   r+   rL   rK   tmp_fdtmp_pathrJ   rM   s
             r   write_tree_cacher^   w   s*   -{;D3KQRY?[i)WY/7799[D
BGGOOD)D9#++0EfUYYvsW5 	)IIdAa(	)


8T" 
$ 4&*7md#4 4 \
	) 	)  EdV2aSQR
4 4sB   D BD- D!D-  E!D*&D- -	E6EEE')r   r?   r*   rW   	threadingdataclassesr   utilsr   
get_loggerr   rH   rA   r   r"   r!   r    Lockr3   r	   r-   r1   r6   r4   r^   r$   r   r   <module>rd      s)  (  	   !  
		H	%  AC tC!<<= B+Y^^-  $
 
 
:K K# K# K<s <s <

s 
 
c>FYAZ]aAa 
S T#~2E-F-M 4 4# 4SR`M`Ha 4fj 4r   