
    \jFR              $          d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
 ddlmZ dd	lmZmZmZmZ dd
lmZ ddlmZmZ ddlmZmZ ddlmZmZ  ej:                  e      Z ed      Z e jC                  dg d      	 	 	 	 d|dee" ed      f   dee# edd      f   deedz   edd      f   dee# edd      f   deddfd        Z$d!e"de#fd"Z%e jC                  d#g d$      	 	 	 	 	 	 d}d!ee"dz   ed%      f   d&ee# ed'd(d)      f   d*ee# ed+d,      f   d-ee# ed.d/d0      f   d1ededdfd2       Z&d3e"dz  d1e"dz  d&e#d*e#d-e#de"dz  ddfd4Z'd!e"d&e#d*e#d-e#de"dz  ddfd5Z(e jC                  d6d7d8g      	 d~dee" ed9      f   deddfd:       Z)e jC                  d;g d<      	 	 	 ddee" ed9      f   d=ee# ed>d?d@      f   dAee# edBdC      f   deddf
dD       Z*e jC                  dEg dF      	 	 	 	 	 	 dd!ee" edG      f   d-ee# ed.d/dH      f   d=ee# ed>d?d@      f   dIee# edJdK      f   dLee+e"   dz   edM      f   dNee+e"   dz   edO      f   deddfdP       Z,e jC                  dQg dR      	 d~dSee" edT      f   dUee" edV      f   deddfdW       Z-e jC                  dXg dY      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZee"dz   ed[      f   d\ee"dz   ed]      f   d;ee# ed^      f   d_ee# ed`da      f   dbee# edcdd      f   deee"dz   edf      f   dgee"dz   edh      f   dIee# edJdi      f   dLee+e"   dz   edj      f   dNee+e"   dz   edk      f   dlee"dz   edm      f   dnee# edodp      f   dqee# edrds      f   dtee# edudvdw      f   deddf dx       Z. e jC                  dyg dz       ed{             y)z7Contains commands to interact with buckets via the CLI.    )	AnnotatedN)logging)BUCKET_PREFIX
BucketFileFilterMatcher_parse_bucket_uri   )REPO_REGIONS   )	SearchOptTokenOpt
get_hf_apityper_factory)make_cp)format_sizeprint_file_listing)ArgumentOption)OutputFormatoutz"Commands to interact with buckets.)helpcreate)zhf buckets create my-bucketz hf buckets create user/my-bucketz-hf buckets create hf://buckets/user/my-bucketz*hf buckets create user/my-bucket --privatez+hf buckets create user/my-bucket --exist-okz,hf buckets create user/my-bucket --region us)nameexamples	bucket_idzTBucket ID: bucket_name, namespace/bucket_name, or hf://buckets/namespace/bucket_nameprivatez	--privatezCreate a private bucket.regionz--regionzdCloud region in which to create the bucket. Can be one of 'us' or 'eu'. Requires Team plan or above.exist_okz
--exist-okz3Do not raise an error if the bucket already exists.tokenreturnc                 r   t        |      }| j                  t              rCt        |       }|j                  r t        j                  d|  dt         d      |j                  } |j                  | |r|nd||      }t        j                  d|j                  j                         |j                         y)	zCreate a new bucket.r   z-Cannot specify a prefix for bucket creation: . Use namespace/bucket_name or namespace/bucket_name.N)r   r   r   zBucket created)uriurl)r   
startswithr   r   path_in_repoclickBadParameteridcreate_bucketr   resultr%   to_urir&   )r   r   r   r   r   apiparsed
bucket_urls           N/root/.hermes/venv/lib/python3.12/site-packages/huggingface_hub/cli/buckets.pyr   r   /   s    T 5
!CM*"9-$$?	{ K11>?UW  II	"""	 # J JJZ^^%:%:%<*..Q    argumentc                 d    | j                  t              r| t        t              d }d|v S | }d|v S )zFCheck if argument is a bucket ID (namespace/name) vs just a namespace.N/)r'   r   len)r4   paths     r2   _is_bucket_idr9   m   s?    =)M*,- $; $;r3   z	list | ls)
zhf buckets listzhf buckets list huggingfacez$hf buckets list --search "my-prefix"zhf buckets list user/my-bucketz!hf buckets list user/my-bucket -Rz!hf buckets list user/my-bucket -hz%hf buckets list user/my-bucket --treez(hf buckets list user/my-bucket --tree -hz+hf buckets list hf://buckets/user/my-bucketz%hf buckets list user/my-bucket/sub -RzyNamespace (user or org) to list buckets, or bucket ID (namespace/bucket_name(/prefix) or hf://buckets/...) to list files.human_readablez--human-readablez-hz$Show sizes in human readable format.as_treez--treez3List files in tree format (only for listing files).	recursivez--recursivez-Rz0List files recursively (only for listing files).searchc                     | duxr t        |       }|r(|t        j                  d      t        | ||||       yt	        | |||||       y)zList buckets or files in a bucket.

    When called with no argument or a namespace, lists buckets.
    When called with a bucket ID (namespace/bucket_name), lists files in the bucket.
    Nz'Cannot use --search when listing files.)r4   r:   r;   r<   r   )	namespacer=   r:   r;   r<   r   )r9   r)   r*   _list_files_list_buckets)r4   r:   r;   r<   r=   r   is_file_modes          r2   list_cmdrC   v   sg    r 4'CM(,CL$$%NOO)	
 	)	
r3   r?   c           	         |rt        j                  d      |rt        j                  d      | 8| j                  t              r#| t	        t              d } | j                  d      } t        |      }|j                  | |      D cg c]T  }|j                  |j                  |rt        |j                  |      n|j                  |j                  |j                  dV }}t        j                  |dd	i
       yc c}w )zList buckets in a namespace.z'Cannot use --tree when listing buckets.z,Cannot use --recursive when listing buckets.Nr6   r"   )r?   r=   )r+   r   sizetotal_files
created_atrE   right)
alignments)r)   r*   r'   r   r7   rstripr   list_bucketsr+   r   r   rE   rF   rG   r   table)	r?   r=   r:   r;   r<   r   r/   bucketitemss	            r2   rA   rA      s       !JKK  !OPP !5!5m!Dc-023	$$S)	
5
!C &&6&J	  ))~~@NK^<TZT_T_!-- ++	
	E 	 IIe 12	s   
AC?c                 *   |r6t         j                  t        j                  k(  rt	        j
                  d      t        |      }t        |       }t        |j                  |j                  |j                  xs d|            }t        ||||       y)zList files in a bucket.z%Cannot use --tree with --format json.r"   Nprefixr<   )r:   r;   r<   )r   moder   jsonr)   r*   r   r   listlist_bucket_treer+   r(   r   )r4   r:   r;   r<   r   r/   r0   rN   s           r2   r@   r@      s     388|000  !HII
5
!Cx(FII&&.$ 	 	
E u^WXabr3   infozhf buckets info user/my-bucketz+hf buckets info hf://buckets/user/my-bucketzFBucket ID: namespace/bucket_name or hf://buckets/namespace/bucket_namec                     t        |      }t        |       }|j                  |j                        }t	        j
                  |d       y)zGet info about a bucket.r"   r+   )id_keyN)r   r   bucket_infor+   r   dict)r   r   r/   r0   rM   s        r2   rV   rV     s8    " 5
!Cy)F__VYY'FHHVD!r3   delete)z hf buckets delete user/my-bucketz-hf buckets delete hf://buckets/user/my-bucketz&hf buckets delete user/my-bucket --yesz-hf buckets delete user/my-bucket --missing-okyesz--yesz-yzSkip confirmation prompt.
missing_okz--missing-okz3Do not raise an error if the bucket does not exist.c                    | j                  t              rDt        |       }|j                  r t	        j
                  d|  dt         d      |j                  } n$d| vr t	        j
                  d|  dt         d      t        j                  d|  d|	       t        |
      }|j                  | |       t        j                  d|        y)zDelete a bucket.

    This deletes the entire bucket and all its contents. Use `hf buckets rm` to remove individual files.
    z-Cannot specify a prefix for bucket deletion: r#   r$   r6   zInvalid bucket ID: z-. Must be in format namespace/bucket_name or z(Are you sure you want to delete bucket ''?r\   r"   )r]   zBucket deleted)r   N)r'   r   r   r(   r)   r*   r+   r   confirmr   delete_bucketr-   )r   r\   r]   r   r0   r/   s         r2   r[   r[     s    J M*"9-$$?	{ K11>?UW  II		I	  !) -;;H/I_a
 	

 KK:9+RHcR
5
!CiJ7JJ95r3   zremove | rm)z)hf buckets remove user/my-bucket/file.txtz2hf buckets rm hf://buckets/user/my-bucket/file.txtz.hf buckets rm user/my-bucket/logs/ --recursivez:hf buckets rm user/my-bucket --recursive --include "*.tmp"z8hf buckets rm user/my-bucket/data/ --recursive --dry-runzBucket path: namespace/bucket_name/path or hf://buckets/namespace/bucket_name/path. With --recursive, namespace/bucket_name is also accepted to target all files.z0Remove files recursively under the given prefix.dry_runz	--dry-runz8Preview what would be deleted without actually deleting.includezQInclude only files matching pattern (can specify multiple). Requires --recursive.excludezLExclude files matching pattern (can specify multiple). Requires --recursive.c                 :   t        |       }|j                  |j                  }|dk(  r|st        j                  d d d      |s|r|st        j                  d      t        |      }	|rt        j                  d      }
g }|	j                  |xs dd	
      D ]B  }t        |t              s|j                  |       |
j                  dt        |       d       D |
j                  dt        |       d       |s|r9t        ||      }|D cg c]   }|j!                  |j"                        s|" }}n|}|D cg c]  }|j"                   }}t%        d |D              }t'        |d	      }|st        j(                  d       yt        |       d| }|sK|sIt        j(                  dj+                  d |D                     t        j,                  d| d dd       |rGt        j(                  dj+                  fd|D                     t        j(                  d| d       y|	j/                  |       t        j0                  d| d dt        |      |       y|}|st        j                  d       |r7t        j(                  d!t2          d"|        t        j(                  d#       yt        j,                  d$| d% d|       |	j/                  |g       t        j0                  d&|'       yc c}w c c}w )(zbRemove files from a bucket.

    To delete an entire bucket, use `hf buckets delete` instead.
     z?No file path specified. To remove files, provide a path (e.g. 'zd/FILE') or use --recursive to remove all files. To delete the entire bucket, use `hf buckets delete z`.z,--include and --exclude require --recursive.r"   zListing files from remoteNTrP   zListing files from remote (z files))include_patternsexclude_patternsc              3   4   K   | ]  }|j                     y wN)rE   ).0fs     r2   	<genexpr>zremove.<locals>.<genexpr>  s     7A7s   )r:   zNo files to remove.z file(s) totaling 
c              3   &   K   | ]	  }d |   yw)z  N )rl   r8   s     r2   rn   zremove.<locals>.<genexpr>  s     BtD6{Bs   zRemove z from 'r_   Fr`   c              3   :   K   | ]  }d t           d|   yw)delete: r6   N)r   )rl   r8   r   s     r2   rn   zremove.<locals>.<genexpr>  s#     cPT-1TFKcs   z
(dry run) z would be removed.)r[   zRemoved ')r   files_deletedrE   zFile path cannot be empty.rs   r6   z"(dry run) 1 file would be removed.zRemove 'z' from 'zFile removed)r8   r   )r   r+   r(   r)   r*   r   r   statusrU   
isinstancer   appendupdater7   doner   matchesr8   sumr   textjoinra   batch_bucket_filesr-   r   )r4   r<   r\   rc   rd   re   r   r0   rQ   r/   rv   	all_filesitemmatcherrm   matched_files
file_paths
total_sizesize_strcount_label	file_pathr   s                        @r2   remover   R  s   z x(F		I  F|I   k "DDM;bR
 	
 	7I  !OPP
5
!C78&(	((>T ) 
 	UD
 $
+  & ;C	N;K7ST	U 	1#i.1AIJg#WwWG(1M1W__QVV5LQMMM%M&34aff4
4777
z$?HH*+Z));H:F7HHTYYBzBBCKK'+gi[COHHTYYcXbccdHHz+.@ABy<

{m79+Q7j/		
 	$$%ABBHHxyk9+FGHH9:hyk)B?SIy)=

>	YGY N 5s    L:LLmove)z/hf buckets move user/old-bucket user/new-bucketz/hf buckets move user/my-bucket my-org/my-bucketzIhf buckets move hf://buckets/user/old-bucket hf://buckets/user/new-bucketfrom_idzMSource bucket ID: namespace/bucket_name or hf://buckets/namespace/bucket_nameto_idzRDestination bucket ID: namespace/bucket_name or hf://buckets/namespace/bucket_namec                    t        |       }|j                  r t        j                  d|  dt         d      t        |      }|j                  r t        j                  d| dt         d      t        |      }|j                  |j                  |j                         t        j                  d|j                  |j                         y)z2Move (rename) a bucket to a new name or namespace.z)Cannot specify a prefix for bucket move: r#   r$   r"   )r   r   zBucket movedN)
r   r(   r)   r*   r   r   move_bucketr+   r   r-   )r   r   r   parsed_from	parsed_tor/   s         r2   r   r     s    2 $G,K  7y A--:O;QS
 	
 "%(I  7w ?--:O;QS
 	

 5
!COOKNN),,O?JJ~{~~Y\\Jr3   sync)z2hf buckets sync ./data hf://buckets/user/my-bucketz2hf buckets sync hf://buckets/user/my-bucket ./dataz;hf buckets sync ./data hf://buckets/user/my-bucket --deletez^hf buckets sync hf://buckets/user/my-bucket ./data --include "*.safetensors" --exclude "*.tmp"zIhf buckets sync ./data hf://buckets/user/my-bucket --plan sync-plan.jsonlz'hf buckets sync --apply sync-plan.jsonlz<hf buckets sync ./data hf://buckets/user/my-bucket --dry-runzChf buckets sync ./data hf://buckets/user/my-bucket --dry-run | jq .sourcezKSource path: local directory or hf://buckets/namespace/bucket_name(/prefix)destzPDestination path: local directory or hf://buckets/namespace/bucket_name(/prefix)z/Delete destination files not present in source.ignore_timesz--ignore-timesz;Skip files only based on size, ignoring modification times.ignore_sizesz--ignore-sizesz<Skip files only based on modification times, ignoring sizes.planz=Save sync plan to JSONL file for review instead of executing.applyz#Apply a previously saved plan file.z5Print sync plan to stdout as JSONL without executing.z6Include files matching pattern (can specify multiple).z6Exclude files matching pattern (can specify multiple).filter_fromz(Read include/exclude patterns from file.existingz
--existingzASkip creating new files on receiver (only update existing files).ignore_existingz--ignore-existingzCSkip updating files that exist on receiver (only create new files).verbosez	--verbosez-vz%Show detailed logging with reasoning.c                     t        |      }|j                  | ||||||||	|
||||t        j                                |r/t        j                         st        j                  d| d       yyy)z0Sync files between local directory and a bucket.r"   )r   r   r[   r   r   r   r   rd   re   r   r   r   rc   r   quietzRun `hf buckets sync --apply z` to execute this plan.N)r   sync_bucketr   is_quiethint)r   r   r[   r   r   r   r   rc   rd   re   r   r   r   r   r   r/   s                   r2   r   r     s~    X 5
!COO!!'lln  " CLLN06MNO #tr3   cp)zEhf buckets cp hf://buckets/username/my-bucket/config.json config.jsonz<hf buckets cp hf://buckets/username/my-bucket/data.csv data/z;hf buckets cp hf://buckets/username/my-bucket/config.json -zQhf buckets cp model.safetensors hf://buckets/username/my-bucket/model.safetensorsz?hf buckets cp config.json hf://buckets/username/my-bucket/logs/z;hf buckets cp - hf://buckets/username/my-bucket/config.jsonzYhf buckets cp hf://buckets/username/my-bucket/data.csv hf://buckets/username/dest-bucket/z_hf buckets cp hf://buckets/username/source-bucket/logs/ hf://buckets/username/dest-bucket/logs/buckets)FNFN)NFFFNNrk   )FFN)FFFNNN)NNFFFNNFNNNFFFN)/__doc__typingr   r)   huggingface_hubr   huggingface_hub._bucketsr   r   r   r   hf_apir
   
_cli_utilsr   r   r   r   _cpr   _file_listingr   r   
_frameworkr   r   _outputr   r   
get_logger__name__loggerbuckets_clicommandstrboolr   r9   rC   rA   r@   rV   r[   rT   r   r   r   rq   r3   r2   <module>r      s%	   >   #  "   : ( & 
		H	% !EF 	  
0 	 	 	90Rg	
	0R +	
	0R tw	
	0R* F	
	+0R8 90R: 
;0R
0RfC D  	  0 	 	 	 	E>
d
W	
	>
 7	
	>
$ F	
	%>
2 C	
	3>
B C>
D E>
F 
G>
>
B 3Tz 3$J 3  3 	 3
  3 : 3 
 3Fccc c 	c
 :c 
c0 	(5   "Y	
	" " 
""  	  . 	 	-.6Y	
	.6 
,	
	
.6 F	
	.6, -.6. 
/.6.6b 	  	6 	 	 	 	 	[Ha	
	H C	
	H$ 
,	
	
%H4 K	
	5HB S	Dd	
	CHN S	D_	
	OHZ [H\ 
]H	HD 	  * "K`	
	"K e	
	"K "K 
"K"KT 		  & 	 	 	 	 	 	 	 	 	 	 	 	 	 	yrPd
^	
	rP d
c	
	rP B	
	rP& N	
	'rP4 O	
	5rPB d
P	
	CrPN d
6	
	OrPZ H	
	[rPh S	DI	
	irPt S	DI	
	urP@ d
;	
	ArPL T	
	MrPZ V	
	[rPh 8	
	irPx yrPz 
{rPrPv  	   
)r3   