
    -:j(              
          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z  fdZdedz  dededz  ddfdZdededz  dedz  ddfdZde
deddfdZde
dededz  ddfdZde
dededdfdZ dededefdZ!de
dedede"ddf
dZ#de
dedee$z  deddf
d Z%y)"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           	           	 	 d
dt         t        t        j                  d      f   dt         t        dz  t        j                  d      f   dt        ddf f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                 :    t        | |       t        | ||       y)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   s      Y/var/www/html/tokenscope/api/venv/lib/python3.12/site-packages/huggingface_hub/cli/_cp.pycpzmake_cp.<locals>.cpB   s    & 	#s+S%     )NN)r   strtyperArgumentr   )r   r   s   ` r   make_cpr#   ;   sw     !NN abd
!
 $JNN ghj
! ! 
!, Ir   r   r   r   c                     | y|t        |      r|n|}t        |      sy| dk(  r t        |      j                  rt        d      | dk(  r!t        |      j                  st        d      yy)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   remotes       r   r   r   [   ss     _3ScFV'l62<<pqq)L$8$B$Bpqq %Cr   r   c                    t        |      }| dk(  }|dk(  }t        |       }|d uxr t        |      }|r1|r/|J |j                  | |       t        j                  d| |       y |s.|s,|t        j                  d      t        j                  d      |r|rt        ||        y t        || |       y |J t        || ||       y )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_hfs           r   r   r   o   s    
5
!C#:L3JM#I42IcNI YsC 

8#. Y;$$%fgg  d
 	

 $S#.S#. ??3S|Dr   r2   c                    t        |      }t        ||      }t               5  t               5 }t        j
                  j                  ||      }t        | ||       t        |d      5 }|j                  d      x}r=t        j                  j                  j                  |       |j                  d      x}r=d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nrbi H)r   _source_filenamer
   r	   ospathjoin_download_singleopenreadsysstdoutbufferwrite)r2   r   urifilenametmp_dirtmp_pathfchunks           r   r/   r/      s    
s
CS)H		  3#% 	3ww||GX6HS#x0h% 3 vvj11e1JJ%%++E2  !vvj11e13	33 33 3	3 	33 3s;   C):C'AC8C C)CCC&	"C))C2c                    t        |      }t        ||      }||}nrt        j                  j	                  |      s0|j                  t        j                        s|j                  d      r!t        j                  j                  ||      }n|}t        j                  j                  |      }|rt        j                  |d       t        | ||       t        j                  d||       y )N/T)exist_ok
Downloadedr*   )r   r8   r9   r:   isdirendswithsepr;   dirnamemakedirsr<   r   r-   )r2   r   r   rC   rD   
local_path
parent_dirs          r   r0   r0      s    
s
CS)H
{
	s	s||BFF3s||C7HWW\\#x0

,J
J.S#z*JJ|*5r   rC   rR   c                    |j                   r,| j                  |j                  |j                  |fgd       yt        j
                  j                  |      xs d}t        d|      5 }| j                  |j                  |j                  |j                  |j                  |      }t	        j                  ||       ddd       y# 1 sw Y   yxY w)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_typerD   revision	local_dirN)r%   download_bucket_filesidpath_in_repor9   r:   rP   r	   hf_hub_downloadtyper[   r   )r2   rC   rR   rS   rE   downloaded_paths         r   r<   r<      s    
 }}!!#&&C,<,<j+I*Jcg!h
 WW__Z07C
#6zB 	4g!11(())! 2 O JJ
3	4 	4 	4s   )ACCc                     | j                   dk(  s|j                  d      rt        j                  d      | j                   j	                  dd      d   S )N rJ   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   )r_   rN   r!   r.   rsplit)rC   r   s     r   r8   r8      sT    
2c!2  K
 	
 ""3*2..r   r+   c                ~   t        |      }|r|j                  dk(  s|j                  d      rt        j                  d      t
        j                  j                  j                         }t        | |||j                         t        j                  dd|j                                y t        j                  j                  |      rt        j                  d      t        j                  j!                  |      st        j                  d|       |j                  }|dk(  r t        j                  j#                  |      }n9|j                  d      r&|dz   t        j                  j#                  |      z   }n|}t        | |||       t        j                  d|t%        ||	      j                                y )
Nrd   rJ   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: )r_   )r   r_   rN   r!   r.   r?   ri   rA   r>   _upload_singler   r-   to_urir9   r:   rM   isfilebasenamer   )r2   r   r   r+   rC   datarW   remote_paths           r   r1   r1      sR   
s
Cr!S\\#%6$$%hiiyy$$&sCs'7'78

:7

=	ww}}S  k
 	
 77>>#  #:3%!@AAF|gg&&s+	c	slRWW%5%5c%::3S+.JJzs+(N(U(U(WXr   sourcero   c                     |j                   r!| j                  |j                  ||fg       y| j                  |||j                  |j                  |j
                         y)z4Upload a single file or bytes (to a repo or bucket).)add)path_or_fileobjr_   rY   rZ   r[   N)r%   batch_bucket_filesr^   upload_filera   r[   )r2   rC   rp   ro   s       r   rj   rj      sT    
}}svvV[,A+BC"$FFhh\\ 	 	
r   )N)&__doc__r9   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/   r0   r<   r8   boolr1   bytesrj    r   r   <module>r      s   
 
  %  ! + o o , $ &'	Y% @ri$. rS rsTz rd r(!E !E3: !EcDj !ET !EH
3% 
3c 
3d 
36 6S 6sTz 6d 6&4% 4e 4 4 4./% /c /c /Y YC Yc YD YUY Y<
 
E 
3; 
S 
UY 
r   