o
    0j,h                  $   @   s  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mZ ddlmZ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eZeddZ de!de"fddZ#de!de$e!e!f fddZ%e j&dg dd			ddee!ej'ddf dee"ej(dddf dee"ej(dddf d eddf
d!dZ)de!de"fd"d#Z*e j&d$g d%d						ddee!dB ej'd&df d'ee"ej(d(d)d*df d+ee"ej(d,d-df d.ee"ej(d/d0d1df d2ed eddfd3d4Z+d5e!dB d2e!dB d'e"d+e"d.e"d e!dB ddfd6d7Z,de!d'e"d+e"d.e"d e!dB ddfd8d9Z-e j&d:d;d<gd	ddee!ej'd=df d eddfd>d:Z.e j&d?g d@d			ddee!ej'd=df dAee"ej(dBdCdDdf dEee"ej(dFdGdf d eddf
dHd?Z/e j&dIg dJd						ddee!ej'dKdf d.ee"ej(d/d0dLdf dAee"ej(dBdCdDdf dMee"ej(dNdOdf dPee0e! dB ej(dQdf dRee0e! dB ej(dSdf d eddfdTdUZ1e j&dVg dWd	ddXee!ej'dYdf dZee!ej'd[df d eddfd\dVZ2e j&d]g d^d															dd_ee!dB ej'd`df daee!dB ej'dbdf d?ee"ej(dcdf ddee"ej(dedfdf dgee"ej(dhdidf djee!dB ej(dkdf dlee!dB ej(dmdf dMee"ej(dNdndf dPee0e! dB ej(dodf dRee0e! dB ej(dpdf dqee!dB ej(drdf dsee"ej(dtdudf dvee"ej(dwdxdf dyee"ej(dzd{d|df d eddf d}d]Z3e j&d~g dd		ddee!ej'ddf dee!dB ej'ddf d eddfdd~Z4dS )z7Contains commands to interact with buckets via the CLI.    N)	Annotated)logging)BUCKET_PREFIX
BucketFileFilterMatcher_is_bucket_path_parse_bucket_path_split_bucket_id_and_prefix)SoftTemporaryDirectorydisable_progress_bars   )	SearchOptTokenOpt
get_hf_apityper_factory)format_sizeprint_file_listing)OutputFormatWithAutooutz"Commands to interact with buckets.)helppathreturnc                 C   s
   |  dS )Nzhf://)
startswithr    r   \/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/huggingface_hub/cli/buckets.py_is_hf_handle4   s   
r   argumentc              	   C   sB   |  tr	t| S zt| W S  ty    td|  dt dw )zParse a bucket argument accepting both 'namespace/name(/prefix)' and 'hf://buckets/namespace/name(/prefix)'.

    Returns:
        tuple: (bucket_id, prefix) where bucket_id is "namespace/bucket_name" and prefix may be empty string.
    zInvalid bucket argument: -. Must be in format namespace/bucket_name or znamespace/bucket_name)r   r   r   r	   
ValueError)r   r   r   r   _parse_bucket_argument8   s   

r    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-ok)nameZexamplesF	bucket_idzTBucket ID: bucket_name, namespace/bucket_name, or hf://buckets/namespace/bucket_nameprivatez	--privatezCreate a private bucket.exist_okz
--exist-okz3Do not raise an error if the bucket already exists.tokenc           	   
   C   s   t |d}| tr6zt| \}}W n ty% } ztt|d}~ww |r4td|  dt d|} |j| |r=|nd|d}t	j
d|j|jd dS )	zCreate a new bucket.r&   Nz-Cannot specify a prefix for bucket creation: . Use namespace/bucket_name or namespace/bucket_name.)r$   r%   zBucket created)handleurl)r   r   r   r    r   typerBadParameterstrZcreate_bucketr   resultr*   r+   )	r#   r$   r%   r&   api	parsed_idprefixeZ
bucket_urlr   r   r   r!   I   s*   
"

c                 C   s.   |  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)r   r   r   r   r   _is_bucket_id   s
   
r6   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                 C   sT   | duot | }|r|durtdt| ||||d dS t| |||||d 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.)r   r7   r8   r9   r&   )	namespacer:   r7   r8   r9   r&   )r6   r,   r-   _list_files_list_buckets)r   r7   r8   r9   r:   r&   Zis_file_moder   r   r   list_cmd   s&   9


r>   r;   c                    s   |rt d|rt d| dur$| tr$| ttd } | d} t|d} fdd|j| |dD }tj	|d	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.Nr4   r'   c                    s6   g | ]}|j |j rt|j n|j|j|jd qS ))idr$   sizetotal_files
created_at)r?   r$   r   r@   rA   rB   ).0bucketr7   r   r   
<listcomp>   s    z!_list_buckets.<locals>.<listcomp>)r;   r:   right)r@   rA   )Z
alignments)
r,   r-   r   r   r5   rstripr   Zlist_bucketsr   table)r;   r:   r7   r8   r9   r&   r0   itemsr   rE   r   r=      s   






r=   c           
   
   C   s   |rt jtjkrtdt|d}zt| \}}W n ty- } ztt	|d}~ww t
|j||p5d|d}	t|	|||d dS )zList files in a bucket.z%Cannot use --tree with --format json.r'   Nr2   r9   )r7   r8   r9   )r   moder   jsonr,   r-   r   r    r   r.   listlist_bucket_treer   )
r   r7   r8   r9   r&   r0   r#   r2   r3   rJ   r   r   r   r<      s"   

r<   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              
   C   s^   t |d}zt| \}}W n ty  } ztt|d}~ww ||}tj|dd dS )zGet info about a bucket.r'   Nr?   )Zid_key)	r   r    r   r,   r-   r.   Zbucket_infor   dict)r#   r&   r0   r1   _r3   rD   r   r   r   rP     s   

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              
   C   s   |  tr2zt| \}}W n ty  } ztt|d}~ww |r/td|  dt d|} nd| vrBtd|  dt dtjd|  d	|d
 t	|d}|j
| |d tjd| d dS )zDelete a bucket.

    This deletes the entire bucket and all its contents. Use `hf buckets rm` to remove individual files.
    Nz-Cannot specify a prefix for bucket deletion: r(   r)   r4   zInvalid bucket ID: r   z(Are you sure you want to delete bucket ''?rT   r'   )rU   zBucket deletedr#   )r   r   r    r   r,   r-   r.   r   confirmr   Zdelete_bucketr/   )r#   rT   rU   r&   r1   r2   r3   r0   r   r   r   rS   6  s0   
%
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              
      s  zt | \ }W n ty } ztt|d}~ww |dkr.|s.td  d  d|s2|r9|s9tdt|d}	|rtd}
g }|	j |	d	pQdd
dD ]}t
|trl|| |
dt| d qU|
dt| d |s||rt||dfdd|D }n|}dd |D }tdd |D }t|d
d}|std dS t| d| }|s|stddd |D  tjd| d  ddd |rtd fdd|D  td| d  dS |	j |d! tjd"| d  d# t||d$ dS |	d	}|std%|r0td&t   d	|  td' dS tjd(| d)  d|d |	j |gd! tjd*| d+ dS ),zbRemove files from a bucket.

    To delete an entire bucket, use `hf buckets delete` instead.
    N 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 remoter4   TrK   zListing files from remote (z files))Zinclude_patternsZexclude_patternsc                    s   g | ]
}  |jr|qS r   )matchesr   rC   f)matcherr   r   rF     s    zremove.<locals>.<listcomp>c                 S   s   g | ]}|j qS r   r   r_   r   r   r   rF     s    c                 s   s    | ]}|j V  qd S N)r@   r_   r   r   r   	<genexpr>  s    zremove.<locals>.<genexpr>rE   zNo files to remove.z file(s) totaling 
c                 s   s    | ]}d | V  qdS )z  Nr   rC   r   r   r   r   rc     s    zRemove z from 'rV   FrW   c                 3   s$    | ]}d t    d| V  qdS )delete: r4   N)r   re   rX   r   r   rc     s   " z
(dry run) z would be removed.)rS   zRemoved ')r#   Zfiles_deletedr@   zFile path cannot be empty.rf   z"(dry run) 1 file would be removed.zRemove 'z' from 'zFile removed)r   r#   )r    r   r,   r-   r.   r   r   statusrO   rH   
isinstancer   appendupdater5   doner   sumr   textjoinrY   batch_bucket_filesr/   r   )r   r9   rT   rZ   r[   r\   r&   r2   r3   r0   rh   	all_filesitemZmatched_files
file_pathsZ
total_sizesize_strZcount_label	file_pathr   )r#   ra   r   removes  s   =










rv   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                 C   s|   t | \}}|rtd|  dt dt |\}}|r(td| dt dt|d}|j||d tjd||d dS )z2Move (rename) a bucket to a new name or namespace.z)Cannot specify a prefix for bucket move: r(   r)   r'   )rx   ry   zBucket movedN)r    r,   r-   r   r   Zmove_bucketr   r/   )rx   ry   r&   Zparsed_from_idZfrom_prefixZparsed_to_idZ	to_prefixr0   r   r   r   rw      s"   
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                 C   s`   t |d}|j| ||||||||	|
||||t d |r,t s.td| d dS dS dS )z0Sync files between local directory and a bucket.r'   )r{   r|   rS   r}   r~   r   r   r[   r\   r   r   r   rZ   r   quietzRun `hf buckets sync --apply z` to execute this plan.N)r   Zsync_bucketr   Zis_quiethint)r{   r|   rS   r}   r~   r   r   rZ   r[   r\   r   r   r   r   r&   r0   r   r   r   rz   2  s*   
l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/srczMSource: local file, any hf:// handle (model, dataset, bucket), or - for stdindstz@Destination: local path, bucket hf://... handle, or - for stdoutc              
   C   s  t |d}t| }|duot|}t| }|duot|}| dk}|dk}	|rO|rOz|| | W n tyD }
 ztt|
d}
~
ww tj	d| |d dS |sc|sc|sc|du r^tdtd|rl|sltd|r|rt
|\}}|d	ks|d
rtd|	r|std|s|stj| rtd|rmt
| \}}|d	ks|d
rtd|d
dd }|	r$t V t B}tj||}||||fg t|d}|d }rtjj| |d }sW d   n1 sw   Y  W d   n	1 sw   Y  W d   n1 sw   Y  dS dS |du r,|}ntj|s@|tjs@|d
rHtj||}n|}tj|}|rZtj|dd ||||fg tj	d| |d dS |rt
|\}}tjj }|j|||fgd tj	dd|d dS tj| std|  t
|\}}|d	krtj | }n|d
r|tj |  }n|}|j|| |fgd tj	d| t! | d
| d dS )zCopy files to or from buckets.r'   N-Z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.r]   r4   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%   Z
Downloaded)addZUploadedstdinzSource file not found: )"r   r   r   Z
copy_filesr   r,   r-   r.   r   r/   r   endswithosr   isdirrsplitr   r
   ro   Zdownload_bucket_filesopenreadsysstdoutbufferwritesepdirnamemakedirsr   rp   isfilebasenamer   )r   r   r&   r0   Z	src_is_hfZ	dst_is_hfZsrc_is_bucketZdst_is_bucketZsrc_is_stdinZdst_is_stdoutr3   rR   r2   r#   filenameZtmp_dirZtmp_pathr`   chunkZ
local_path
parent_dirZremote_pathdatar   r   r   r     s   







&
	(
")FFN)NFFFNNrb   )FFFNNN)NNFFFNNFNNNFFFN)NN)5__doc__r   r   typingr   r,   Zhuggingface_hubr   Zhuggingface_hub._bucketsr   r   r   r   r   r	   Zhuggingface_hub.utilsr
   r   Z
_cli_utilsr   r   r   r   Z_file_listingr   r   _outputr   r   Z
get_logger__name__loggerZbuckets_clir.   boolr   tupler    commandZArgumentOptionr!   r6   r>   r=   r<   rP   rS   rN   rv   rw   rz   r   r   r   r   r   <module>   s   

.	
!"#A
#
4
!
'
-. *!'-4
:
@FMT\]z