o
    /ѹgL                  	   @   sF  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
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eee	e f f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)d#ed$e	e d%e
e ddfd&d'Z&dS )*    N)datetime)Path)BinaryIOListOptionalUnion)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_idZgroup_or_ownername r   ^/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/site-packages/modelscope/hub/utils/utils.pymodel_id_to_group_owner_name   s   r   	raw_inputc                 C   s   d }t | tr#t }d| v rdd | dD }|S ||   |S t | trNt }| D ] }t |trMd|v rF|dd |dD  q-||  q-|S )N,c                 S      g | ]}|  qS r   strip).0sr   r   r   
<listcomp>'       z$convert_patterns.<locals>.<listcomp>c                 S   r   r   r   )r   ssr   r   r   r   /   r   )
isinstancestrlistr   appendr   extend)r   outputr   r   r   r   convert_patterns"   s"   
	

r'   c                 C   s*   |  dd}tj|d |dd}|S )N/   r   .Z___)rsplitospathjoinreplace)	directoryr   partsZmasked_directoryr   r   r   get_model_masked_directory7   s   r2   c                 C   sZ   dd l }| dkr
dS d}t||| d}|d|}t| | d}| d||  S )Nr   Z0B)	BZKBZMBZGBZTBZPBZEBZZBZYBi   r)    )mathintfloorlogpowround)Z
size_bytesr5   Z	size_nameipr   r   r   r   convert_readable_size>   s   r=   c                 C   s2   d}t | dD ]}| r|| j7 }q	|S )Nr   *)r   rglobis_filestatst_size)folder_pathZ
total_sizer-   r   r   r   get_folder_sizeI   s   rD   returnc                 C   s   t t| dS )NrC   )r=   rD   rF   r   r   r   get_readable_folder_sizeR   s   rG   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
modelscopeZMODELSCOPE_CACHEZhubNr(   )r   homejoinpathr,   getenvr-   r.   )r   Zdefault_cache_dir	base_pathr   r   r   get_cache_dirV   s   rM   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,   environr6   r:   r   now	timestamprH   rN   strptimeZ__release_datetime__)rtrN   r   r   r   get_release_datetimee   s   
rT   c                  C   s   t dt} t|  S )NZMODELSCOPE_DOMAIN)r,   rK   r   r   )Zmodelscope_domainr   r   r   get_endpointq   s   rU   c                 C   sb   d}t  }t| d}	 ||}|sq|| qW d    | S 1 s(w   Y  | S )Ni   rbT)hashlibsha256openreadupdate	hexdigest)	file_pathBUFFER_SIZEZsha256_hashfdatar   r   r   compute_hashw   s   


ra   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)ra   r,   removeloggererrorr   )r]   Zexpected_sha256Zfile_sha256msgr   r   r   file_integrity_validation   s   

rf   F	file_namepatternscommit_messagec              
   C   sb  t |tr|g}|d u rd|d  d| }| j}tj||}tj|rBt|ddd}| }W d    n1 s<w   Y  nd}|}	|D ]}
|
|	vrdt	|	dkr]|	
ds]|	d7 }	|	|
 d7 }	qH|	|krt|d	dd}td
| d|	  ||	 W d    n1 sw   Y  z| | W d S  ty } z|rn|W Y d }~d S d }~ww )NzAdd `r   z` patterns to rzutf-8)encoding 
wzWriting z file. Content: )r!   r"   Z	model_dirr,   r-   r.   existsrY   rZ   lenendswithrc   debugwritepush	Exception)reporg   rh   ri   Zignore_push_errorrepo_dirr]   r_   Zcurrent_contentcontentpatterner   r   r   add_content_to_file   s@   

r{   )N)NF)'rW   r,   shutiltempfiler   pathlibr   typingr   r   r   r   requestsZmodelscope.hub.constantsr   r	   r
   r   r   Zmodelscope.hub.errorsr   Zmodelscope.utils.file_utilsr   Zmodelscope.utils.loggerr   rc   r   r"   r'   r2   r=   rD   rG   rM   rT   rU   ra   rf   r{   r   r   r   r   <module>   sF   
	