o
    2j(                  
   @   s  d Z ddlZddlZddlmZ ddlmZmZ ddlZddl	m
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 dd
lmZ g dZed Zd*dedB fddZdedB dededB ddfddZdededB dedB ddfddZde
deddfddZde
dededB ddfddZde
dededdfdd Z dededefd!d"Z!de
deded#e"ddf
d$d%Z#de
ded&ee$B d'eddf
d(d)Z%dS )+a  Shared ``cp`` command to copy files between local paths, repositories and buckets.

This single command backs three identical CLI entry points: ``hf cp`` (top-level),
``hf repos cp`` and ``hf buckets cp``. It supports any source/destination combination
of local file, repo/bucket ``hf://`` URI, and ``-`` (stdin/stdout), with two exceptions:
- bucket-to-repo copies are not supported (server limitation), and
- local-to-local copies (use a regular ``cp`` for that).
    N)replace)	AnnotatedLiteral)HfApi)CLIError)HfUriSoftTemporaryDirectorydisable_progress_bars	is_hf_uriparse_hf_uri   )TokenOpt
get_hf_api)out)
z(hf cp hf://username/my-model/config.jsonz6hf cp hf://username/my-model/config.json ./config.jsonz8hf cp hf://datasets/username/my-dataset/data.csv ./data/z3hf cp hf://buckets/username/my-bucket/config.json -zBhf cp ./model.safetensors hf://username/my-model/model.safetensorsz9hf cp ./config.json hf://buckets/username/my-bucket/logs/z3hf cp - hf://buckets/username/my-bucket/config.jsonz;hf cp hf://username/source-model/ hf://username/dest-model/z]hf cp hf://datasets/username/my-dataset/processed/ hf://buckets/username/my-bucket/processed/zihf cp hf://buckets/username/my-bucket/logs/ hf://buckets/username/archive-bucket/  # copies contents only)reposbucketscontextc              	      sL   		ddt ttjddf dt tdB tjddf dtddf fd	d
}|S )zBuild the ``cp`` command function for a given alias.

    The three entry points (`hf cp`, `hf repos cp`, `hf buckets cp`) share the exact same logic;
    'context' only adds a guardrail on the remote endpoint type (see `_enforce_context`).
    Nsrcz?Source: local file, hf:// URI (repo or bucket), or - for stdin.)helpdstzEDestination: local path, hf:// URI (repo or bucket), or - for stdout.tokenreturnc                    s   t  | | t| || dS )a  Copy files between local paths, repositories, and buckets.

        Handles uploads (local/stdin -> repo/bucket), downloads (repo/bucket -> local/stdout) and
        remote-to-remote copies (repo/bucket -> repo/bucket). Bucket-to-repo and local-to-local
        copies are not supported. For directories, use `hf upload`/`hf download` (repos) or
        `hf buckets sync` (buckets). Remote-to-remote copies only work within the same storage
        region (https://huggingface.co/docs/hub/storage-regions).
        N)_enforce_context_run_cp)r   r   r   r    R/var/www/html/whisper/venv/lib/python3.10/site-packages/huggingface_hub/cli/_cp.pycpB   s   zmake_cp.<locals>.cp)NN)r   strtyperArgumentr   )r   r   r   r   r   make_cp;   s(   

	
r!   r   r   r   c                 C   sl   | du rdS |durt |r|n|}t |sdS | dkr%t|jr%td| dkr2t|js4tddS dS )a  Guardrail for the `hf repos cp` / `hf buckets cp` aliases.

    These aliases are exact duplicates of `hf cp`, so a bare `hf repos cp` could otherwise touch a
    bucket (and vice versa). We validate the type of the remote side: the destination for uploads and
    remote-to-remote copies, or the source when downloading to a local path / stdout. The top-level
    `hf cp` (i.e. 'context' is None) accepts any combination.
    Nr   zW`hf repos cp` only works with repositories. Use `hf cp` or `hf buckets cp` for buckets.r   zW`hf buckets cp` only works with buckets. Use `hf cp` or `hf repos cp` for repositories.)r
   r   	is_bucketr   )r   r   r   remoter   r   r   r   [   s   r   r   c                 C   s   t |d}| dk}|dk}t| }|d uot|}|r3|r3|d us#J || | tjd| |d d S |sE|sE|d u r@tdtd|rX|rPt||  d S t|| | d S |d us^J t	|| ||d d S )N)r   -Copiedr   r   z?Missing destination. Provide a repo or bucket hf:// URI as DST.zVOne of SRC or DST must be a repo (hf://username/...) or bucket (hf://buckets/...) URI.)src_is_stdin)
r   r
   
copy_filesr   resultr   BadParameter_download_file_to_stdout_download_file_to_local_upload_file_to_remote)r   r   r   apir'   dst_is_stdout	src_is_hf	dst_is_hfr   r   r   r   o   s0   


r   r.   c              
   C   s   t |}t||}t [ t ?}tj||}t| || t|d}|	d }r9t
jj| |	d }s+W d    n1 sCw   Y  W d    n1 sRw   Y  W d    d S W d    d S 1 sjw   Y  d S )Nrbi H)r   _source_filenamer	   r   ospathjoin_download_singleopenreadsysstdoutbufferwrite)r.   r   urifilenametmp_dirtmp_pathfchunkr   r   r   r+      s"   
"r+   c                 C   s   t |}t||}|d u r|}ntj|s!|tjs!|dr)tj||}n|}tj|}|r:tj	|dd t
| || tjd||d d S )N/T)exist_ok
Downloadedr&   )r   r3   r4   r5   isdirendswithsepr6   dirnamemakedirsr7   r   r)   )r.   r   r   r>   r?   
local_path
parent_dirr   r   r   r,      s   
"r,   r>   rL   c                 C   s   |j r| j|j|j|fgdd dS tj|pd}td|d}| j|j|j	|j|j
|d}t|| W d   dS 1 s@w   Y  dS )zDownload a single file (repo or bucket) to ``local_path``.

    Used by `_download_file_to_local` and `_download_file_to_stdout`.
    T)raise_on_missing_files.z.tmp)prefixdir)repo_id	repo_typer?   revision	local_dirN)r"   download_bucket_filesidpath_in_repor4   r5   rJ   r   hf_hub_downloadtyperT   r   )r.   r>   rL   rM   r@   downloaded_pathr   r   r   r7      s   "r7   c                 C   s0   | j dks
|drtd| j ddd S )N rD   zSource path must include a file name, not just a repo/bucket or directory path. Use `hf download` or `hf buckets sync` to copy directories.r   )rX   rH   r   r*   rsplit)r>   r   r   r   r   r3      s
   r3   r'   c                C   s   t |}|r/|jdks|drtdtjj }t	| |||j t
jdd| d d S tj|r:tdtj|sHtd| |j}|dkrVtj|}n|drf|d tj| }n|}t	| ||| t
jd|t||d	 d d S )
Nr\   rD   zAStdin upload requires a full destination path including filename.Uploadedstdinr&   z]Source must be a file, not a directory. Use `hf upload` or `hf buckets sync` for directories.zSource file not found: )rX   )r   rX   rH   r   r*   r:   r`   r<   r9   _upload_singler   r)   to_urir4   r5   rG   isfilebasenamer   )r.   r   r   r'   r>   datarP   remote_pathr   r   r   r-      s,   

 r-   sourcerf   c                 C   s>   |j r| j|j||fgd dS | j|||j|j|jd dS )z4Upload a single file or bytes (to a repo or bucket).)add)path_or_fileobjrX   rR   rS   rT   N)r"   batch_bucket_filesrW   upload_filerZ   rT   )r.   r>   rg   rf   r   r   r   ra      s   
ra   )N)&__doc__r4   r:   dataclassesr   typingr   r   r   huggingface_hubr   huggingface_hub.errorsr   huggingface_hub.utilsr   r   r	   r
   r   
_cli_utilsr   r   _outputr   CP_EXAMPLES	CpContextr!   r   r   r   r+   r,   r7   r3   boolr-   bytesra   r   r   r   r   <module>   s,   	" "$	&