
    9i/                        S 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Jr  SSKJr  SSKJr  \R$                  " \S	5      R'                  5       R)                  5       S
:H  r  " S S\5      r " S S\5      rg)z
caching tools
    N)movermtree)Dict)	FILE_HASH'AISTUDIO_ENABLE_DEFAULT_HASH_VALIDATION	CACHE_KEY   )compute_hash)logFalsetruec                   n    \ rS 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   infoz.mscz.mdlidz.mvcache_root_locationc                 Z    [         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)osmakedirsr   
load_cache)selfr   s     Z/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/aistudio_sdk/utils/caching.py__init__FileSystemCache.__init__   s#     	'$7#6     c                     U R                   $ )location)r   r   s    r   get_root_location!FileSystemCache.get_root_location-   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)loadrbN)cached_filesr   pathjoinr   r   KEY_FILE_NAMEexistsopenpickler%   )r   cache_keys_file_pathfs      r   r   FileSystemCache.load_cache1   st    !ww||D,D,D,;,I,I K77>>.//*D1Q$*KKN! 21 011s   0B
B$c                 F   [         R                  R                  U R                  [        R
                  5      n[        R                  " 5       u  p#[        US5       n[        R                  " U R                  U5        SSS5        [        X15        g! , (       d  f       N= f)zSave cache metadata.wbN)r   r(   r)   r   r   r*   tempfilemkstempr,   r-   dumpr'   r   )r   r.   fdfnr/   s        r   save_cached_files!FileSystemCache.save_cached_files:   sm      "ww||D,D,D,;,I,I K!!#"d^qKK))1- R& ^s   "B
B c                     g)zCheck the key is in the cache, if exists, return the file, otherwise return None.

Args:
    key(str): The cache key.

Raises:
    None
N r   keys     r   get_fileFileSystemCache.get_fileD   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
Nr;   )r   r=   r    s      r   put_fileFileSystemCache.put_fileO   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'   remover8   r<   s     r   
remove_keyFileSystemCache.remove_key[   s5     ###$$S)""$ $r   c                 8    U R                    H
  nX!:X  d  M
    g   g)checkTF)r'   )r   r=   
cache_files      r   r+   FileSystemCache.existse   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_cachem   s    
 	t''(r   c                 f    [         R                  " UR                  5       5      R                  5       $ )name)hashlibsha256encode	hexdigestr<   s     r   	hash_nameFileSystemCache.hash_nameu   s     ~~cjjl+5577r   )r   r'   N)__name__
__module____qualname____firstlineno____doc__r*   MODEL_META_FILE_NAMEMODEL_META_MODEL_IDMODEL_VERSION_FILE_NAMEstrr   r"   r   r8   r>   rA   rE   r+   rL   rT   __static_attributes__r;   r   r   r   r      s[    M!# (3'	
%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$ )ModelFileSystemCachez   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)zPut file to the cache
Args:
    cache_root(`str`): The aistudio local cache root(default: ~/.cache/aistudio/)
    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   r\   
model_metasave_model_metaload_model_versioncached_model_revision)r   
cache_rootownerrO   	__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)zget metar&   Nunknown)r   r(   r)   r   r   r[   r+   r,   r-   r%   rg   r\   r   meta_file_pathr/   s      r   rf   $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)zuse version inforN)
r   r(   r)   r   r   r]   r+   r,   readstrip)r   model_version_file_pathr/   s      r   ri   'ModelFileSystemCache.load_model_version   sn    "$'',,$$o&M&M#O77>>122-s3qvvx~~' 43  43s   )B
B revision_infoc                 z   [         R                  R                  U R                  [        R
                  5      n[        US5       n[        U[        5      (       a7  SUS   < SUR                  S5      =(       d    S< 3nUR                  U5        OUR                  U5        SSS5        g! , (       d  f       g= f)zsave file infowz	Revision:Revisionz,CreatedAt:	CreatedAtrp   N)r   r(   r)   r   r   r]   r,   
isinstancedictgetwrite)r   rz   rx   r/   version_info_strs        r   save_model_version'ModelFileSystemCache.save_model_version   s    "$'',,$$o&M&M#O)3/1-..!*-}/@/@/M/ZQZ/Z$\ ()& 0//s   AB,,
B:c                 <    U R                   [        R                     $ )r   )rg   r   r\   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)saver2   N)
r   r(   r)   r   r   r[   r,   r-   r5   rg   rq   s      r   rh   $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+   rE   )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+   rE   )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+   rE   )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(   sha)r   r}   r;   )r   r   r   s      r   __get_cache_key$ModelFileSystemCache.__get_cache_key  s!    #F+'.
	 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(   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
   r   r   rE   )	r   r   r=   	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   r(   N)r'   rE   r   r(   r)   r   r+   rD   )r   r   r   r   s       r   remove_if_exists%ModelFileSystemCache.remove_if_exists4  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'   appendr8   )r   r   model_file_locationr   cache_full_pathcache_file_dirs         r   rA   ModelFileSystemCache.put_fileC  s     	o.((9	'',,$$f 9ww~~n--KK6 2  + r   )rj   rg   )NN)rV   rW   rX   rY   rZ   r   rf   ri   r   r   r   rh   r   r   r   r   r+   r   rA   r_   __classcell__)rm   s   @r   ra   ra   z   sX    
?2O
' 
'D,(,,'R r   ra   )rZ   rP   r   r-   r3   shutilr   r   typingr   aistudio_sdk.configr   r   r   utilr
   aistudio_sdkr   getenvrw   lowerr   objectr   ra   r;   r   r   <module>r      s     	    C C   II5w?EEGMMOSYY a8f a8H_? _r   