o
    ߥiB                     @   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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   N/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/hub/utils/utils.pymodel_id_to_group_owner_name   s   r   r   c                 C   sD   t  dd}tdtj|d}| du r|S tj|| d S )zcache 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hubN/)r   homejoinpathosgetenvpathjoin)r   default_cache_dir	base_pathr   r   r   get_cache_dir    s   r!   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   environintroundr   now	timestampr   r"   strptime__release_datetime__)rtr"   r   r   r   get_release_datetime/   s   
r+   c                  C   s   t dt} t|  S )NMODELSCOPE_DOMAIN)r   r   r   r	   )modelscope_domainr   r   r   get_endpoint;   s   r.   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_SIZEsha256_hashfdatar   r   r   compute_hashA   s   


r;   c                 C   s>   t | }||kst|  d| ||f }t| t|dS )zValidate 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.

    zFile %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
   )r6   expected_sha256file_sha256msgr   r   r   file_integrity_validationM   s   

rB   )N)r0   r   r   pathlibr   typingr   requestsmodelscope.hub.constantsr   r   r   r   r	   modelscope.hub.errorsr
   modelscope.utils.file_utilsr   modelscope.utils.loggerr   r=   r   strr!   r+   r.   r;   rB   r   r   r   r   <module>   s"   
