o
    2ôjË0  ã                   @   s†   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fded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_pathc                 C   s   || _ tj |¡| _tj |¡| _|sd}t|ƒ‚ddlm	} |r%|| _
n| ¡ | _
tƒ }| ¡ s6t d¡ t|ƒ| _tj| j dd� |  |¡}	t | j ¡r`|  ¡ }
| j |
¡}
|
r`|
|	kr`dS | j | j| j
|	| j|¡ | ¡ rw| | 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'

        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)r   ÚosÚpathÚdirnameÚmodel_base_dirÚbasenameÚmodel_repo_namer   Úmodelscope.hub.apir   r   Ú	get_tokenr   Úis_lfs_installedÚloggerÚerrorÚgit_wrapperÚmakedirsÚ_get_model_id_urlÚlistdirÚ_get_remote_urlÚremove_token_from_urlÚcloneÚgit_lfs_installÚadd_user_infoÚconfig_auth_token)Úselfr   r   r   r   r   Úerr_msgr   r$   ÚurlÚ
remote_url© r2   úl/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/modelscope/hub/repository.pyÚ__init__   s@   



ÿ
ÿÿzRepository.__init__c                 C   s   t ƒ › d|› d�}|S )Nú/ú.gitr   )r.   Úmodel_idr0   r2   r2   r3   r&   P   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.   Úremoter2   r2   r3   r(   T   ó   þþzRepository._get_remote_urlÚoriginÚmasterr<   Úbranchc                 C   s   | j j| j||d� dS )zÂPull 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@   r2   r2   r3   rA   [   s   zRepository.pullÚfile_name_suffixc                 C   s"   t  d|t j | jd¡f ¡ dS )z—Add 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.   rB   r2   r2   r3   Úadd_lfs_typed   s
   ÿÿzRepository.add_lfs_typeFÚcommit_messageÚlocal_branchÚremote_branchÚforcec                 C   s°   |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 )a  Push local files to remote, this method will do.
        Execute git pull, git add, git commit, git push in order.

        Args:
            commit_message (str): commit message
            local_branch(str, optional): The local branch, default master.
            remote_branch (str, optional): The remote branch to push, default master.
            force (bool, optional): whether to use forced-push.

        Raises:
            InvalidParameter: no commit message.
            NotLoginException: no auth token.
        Nú commit_message must be provided!úforce must be boolú'Must login to push, please login first.T©Ú	all_files©Úrepo_dirÚtokenr0   rG   rH   )Ú
isinstanceÚstrr   Úboolr   r   r$   r-   r   r,   r   r   r:   ÚaddÚcommitÚpush)r.   rF   rG   rH   rI   Úmsgr0   r2   r2   r3   rW   o   s*   

ÿ
û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.)rP   rY   rZ   r[   )r   r$   Útagr   )r.   rY   rZ   r[   rX   r2   r2   r3   r]   ™   s   
üzRepository.tagc                 C   s$   |   |||¡ | jj| j|d� dS )züCreate 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.
        )rP   rY   N)r]   r$   Úpush_tagr   )r.   rY   rZ   r[   r2   r2   r3   Útag_and_push³   s   zRepository.tag_and_push)r>   r?   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rS   r   r4   r&   r(   rA   rE   rT   rW   r   r]   r_   r2   r2   r2   r3   r      s`    ûÿþýü
û;	üÿþý
ü-ýÿþ
ýýÿþýr   c                   @   s„   e Zd ZdZeddfded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   c                 C   sÂ   || _ |r
t|tƒsd}t|ƒ‚| d¡| _| jsd}t|ƒ‚tj | j¡| _	tj 
| j¡| _|s7d}t|ƒ‚|| _ddlm} |rF|| _n| ¡ | _t|ƒ| _tj| jdd� | j|d	�| _d
S )au  
        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'

        Raises:
            InvalidParameter: parameter invalid.
        z"dataset_work_dir must be provided!r5   z%dataset_work_dir can not be root dir!r   r   r   Tr   )rf   N)rf   rR   rS   r   Úrstripre   r   r   r   Úrepo_base_dirr   Ú	repo_namer   r   r   r   r    r   r$   r%   Ú_get_repo_urlÚrepo_url)r.   re   rf   r   r   r   r/   r   r2   r2   r3   r4   Ç   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'   re   r(   r$   r)   rk   r"   ÚinfoÚformatr*   rh   r   ri   r   )r.   r1   r2   r2   r3   r*   ö   s   ÿzDatasetRepository.cloneFrF   r@   rI   c                 C   sÄ   |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 )a¶  Push local files to remote, this method will do.
           git pull
           git add
           git commit
           git push

        Args:
            commit_message (str): commit message
            branch (str, optional): which branch to push.
            force (bool, optional): whether to use forced-push.

        Raises:
            InvalidParameter: no commit message.
            NotLoginException: no access token.
        NrJ   rK   rL   TrM   rO   )rR   rS   r   rT   r   r   r$   r-   re   r,   rh   ri   r(   r)   rA   rU   rV   rW   )r.   rF   r@   rI   rX   r1   r2   r2   r3   rW     s*   

ûzDatasetRepository.pushc                 C   s   t ƒ › d|› d�S )Nz
/datasets/r6   r   )r.   rf   r2   r2   r3   rj   1  s   zDatasetRepository._get_repo_urlc                 C   r8   r9   )r$   r:   re   r   r;   r2   r2   r3   r(   4  r=   z!DatasetRepository._get_remote_url)r`   ra   rb   rc   r   rS   r   r4   r*   rT   rW   rj   r(   r2   r2   r2   r3   rd   Ã   s8    ûÿþýü
û/ýÿþ
ý-rd   )r   Útypingr   Úmodelscope.hub.errorsr   r   r   Úmodelscope.utils.constantr   r   r   Úmodelscope.utils.loggerr	   Úgitr   Úutils.utilsr   r"   r   rd   r2   r2   r2   r3   Ú<module>   s    3