
    9i2                         S SK r S SKrS SKJr  S SKJrJrJr  S SKJ	r	J
r
Jr  S SKJr  SSKJr  SSKJr  \" 5       r " S	 S
5      r " S S5      rg)    N)Optional)GitErrorInvalidParameterNotLoginException)DEFAULT_DATASET_REVISIONDEFAULT_REPOSITORY_REVISIONMASTER_MODEL_BRANCH)
get_logger   )GitCommandWrapperget_endpointc                       \ rS rSrSr\SS4S\S\S\\   S\\   S\\   4
S	 jjrS
 r	S r
SS\S\4S jjrS\4S jr\\S4S\S\\   S\\   S\\   4S jjr\4S\S\S\\   4S jjr\4S\S\S\\   4S jjrSrg)
Repository   z8A local representation of the model git repository.
    N	model_dir
clone_fromrevision
auth_tokengit_pathc                 z   Xl         [        R                  R                  U5      U l        [        R                  R                  U5      U l        U(       d  Sn[        U5      eSSKJ	n  U(       a  X@l
        OUR                  5       U l
        [        5       nUR                  5       (       d  [        R                  S5        [        U5      U l        [        R"                  " U R                   SS9  U R%                  U5      n	[        R&                  " U R                   5      (       a8  U R)                  5       n
U R                   R+                  U
5      n
U
(       a  X:X  a  gU R                   R-                  U R                  U R                  U	U R                  U5        UR                  5       (       a  UR/                  U R                   5        U R                   R1                  U R                  U R                  5        U R                  (       a1  U R                   R3                  U R                   U R                  5        gg)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_urls              Y/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/hub/repository.py__init__Repository.__init__   s   * # ggooi8!ww//	:HG"7++7(O.88:DO')++--LLDE,X6
DNNT2$$Z0::dnn%%--/J))??
KJj/t22DOOS#33X	? ''))''7 	&&t':':'+';';	=??..t~~tO     c                 &    [        5        SU S3nU$ )N/.gitr   )r2   model_idr4   s      r6   r*   Repository._get_model_id_urlQ   s     (40
r9   c                 x     U R                   R                  U R                  5      nU$ ! [         a    S n U$ f = fN)r(   get_repo_remote_urlr   r   r2   remotes     r6   r,   Repository._get_remote_urlU   sC    	%%99$..IF   	F	   %) 99rC   branchc                 L    U R                   R                  U R                  XS9  g)zPull remote branch

Args:
    remote (str, optional): The remote name. Defaults to 'origin'.
    branch (str, optional): The remote branch. Defaults to 'master'.
)rC   rF   N)r(   pullr   )r2   rC   rF   s      r6   rH   Repository.pull\   s!     	dnnVKr9   file_name_suffixc           	          [         R                  " SU< S[         R                  R                  U R                  S5      < 35        g)zwAdd file suffix to lfs list.

Args:
    file_name_suffix (str): The file name suffix.
        examples '*.safetensors'
z	printf '
z( filter=lfs diff=lfs merge=lfs -text
'>>z.gitattributesN)r   systemr   joinr   )r2   rJ   s     r6   add_lfs_typeRepository.add_lfs_typee   s0     			rww||DNN<LMO	Pr9   Fcommit_messagelocal_branchremote_branchforcec                    [         R                  " S[        SS9   Ub  [        U[        5      (       d  Sn[        U5      e[        U[        5      (       d  [        S5      eU R                  (       d  [        S5      eU R                  R                  U R                  U R                  5        U R                  R                  U R                  U R                  5        U R                  R                  U R                  5      nU R                  R!                  U R                  SS9  U R                  R#                  U R                  U5        U R                  R%                  U R                  U R                  UUUS	9  g 
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tokenr4   rQ   rR   )warningswarnDeprecationWarning
isinstancestrr   boolr   r   r(   r1   r   r0   r    r"   rA   addcommitpush)r2   rP   rQ   rR   rS   msgr4   s          r6   rc   Repository.pushp   s7   
 	T		
	 !NC)H)H4C"3''%&&"#788#$MNN**4>>4??K&&t':':'+';';	= 224>>BT^^t<?^^//%' 	 	)r9   tag_namemessagerefc                     Ub  US:X  a  Sn[        U5      eUb  US:X  a  Sn[        U5      eU R                  R                  U R                  UUUS9  g)zCreate 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.)rY   rf   rg   rh   )r   r(   tagr   )r2   rf   rg   rh   rd   s        r6   rk   Repository.tag   sd     x2~ZC"3''?gmQC"3''^^	 	 	r9   c                 p    U R                  XU5        U R                  R                  U R                  US9  g)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.
)rY   rf   N)rk   r(   push_tagr   )r2   rf   rg   rh   s       r6   tag_and_pushRepository.tag_and_push   s/     	C(!!4>>H!Mr9   )r   r(   r    r   r"   )originmaster)__name__
__module____qualname____firstlineno____doc__r   r_   r   r7   r*   r,   rH   rN   r`   rc   r	   rk   ro   __static_attributes__ r9   r6   r   r      s#    ,G-1+/9P9P 9P $C=9P &c]	9P
 $C=9PvL3 L3 L	PS 	P ,G,G%*	-) -)#C=-) %SM-) TN	-)d "5 #: +>N"N!N #3-N Nr9   r   c                       \ rS rSrSr\SS4S\S\S\\   S\\   S\\   4
S	 jjrS
\4S jr	\S4S\S\\   S\\
   4S jjrS rS rSrg)DatasetRepository   zEA local representation of the dataset (metadata) git repository.
    Nrepo_work_dir
dataset_idr   r   r   c                    X l         U(       a  [        U[        5      (       d  Sn[        U5      eUR	                  S5      U l        U R
                  (       d  Sn[        U5      e[        R                  R                  U R
                  5      U l	        [        R                  R                  U R
                  5      U l        U(       d  Sn[        U5      eX0l        SSKJn  U(       a  X@l        OUR!                  5       U l        [#        U5      U l        [        R&                  " U R
                  SS9  U R)                  US	9U l        g
)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'

Raises:
    InvalidParameter: parameter invalid.
z"dataset_work_dir must be provided!r;   z%dataset_work_dir can not be root dir!r   r   r   Tr   )r~   N)r~   r^   r_   r   rstripr}   r   r   r   repo_base_dirr!   	repo_namer   r#   r   r   r$   r   r(   r)   _get_repo_urlrepo_url)r2   r}   r~   r   r   r   r3   r   s           r6   r7   DatasetRepository.__init__   s    , %J}c$B$B:G"7++*11#6!!=G"7++WW__T-?-?@))$*<*<=HG"7++ 7(O.88:DO,X6
D&&6**j*Ar9   returnc                    [         R                  " U R                  5      (       aB  U R                  5       nU R                  R                  U5      nU(       a  XR                  :X  a  g[        R                  SR                  U R                  5      5        U R                  R                  U R                  U R                  U R                  U R                  U R                  5        U R                  $ )Nrj   zCloning repo from {} )r   r+   r}   r,   r(   r-   r   r&   infoformatr.   r   r   r   r   )r2   r5   s     r6   r.   DatasetRepository.clone   s    ::d(())--/J))??
KJjMM9+224==ABt114??#}}dnndmm	M!!!r9   FrP   rF   rS   c                 R   [         R                  " S[        SS9   Ub  [        U[        5      (       d  Sn[        U5      e[        U[        5      (       d  [        S5      eU R                  (       d  [        S5      eU R                  R                  U R                  U R                  5        U R                  R                  U R                  U R                  5        U R                  5       nU R                  R!                  U5      nU R                  R#                  U R                  5        U R                  R%                  U R                  SS9  U R                  R'                  U R                  U5        U R                  R)                  U R                  U R                  UUUS	9  g rU   )r[   r\   r]   r^   r_   r   r`   r   r   r(   r1   r}   r0   r   r   r,   r-   rH   ra   rb   rc   )r2   rP   rF   rS   rd   r5   s         r6   rc   DatasetRepository.push
  s\    	T		
	 !NC)H)H4C"3''%&&"#788#$MNN**4+=+=tO&&t'9'94>>J))+
%%;;JG
d001T//4@ 2 2NC''//  	 	"r9   c                 "    [        5        SU S3$ )Nz
/datasets/r<   r   )r2   r~   s     r6   r   DatasetRepository._get_repo_url<  s    .!J<t<<r9   c                 x     U R                   R                  U R                  5      nU$ ! [         a    S n U$ f = fr@   )r(   rA   r}   r   rB   s     r6   r,   !DatasetRepository._get_remote_url?  sE    	%%99$:L:LMF   	F	rE   )r   r~   r(   r   r   r   r}   r   )rs   rt   ru   rv   rw   r   r_   r   r7   r.   r`   rc   r   r,   rx   ry   r9   r6   r{   r{      s     ,D-1+/-B #-B -B $C=-B &c]	-B
 $C=-B^"s "  &>%*0" 0"c]0" TN0"d=r9   r{   )r   r[   typingr   modelscope.hub.errorsr   r   r   modelscope.utils.constantr   r   r	   modelscope.utils.loggerr
   gitr   utils.utilsr   r&   r   r{   ry   r9   r6   <module>r      sM    
   O O< < / " %	tN tNn{ {r9   