o
    2ôj+  ã                   @   st   d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
 e
ƒ Z	 G dd„ deƒZG dd„ deƒZdS )	é    N)ÚmoveÚrmtree)ÚDict)Ú
get_loggerc                   @   st   e Zd ZdZdZdZdZ	 defdd„Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚFileSystemCachez.mscz.mdlÚidz.mvÚcache_root_locationc                 K   s    t j|dd� || _|  ¡  dS )z³Base 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   Úkwargs© r   úo/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/modelscope/hub/utils/caching.pyÚ__init__   s   zFileSystemCache.__init__c                 C   s   | j S ©N)r   ©r   r   r   r   Úget_root_location(   s   z!FileSystemCache.get_root_locationc                 C   sd   g | _ tj | jtj¡}tj |¡r0t|dƒ�}t	 
|¡| _ W d   ƒ d S 1 s)w   Y  d S d S )NÚrb)Úcached_filesr   ÚpathÚjoinr   r   ÚKEY_FILE_NAMEÚexistsÚopenÚpickleÚload)r   Úcache_keys_file_pathÚfr   r   r   r   +   s   
ÿ"ÿÿzFileSystemCache.load_cachec                 C   sd   t j | jtj¡}t ¡ \}}t|dƒ�}t	 
| j|¡ W d  ƒ n1 s&w   Y  t||ƒ dS )zSave cache metadata.ÚwbN)r   r   r   r   r   r   ÚtempfileÚmkstempr   r   Údumpr   r   )r   r   ÚfdÚfnr    r   r   r   Úsave_cached_files3   s   
ÿÿz!FileSystemCache.save_cached_filesc                 C   ó   dS )z¯Check the key is in the cache, if exist, return the file, otherwise return None.

        Args:
            key(str): The cache key.

        Raises:
            None
        Nr   ©r   Úkeyr   r   r   Úget_file>   s   	zFileSystemCache.get_filec                 C   r(   )zÇPut 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*   Úlocationr   r   r   Úput_fileI   s   
zFileSystemCache.put_filec                 C   s&   || j v r| j  |¡ |  ¡  dS dS )zvRemove cache key in index, The file is removed manually

        Args:
            key (dict): The cache key.
        N)r   Úremover'   r)   r   r   r   Ú
remove_keyU   s   
þzFileSystemCache.remove_keyc                 C   s   | j D ]	}||kr dS qdS )NTF)r   )r   r*   Ú
cache_filer   r   r   r   _   s
   
ÿzFileSystemCache.existsc                 C   s   t | jƒ |  ¡  dS )z¸Remove 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   r   r   r   Úclear_cachef   s   
zFileSystemCache.clear_cachec                 C   s   t  | ¡ ¡ ¡ S r   )ÚhashlibÚsha256ÚencodeÚ	hexdigestr)   r   r   r   Ú	hash_namen   s   zFileSystemCache.hash_nameN)Ú__name__Ú
__module__Ú__qualname__r   ÚMODEL_META_FILE_NAMEÚMODEL_META_MODEL_IDÚMODEL_VERSION_FILE_NAMEÚstrr   r   r   r'   r+   r-   r/   r   r1   r6   r   r   r   r   r      s$    
þ
r   c                       sˆ   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	efd
d„Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚModelFileSystemCachezŸLocal cache file layout
       cache_root/owner/model_name/individual cached files and cache index file '.mcs'
       Save only one version for each file.
    Nc                    sp   |du s|du rt ƒ  tj |¡¡ |  ¡  nt ƒ  tj |||¡¡ tjd||f i| _|  	¡  |  
¡ | _dS )aY  Put file to the cache
        Args:
            cache_root(`str`): The modelscope local cache root(default: ~/.cache/modelscope/)
            owner(`str`): The model owner.
            name('str'): The name of the model
        Returns:
        Raises:
            None
        <Tip>
            model_id = {owner}/{name}
        </Tip>
        Nz%s/%s)Úsuperr   r   r   r   Úload_model_metar   r;   Ú
model_metaÚsave_model_metaÚload_model_versionÚcached_model_revision)r   Z
cache_rootÚownerÚname©Ú	__class__r   r   r   x   s   
ÿzModelFileSystemCache.__init__c                 C   sj   t j | jtj¡}t j |¡r-t|dƒ�}t 	|¡| _
W d   ƒ d S 1 s&w   Y  d S tjdi| _
d S )Nr   Úunknown)r   r   r   r   r   r:   r   r   r   r   rA   r;   ©r   Zmeta_file_pathr    r   r   r   r@   ‘   s   
ÿ"ÿz$ModelFileSystemCache.load_model_metac                 C   s\   t j | jtj¡}t j |¡r,t|dƒ�}| ¡  	¡ W  d   ƒ S 1 s%w   Y  d S d S )NÚr)
r   r   r   r   r   r<   r   r   ÚreadÚstrip)r   Úmodel_version_file_pathr    r   r   r   rC   š   s   ÿ
$ÿz'ModelFileSystemCache.load_model_versionÚrevision_infoc                 C   s†   t j | jtj¡}t|dƒ�+}t|tƒr$d|d |d f }| 	|¡ n| 	|¡ W d   ƒ d S W d   ƒ d S 1 s<w   Y  d S )NÚwzRevision:%s,CreatedAt:%sÚRevisionÚ	CreatedAt)
r   r   r   r   r   r<   r   Ú
isinstanceÚdictÚwrite)r   rO   rN   r    Zversion_info_strr   r   r   Úsave_model_version£   s   ÿ
ÿú"üz'ModelFileSystemCache.save_model_versionc                 C   s   | j tj S r   )rA   r   r;   r   r   r   r   Úget_model_id®   s   z!ModelFileSystemCache.get_model_idc                 C   sP   t j | jtj¡}t|dƒ�}t | j	|¡ W d   ƒ d S 1 s!w   Y  d S )Nr!   )
r   r   r   r   r   r:   r   r   r$   rA   rJ   r   r   r   rB   ±   s   
ÿ"ÿz$ModelFileSystemCache.save_model_metac                 C   sN   | j D ]!}||d kr$tj | j|d ¡}tj |¡r|  S |  |¡ qdS )zÃRetrieve 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   r/   )r   Ú	file_pathÚcached_fileÚcached_file_pathr   r   r   Úget_file_by_path·   s   
	
ÿ
€z%ModelFileSystemCache.get_file_by_pathc                 C   sj   | j D ]/}||d kr2|d  |¡s| |d ¡r2tj | j|d ¡}tj |¡r-|  S |  |¡ qdS )zúRetrieve 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.
        rX   rQ   N)r   Ú
startswithr   r   r   r   r   r/   )r   rY   Ú	commit_idrZ   r[   r   r   r   Úget_file_by_path_and_commit_idË   s   

ÿÿ
ÿ
€z3ModelFileSystemCache.get_file_by_path_and_commit_idc                 C   sZ   |   |¡}| jD ]"}||kr*tj | j|d ¡}tj |¡r"|  S |  |¡  dS qdS )z³Check if exist cache file.

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

        Returns:
            str: The file path.
        rX   N)Ú$_ModelFileSystemCache__get_cache_keyr   r   r   r   r   r   r/   )r   Úmodel_file_infoÚ	cache_keyrZ   Ú	orig_pathr   r   r   Úget_file_by_infoá   s   
	

ÿ
÷	z%ModelFileSystemCache.get_file_by_infoc                 C   s   |d |d dœ}|S )NrX   rQ   )rX   rQ   r   )r   ra   rb   r   r   r   Z__get_cache_key÷   s   þz$ModelFileSystemCache.__get_cache_keyc                 C   sŒ   |   |¡}d}| jD ] }|d |d kr*|d  |d ¡s&|d  |d ¡r*d} nq
tj | j|d ¡}|rDtj |¡r?dS |  |¡ dS )zÅCheck the file is cached or not.

        Args:
            model_file_info (CachedFileInfo): The cached file info

        Returns:
            bool: If exists return True otherwise False
        FrX   rQ   T)	r`   r   r]   r   r   r   r   r   r/   )r   ra   r*   Ú	is_existsZ
cached_keyrY   r   r   r   r   þ   s*   
	
ÿþ€
ÿÿzModelFileSystemCache.existsc                 C   sZ   | j D ]'}|d |d kr*|  |¡ tj | j|d ¡}tj |¡r't |¡  dS qdS )z„We in cache, remove it.

        Args:
            model_file_info (ModelFileInfo): The model file information from server.
        rX   N)r   r/   r   r   r   r   r   r.   )r   ra   rZ   rY   r   r   r   Úremove_if_exists  s   


ÿ
úÿz%ModelFileSystemCache.remove_if_existsc                 C   sp   |   |¡ |  |¡}tj | j|d ¡}tj |¡}tj |¡s'tj|dd� t	||ƒ | j
 |¡ |  ¡  |S )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.
        rX   Tr	   )rf   r`   r   r   r   r   Údirnamer   r   r   r   Úappendr'   )r   ra   Úmodel_file_locationrb   Zcache_full_pathZcache_file_dirr   r   r   r-   (  s   


þ
zModelFileSystemCache.put_file)NN)r7   r8   r9   Ú__doc__r   r@   rC   r   rV   rW   rB   r\   r_   rd   r`   r   rf   r-   Ú__classcell__r   r   rG   r   r>   r   s    		r>   )r2   r   r   r"   Úshutilr   r   Útypingr   Úmodelscope.utils.loggerr   ÚloggerÚobjectr   r>   r   r   r   r   Ú<module>   s   a