
    gj(              
          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%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           	           	 	 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                 L    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   s      [/var/www/app.qaans.com/backend/venv/lib/python3.11/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#   ;   s     ! !N abbbd
!
 $JN ghhhj
! ! 
! ! ! ! ! !, Ir   r   r   r   c                    | dS |t          |          r|n|}t          |          sdS | dk    r#t          |          j        rt          d          | dk    r#t          |          j        st          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   remotes       r   r   r   [   s     _3_SScFV 'l622<pqqq)L$8$8$Bpqqq r   r   c                    t          |          }| dk    }|dk    }t          |           }|d uot          |          }|r5|r3|J |                    | |           t          j        d| |           d S |s,|s*|t          j        d          t          j        d          |r'|rt          ||            d S t          || |           d S |J t          || ||           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_hfs           r   r   r   o   s3   
5
!
!
!C#:L3JM#I42IcNNI  Y sC   
8#....  
Y 
;$%fggg d
 
 	

   	$S#...FS#... ???3S|DDDDDDr   r2   c                 4   t          |          }t          ||          }t                      5  t                      5 }t          j                            ||          }t          | ||           t          |d          5 }|	                    d          x}r;t          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            d S # 1 swxY w Y   d S )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#%% 	3w||GX66HS#x000h%% 3 vvj111e 3J%++E222  !vvj111e 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3sZ   DAC5?ACC5C""C5%C"&C5)D5C9	9D<C9	=DDDc                    t          |          }t          ||          }||}nvt          j                            |          s4|                    t          j                  s|                    d          r!t          j                            ||          }n|}t          j                            |          }|rt          j	        |d           t          | ||           t          j        d||           d S )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||BF33 s||C7H7H W\\#x00


,,J /
J....S#z***J|*555555r   rC   rR   c                 ~   |j         r'|                     |j        |j        |fgd           dS t          j                            |          pd}t          d|          5 }|                     |j        |j	        |j        |j
        |          }t	          j        ||           ddd           dS # 1 swxY 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_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   
 } 4!!#&C,<j+I*Jcg!hhhhh
 W__Z007C
#6zBBB 	4g!11()! 2  O J
333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s   !AB22B69B6c                     | j         dk    s|                    d          rt          j        d          | 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      s\    
2c!2!2 K
 
 	
 ""3**2..r   r+   c                   t          |          }|r|j        dk    s|                    d          rt          j        d          t
          j        j                                        }t          | |||j                   t          j        dd|                                           d S t          j                            |          rt          j        d          t          j                            |          st          j        d|           |j        }|dk    r t          j                            |          }n=|                    d          r&|dz   t          j                            |          z   }n|}t          | |||           t          j        d|t%          ||	                                                     d S )
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      s   
s

C r!!S\\#%6%6!$%hiiiy$$&&sCs'7888
:7

====	w}}S 
 k
 
 	
 7>># B !@3!@!@AAAF||g&&s++	c		 slRW%5%5c%:%::3S+...Jzs+(N(N(N(U(U(W(WXXXXXXr   sourcero   c                     |j         r!|                     |j        ||fg           dS |                     |||j        |j        |j                   dS )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      so    
} 	
svV[,A+BCCCCC"$Fh\ 	 	
 	
 	
 	
 	
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 o o o o o o o o o o o o , , , , , , , ,        $ &'	 Y%    @ri$. rS rsTz rd r r r r(!E !E3: !EcDj !ET !E !E !E !EH
3% 
3c 
3d 
3 
3 
3 
36 6S 6sTz 6d 6 6 6 6&4% 4e 4 4 4 4 4 4./% /c /c / / / /Y YC Yc YD YUY Y Y Y Y<
 
E 
3; 
S 
UY 
 
 
 
 
 
r   