o
    /ѹg                     @   s   d dl Z d dlmZmZmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d d	lmZ e Zd
edefddZ	dd
edeeeef  fddZddedee fddZdS )    N)DictOptionalUnion)urlparse)HubApiModelScopeConfig)	FILE_HASH)GitCommandWrapper)ModelFileSystemCache)compute_hash)
get_loggermodel_root_pathreturnc                 C   sn   d }t jt j| dr-t }|| }|dr |d d }t|}|jdd  }|S t| }|	 }|S )Nz.git   )
ospathexistsjoinr	   Zget_repo_remote_urlendswithr   r
   Zget_model_id)r   model_cacheZgit_cmd_wrapperZgit_urlZu_parsemodel_id r   ^/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/site-packages/modelscope/hub/check_model.pyget_model_id_from_cache   s   

r   
user_agentc                 C   sZ  zt | }|dd}dtj|di}t }dtjv r|ni |ddi}tdd	}z|j||d
\}}t	|dkr@|d }	nd}	W n   d}	Y |j
||	d||d}
|
D ]M}|d dkr_qVtdur{t|riqVtd|d  d|	 d  W dS t|v rttj| |d }||t krqVtd|d  d|	 d  W dS qVW dS    Y dS )z`Check local model repo is latest.
    Check local model repo is same as hub latest version.
    Z___.z
user-agent)r   ZCI_TESTZSnapshotTrueg      ?)timeout)r   use_cookiesr   ZmasterT)r   revision	recursiveheadersr   TypetreeNzModel file Namez' is different from the latest version `zQ`,This is because you are using an older version or the file is updated manually.Path)r   replacer   Zget_user_agentZget_cookiesr   environr   Zget_model_branches_and_tagslenZget_model_filesr   r   loggerinfor   r   r   r   )r   r   r   r"   cookiesZsnapshot_header_api_Z	revisionsZlatest_revisionZmodel_filesZ
model_fileZlocal_file_hashr   r   r   check_local_model_is_latest!   sn   




r/   r   tokenc                 C   sP   | d u s
t j| rdS t }|| z	|j| d W dS  ty'   Y dS w )NF)r   T)r   r   r   r   loginZ	get_model	Exception)r   r0   r-   r   r   r   check_model_is_idg   s   
r3   )N)r   typingr   r   r   urllib.parser   Zmodelscope.hub.apir   r   Zmodelscope.hub.constantsr   Zmodelscope.hub.gitr	   Zmodelscope.hub.utils.cachingr
   Zmodelscope.hub.utils.utilsr   Zmodelscope.utils.loggerr   r*   strr   r/   r3   r   r   r   r   <module>   s$   
F