
    9iJ                        S r SSKrSSKJr  SSKJr  SSKJrJrJ	r	J
r
JrJrJr  SSKJrJr  SSKJr  SSKJrJrJr  SS	KJrJr  SS
KJr  SSKJr  \" 5       rSS/r\" SS9 " S S5      5       r \" SS9 " S S5      5       r!\" SS9 " S S5      5       r"\" SS9 " S S5      5       r# S"S\\\$\4      S\#4S jjr%S\S\&4S jr'S#S\S\$S\&4S jjr(S \S\$S\\"   4S! jr)g)$z<Contains utilities to manage the ModelScope cache directory.    N)	dataclass)Path)Dict	FrozenSetListLiteralOptionalSetUnion)CacheNotFoundCorruptedCacheException)ModelFileSystemCache)convert_readable_sizeformat_timesincetabulate)REPO_TYPE_DATASETREPO_TYPE_MODEL)get_modelscope_cache_dir)
get_loggerz	.DS_Storez
._____tempT)frozenc                       \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   \\S'   \	\S'   \	\S	'   \
S
\4S j5       r\
S
\4S j5       r\
S
\4S j5       rSrg)CachedFileInfo   a  Frozen data structure holding information about a single cached file.

Args:
    file_name (`str`):
        Name of the file. Example: `config.json`.
    file_path (`Path`):
        Path of the file in the `snapshots` directory. The file path is a symlink
        referring to a blob in the `blobs` folder.
    blob_path (`Path`):
        Path of the blob file. This is equivalent to `file_path.resolve()`.
    size_on_disk (`int`):
        Size of the blob file in bytes.
    blob_last_accessed (`float`):
        Timestamp of the last time the blob file has been accessed (from any
        revision).
    blob_last_modified (`float`):
        Timestamp of the last time the blob file has been modified/created.
	file_name	file_pathfile_revision_hash	blob_pathsize_on_diskblob_last_accessedblob_last_modifiedreturnc                 ,    [        U R                  5      $ )z
(property) Timestamp of the last time the blob file has been accessed (from any
revision), returned as a human-readable string.

Example: "2 weeks ago".
)r   r   selfs    \/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/hub/cache_manager.pyblob_last_accessed_str%CachedFileInfo.blob_last_accessed_str4          7 788    c                 ,    [        U R                  5      $ )z
(property) Timestamp of the last time the blob file has been modified, returned
as a human-readable string.

Example: "2 weeks ago".
)r   r    r#   s    r%   blob_last_modified_str%CachedFileInfo.blob_last_modified_str>   r(   r)   c                 ,    [        U R                  5      $ )zQ
(property) Size of the blob file as a human-readable string.

Example: "42.2K".
r   r   r#   s    r%   size_on_disk_strCachedFileInfo.size_on_disk_strH        %T%6%677r)    N)__name__
__module____qualname____firstlineno____doc__str__annotations__r   intfloatpropertyr&   r+   r/   __static_attributes__r2   r)   r%   r   r      s    & NOO9 9 9 9 9 9 8# 8 8r)   r   c                       \ rS rSr% Sr\\S'   \\S'   \\S'   \	\
   \S'   \\S'   \S\4S	 j5       r\S\4S
 j5       r\S\4S j5       rSrg)CachedRevisionInfoR   a  Frozen data structure holding information about a revision.

Args:
    commit_hash (`str`):
        Hash of the revision (unique).
        Example: `"9338f7b671827df886678df2bdd7cc7b4f36dffd"`.
    snapshot_path (`Path`):
        Path to the revision directory in the `snapshots` folder. It contains the
        exact tree structure as the repo on the Hub.
    files: (`FrozenSet[CachedFileInfo]`):
        Set of [`~CachedFileInfo`] describing all files contained in the snapshot.
    size_on_disk (`int`):
        Sum of the blob file sizes that are symlink-ed by the revision.
    last_modified (`float`):
        Timestamp of the last time the revision has been created/modified.
commit_hashsnapshot_pathr   fileslast_modifiedr!   c                 ,    [        U R                  5      $ )z
(property) Timestamp of the last time the revision has been modified, returned
as a human-readable string.

Example: "2 weeks ago".
r   rD   r#   s    r%   last_modified_str$CachedRevisionInfo.last_modified_strl          2 233r)   c                 ,    [        U R                  5      $ zV
(property) Sum of the blob file sizes as a human-readable string.

Example: "42.2K".
r.   r#   s    r%   r/   #CachedRevisionInfo.size_on_disk_strv   r1   r)   c                 ,    [        U R                  5      $ )z3
(property) Total number of files in the revision.
)lenrC   r#   s    r%   nb_filesCachedRevisionInfo.nb_files   s    
 4::r)   r2   N)r3   r4   r5   r6   r7   r8   r9   r   r:   r   r   r;   r<   rG   r/   rO   r=   r2   r)   r%   r?   r?   R   s{    " ^$$43 4 4 8# 8 8 #  r)   r?   c                       \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   \\S'   \	\
   \S'   \\S	'   \\S
'   \S\4S j5       r\S\4S j5       r\S\4S j5       rSrg)CachedRepoInfo   a>  Frozen data structure holding information about a cached repository.

Args:
    repo_id (`str`):
        Repo id of the repo on the Hub. Example: `"damo/bert-base-chinese"`.
    repo_type (`Literal["dataset", "model"]`):
        Type of the cached repo.
    repo_path (`Path`):
        Local path to the cached repo.
    size_on_disk (`int`):
        Sum of the blob file sizes in the cached repo.
    nb_files (`int`):
        Total number of blob files in the cached repo.
    revisions (`FrozenSet[CachedRevisionInfo]`):
        Set of [`~CachedRevisionInfo`] describing all revisions cached in the repo.
    last_accessed (`float`):
        Timestamp of the last time a blob file of the repo has been accessed.
    last_modified (`float`):
        Timestamp of the last time a blob file of the repo has been modified/created.
repo_id	repo_type	repo_pathr   rO   	revisionslast_accessedrD   r!   c                 ,    [        U R                  5      $ )z
(property) Last time a blob file of the repo has been accessed, returned as a
human-readable string.

Example: "2 weeks ago".
)r   rX   r#   s    r%   last_accessed_str CachedRepoInfo.last_accessed_str   rI   r)   c                 ,    [        U R                  5      $ )z
(property) Last time a blob file of the repo has been modified, returned as a
human-readable string.

Example: "2 weeks ago".
rF   r#   s    r%   rG    CachedRepoInfo.last_modified_str   rI   r)   c                 ,    [        U R                  5      $ rK   r.   r#   s    r%   r/   CachedRepoInfo.size_on_disk_str   r1   r)   r2   N)r3   r4   r5   r6   r7   r8   r9   r   r:   r   r?   r;   r<   rZ   rG   r/   r=   r2   r)   r%   rR   rR      s    * LNOM+,,43 4 4 43 4 4 8# 8 8r)   rR   c                   j    \ rS rSr% Sr\\S'   \\   \S'   \	\
   \S'   \S\4S j5       rS\4S jrS	rg
)ModelScopeCacheInfo   a  Frozen data structure holding information about the entire cache-system.

This data structure is returned by [`scan_cache_dir`] and is immutable.

Args:
    size_on_disk (`int`):
        Sum of all valid repo sizes in the cache-system.
    repos (`FrozenSet[CachedRepoInfo]`):
        Set of [`~CachedRepoInfo`] describing all valid cached repos found on the
        cache-system while scanning.
    warnings (`List[CorruptedCacheException]`):
        List of [`~CorruptedCacheException`] that occurred while scanning the cache.
        Those exceptions are captured so that the scan can continue. Corrupted repos
        are skipped from the scan.
r   reposwarningsr!   c                 ,    [        U R                  5      $ )zX
(property) Sum of all valid repo sizes in the cache-system as a human-readable
string.
r.   r#   s    r%   r/   $ModelScopeCacheInfo.size_on_disk_str   s     %T%6%677r)   c           	          S[         S[        S[        [           4S jn/ SQn[	        U R
                  S S9 VVs/ s H(  n[	        UR                  S S9  H  nU" X45      PM     M*     nnn[        UUS	9$ s  snnf )
a  Generate a detailed table from the [`ModelScopeCacheInfo`] object.

Returns a table with a row per repo and revision (thus multiple rows can appear for a single repo), with columns
"repo_id", "repo_type", "revision", "size_on_disk", "nb_files", "last_modified", "local_path".

Example:
```py
>>> from modelscope.hub.cache_manager import scan_cache_dir

>>> ms_cache_info = scan_cache_dir()
ModelScopeCacheInfo(...)

>>> print(ms_cache_info.export_as_table())
REPO ID                REPO TYPE REVISION   SIZE ON DISK NB FILES LAST_MODIFIED LOCAL PATH
---------------------- --------- ---------- ------------ -------- -------------  -------------------------------------------------------------
damo/bert-base-chinese model     master             2.7M        5 1 week ago     ~/.cache/modelscope/hub/models--damo--bert-base-chinese/...
damo/structured-bert   model     master             8.8K        1 1 week ago     ~/.cache/modelscope/hub/models--damo--structured-bert/...
damo/t5-base           model     master           893.8M        4 7 months ago   ~/.cache/modelscope/hub/models--damo--t5-base/...
```

Returns:
    `str`: The table as a string.
reporevisionr!   c           
          U R                   U R                  UR                  SR                  U R                  5      U R
                  U R                  U R                  [        U R                  5      /$ )zHFormat a single repo and revision into a list of strings for tabulation.z{:>12})
rT   rU   rA   formatr/   rO   rZ   rG   r8   rV   )rh   ri   s     r%   format_repo_revisionAModelScopeCacheInfo.export_as_table.<locals>.format_repo_revision   s\     $$ 5 56&&&&DNN#	 	r)   )zREPO IDz	REPO TYPEREVISIONzSIZE ON DISKzNB FILESLAST_ACCESSEDLAST_MODIFIEDz
LOCAL PATHc                     U R                   $ N)rT   )rh   s    r%   <lambda>5ModelScopeCacheInfo.export_as_table.<locals>.<lambda>  s    DLLr)   )keyc                     U R                   $ rr   )rA   )ri   s    r%   rs   rt     s
    X5I5Ir)   )rowsheaders)rR   r?   r   r8   sortedrc   rW   r   )r$   rl   column_headersrh   ri   
table_datas         r%   export_as_table#ModelScopeCacheInfo.export_as_table   s    2	~ 	+=	BFs)		
 tzz/HI
I"$IK !0K 1I 	 
 "
 	

s   /A7r2   N)r3   r4   r5   r6   r7   r:   r9   r   rR   r   r   r<   r8   r/   r|   r=   r2   r)   r%   ra   ra      sK      ^$$*++8# 8 8<
 <
r)   ra   	cache_dirr!   c                 B   U c
  [        5       n [        U 5      R                  5       R                  5       n U R	                  5       (       d  [        SU  S3U S9eU R                  5       (       a  [        SU  S35      e[        5       n/ nU S-  nU S-  nUR	                  5       (       aG  UR                  5       (       a2  [        U[        S9u  pVUR                  U5        UR                  U5        UR	                  5       (       aG  UR                  5       (       a2  [        U[        S9u  pxUR                  U5        UR                  U5        [        U [        SS	9u  pUR                  U	5        UR                  U
5        [        [!        U5      [#        S
 U 5       5      US9$ )a  Scan the entire ModelScope cache-system and return a [`ModelScopeCacheInfo`] structure.

Use `scan_cache_dir` to programmatically scan your cache-system. The cache
will be scanned repo by repo. If a repo is corrupted, a [`~CorruptedCacheException`]
will be thrown internally but captured and returned in the [`~ModelScopeCacheInfo`]
structure. Only valid repos get a proper report.

```py
>>> from modelscope.hub.utils import scan_cache_dir

>>> ms_cache_info = scan_cache_dir()
ModelScopeCacheInfo(
    size_on_disk=3398085269,
    repos=frozenset({
        CachedRepoInfo(
            repo_id='damo/t5-small',
            repo_type='model',
            repo_path=PosixPath(...),
            size_on_disk=970726914,
            nb_files=11,
            revisions=frozenset({
                CachedRevisionInfo(
                    commit_hash='master',
                    size_on_disk=970726339,
                    snapshot_path=PosixPath(...),
                    files=frozenset({
                        CachedFileInfo(
                            file_name='config.json',
                            size_on_disk=1197
                            file_path=PosixPath(...),
                            blob_path=PosixPath(...),
                        ),
                        CachedFileInfo(...),
                        ...
                    }),
                ),
                CachedRevisionInfo(...),
                ...
            }),
        ),
        CachedRepoInfo(...),
        ...
    }),
    warnings=[
        CorruptedCacheException("Snapshots dir doesn't exist in cached repo: ..."),
        CorruptedCacheException(...),
        ...
    ],
)
```

Args:
    cache_dir (`str` or `Path`, `optional`):
        Cache directory to scan. Defaults to the default ModelScope cache directory.

Raises:
    `CacheNotFound`: If the cache directory does not exist.
    `ValueError`: If the cache directory is a file, instead of a directory.

Returns: a [`ModelScopeCacheInfo`] object.
zCache directory not found: zQ. Please use `cache_dir` argument or set `MODELSCOPE_CACHE` environment variable.)r~   z1Scan cache expects a directory but found a file: modelsdatasetsrU   T)rU   inplacec              3   8   #    U  H  oR                   v   M     g 7frr   )r   ).0rh   s     r%   	<genexpr>!scan_cache_dir.<locals>.<genexpr>  s     =ut**u   )rc   r   rd   )r   r   
expanduserresolveexistsr   is_file
ValueErrorsetis_dir	_scan_dirr   updateextendr   ra   	frozensetsum)r~   rc   rd   	model_dirdataset_dirmodel_reposmodel_warningsdataset_reposdataset_warningsother_reposother_warningss              r%   scan_cache_dirr   #  s   ~ ,.	Y**,446I))  5F  G
 	

 ?	{  K\  ]
 	
 "%E.0H H$Ij(K i..00&/'2#[!'  2 2 4 4*3#4+6']#() #,_d#<K	LLOON#=u== r)   dirc                     U R                  5       (       d  gU R                  5       (       d  gU R                  5       (       a  gU R                  [        ;   a  gg)z+Check if a directory is valid for scanning.FT)r   r   
is_symlinknameFILES_TO_IGNORE)r   s    r%   _is_valid_dirr     s@    ::<<::<<
~~
xx?"r)   rU   r   c                    [        5       n/ nU R                  5        Hw  nU(       a  UR                  S;   a  M  [        U5      (       d  M.  UR                  5        H5  n[        U5      (       d  M   [	        XaS9nUb  UR                  U5        M5  M7     My     X44$ ! [         a  nUR                  U5         SnAMa  SnAff = f)zWScan a directory for cached repos and return a set of [`~CachedRepoInfo`] and warnings.)r   r   hubr   N)r   iterdirr   r   _scan_cached_repoaddr   append)	r   rU   r   rc   rd   	owner_dirname_dirinfoes	            r%   r   r     s    EEH[[]	y~~)FFY''!))+H **#(G#IIdO $ , #  ? + #""#s   1B
C%B<<CrV   c                   ^ U R                  5       (       d  [        SU  35      e [        [        U 5      5      nUR                  nUR
                  nUR                  5       R                  SS5      nUS:X  a  g 0 m[        5       nU H  n[        R                  R                  XS   5      n	UR                  SS	5      n
[        R                  R                  U	5      (       d  M]  [        U	5      nUR!                  5       TU'   UR#                  [%        [        R                  R'                  US   5      UU
TU   R(                  UTU   R*                  TU   R,                  S
95        M     SnU(       a,  SU;   a&  UR/                  S5      S   R/                  S5      S   nU(       a  [1        U4S jU 5       5      nOU R!                  5       R,                  n[3        U[5        U5      [7        U4S jU 5       5      U US9nT(       aA  [1        S TR9                  5        5       5      n[1        S TR9                  5        5       5      nO(U R!                  5       nUR*                  nUR,                  n[;        [=        T5      UU U[5        U/5      [7        S TR9                  5        5       5      UUS9$ ! [         a  n[        SU 35      eSnAff = f)z~Scan a single cache repo and return information about it.

Any unexpected behavior will raise a [`~CorruptedCacheException`].
zRepo path is not a directory: ___.unknownNz"Failed to load cache information: r   Revision )r   r   r   r   r   r   r    masterz	Revision:   ,r   c              3   V   >#    U  H  nTUR                      R                  v   M      g 7frr   )r   st_mtimer   file
blob_statss     r%   r   $_scan_cached_repo.<locals>.<genexpr>  s(      %D1B &0%?%H%H1B   &)c              3   V   >#    U  H  nTUR                      R                  v   M      g 7frr   )r   st_sizer   s     r%   r   r     s'      8%6T $DNN3;;%6r   )rA   rC   r   rB   rD   c              3   8   #    U  H  oR                   v   M     g 7frr   )st_atimer   stats     r%   r   r           O;N4;Nr   c              3   8   #    U  H  oR                   v   M     g 7frr   )r   r   s     r%   r   r     r   r   c              3   8   #    U  H  oR                   v   M     g 7frr   )r   r   s     r%   r   r     s     F2E$2Er   )rO   rT   rV   rU   rW   r   rX   rD   )r   r   r   r8   cached_filescached_model_revisionget_model_idreplace	Exceptionr   ospathjoingetr   r   r   r   r   basenamer   r   r   splitmaxr?   r   r   valuesrR   rN   )rV   rU   cacher   r   rT   r   cached_files_infocached_filer   r   r   revision_hashrevision_last_modifiedcached_revisionrepo_last_accessedrepo_last_modified
repo_statsr   s                     @r%   r   r     s    %,YK8: 	:	P$S^4)) % ; ;$$&..uc:i   J $GGLL,?@	(__Z<ww~~i((O	 ) 0
9 	''**;v+>?##5'	2::##-i#8#A#A#-i#8#A#A		 $, M//177DQGMMM !$ %D1B%D "D "+!1!:!: )!)* 8%68 8,O   O:;L;L;N OO  O:;L;L;N OO^^%
'00'00 Z_-.F*2C2C2EFF((	 	A  P%(J1#&NOOPs   AJ2 2
K<K

Krr   )F)*r7   r   dataclassesr   pathlibr   typingr   r   r   r   r	   r
   r   modelscope.hub.errorsr   r   modelscope.hub.utils.cachingr   modelscope.hub.utils.utilsr   r   r   modelscope.utils.constantr   r   modelscope.utils.file_utilsr   modelscope.utils.loggerr   loggerr   r   r?   rR   ra   r8   r   boolr   r   r   r2   r)   r%   <module>r      sE   B 	 !  G G G H =D D H @ .	 - $88 88 88v $1 1 1h $;8 ;8 ;8| $Y
 Y
 Y
z 15pE#t),-p9Lpf
t 
 
4 C $ .\ \!$\)1.)A\r)   