
    5pj                        S r SSKJr  SSKrSSKJrJr  SSKJr  SSK	J
r
  SSKJr  S	S
KJrJrJrJrJrJrJr  S	SKJrJrJrJrJr  SS jr " S S\5      rg)uH   ``ms download`` command — fetch a single file or a full repo snapshot.    )annotationsN)ThreadPoolExecutoras_completed)Path   )HubApi)RepoType   )
CLICommand
SubParsersadd_repo_type_arginfomake_apisuccesswarn)PatternActionadd_legacy_download_argsadd_subcmd_token_endpointnormalize_download_argsnormalize_patternsc                    U (       d  gU R                  5       R                  S5      n U (       d  gU R                  S5      (       d  U R                  S5      (       d  SU  3n U $ )z2Auto-complete bare domain names with ``https://``.N/zhttp://zhttps://)striprstrip
startswith)endpoints    W/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/modelscope_hub/cli/download.py_resolve_cli_endpointr      sW    ~~&&s+Hy))(2E2Ej2Q2QhZ(O    c                  J    \ rS rSrSr\S	S j5       rS
S jrSS jrS
S jr	Sr
g)DownloadCommand!   z9Download files or whole repositories from ModelScope Hub.c           	        U R                  SSS9nUR                  SSS SS9  UR                  SS	S
S9  [        U[        R                  R
                  [        R                  R
                  /[        R                  R
                  SS9  UR                  SS SS9  UR                  SSS SS9  UR                  SSS SS9  UR                  SS[        SSS9  UR                  SSS[        S S S!9  UR                  S"S#S[        S S$S!9  UR                  S%S&S'S(9  [        U5        [        U5        UR                  [        S)9  g )*Ndownloadz1Download a file or full snapshot of a repository.)helprepo_id?z"Canonical 'owner/name' identifier.)nargsdefaultr%   files*z?Optional list of file paths to download. Empty = full snapshot.)r(   r%   F)choicesr)   requiredz
--revisionz(Branch / tag / commit (default: master).)r)   r%   z--cache-dir	cache_dirzOverride cache directory.)destr)   r%   z--local-dir	local_dirz5Download directly to this directory (bypasses cache).z--max-workersmax_workers   z-Concurrency for full-repo snapshot downloads.)r/   typer)   r%   z	--includeallow_patterns+z,Glob to include (snapshot mode). Repeatable.)r/   r(   actionr)   r%   z	--excludeignore_patternsz,Glob to exclude (snapshot mode). Repeatable.z--force
store_truezRe-download even if cached.)r6   r%   )_command)
add_parseradd_argumentr   r	   MODELvalueDATASETintr   r   r   set_defaultsr!   )
subparsersps     r   registerDownloadCommand.register$   sy   !!D " 
 	
5	 	 	
 	
R 	 	

 	^^))8+;+;+A+ABNN((		
 	
|T8bc	};Kfg	T@w 	 	
 	
@ 	 	
 	
! ? 	 	
 	
" ? 	 	
 	
y<YZ 	!#!!$	0r   c                   [        U R                  5        U R                  R                  S:X  a  U R                  5         g U R	                  5       nU R                  R
                  (       a  [        U R                  R
                  5      OS nU R                  R                  (       a  [        U R                  R                  5      OS nU R                  R                  (       a  U R                  R                   Hx  nUR                  U R                  R                  U R                  R                  UU R                  R                  UUU R                  R                  S9n[        U SU 35        Mz     g [        SU R                  R                   SU R                  R                   S35        UR                  U R                  R                  U R                  R                  U R                  R                  UU[!        U R                  R"                  5      [!        U R                  R$                  5      U R                  R&                  S9n[        SU 35        g )	N
collection)revisionr.   r0   force    → zDownloading snapshot of z (u   )…)rG   r.   r0   r4   r7   r1   zSnapshot ready at )r   args	repo_type_download_collection_make_api_with_endpointr.   r   r0   r*   download_filer&   rG   rH   r   r   download_repor   r4   r7   r1   )selfapir.   r0   	file_pathlocaloutputs          r   executeDownloadCommand.execute_   s   		* 99,.%%'**,>Bii>Q>Qdii&9&9!:W[	>Bii>Q>Qdii&9&9!:W[	99??!YY__	))II%%II''!YY//''))// *  9+U5'23 - '		(9(9':"TYY=P=P<QQUVW""IIIIYY''-dii.F.FG.tyy/H/HI		-- # 	
 	$VH-.r   c                f   [        U R                  SS5      n[        U R                  SS5      nU(       a  [        U5      n[        X!S9$ [	        U R                  5      n UR                  U R                  R                  U R                  R                  S9n[        X$S9$ ! [         a    Us $ f = f)zBuild HubApi with resolved endpoint.

Bare domains (e.g. ``modelscope.cn``) are auto-completed with
``https://``.  When no endpoint is provided, auto-detection via
``resolve_endpoint_for_read()`` is attempted.
r   Ntoken)rX   r   )rK   )	getattrrJ   r   r   r   resolve_endpoint_for_readr&   rK   	Exception)rP   r   rX   rQ   resolveds        r   rM   'DownloadCommand._make_api_with_endpoint   s     499j$7		7D1,X6H99tyy!	44		!!))-- 5 H 99 	J	s    A B! !B0/B0c                b  ^^ [        U R                  5      mU R                  R                  mU R                  R                  nTR                  R                  U5      nUR                  S0 5      R                  S/ 5      nU Vs/ s H4  oDR                  S5      (       d  M  UR                  S5      (       d  M2  UPM6     nnU(       d  [        SU 35        g[        S[        U5       S35        SUU4S	 jjn/ / p[        U R                  R                  S
9 n	U Vs0 s H  oIR                  Xd5      U_M     n
n[        U
5       He  nUR                  5       u  pnU(       a%  UR                  X45        [        SU SU 35        MB  UR                  X45        [!        SU SU 35        Mg     SSS5        [        S[        U5       S[        U5       S35        U(       a3  U H  u  p[        SU SU 35        M     ["        R$                  " S5        ggs  snf s  snf ! , (       d  f       Nw= f)z&Download all skills from a collection.CollectionElementsCollectionElementVoListElementPathElementNamez-No valid skill elements found in collection: NzFound u'    skill(s) in collection, downloading…c                  > U S    SU S    3nU S   nT(       a  [        [        T5      U-  5      OS n TR                  U[        R                  US9nU[        U5      S 4$ ! [
         a  nUS [        U5      4s S nA$ S nAff = f)Nra   r   rb   )rK   r0   )strr   rO   r	   SKILLr[   )elemskill_id
skill_name	skill_dirresultexcrQ   r0   s         r   _download_one;DownloadCommand._download_collection.<locals>._download_one   s    }-.a]0C/DEHm,J=FDOj89DI0**&nn' + 
  Vd22 0s3x//0s   ,A" "
B,A?9B?B)r1   zFailed to download skill z: zskill rI   zDownload complete: z succeeded, z failedz  r
   )rf   dictreturnz"tuple[str, str | None, str | None])r   rJ   r0   r&   legacyget_collectiongetr   r   lenr   r1   submitr   rj   appendr   sysexit)rP   collection_iddataelementsevalidrl   	succeededfailedexecutorfuturesfuturesidpatherrorerrrQ   r0   s                   @@r   rL   $DownloadCommand._download_collection   s   tyy!II''			))zz((7880"599:SUWX$VHqm(<}AUHV@PQvc%j\!HIJ	0 	0 6DII,A,ABhEJKU}8!;UGK&w/#)==? 5MM3,/4SEE7CD$$c[1fSEtf56 0 C 	"3y>"2,s6{m7ST"r#b&' #HHQK E W. L CBs1   H"H:HH H9A6H H  
H. N)rA   r   ro   None)ro   r   )ro   r   )__name__
__module____qualname____firstlineno____doc__staticmethodrC   rU   rM   rL   __static_attributes__r   r   r   r!   r!   !   s(    C81 81t%/T0-r   r!   )r   
str | Nonero   r   )r   
__future__r   rv   concurrent.futuresr   r   pathlibr   rQ   r   	constantsr	   baser   r   r   r   r   r   r   compatr   r   r   r   r   r   r!   r   r   r   <module>r      s@    N " 
 ?     Z Z Z 	mj mr   