
    5pj;                        S r SSKJr  SSKrSSKrSSKJr  SSKJr  SSK	J
r
Jr  SSKJr  SS	KJr  S
SKJrJr  SrSS jrSSS jjrSS jrSS jrSS jrSS.SS jjr " S S\5      rg)a  ``ms agent`` command -- low-level raw file transfer for agent repositories.

This is the *slim* Hub CLI. It supports only ``download``/``upload``/``list``
for raw file transfer to and from remote agent repositories, with no framework
awareness.  Framework-aware operations (convert, watch/sync, status, backups,
restore, stop) live in **modelscope-agent** -- use ``ms-agent agent ...``.
    )annotationsN)RawDescriptionHelpFormatter)Path   )AgentApiis_lfs_file)
Visibility)APIError   )
CLICommand
SubParserszThis command transfers raw files only. For framework-aware conversion, watch/sync, status, backups and restore, use the modelscope-agent CLI: `ms-agent agent ...`.c                :    [        SU  3[        R                  S9  g)NzError: filer   )printsysstderr)messages    T/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/modelscope_hub/cli/agent.py_failr       s    	GG9
CJJ/    c                    U R                   =(       d    SnUS:X  a  gUS:X  a  gUS:X  a  gUS:  a  g	U S
U SU R                   S3$ )Nr   i  z*authentication failed. Please login again.i  z;permission denied. You do not have access to this resource.i  z<resource not found. Check the repository name and try again.i  z@server encountered an issue. Please wait a moment and try again.z failed (HTTP : ))status_coder   )eactionstatuss      r   _api_error_messager   %   sU    ]]aF};}L}M}QX^F82aii[::r   c                B    SU ;   a  U R                  S5      u  p#nX$4$ X4$ )zResolve ``repo`` into ``(group, name)``.

- ``owner/name`` -> ``(owner, name)`` (username ignored)
- ``name``       -> ``(username, name)``
/)	partition)repousernameowner_names        r   _resolve_repor(   2   s-     d{,${>r   c                  U(       d  [        S5      $ [        X4=(       d    SS9n UR                  XUS9nUR                  S5      =(       d    / nUR                  S	[        U5      5      n	U(       d  [        S
5        g/ SQn
/ nU GHR  nUR                  S5      =(       d    UR                  S5      =(       d    SnUR                  S5      =(       d    UR                  S5      =(       d    SnU(       a  U SU 3OUnUR                  S5      =(       d    UR                  S5      =(       d    SnUR                  S5      =(       d    UR                  S5      =(       d    SnUR                  S5      =(       d    UR                  S5      =(       d    Sn[        U[        5      (       a  SU;   a  UR                  S5      S   nUR                  UUUU45        GMU     U
 Vs/ s H  n[        U5      PM     nnU H<  n[        U5       H*  u  nn[        UU   [        [        U5      5      5      UU'   M,     M>     SR                  S U 5       5      n[        UR                   " U
6 5        [        UR                   " U Vs/ s H  nSU-  PM
     sn6 5        U H5  n[        UR                   " U Vs/ s H  n[        U5      PM     sn6 5        M7     [        SU SU	 SU S35        g! [         a  n[        [	        US5      5      s SnA$ SnAf[
         a  n[        SU 35      s SnA$ SnAff = fs  snf s  snf s  snf ) zList remote agent repositories. not logged in. Provide endpoint. endpointtoken)r%   page_number	page_sizelistNzlist failed: itemstotal_countz(no agent repositories found)r   )repo_id	framework
visibilityupdatedr   pathNamer'   r!   	Frameworkr5   -r	   r6   LastUpdatedDatelast_updated_dateT  c              3  .   #    U  H  nS U S3v   M     g7f)z{:<}N ).0ws     r   	<genexpr>_cmd_list.<locals>.<genexpr>d   s     5*Qd1#RL*s   z
page z	 / total z (page_size=r   )r   r   list_agentsr
   r   	Exceptiongetlenr   
isinstancestrsplitappend	enumeratemaxjoinformat)r%   r/   r0   r-   r.   clientresultr   r2   totalheadersrowsitem
owner_namer'   r4   fwvisr7   h
col_widthsrowivalfmtrD   vs                              r   	_cmd_listrc   A   s   788x{;F*##%T]#^ JJw%2EJJ}c%j1E-.?GDXXf%?&)9?R
xx9488F#39r,6ZL$(DXXk"Bdhh{&;Bshh|$E(>E#((,-U:M1NURUgs##wmmC(+GWb#w/0  #**'Q#a&'J*nFAs
1s3s8}=JqM %  ))5*5
5C	#**g
	#**
3
1sQw
3
45cjj3/3a3q63/01 	GK=	%YKq
IJE  4'6233 *}QC())*, + 4/sA   K' !L3?L80L='
L01LL0L0L+%L0+L0c          	     H   U (       d  [        S5      $ U(       d  [        S5      $ SU ;  a  U(       d  [        SU  S35      $ [        X=(       d    S5      u  pg[        X4=(       d    SS9n UR                  Xg5      c  [        S	U SU S
35      $ UR	                  XgUS9n	U	(       d  [        S	U SU S35      $ U(       a  [        U5      R                  5       O[        R                  " 5       U-  nUR                  SSS9  [        U	5      n[        U	S5       HY  u  p[        SU SU SU 3SS9   UR                  XgXSS9nX-  nUR                   R                  SSS9  UR#                  U5        M[     [        SU SU 35        g! [
         a  n
[        [        U
S5      5      s Sn
A
$ Sn
A
f[         a  n
[        SU
 35      s Sn
A
$ Sn
A
ff = f! [
         a!  n
[        [        U
S5      5      s Sn
A
s  $ Sn
A
f[         a  n
[        SU SU
 35      s Sn
A
s  $ Sn
A
ff = f)zCDownload all raw files of a remote repository to a local directory.0--repo is required (the remote repository name).r*   r!   --repo 'S' requires login to resolve owner. Use 'owner/name' format or run 'ms login' first.r+   r,   Nzrepository z not found.)revisiondownloadzdownload failed: z has no files.T)parentsexist_okr   z  [z] downloading )flush)rh   binaryzdownload failed for r   zDownloaded  file(s) to r   )r   r(   r   	repo_infolist_repo_filesr
   r   rH   r   
expandusercwdmkdirrJ   rO   r   download_repo_fileparentwrite_bytes)r#   	local_dirrh   r-   r.   r$   groupr'   rS   pathsr   destrU   r_   reldataouts                    r   _cmd_downloadr~   m   s   GHH788
$xtfop
 	
  n"5KEx{;F.E(0;ugQtfK@AA&&uX&F
 {5'4&?@@+44	?%%'$((*t:KDJJtdJ+JEE1%A3awnSE2$?	<,,U#Y],^D
 j

5 & 
Kwl4&
12-  8':677 .(,--.  	<+Az:;; 	</uBqc:;;	<sf   *#F F /G
GF)#G)G6G	G	G
H!G7/H!7H!HH!H!publicr6   c                  U (       d  [        S5      $ U(       a  [        U5      R                  5       O[        R                  " 5       nUR	                  5       (       d  [        SU 35      $ / n	UR                  5       (       a  U	R                  UR                  U45        Oh[        UR                  S5      5       HJ  n
U
R                  5       (       d  M  U	R                  U
R                  U5      R                  5       U
45        ML     U	(       d  [        SU S35      $ U(       aZ  [        S[        U	5       SU  S35        [        U	5       H/  u  p[        S	U S
U
R                  5       R                   S35        M1     gU(       a  U(       d  [        S5      $ SU ;  a  U(       d  [        SU  S35      $ [!        X=(       d    S5      u  p[#        XES9n UR%                  X5      (       d  UR'                  XUS9  / n/ n[        U	5       H  u  pU
R                  5       R                  n[/        UU5      (       a  UR                  X45        MD  UR                  SUSUS[0        R2                  " U
R5                  5       5      R7                  S5      SS.5        M      U(       a  UR9                  XUUSS9  U H+  u  pUR;                  UUUU
R5                  5       SUSU 3S 9  M-     [        S#[        U	5       S$U SU 35        g! [(         a'  n[        SU S3[*        R,                  S9   SnAGN6SnAff = f! [<         a  n[        [?        US!5      5      s SnA$ SnAf[(         a  n[        S"U 35      s SnA$ SnAff = f)%z:Upload raw files from a local path to a remote repository.re   zlocal path not found: *zno files found under .z[dry-run] would upload z file(s) to 'z':r?   z (z B)r   z$not logged in. Run 'ms login' first.r!   rf   rg   r+   r,   r   z#warning: create_repo check failed (z), proceeding anyway.r   Ncreatenormalasciibase64)r   r8   typesizesha256contentencodingzupload normal files)rh   commit_messagezupload LFS )r   rh   r   uploadzupload failed: z	Uploaded rn   ) r   r   rq   rr   existsis_filerN   r'   sortedrglobrelative_toas_posixr   rJ   statst_sizer(   r   
check_repocreate_reporH   r   r   r   r   	b64encode
read_bytesdecodecommit_filesupload_lfs_filer
   r   )r#   rw   rh   dry_runr-   r.   r$   r6   srcentriesfpr{   rx   r'   rS   excnormal_actionslfs_entriesr   r   s                       r   _cmd_uploadr      s'   GHH*3$y/
$
$
&C::<<-cU344 ')G
{{}}#'3(Bzz||s 3 < < >CD ) ,SE344'G~]4&KLgGCBse2bggi//045 '5;<<
$xtfop
 	
  n"5KEx5Fa  --uzB "$N*,K'?wwy  sD!!y)!!&$  %//@GGP (
 # ,^h_tu"GC""!!,SE2 #  # 
Ic'l^<wav
>?W  a3C58MNUXU_U_``aJ  6'8455 ,qc*++,sI   &L A
L8 
L5L00L58
NMNN)M<6N<Nc                  6    \ rS rSrSr\SS j5       rSS jrSrg)	AgentCommand   z;Raw agent-repository file transfer: download, upload, list.c                f   S[          S3nU R                  SSS[         -   U[        S9nUR                  [        S9  UR                  SS	S
9nSUl        UR                  SS[        S[         -   S9nUR                  SSSSS9  UR                  SS SS9  UR                  SSSS9  UR                  SS[        S[         -   S9nUR                  SSSSS9  UR                  SS SS9  UR                  SSSS9  UR                  S[        R                  R                  [        R                  R                  /[        R                  R                  SS 9  UR                  S!S"S#S$9  UR                  S%S&S'S(9nUR                  S)S S*S9  UR                  S+S,[        S-S.S/9  UR                  S0S1[        S2S3S/9  g )4Nzsubcommand arguments:
  download  -r REPO [--local-dir DIR] [--revision REV]
  upload    -r REPO [--local-dir DIR] [--revision REV] [--dry-run]
  list      [--owner OWNER] [--page N] [--page-size N]

note:
  z

examples:
  ms agent download -r user/my-agent --local-dir ./my-agent
  ms agent upload -r user/my-agent --local-dir ./my-agent
  ms agent list --owner user
agentz=Transfer raw agent repository files (download, upload, list).z;Low-level raw file transfer for remote agent repositories. )helpdescriptionepilogformatter_class)_commandagent_commandACTION)rz   metavarTri   z1Download raw agent files from a remote repositoryzFDownload all files of a remote agent repository to a local directory.
)r   r   r   z-rz--repozGRemote repo identifier, supports owner/name format (e.g. user/my-agent))requiredr   z--local-dirz8Destination directory (default: ./<repo-name> under CWD))defaultr   z
--revisionmasterz%Repository revision (default: master)r   z-Upload raw agent files to a remote repositoryz=Upload files from a local path to a remote agent repository.
z8Source path (file or directory) to upload (default: CWD)z--visibilityz<Visibility of the remote repo when created (default: public))choicesr   r   z	--dry-run
store_truez=List files that would be uploaded, without actually uploading)r   r   r1   zList remote agent repositoriesz<Query and display remote agent repositories with pagination.)r   r   z--ownerz-Filter by owner username or organization namez--pager/   r   z'Page number for pagination (default: 1))rz   r   r   r   z--page-sizer0   
   z&Number of items per page (default: 10))_CONVERT_HINT
add_parserr   set_defaultsr   add_subparsersr   add_argumentr	   PUBLIClabelPRIVATEint)
subparsers_epilogagent_parser	agent_sub
p_downloadp_uploadp_lists          r   registerAgentCommand.register   sT      -- 	 ",,PUXee7 - 
 	!!<!8 //_h/W	!	 ))D7adqq	 * 

 	Z	 	  	
 	4.h 	  	
 	hElm ''@7X[hh	 ( 
 	Z	 	 	
 	4.h 	 	
 	lHCjk&&,,j.@.@.F.FG%%++O	 	 	
 	3r 	 	

 %%1V & 

 	It:ij=sADm 	 	
 	#rHp 	 	
r   c                   U R                   nUR                  nSSKJn  U" [	        USS 5      [	        USS 5      S9nUR
                  nUR                  nSnUS:H  =(       d    US:H  =(       a    S	[	        US
S5      ;  nU(       a_  U(       aX  SSKJn	   U	" US9n
U
R                  5       =(       d    0 nUR                  S5      =(       d    UR                  S5      =(       d    SnUS:X  a-  [        UR                  UR                  UR                  UUUS9nOUS:X  aC  [!        UR                  UR                  UR                  UR"                  UR$                  UUUS9nOBUS:X  a,  ['        UR(                  UR*                  UR,                  UUS9nO[/        SU 35        SnUS:w  a  [1        U5      eg ! [         a    Sn Nf = f)Nr   )	HubConfigr-   r.   r,   r+   r   ri   r!   r#   )OpenAPIClient)configr$   Username)r#   rw   rh   r-   r.   r$   )r#   rw   rh   r   r6   r-   r.   r$   r1   )r%   r/   r0   r-   r.   zUnknown agent action: r   r   )argsr   r   r   getattrr.   r-   _openapir   get_current_userrI   rH   r~   r#   rw   rh   r   r   r6   rc   r%   r/   r0   r   
SystemExit)selfr   r   r   r   r.   r-   r$   
needs_userr   openapi	user_datarcs                r   executeAgentCommand.executeH  s   yy##&T:t4$.
 ?? x'jFj,@,iSPWX\^dfhPiEi
%0'v6#446<"	$==4W	j8QWUW ZYY..!!B xYY..??!!	B vjj ,,..!B *6(34B7R.  G  s   AF4 4GGrB   N)r   r   returnNone)r   r   )	__name__
__module____qualname____firstlineno____doc__staticmethodr   r   __static_attributes__rB   r   r   r   r      s    ES
 S
j;!r   r   )r   rL   r   r   )request)r   r
   r   rL   r   rL   )r#   rL   r$   rL   r   ztuple[str, str])r   r   )r   
__future__r   r   r   argparser   pathlibr   r   r   r   	constantsr	   errorsr
   baser   r   r   r   r   r(   rc   r~   r   r   rB   r   r   <module>r      sh    #  
 0  ) "  ( 

;	)X'T ^f RpT!: T!r   