o
    )j4                     @   s   d dl Z d dlZd dlmZ d dlmZmZmZ d dlm	Z	m
Z
mZ d dlmZ ddlmZ ddlmZ e ZG d	d
 d
ZG dd dZdS )    N)Optional)GitErrorInvalidParameterNotLoginException)DEFAULT_DATASET_REVISIONDEFAULT_REPOSITORY_REVISIONMASTER_MODEL_BRANCH)
get_logger   )GitCommandWrapper)get_endpointc                   @   s   e Zd ZdZedddfdededee dee dee dee fd	d
Zdd Zdd Z	d&dedefddZ
defddZeedfdedee dee dee fddZefded ed!ee fd"d#Zefded ed!ee fd$d%ZdS )'
Repositoryz8A local representation of the model git repository.
    N	model_dir
clone_fromrevision
auth_tokengit_pathendpointc                 C   s&  || _ || _tj|| _tj|| _|sd}t|ddl	m
} |r(|| _n| | _t }	|	 s9td t|| _tj| jdd | |}
t| jrc|  }| j|}|rc||
krcdS | j| j| j|
| j| |	 rz|	| j | j| j| j | jr| j| j| j dS dS )a  Instantiate a Repository object by cloning the remote ModelScopeHub repo

        Args:
            model_dir (str): The model root directory.
            clone_from (str): model id in ModelScope-hub from which git clone
            revision (str, optional): revision of the model you want to clone from.
                     Can be any of a branch, tag or commit hash
            auth_token (str, optional): token obtained when calling `HubApi.login()`.
                        Usually you can safely ignore the parameter as the token is already
                        saved when you login the first time, if None, we will use saved token.
            git_path (str, optional): The git command line path, if None, we use 'git'
            endpoint (str, optional): The ModelScope endpoint URL. If None, use default endpoint.

        Raises:
            InvalidParameter: revision is None.
        0a non-default value of revision cannot be empty.r   ModelScopeConfigz)git lfs is not installed, please install.Texist_okN)	_endpointr   ospathdirnamemodel_base_dirbasenamemodel_repo_namer   modelscope.hub.apir   r   	get_tokenr   Zis_lfs_installedloggererrorgit_wrappermakedirs_get_model_id_urllistdir_get_remote_urlremove_token_from_urlcloneZgit_lfs_installadd_user_infoconfig_auth_token)selfr   r   r   r   r   r   err_msgr   r$   url
remote_url r1   Z/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/hub/repository.py__init__   sB   




zRepository.__init__c                 C   s&   | j r| j nt }| d| d}|S )N/.gitr   r   )r-   Zmodel_idr   r/   r1   r1   r2   r&   T   s   zRepository._get_model_id_urlc                 C   .   z
| j | j}W |S  ty   d }Y |S w N)r$   get_repo_remote_urlr   r   r-   remoter1   r1   r2   r(   Y      zRepository._get_remote_urloriginmasterr;   branchc                 C   s   | j j| j||d dS )zPull remote branch

        Args:
            remote (str, optional): The remote name. Defaults to 'origin'.
            branch (str, optional): The remote branch. Defaults to 'master'.
        )r;   r?   N)r$   pullr   )r-   r;   r?   r1   r1   r2   r@   `   s   zRepository.pullfile_name_suffixc                 C   s"   t d|t j| jdf  dS )zAdd file suffix to lfs list.

        Args:
            file_name_suffix (str): The file name suffix.
                examples '*.safetensors'
        z5printf '
%s filter=lfs diff=lfs merge=lfs -text
'>>%sz.gitattributesN)r   systemr   joinr   )r-   rA   r1   r1   r2   add_lfs_typei   s
   zRepository.add_lfs_typeFcommit_messagelocal_branchremote_branchforcec                 C   s   t jdtdd 	 |d u st|tsd}t|t|ts!td| js(td| j	
| j| j | j	| j| j | j	| j}| j	j| jdd | j	| j| | j	j| j| j|||d	 d S 
NzThis function is deprecated and will be removed in future versions. Please use git command directly or use HubApi().upload_folder instead   )
stacklevelz commit_message must be provided!zforce must be boolz'Must login to push, please login first.T)	all_files)repo_dirtokenr/   rF   rG   )warningswarnDeprecationWarning
isinstancestrr   boolr   r   r$   r,   r   r+   r   r   r9   addcommitpush)r-   rE   rF   rG   rH   msgr/   r1   r1   r2   rW   t   s6   


zRepository.pushtag_namemessagerefc                 C   sR   |du s|dkrd}t ||du s|dkrd}t || jj| j|||d dS )a  Create a new tag.

        Args:
            tag_name (str): The name of the tag
            message (str): The tag message.
            ref (str, optional): The tag reference, can be commit id or branch.

        Raises:
            InvalidParameter: no commit message.
        N zFWe use tag-based revision, therefore tag_name cannot be None or empty.z=We use annotated tag, therefore message cannot None or empty.)rM   rY   rZ   r[   )r   r$   tagr   )r-   rY   rZ   r[   rX   r1   r1   r2   r]      s   
zRepository.tagc                 C   s$   |  ||| | jj| j|d dS )zCreate tag and push to remote

        Args:
            tag_name (str): The name of the tag
            message (str): The tag message.
            ref (str, optional): The tag ref, can be commit id or branch. Defaults to MASTER_MODEL_BRANCH.
        )rM   rY   N)r]   r$   Zpush_tagr   )r-   rY   rZ   r[   r1   r1   r2   tag_and_push   s   zRepository.tag_and_push)r=   r>   )__name__
__module____qualname____doc__r   rS   r   r3   r&   r(   r@   rD   rT   rW   r   r]   r^   r1   r1   r1   r2   r      sf    
>	
2
r   c                   @   s   e Zd ZdZedddfdededee dee dee dee fd	d
ZdefddZedfdedee dee	 fddZ
dd Zdd ZdS )DatasetRepositoryzEA local representation of the dataset (metadata) git repository.
    Nrepo_work_dir
dataset_idr   r   r   r   c           	      C   s   || _ || _|rt|tsd}t||d| _| js"d}t|tj	| j| _
tj| j| _|s:d}t||| _ddlm} |rI|| _n| | _t|| _tj| jdd | j|d	| _d
S )a  
        Instantiate a Dataset Repository object by cloning the remote ModelScope dataset repo

        Args:
            repo_work_dir (str): The dataset repo root directory.
            dataset_id (str): dataset id in ModelScope from which git clone
            revision (str, optional): revision of the dataset you want to clone from.
                                      Can be any of a branch, tag or commit hash
            auth_token (str, optional): token obtained when calling `HubApi.login()`.
                                        Usually you can safely ignore the parameter as the token is
                                        already saved when you login the first time, if None, we will use saved token.
            git_path (str, optional): The git command line path, if None, we use 'git'
            endpoint (str, optional): The ModelScope endpoint URL. If None, use default endpoint.

        Raises:
            InvalidParameter: parameter invalid.
        z"dataset_work_dir must be provided!r4   z%dataset_work_dir can not be root dir!r   r   r   Tr   )re   N)r   re   rR   rS   r   rstriprd   r   r   r   repo_base_dirr   	repo_namer   r    r   r   r!   r   r$   r%   _get_repo_urlrepo_url)	r-   rd   re   r   r   r   r   r.   r   r1   r1   r2   r3      s,   

zDatasetRepository.__init__returnc                 C   sh   t | jr|  }| j|}|r|| jkrdS td	| j | j
| j| j| j| j| j | jS )Nr\   zCloning repo from {} )r   r'   rd   r(   r$   r)   rj   r"   infoformatr*   rg   r   rh   r   )r-   r0   r1   r1   r2   r*     s   zDatasetRepository.cloneFrE   r?   rH   c                 C   s   t jdtdd 	 |d u st|tsd}t|t|ts!td| js(td| j	
| j| j | j	| j| j |  }| j	|}| j	| j | j	j| jdd | j	| j| | j	j| j| j|||d	 d S rI   )rO   rP   rQ   rR   rS   r   rT   r   r   r$   r,   rd   r+   rg   rh   r(   r)   r@   rU   rV   rW   )r-   rE   r?   rH   rX   r0   r1   r1   r2   rW     s6   

zDatasetRepository.pushc                 C   s"   | j r| j nt }| d| dS )Nz
/datasets/r5   r6   )r-   re   r   r1   r1   r2   ri   C  s   zDatasetRepository._get_repo_urlc                 C   r7   r8   )r$   r9   rd   r   r:   r1   r1   r2   r(   G  r<   z!DatasetRepository._get_remote_url)r_   r`   ra   rb   r   rS   r   r3   r*   rT   rW   ri   r(   r1   r1   r1   r2   rc      s>    
2
2rc   )r   rO   typingr   Zmodelscope.hub.errorsr   r   r   Zmodelscope.utils.constantr   r   r   Zmodelscope.utils.loggerr	   gitr   Zutils.utilsr   r"   r   rc   r1   r1   r1   r2   <module>   s    <