
    _j0e              $          d Z ddlZddl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mZ ddl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mZ d	dlmZmZ  ej        e           Z! ed          Z"e"#                    dg d          	 	 	 	 ddee$ ej%        d          f         dee& ej'        dd          f         deedz   ej'        dd          f         dee& ej'        dd          f         deddfd             Z(d!e$de&fd"Z)e"#                    d#g d$          	 	 	 	 	 	 dd!ee$dz   ej%        d%          f         d&ee& ej'        d'd(d)          f         d*ee& ej'        d+d,          f         d-ee& ej'        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"#                    d6d7d8g          	 ddee$ ej%        d9          f         deddfd:            Z-e"#                    d;g d<          	 	 	 ddee$ ej%        d9          f         d=ee& ej'        d>d?d@          f         dAee& ej'        dBdC          f         deddf
dD            Z.e"#                    dEg dF          	 	 	 	 	 	 dd!ee$ ej%        dG          f         d-ee& ej'        d.d/dH          f         d=ee& ej'        d>d?d@          f         dIee& ej'        dJdK          f         dLee/e$         dz   ej'        dM          f         dNee/e$         dz   ej'        dO          f         deddfdP            Z0e"#                    dQg dR          	 ddSee$ ej%        dT          f         dUee$ ej%        dV          f         deddfdW            Z1e"#                    dXg dY          	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZee$dz   ej%        d[          f         d\ee$dz   ej%        d]          f         d;ee& ej'        d^          f         d_ee& ej'        d`da          f         dbee& ej'        dcdd          f         deee$dz   ej'        df          f         dgee$dz   ej'        dh          f         dIee& ej'        dJdi          f         dLee/e$         dz   ej'        dj          f         dNee/e$         dz   ej'        dk          f         dlee$dz   ej'        dm          f         dnee& ej'        dodp          f         dqee& ej'        drds          f         dtee& ej'        dudvdw          f         deddf dx            Z2e"#                    dyg dz          	 	 dd{ee$ ej%        d|          f         d}ee$dz   ej%        d~          f         deddfd            Z3dS )z7Contains commands to interact with buckets via the CLI.    N)	Annotated)logging)BUCKET_PREFIX
BucketFileFilterMatcher_is_bucket_path_parse_bucket_uri)SoftTemporaryDirectorydisable_progress_bars	is_hf_uri   )REPO_REGIONS   )	SearchOptTokenOpt
get_hf_apityper_factory)format_sizeprint_file_listing)OutputFormatWithAuto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examplesF	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                 t   t          |          }|                     t                    r=t          |           }|j        r t          j        d|  dt           d          |j        } |                    | |r|nd||          }t          j
        d|j                                        |j                   dS )	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typerBadParameteridcreate_bucketr   resultr&   to_urir'   )r   r   r   r   r    apiparsed
bucket_urls           X/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/huggingface_hub/cli/buckets.pyr   r   1   s    T 5
!
!
!CM** "9-- 	$W	 W W1>W W W   I	""",	 #  J JZ^%:%:%<%<*.QQQQQQ    argumentc                 |    |                      t                    r| t          t                    d         }n| }d|v S )zFCheck if argument is a bucket ID (namespace/name) vs just a namespace.N/)r(   r   len)r5   paths     r3   _is_bucket_idr:   o   sA    =)) M**,,-$;r4   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ot          |           }|r,|t          j        d          t          | ||||           dS t	          | |||||           dS )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.)r5   r;   r<   r=   r    )	namespacer>   r;   r<   r=   r    )r:   r*   r+   _list_files_list_buckets)r5   r;   r<   r=   r>   r    is_file_modes          r3   list_cmdrD   x   s    r 4'CM(,C,CL 
$%NOOO)	
 	
 	
 	
 	
 	
 	)	
 	
 	
 	
 	
 	
r4   r@   c                    |rt          j        d          |rt          j        d          | K|                     t                    r1| t	          t                    d         } |                     d          } t          |          }fd|                    | |          D             }t          j	        |ddd	
           dS )zList buckets in a namespace.z'Cannot use --tree when listing buckets.z,Cannot use --recursive when listing buckets.Nr7   r#   c                     g | ]:}|j         |j        rt          |j                  n|j        |j        |j        d ;S ))r,   r   sizetotal_files
created_at)r,   r   r   rG   rH   rI   ).0bucketr;   s     r3   
<listcomp>z!_list_buckets.<locals>.<listcomp>   se     	 	 	  )~@N_K^<<<TZT_!- +	
 	
	 	 	r4   )r@   r>   right)rG   rH   )
alignments)
r*   r+   r(   r   r8   rstripr   list_bucketsr   table)r@   r>   r;   r<   r=   r    r0   itemss     `     r3   rB   rB      s      L !JKKK Q !OPPP !5!5m!D!Dc-00223	$$S))	
5
!
!
!C	 	 	 	 &&6&JJ	 	 	E Ie I IJJJJJJr4   c                 ,   |r.t           j        t          j        k    rt	          j        d          t          |          }t          |           }t          |	                    |j
        |j        pd|                    }t          ||||           dS )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   )r5   r;   r<   r=   r    r0   r1   rR   s           r3   rA   rA      s      J383888 !HIII
5
!
!
!Cx((FI&.$ 	 	
 	
 E u^WXabbbbbbr4   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                  }t	          j        |d           dS )zGet info about a bucket.r#   r,   )id_keyN)r   r	   bucket_infor,   r   dict)r   r    r0   r1   rK   s        r3   rZ   rZ     sO    " 5
!
!
!Cy))F__VY''FHVD!!!!!!r4   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                    |                      t                    r>t          |           }|j        r t	          j        d|  dt           d          |j        } n$d| vr t	          j        d|  dt           d          t          j        d|  d|	           t          |
          }|
                    | |           t          j        d|            dS )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%   r7   zInvalid bucket ID: z-. Must be in format namespace/bucket_name or z(Are you sure you want to delete bucket ''?r`   r#   )ra   zBucket deleted)r   N)r(   r   r	   r)   r*   r+   r,   r   confirmr   delete_bucketr.   )r   r`   ra   r    r1   r0   s         r3   r_   r_     s-   J M** 
"9-- 	$W	 W W1>W W W   I			I		 a) a a;Ha a a
 
 	

 KH9HHHcRRRR
5
!
!
!CiJ777J9555555r4   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                 L   t          |           }|j        |j        }|dk    r|st          j        d d d          |s|r|st          j        d          t          |          }	|rt          j        d          }
g }|	                    |pdd	
          D ]R}t          |t                    r;|                    |           |
                    dt          |           d           S|
                    dt          |           d           |s|r t          ||          fd|D             }n|}d |D             }t!          d |D                       }t#          |d	          }|st          j        d           dS t          |           d| }|sP|sNt          j        d                    d |D                                  t          j        d| d dd           |rMt          j        d                    fd|D                                  t          j        d| d           dS |	                    |           t          j        d| d d t          |          |!           dS |}|st          j        d"          |r7t          j        d#t.            d$|            t          j        d%           dS t          j        d&| d' d|           |	                    |g           t          j        d(|)           dS )*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 remoteNTrT   zListing files from remote (z files))include_patternsexclude_patternsc                 H    g | ]}                     |j                  |S  )matchesr9   )rJ   fmatchers     r3   rL   zremove.<locals>.<listcomp>  s,    MMM1W__QV5L5LMQMMMr4   c                     g | ]	}|j         
S ro   )r9   rJ   rq   s     r3   rL   zremove.<locals>.<listcomp>  s    444af444r4   c              3   $   K   | ]}|j         V  d S N)rG   rt   s     r3   	<genexpr>zremove.<locals>.<genexpr>  s$      77A777777r4   )r;   zNo files to remove.z file(s) totaling 
c              3       K   | ]	}d | V  
dS )z  Nro   )rJ   r9   s     r3   rw   zremove.<locals>.<genexpr>  s(      BBt{D{{BBBBBBr4   zRemove z from 'rc   Frd   c              3   6   K   | ]}d t             d| V  dS )delete: r7   N)r   )rJ   r9   r   s     r3   rw   zremove.<locals>.<genexpr>  s:      ccPTK-KKKTKKccccccr4   z
(dry run) z would be removed.)r_   zRemoved ')r   files_deletedrG   zFile path cannot be empty.r{   r7   z"(dry run) 1 file would be removed.zRemove 'z' from 'zFile removed)r9   r   )r	   r,   r)   r*   r+   r   r   statusrY   
isinstancer   appendupdater8   doner   sumr   textjoinre   batch_bucket_filesr.   r   )r5   r=   r`   rg   rh   ri   r    r1   rU   r0   r~   	all_filesitemmatched_files
file_paths
total_sizesize_strcount_label	file_pathr   rr   s                      @@r3   remover   T  s   z x((F	I F||I| R R RDMR R R
 
 	
 	 Q7 QI Q !OPPP
5
!
!
!C <H788&(	((>T ) 
 
 	U 	UD
 $
++ U  &&&SC	NNSSSTTTI#i..IIIJJJ 	&g 	&#WwWWWGMMMM	MMMMM%M44m444
7777777
z$??? 	H*+++FZFFHFF 	P7 	PHTYYBBzBBBBBCCCKC+CCiCCCOOOO 	HTYYccccXbcccccdddHA+AAABBBFy<<<
7{779777j//		
 	
 	
 	
 	
 	
 	 	C$%ABBB 	HFFyFF9FFGGGH9:::F?y??)???SIIIIy)===
>	YGGGGGGr4   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                   t          j	        d|j        |j                   dS )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_tor0   s         r3   r   r     s    2 $G,,K 
 S S S-:S S S
 
 	
 "%((I 
 S S S-:S S S
 
 	

 5
!
!
!COOKN),O???J~{~Y\JJJJJJr4   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          |          }|                    | ||||||||	|
||||t          j                               |r-t          j                    st          j        d| d           dS dS dS )z0Sync files between local directory and a bucket.r#   )r   r   r_   r   r   r   r   rh   ri   r   r   r   rg   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   rg   rh   ri   r   r   r   r   r    r0   s                   r3   r   r     s    X 5
!
!
!COO!!'lnn    "  PCLNN PNNNNOOOOOP P P Pr4   cp)z5hf buckets cp hf://buckets/user/my-bucket/config.jsonz=hf buckets cp hf://buckets/user/my-bucket/config.json ./data/zDhf buckets cp hf://buckets/user/my-bucket/config.json my-config.jsonz7hf buckets cp hf://buckets/user/my-bucket/config.json -z8hf buckets cp my-config.json hf://buckets/user/my-bucketz>hf buckets cp my-config.json hf://buckets/user/my-bucket/logs/zKhf buckets cp my-config.json hf://buckets/user/my-bucket/remote-config.jsonz7hf buckets cp - hf://buckets/user/my-bucket/config.jsonzchf buckets cp hf://buckets/user/my-bucket/logs hf://buckets/user/archive-bucket/  # nests logs/ dirzihf buckets cp hf://buckets/user/my-bucket/logs/ hf://buckets/user/archive-bucket/  # copies contents onlyzehf buckets cp hf://datasets/user/my-dataset/processed/ hf://buckets/user/my-bucket/dataset/processed/srczJSource: local file, any hf:// URI (model, dataset, bucket), or - for stdindstz=Destination: local path, bucket hf://... URI, or - for stdoutc                 
   t          |          }t          |           }|duot          |          }t          |           }|duot          |          }| dk    }|dk    }	|rf|rd	 |                    | |           n3# t          $ r&}
t          j        t          |
                    d}
~
ww xY wt          j	        d| |           dS |s.|s,|s*|t          j        d          t          j        d          |r|st          j        d          |rE|rCt          |          }|j        d	k    s|                    d
          rt          j        d          |	r|st          j        d          |s5|s3t          j                            |           rt          j        d          |rRt          |           }|j        |j        }}|d	k    s|                     d
          rt          j        d          |                    d
d          d         }|	rt%                      5  t'                      5 }t          j                            ||          }|                    |||fg           t-          |d          5 }|                    d          x}r;t0          j        j                            |           |                    d          x};ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   dS dS ||}nvt          j                            |          s4|                    t          j                  s|                    d
          r!t          j                            ||          }n|}t          j                            |          }|rt          j        |d           |                    |||fg           t          j	        d| |           dS |rot          |          }t0          j        j                                        }|                     |j        ||j        fg           t          j	        dd|           dS t          j        !                    |           st          j        d|            t          |          }|j        |j        }}|d	k    r t          j        "                    |           }n=|                    d
          r&|d
z   t          j        "                    |           z   }n|}|                     || |fg           t          j	        d| tF           | d
|            dS )zCopy files to or from buckets.r#   N-Copied)r   r   z2Missing destination. Provide a bucket path as DST.z;One of SRC or DST must be a bucket path (hf://buckets/...).z+Stdin upload requires a bucket destination.rk   r7   zAStdin upload requires a full destination path including filename.z"Cannot pipe to stdout for uploads.zNSource must be a file, not a directory. Use `hf buckets sync` for directories.zJSource path must include a file name, not just a bucket or directory path.r   rbi HT)r   
Downloaded)addUploadedstdinzSource file not found: )$r   r   r   
copy_files
ValueErrorr*   r+   strr   r.   r	   r)   endswithosr9   isdirr,   rsplitr   r
   r   download_bucket_filesopenreadsysstdoutbufferwritesepdirnamemakedirsr   r   isfilebasenamer   )r   r   r    r0   	src_is_hf	dst_is_hfsrc_is_bucketdst_is_bucketsrc_is_stdindst_is_stdouter1   r   rU   filenametmp_dirtmp_pathrq   chunk
local_path
parent_dirdataremote_paths                          r3   r   r     s_   4 5
!
!
!C#I42IcNNI#C((MtO<(<(<M#:L3JM  Y 	-NN3$$$$ 	- 	- 	-$SVV,,,	- 	
8#....  ` `| `;$%YZZZ !^___ PM P !NOOO j j"3''"$$S(9(9$$%hiii G] G !EFFF s s"'--2D2D s !qrrr  ;Y"3''"Iv':6	R<<3<<,,<$%qrrr==a((, 	> '(( ; ;+-- ;!w||GX>>H--i68:L9MNNNh-- ;'(vvj'9'99e ;J-33E::: ()vvj'9'99e ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; {%

s## !s||BF';'; !s||C?P?P !W\\#x88

 
 44J 7J6666%%i6:2F1GHHHJ|*======	 Y"3''y$$&&vyf6I/J.KLLL
:7444444 w~~c"" 	F$%Ds%D%DEEE"3''"Iv':6	R<<'**3//KK\\# 	! 3,)9)9#)>)>>KK Ky[/A.BCCC
:3},Wi,W,W+,W,WXXXXXXst   &A= =
B-!B((B-#L	2A
K2<AKK2KK2"K#K2&L	2K6	6L	9K6	:L		LL)FNFN)NFFFNNrv   )FFN)FFFNNN)NNFFFNNFNNNFFFN)NN)4__doc__r   r   typingr   r*   huggingface_hubr   huggingface_hub._bucketsr   r   r   r   r	   huggingface_hub.utilsr
   r   r   hf_apir   
_cli_utilsr   r   r   r   _file_listingr   r   _outputr   r   
get_logger__name__loggerbuckets_clicommandr   ArgumentboolOptionr   r:   rD   rB   rA   rZ   r_   rX   r   r   r   r   ro   r4   r3   <module>r      s   > = 				 



        # # # # # #              [ Z Z Z Z Z Z Z Z Z ! ! ! ! ! !            ; : : : : : : : . . . . . . . . 
	H	%	% m!EFFF 	    
 
0 	 	 	90R 0Rg	
 	
 	
	0R +	
 	
 	
	0R tw	
 	
 	
	0R* F	
 	
 	
	+0R8 90R: 
;0R 0R 0R
 
0RfC D     	     0 	 	 	 	E>
 >
d
W	
 	
 	
	>
 7	
 	
 	
	>
$ F	
 	
 	
	%>
2 C	
 	
 	
	3>
B C>
D E>
F 
G>
 >
 >
 >
B KTz K$J K  K 	 K
  K : K 
 K  K  K  KFccc c 	c
 :c 
c c c c0 	(5    " "Y	
 	
 	
	" " 
" " " "  	     . 	 	-.6 .6Y	
 	
 	
	.6 
,	
 	
 	
	
.6 F	
 	
 	
	.6, -.6. 
/.6 .6 .6 .6b 	    	 	6 	 	 	 	 	[H 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 H	 	HD 	     * "K "K`	
 	
 	
	"K e	
 	
 	
	"K "K 
"K "K "K "KT 		 	 	   & 	 	 	 	 	 	 	 	 	 	 	 	 	 	yrP 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 rP rPt 	     , 	nY nY	^U^!mnnnn
nY 
d
NEN(ghhhh
	nY nY 
nY nY nY!  nY nY nYr4   