o
    2ôj�  ã                   @   sè   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlZd dlm	Z	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZ eƒ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdefdd„Zddee fdd„Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) é    N)Údatetime)ÚPath)ÚOptional)ÚDEFAULT_MODELSCOPE_DOMAINÚDEFAULT_MODELSCOPE_GROUPÚMODEL_ID_SEPARATORÚMODELSCOPE_SDK_DEBUGÚMODELSCOPE_URL_SCHEME)ÚFileIntegrityError)Ú get_default_modelscope_cache_dir)Ú
get_loggerc                 C   s<   t | v r|  t ¡d }|  t ¡d }||fS t}| }||fS )Nr   é   )r   Úsplitr   )Úmodel_idÚgroup_or_ownerÚname© r   úm/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/modelscope/hub/utils/utils.pyÚmodel_id_to_group_owner_name   s   þr   c                 C   s*   |   dd¡}tj |d | dd¡¡}|S )Nú/é   r   Ú.Z___)ÚrsplitÚosÚpathÚjoinÚreplace)Ú	directoryr   ÚpartsÚmasked_directoryr   r   r   Úget_model_masked_directory"   s   r    c                 C   sZ   dd l }| dkr
dS d}t| | | d¡¡ƒ}| d|¡}t| | dƒ}|› d|| › �S )Nr   Z0B)	ÚBÚKBÚMBÚGBÚTBÚPBÚEBÚZBÚYBi   r   ú )ÚmathÚintÚfloorÚlogÚpowÚround)Ú
size_bytesr+   Z	size_nameÚiÚpÚsr   r   r   Úconvert_readable_size)   s   r5   c                 C   s2   d}t | ƒ d¡D ]}| ¡ r|| ¡ j7 }q	|S )Nr   Ú*)r   ÚrglobÚis_fileÚstatÚst_size)Úfolder_pathÚ
total_sizer   r   r   r   Úget_folder_size4   s   €r=   Úreturnc                 C   s   t t| d�ƒS )N©r;   )r5   r=   r?   r   r   r   Úget_readable_folder_size=   s   r@   r   c                 C   sD   t  ¡  dd¡}t dtj |d¡¡}| du r|S tj || d ¡S )zòcache dir precedence:
        function parameter > environment > ~/.cache/modelscope/hub
    Args:
        model_id (str, optional): The model id.
    Returns:
        str: the model_id dir if model_id not None, otherwise cache root dir.
    z.cacheÚ
modelscopeÚMODELSCOPE_CACHEÚhubNr   )r   ÚhomeÚjoinpathr   Úgetenvr   r   )r   Údefault_cache_dirÚ	base_pathr   r   r   Úget_cache_dirA   s   ÿÿrI   c                  C   sL   t tjv rttt ¡  ¡ ƒƒ} | S ddlm	} ttt 
|jd¡ ¡ ƒƒ} | S )Nr   )Úversionz%Y-%m-%d %H:%M:%S)r   r   Úenvironr,   r0   r   ÚnowÚ	timestamprA   rJ   ÚstrptimeÚ__release_datetime__)ÚrtrJ   r   r   r   Úget_release_datetimeP   s   
ûÿþÿrQ   c                  C   s   t  dt¡} t|  S )NZMODELSCOPE_DOMAIN)r   rF   r   r	   )Zmodelscope_domainr   r   r   Úget_endpoint\   s   ÿrR   c                 C   sb   d}t  ¡ }t| dƒ�}	 | |¡}|sn| |¡ qW d   ƒ | ¡ S 1 s(w   Y  | ¡ S )Ni   Úrb)ÚhashlibÚsha256ÚopenÚreadÚupdateÚ	hexdigest)Ú	file_pathÚBUFFER_SIZEZsha256_hashÚfÚdatar   r   r   Úcompute_hashb   s   

ü
üúr^   c                 C   s>   t | ƒ}||kst | ¡ d| ||f }t |¡ t|ƒ‚dS )zþValidate the file hash is expected, if not, delete the file

    Args:
        file_path (str): The file to validate
        expected_sha256 (str): The expected sha256 hash

    Raises:
        FileIntegrityError: If file_path hash is not expected.

    z€File %s integrity check failed, expected sha256 signature is %s, actual is %s, the download may be incomplete, please try again.N)r^   r   ÚremoveÚloggerÚerrorr
   )rZ   Zexpected_sha256Zfile_sha256Úmsgr   r   r   Úfile_integrity_validationn   s   
ÿ
ûrc   )N) rT   r   r   Úpathlibr   Útypingr   ÚrequestsÚmodelscope.hub.constantsr   r   r   r   r	   Úmodelscope.hub.errorsr
   Úmodelscope.utils.file_utilsr   Úmodelscope.utils.loggerr   r`   r   r    r5   r=   Ústrr@   rI   rQ   rR   r^   rc   r   r   r   r   Ú<module>   s*   	