
    9i!2                        S SK r S SKrS SKrS SKrS SKrS SKJrJr  S SKJ	r	  S SK
JrJr  S SKJr  S SKJr  \" 5       r\R$                  " \S5      R'                  5       R)                  5       S:H  r  " S	 S
\5      r " S S\5      rg)    N)movermtree)Dict)	FILE_HASH)MODELSCOPE_ENABLE_DEFAULT_HASH_VALIDATION)compute_hash)
get_loggerFalsetruec                   j    \ rS rSrSrSrSrSr S\4S jr	S r
S	 rS
 rS rS rS rS rS rS rSrg)FileSystemCache   z.mscz.mdlidz.mvcache_root_locationc                     [         R                  " 5       U l        [        R                  " USS9  Xl        U R                  5         g)zBase file system cache interface.

Args:
    cache_root_location (str): The root location to store files.
    kwargs(dict): The keyword arguments.
Texist_okN)	threadingRLock_cache_lockosmakedirsr   
load_cache)selfr   kwargss      \/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/hub/utils/caching.py__init__FileSystemCache.__init__    s2     %??,
'$7#6     c                     U R                   $ N)r   r   s    r   get_root_location!FileSystemCache.get_root_location1   s    '''r   c                 N   / U l         [        R                  R                  U R                  [
        R                  5      n[        R                  R                  U5      (       a1  [        US5       n[        R                  " U5      U l         S S S 5        g g ! , (       d  f       g = f)Nrb)cached_filesr   pathjoinr   r   KEY_FILE_NAMEexistsopenpickleload)r   cache_keys_file_pathfs      r   r   FileSystemCache.load_cache4   st    !ww||D,D,D,;,I,I K77>>.//*D1Q$*KKN! 21 011s   0B
B$c                    U R                      [        R                  R                  U R                  [
        R                  5      n[        R                  " SU R                  S9u  p# [        R                  " US5       n[        R                  " U R                  U5        SSS5        [        X15         SSS5        g! , (       d  f       N#= f! [         ad     [        R                  " U5        O! [          a     Of = f[        R                  R#                  U5      (       a  [        R$                  " U5        e f = f! , (       d  f       g= f)z
Save cache metadata in order to verify that the cached content is consistent with the remote content.

Example of the cached content:
    [{'Path': 'configuration.json', 'Revision': 'f01dxxx'}, {'Path': 'model.bin', 'Revision': '1159xxx'}, ...]
z.tmp)suffixdirwbN)r   r   r(   r)   r   r   r*   tempfilemkstempfdopenr-   dumpr'   r   	ExceptioncloseOSErrorr+   unlink)r   r/   fdtemp_filenamer0   s        r   save_cached_files!FileSystemCache.save_cached_files<   s     #%77<<0H0H0?0M0M$O  ( 0 04#;#;!=BYYr4(AKK 1 115 )]9  )(  HHRL 77>>-00IIm, sZ   AE (C?"B>!C>
C	C
D=C10D=1
C>;D==C>>?D==E  
Ec                     g)zCheck the key is in the cache, if exist, return the file, otherwise return None.

Args:
    key(str): The cache key.

Raises:
    None
N r   keys     r   get_fileFileSystemCache.get_fileV   s     	r   c                     g)zPut file to the cache.

Args:
    key (str): The cache key
    location (str): Location of the file, we will move the file to cache.

Raises:
    None
NrC   )r   rE   locations      r   put_fileFileSystemCache.put_filea   s     	r   c                 z    XR                   ;   a,  U R                   R                  U5        U R                  5         gg)z^Remove cache key in index, The file is removed manually

Args:
    key (dict): The cache key.
N)r'   remover@   rD   s     r   
remove_keyFileSystemCache.remove_keym   s5     ###$$S)""$ $r   c                 8    U R                    H
  nX!:X  d  M
    g   g)NTF)r'   )r   rE   
cache_files      r   r+   FileSystemCache.existsw   s     ++J  , r   c                 N    [        U R                  5        U R                  5         g)zRemove all files and metadata from the cache
In the case of multiple cache locations, this clears only the last one,
which is assumed to be the read/write one.
N)r   r   r   r"   s    r   clear_cacheFileSystemCache.clear_cache~   s    
 	t''(r   c                 f    [         R                  " UR                  5       5      R                  5       $ r!   )hashlibsha256encode	hexdigestrD   s     r   	hash_nameFileSystemCache.hash_name   s     ~~cjjl+5577r   )r   r   r'   N)__name__
__module____qualname____firstlineno__r*   MODEL_META_FILE_NAMEMODEL_META_MODEL_IDMODEL_VERSION_FILE_NAMEstrr   r#   r   r@   rF   rJ   rN   r+   rT   r[   __static_attributes__rC   r   r   r   r      sX    M!# "(34	
%8r   r   c                      ^  \ rS rSrSrSU 4S jjrS rS rS\4S jr	S r
S	 rS
 rS rS rS rS rS rS rSrU =r$ )ModelFileSystemCache   zLocal cache file layout
cache_root/owner/model_name/individual cached files and cache index file '.mcs'
Save only one version for each file.
c                 l  > Ub  Uc=  [         TU ]  [        R                  R	                  U5      5        U R                  5         O[[         TU ]  [        R                  R	                  XU5      5        [        R                  U< SU< 30U l        U R                  5         U R                  5       U l        g)a  Put file to the cache
Args:
    cache_root(`str`): The modelscope local cache root(default: ~/.cache/modelscope/hub)
    owner(`str`): The model owner.
    name('str'): The name of the model
Returns:
Raises:
    None
<Tip>
    model_id = {owner}/{name}
</Tip>
N/)superr   r   r(   r)   load_model_metar   rb   
model_metasave_model_metaload_model_versioncached_model_revision)r   
cache_rootownername	__class__s       r   r   ModelFileSystemCache.__init__   s     =DLGRWW\\*56  "GRWW\\*TBC33t5LDO   "%)%<%<%>"r   c                 n   [         R                  R                  U R                  [        R
                  5      n[         R                  R                  U5      (       a1  [        US5       n[        R                  " U5      U l
        S S S 5        g [        R                  S0U l
        g ! , (       d  f       g = f)Nr&   unknown)r   r(   r)   r   r   ra   r+   r,   r-   r.   rm   rb   r   meta_file_pathr0   s      r   rl   $ModelFileSystemCache.load_model_meta   sy    d&>&>&5&J&JL77>>.))nd+q"(++a. ,+  /BBINDO ,+s   )B&&
B4c                 F   [         R                  R                  U R                  [        R
                  5      n[         R                  R                  U5      (       a4  [        US5       nUR                  5       R                  5       sS S S 5        $ g ! , (       d  f       g = f)Nr)
r   r(   r)   r   r   rc   r+   r,   readstrip)r   model_version_file_pathr0   s      r   ro   'ModelFileSystemCache.load_model_version   sn    "$'',,$$o&M&M#O77>>122-s3qvvx~~' 43  43s   )B
B revision_infoc                 P   [         R                  R                  U R                  [        R
                  5      n[        US5       n[        U[        5      (       a"  SUS   < SUS   < 3nUR                  U5        OUR                  U5        S S S 5        g ! , (       d  f       g = f)Nwz	Revision:Revisionz,CreatedAt:	CreatedAt)
r   r(   r)   r   r   rc   r,   
isinstancedictwrite)r   r   r   r0   version_info_strs        r   save_model_version'ModelFileSystemCache.save_model_version   s~    "$'',,$$o&M&M#O)3/1-..!*-}[/I$K ()& 0//s   A	B
B%c                 <    U R                   [        R                     $ r!   )rm   r   rb   r"   s    r   get_model_id!ModelFileSystemCache.get_model_id   s    BBCCr   c                    [         R                  R                  U R                  [        R
                  5      n[        US5       n[        R                  " U R                  U5        S S S 5        g ! , (       d  f       g = f)Nr5   )
r   r(   r)   r   r   ra   r,   r-   r9   rm   rx   s      r   rn   $ModelFileSystemCache.save_model_meta   sP    d&>&>&5&J&JL.$'1KK+ (''s   "A00
A>c                 
   U R                    Hs  nXS   :X  d  M  [        R                  R                  U R                  US   5      n[        R                  R                  U5      (       a  Us  $ U R                  U5        Mu     g)zRetrieve the cache if there is file match the path.

Args:
    file_path (str): The file path in the model.

Returns:
    path: the full path of the file.
PathN)r'   r   r(   r)   r   r+   rN   )r   	file_pathcached_filecached_file_paths       r   get_file_by_path%ModelFileSystemCache.get_file_by_path   sn      ,,K//#%77<<0H0H0;F0C$E 77>>"233++OOK0 - r   c                 r   U R                    H  nXS   :X  d  M  US   R                  U5      (       d  UR                  US   5      (       d  MA  [        R                  R	                  U R
                  US   5      n[        R                  R                  U5      (       a  Us  $ U R                  U5        M     g)zRetrieve the cache if there is file match the path.

Args:
    file_path (str): The file path in the model.
    commit_id (str): The commit id of the file

Returns:
    path: the full path of the file.
r   r   N)r'   
startswithr   r(   r)   r   r+   rN   )r   r   	commit_idr   r   s        r   get_file_by_path_and_commit_id3ModelFileSystemCache.get_file_by_path_and_commit_id   s      ,,K//J'229==AUAUVablVmAnAn#%77<<0H0H0;F0C$E 77>>"233++OOK0 - r   c                 &   U R                  U5      nU R                   Hp  nX2:X  d  M
  [        R                  R	                  U R
                  US   5      n[        R                  R                  U5      (       a  Us  $ U R                  U5          g   g)zCheck if exist cache file.

Args:
    model_file_info (ModelFileInfo): The file information of the file.

Returns:
    str: The file path.
r   N)$_ModelFileSystemCache__get_cache_keyr'   r   r(   r)   r   r+   rN   )r   model_file_info	cache_keyr   	orig_paths        r   get_file_by_info%ModelFileSystemCache.get_file_by_info   s|     ((9	,,K'GGLL)A)A)4V)<>	77>>),,$$OOK0 - r   c                     US   US   S.nU$ )Nr   r   )r   r   rC   )r   r   r   s      r   __get_cache_key$ModelFileSystemCache.__get_cache_key  s!    #F+'
3
	 r   c                    U R                  U5      nSnUS   n[        R                  R                  U R                  US   5      nU R
                   H  nUS   U:X  d  M  US   R                  US   5      (       d  US   R                  US   5      (       d  MH  U[           nUc  MV  [        R                  R                  U5      (       d  M|  [        (       a  [        U5      nOUnXx:X  a  Sn  O[        R                  SU S35        M     U(       a6  [        R                  R                  U5      (       a  gU R                  U5        g)zCheck the file is cached or not. Note existence check will also cover digest check

Args:
    model_file_info (CachedFileInfo): The cached file info

Returns:
    bool: If exists and has the same hash, return True otherwise False
Fr   r   TzFile [z?] exists in cache but with a mismatched hash, will re-download.)r   r   r(   r)   r   r'   r   r   r+   enable_default_hash_validationr   loggerinforN   )	r   r   rE   	is_existsr   cache_file_path
cached_keyexpected_hashcache_file_sha256s	            r   r+   ModelFileSystemCache.exists  s*    ""?3	K	'',,t'?'?'6v'>@++J&!Y.z*55c*oFF:11*Z2HII /	 : ,'2) 2) 65,8,I),9)$9$(	$YK/no! ,& ww~~o..#%r   c                 6   U R                    H  nUS   US   :X  d  M  U R                  U5        [        R                  R	                  U R
                  US   5      n[        R                  R                  U5      (       a  [        R                  " U5          g   g)zlWe in cache, remove it.

Args:
    model_file_info (ModelFileInfo): The model file information from server.
r   N)r'   rN   r   r(   r)   r   r+   rM   )r   r   r   r   s       r   remove_if_exists%ModelFileSystemCache.remove_if_exists?  sv      ,,K6"of&==,GGLL)A)A)4V)<>	77>>),,IIi( -r   c                    U R                  U5        U R                  U5      n[        R                  R	                  U R
                  US   5      n[        R                  R                  U5      n[        R                  R                  U5      (       d  [        R                  " USS9  [        X$5        U R                  R                  U5        U R                  5         U$ )a+  Put model on model_file_location to cache, the model first download to /tmp, and move to cache.

Args:
    model_file_info (str): The file description returned by get_model_files.
    model_file_location (str): The location of the temporary file.

Returns:
    str: The location of the cached file.
r   Tr   )r   r   r   r(   r)   r   dirnamer+   r   r   r'   appendr@   )r   r   model_file_locationr   cache_full_pathcache_file_dirs         r   rJ   ModelFileSystemCache.put_fileN  s     	o.((9	'',,$$f 9ww~~n--KK6 2  + r   )rp   rm   )NN)r]   r^   r_   r`   __doc__r   rl   ro   r   r   r   rn   r   r   r   r   r+   r   rJ   re   __classcell__)rt   s   @r   rg   rg      sX    
?2O	' 	'D,(,,'R r   rg   )rW   r   r-   r6   r   shutilr   r   typingr   modelscope.hub.constantsr   r   modelscope.hub.utils.utilsr   modelscope.utils.loggerr	   r   getenvr~   lowerr   objectr   rg   rC   r   r   <module>r      s~     	     : 3 .	 II7AGGIOOQU[[ o8f o8dZ? Zr   