
    {-j              &          d Z ddlZddlZddlZddlZddlZddlZddlZddlm	Z	 ddl
mZmZ ddlmZmZ ddlmZ ddlmZmZmZ dd	lmZmZ dd
lmZ ddlmZmZmZmZmZm Z m!Z! erddl"m#Z#  ej$        e%          Z&dZ'dZ(e G d d                      Z)e G d d                      Z*e G d d                      Z+e G d d                      Z, ed           G d d                      Z-e G d d                      Z.e G d d                      Z/e G d d                       Z0d!e1d"efd#Z2d!e1d"e3fd$Z4e G d% d&                      Z5e G d' d(                      Z6 G d) d*          Z7d+e1d"e8e9e1e1f                  fd,Z:d!e1d"e9e;e<f         dz  fd-Z=d.e1d"e	e9e1e;e<f                  fd/Z>d0d1d2e1d3e1d"e	e9e1e;e<ef                  fd4Z?d5e<d"e1fd6Z@dd7d!e1d8ed9         d:e;d;e<d<e;d=e<d>e1d?e1d@e3dAe3dBe3dCedz  d"e5fdDZA	 	 	 	 	 	 	 d^dFe1dGe1d0d1dHe3dAe3d@e3dIe3dBe3dJe7dz  dKedz  d"e6fdLZBdMe6d"dfdNZCdMe6dOe1d"dfdPZDdOe1d"e6fdQZEd_dMe6d0d1dRe3dKedz  d"df
dSZFdMe6d"dfdTZG	 	 d`dEdEdEdEdEddddddEdEdEddUdFe1dz  dGe1dz  d0d1dHe3dAe3d@e3dIe3dBe3dVe8e1         dz  dWe8e1         dz  dXe1dz  dMe1dz  dYe1dz  dZe3dRe3d[e3d\e3e1z  dz  d"e6f$d]ZHdS )azzShared logic for bucket operations.

This module contains the core buckets logic used by both the CLI and the Python API.
    N)Iterator)	dataclassfield)datetimetimezone)Path)TYPE_CHECKINGAnyLiteral   )	constantslogging)BucketNotFoundError)HfUri
StatusLineXetFileDatadisable_progress_barsenable_progress_barsparse_datetimeparse_hf_uriHfApizhf://buckets/  c                   L    e Zd ZU dZeed<   eed<   eed<   eed<   eed<   d Z	dS )	
BucketInfoa  
    Contains information about a bucket on the Hub. This object is returned by [`bucket_info`] and [`list_buckets`].

    Attributes:
        id (`str`):
            ID of the bucket.
        private (`bool`):
            Is the bucket private.
        created_at (`datetime`):
            Date of creation of the bucket on the Hub.
        size (`int`):
            Size of the bucket in bytes.
        total_files (`int`):
            Total number of files in the bucket.
    idprivate
created_atsizetotal_filesc                 H   |                     d          | _        |                     d          | _        t          |                     d                    | _        |                     d          | _        |                     d          | _         | j        j        di | d S )Nr   r   	createdAtr   
totalFiles )	popr   r   r   r   r   r    __dict__update)selfkwargss     X/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/huggingface_hub/_buckets.py__init__zBucketInfo.__init__U   s    **T""zz),,(K)@)@AAJJv&&	!::l33&&v&&&&&    N)
__name__
__module____qualname____doc__str__annotations__boolr   intr+   r$   r,   r*   r   r   =   sd            	GGGMMM
III' ' ' ' 'r,   r   c                       e Zd ZU eez  ez  ed<   eed<    ed          Zedz  ed<    ed          Z	e
dz  ed<    ed          Ze
ed	<    ed          Zedz  ed
<   ddZdS )_BucketAddFilesourcedestinationNdefaultxet_hashr   Finitmtimecontent_typereturnc                    d | _         t          | j        t          t          f          r$t          j        | j                  d         | _         | j         $t          j        | j                  d         | _         t          t          | j        t                    s't          j                            | j                  dz  nt          j                    dz            | _        d S Nr   r   )r?   
isinstancer7   r1   r   	mimetypes
guess_typer8   r4   bytesospathgetmtimetimer>   r(   s    r*   __post_init__z_BucketAddFile.__post_init__h   s     dkC;// 	E ) 4T[ A A! DD$ ) 4T5E F Fq ID8B4;PU8V8VnBGT[))D00\`\e\g\gjn\n
 



r,   r@   N)r-   r.   r/   r1   r   rF   r2   r   r;   r   r4   r>   r?   rL   r$   r,   r*   r6   r6   ^   s         $J 5...HcDj...uT***D#****E"""E3"""$u%000L#*000	
 	
 	
 	
 	
 	
r,   r6   c                       e Zd ZU eed<   eed<   eed<   eed<    ed          Zedz  ed<    ed	          Zeed
<    ed	          Z	edz  ed<   ddZ
dS )_BucketCopyFiler8   r;   source_repo_typesource_repo_idNr9   r   Fr<   r>   r?   r@   c                     t          j        | j                  d         | _        t	          t          j                    dz            | _        d S rB   )rD   rE   r8   r?   r4   rJ   r>   rK   s    r*   rL   z_BucketCopyFile.__post_init__~   s;    %01ABB1Et+,,


r,   rM   )r-   r.   r/   r1   r2   r   r   r4   r>   r?   rL   r$   r,   r*   rO   rO   t   s         MMMuT***D#****E"""E3"""$u%000L#*000- - - - - -r,   rO   c                       e Zd ZU eed<   dS )_BucketDeleteFilerH   N)r-   r.   r/   r1   r2   r$   r,   r*   rT   rT      s         
IIIIIr,   rT   T)frozenc                   (    e Zd ZU dZeed<   eed<   dS )BucketFileMetadataa%  Data structure containing information about a file in a bucket.

    Returned by [`get_bucket_file_metadata`].

    Args:
        size (`int`):
            Size of the file in bytes.
        xet_file_data (`XetFileData`):
            Xet information for the file (hash and refresh route).
    r   xet_file_dataN)r-   r.   r/   r0   r4   r2   r   r$   r,   r*   rW   rW      s3         	 	 IIIr,   rW   c                       e Zd ZU dZeed<   dZeed<    ed          Zeed<    ed          Z	eed<    ed          Z
eed	<   ddZdS )	BucketUrla  Describes a bucket URL on the Hub.

    `BucketUrl` is returned by [`create_bucket`]. At initialization, the URL is parsed to populate properties:
    - endpoint (`str`)
    - namespace (`str`)
    - bucket_id (`str`)
    - url (`str`)
    - uri (`HfUri`)

    Args:
        url (`str`):
            String value of the bucket url.
        endpoint (`str`, *optional*):
            Endpoint of the Hub. Defaults to <https://huggingface.co>.
    url endpointFr<   	namespace	bucket_idurir@   Nc                    | j         pt          j        | _         | j                            | j         d                              d          }|                    d          r|t          d          d          }t          |          }|j	        rt          d| j                   |j                            d          d         | _        |j        | _        || _        d S )Nr\   /zbuckets/zUnable to parse bucket URL: r   )r]   r   ENDPOINTr[   replacestrip
startswithlen_parse_bucket_uripath_in_repo
ValueErrorr   splitr^   r_   r`   )r(   url_pathparseds      r*   rL   zBucketUrl.__post_init__   s    ;); 8##DM266<<SAAz** 	3J 1 12H"8,, 	HFDHFFGGG--a0r,   rM   )r-   r.   r/   r0   r1   r2   r]   r   r^   r_   r`   r   rL   r$   r,   r*   rZ   rZ      s            
HHHHcU&&&Is&&&U&&&Is&&&E"""C"""     r,   rZ   c                   n    e Zd ZU dZed         ed<   eed<   eed<   eed<   edz  ed<   edz  ed	<   d
 Z	dS )
BucketFilez
    Contains information about a file in a bucket on the Hub. This object is returned by [`list_bucket_tree`].

    Similar to [`RepoFile`] but for files in buckets.
    filetyperH   r   r;   Nr>   uploaded_atc                    |                     d          | _        |                     d          | _        |                     d          | _        |                     d          | _        |                     dd           }|rt          |          nd | _        |                     dd           }|rt          |          nd | _        d S )Nrq   rH   r   xetHashr>   
uploadedAt)r%   rq   rH   r   r;   r   r>   rr   )r(   r)   r>   rr   s       r*   r+   zBucketFile.__init__   s    JJv&&	JJv&&	JJv&&	

9--

7D)).3=^E***
jjt44:EO>+6664r,   )
r-   r.   r/   r0   r   r2   r1   r4   r   r+   r$   r,   r*   ro   ro      s           &/
III
IIIMMMd?D   P P P P Pr,   ro   c                   J    e Zd ZU dZed         ed<   eed<   edz  ed<   d ZdS )BucketFolderz
    Contains information about a directory in a bucket on the Hub. This object is returned by [`list_bucket_tree`].

    Similar to [`RepoFolder`] but for directories in buckets.
    	directoryrq   rH   Nrr   c                 $   |                     d          | _        |                     d          | _        |                     dd           p|                     dd           }|r&t          |t                    r|nt          |          nd | _        d S )Nrq   rH   ru   rr   )r%   rq   rH   rC   r   r   rr   )r(   r)   rr   s      r*   r+   zBucketFolder.__init__   s    JJv&&	JJv&&	jjt44W

=RV8W8W J{H==^[[>R]C^C^C^ 	r,   )	r-   r.   r/   r0   r   r2   r1   r   r+   r$   r,   r*   rw   rw      s[           +

IIID   
 
 
 
 
r,   rw   rH   r@   c                    |                      t          j                  r+t          |           }|j        st          d|  d          |S |                     dd          }t          |          dk     s|d         r|d         st          d|  d          |d          d|d          }d                    |dd	                   }t          d
||          S )zParse a bucket path into a HfUri.

    Accepts both `hf://buckets/namespace/name(/path/in/repo)` and plain `namespace/name(/path/in/repo)`.
    zInvalid bucket path: z*. Must be a bucket URI (hf://buckets/...).rb      r   r   zInvalid bucket path: 'z)'. Expected format: namespace/bucket_nameNbucket)rq   r   ri   )
rf   r   HF_PROTOCOLr   	is_bucketrj   rk   rg   joinr   )rH   rm   partsr_   prefixs        r*   rh   rh      s    
 y,-- d## 	geTeeefffJJsAE
5zzA~~U1X~U1X~a$aaabbb8((eAh((IXXeABBi  Fh96BBBBr,   c                 l    |                      t          j                  sdS t          |           j        S )zCheck if a path is a bucket path.

    Do not raise if the path is not a hf:// URI.
    Raise if the path is a hf:// URI but with an incorrect format.
    F)rf   r   r}   r   r~   )rH   s    r*   _is_bucket_pathr   	  s1     ??9011 u''r,   c                       e Zd ZU dZed         ed<   eed<   dZedz  ed<   dZ	eed<   dZ
edz  ed	<   dZedz  ed
<   dZedz  ed<   dS )SyncOperationz,Represents a sync operation to be performed.)uploaddownloaddeleteskipactionrH   Nr   r\   reasonlocal_mtimeremote_mtimebucket_file)r-   r.   r/   r0   r   r2   r1   r   r4   r   r   r   r   ro   r$   r,   r*   r   r     s         66:;;;;
IIID#*FC"Kt"""#L#*###%)Kd")))))r,   r   c                       e Zd ZU dZeed<   eed<   eed<    ee          Zee	         ed<   de
eeez  f         fdZd	S )
SyncPlanz Represents a complete sync plan.r7   dest	timestamp)default_factory
operationsr@   c                 >   t          d | j        D                       }t          d | j        D                       }t          d | j        D                       }t          d | j        D                       }t          d | j        D                       }|||||dS )Nc              3   0   K   | ]}|j         d k    dV  dS )r   r   Nr   .0ops     r*   	<genexpr>z#SyncPlan.summary.<locals>.<genexpr>0  .      KKBRY(5J5Ja5J5J5J5JKKr,   c              3   0   K   | ]}|j         d k    dV  dS )r   r   Nr   r   s     r*   r   z#SyncPlan.summary.<locals>.<genexpr>1  s.      OObryJ7N7N7N7N7N7NOOr,   c              3   0   K   | ]}|j         d k    dV  dS )r   r   Nr   r   s     r*   r   z#SyncPlan.summary.<locals>.<genexpr>2  r   r,   c              3   0   K   | ]}|j         d k    dV  dS )r   r   Nr   r   s     r*   r   z#SyncPlan.summary.<locals>.<genexpr>3  s.      GG"293F3FA3F3F3F3FGGr,   c              3   :   K   | ]}|j         d v |j        pdV  dS )r   r   r   N)r   r   r   s     r*   r   z#SyncPlan.summary.<locals>.<genexpr>4  s5      gg"29PfCfCfACfCfCfCfggr,   )uploads	downloadsdeletesskips
total_size)sumr   )r(   r   r   r   r   r   s         r*   summaryzSyncPlan.summary/  s    KK$/KKKKKOODOOOOOO	KK$/KKKKKGGGGGGGggggggg
"$
 
 	
r,   N)r-   r.   r/   r0   r1   r2   r   listr   r   dictr4   r   r$   r,   r*   r   r   &  s         **KKK
IIINNN&+eD&A&A&AJ]#AAA
c39n- 
 
 
 
 
 
r,   r   c            
           e Zd ZdZ	 	 	 d
dee         dz  dee         dz  deeeef                  dz  fdZdedefd	Z	dS )FilterMatcherz4Matches file paths against include/exclude patterns.Ninclude_patternsexclude_patternsfilter_rulesc                 <    |pg | _         |pg | _        |pg | _        dS )a  Initialize the filter matcher.

        Args:
            include_patterns: Patterns to include (from --include)
            exclude_patterns: Patterns to exclude (from --exclude)
            filter_rules: Rules from filter file as list of ("+"/"-", pattern) tuples
        Nr   r   r   )r(   r   r   r   s       r*   r+   zFilterMatcher.__init__F  s1     !1 6B 0 6B(.Br,   rH   r@   c                     | j         D ]"\  }}t          j        ||          r|dk    c S #| j        D ]}t          j        ||          r dS | j        D ]}t          j        ||          r dS | j        rdS dS )zCheck if a path should be included based on the filter rules.

        Filtering rules:
        - Filters are evaluated in order, first matching rule decides
        - If no rules match, include by default (unless include patterns are specified)
        +FT)r   fnmatchr   r   )r(   rH   signpatterns       r*   matcheszFilterMatcher.matchesW  s     ". 	# 	#MD'tW-- #s{"""# , 	 	GtW-- uu , 	 	GtW-- tt   	5 tr,   )NNN)
r-   r.   r/   r0   r   r1   tupler+   r3   r   r$   r,   r*   r   r   C  s        >> .2-159	/ /s)d*/ s)d*/ 5c?+d2	/ / / /"C D      r,   r   filter_filec                    g }t          |           5 }|D ]}|                                }|r|                    d          r.|                    d          r2|                    d|dd                                         f           u|                    d          r2|                    d|dd                                         f           |                    d|f           	 ddd           n# 1 swxY w Y   |S )zParse a filter file and return a list of (sign, pattern) tuples.

    Filter file format:
    - Lines starting with "+" are include patterns
    - Lines starting with "-" are exclude patterns
    - Empty lines and lines starting with "#" are ignored
    #r   r   N-)openre   rf   append)r   rulesflines       r*   _parse_filter_filer   t  sJ    E	k		 *a 
	* 
	*D::<<D 4??3// s## *c48>>#3#345555%% *c48>>#3#345555 c4[))))
	** * * * * * * * * * * * * * * Ls   CC66C:=C:c                     	 t          j        |           }n# t          $ r Y dS w xY wt          j        |j                  rdS |j        |j        dz  fS )zStat a local file and return (size, mtime_ms).

    Returns None if the path is missing or is a directory. Uses a single
    ``os.stat`` call so callers don't pay for multiple syscalls per file.
    Nr   )rG   statOSErrorS_ISDIRst_modest_sizest_mtime)rH   sts     r*   _stat_localr     se    WT]]   tt|BJ t:r{T)))s    
%%
local_pathc              #     K   t           j                            |           } t           j                            |           st	          d|            t          j        |           D ]\  }}}|D ]}t           j                            ||          }t          |          }|4t           j                            ||           }|	                    t           j
        d          }||d         |d         fV  dS )zsList all files in a local directory.

    Yields:
        tuple: (relative_path, size, mtime_ms) for each file
    z Local path must be a directory: Nrb   r   r   )rG   rH   abspathisdirrj   walkr   r   relpathrd   sep)r   root_filesfilename	full_path	stat_inforel_paths           r*   _list_local_filesr     s       ,,J7==$$ JHJHHIII'*-- 	7 	7a 	7 	7HT844I#I..I wy*==H''44HIaL)A,66666	7	7 	7r,   apir   r_   r   c              #     K   |                      ||pdd          D ]}t          |t                    r|j        }|r]|                    |dz             r|t          |          dz   d         }n,||k    r#d|v r|                    dd          d         n|}n~|}|j        r|j                                        dz  nd}||j	        ||fV  dS )	zList all files in a bucket with a given prefix.

    Yields:
        tuple: (relative_path, size, mtime_ms, bucket_file) for each file.
            bucket_file is the BucketFile object from list_bucket_tree.
    NT)r   	recursiverb   r   r   r   )
list_bucket_treerC   rw   rH   rf   rg   rsplitr>   r   r   )r   r_   r   itemrH   r   mtime_mss          r*   _list_remote_filesr     s      $$Yv~QU$VV 2 2dL)) 	y  	v|,, Fa 1 1269Tkk4;;sA..r22t H48JE4:''))D00A	8T11111'2 2r,   r   c                 l    t          j        | dz  t          j                                                  S )z3Convert mtime in milliseconds to ISO format string.r   )tz)r   fromtimestampr   utc	isoformat)r   s    r*   _mtime_to_isor     s*    !(T/hlCCCMMOOOr,   )r   r   r   source_sizesource_mtime	dest_size
dest_mtimesource_newer_labeldest_newer_labelignore_sizesignore_timesignore_existingr   c                    t          |dk    r|n|          }t          |dk    r|n|          }| |||d}|
rt          dddd|S ||k    }||z
  t          k    }|r5|rt          d|||d|S ||z
  t          k    }|r|nd}t          dd|d|S |	r!|rt          d|d|d|S t          ddd	d|S |s|r|rdn|}t          d|||d|S t          ddd
d|S )a  Compare source and dest files and return the appropriate sync operation.

    This is a unified helper for both upload and download directions.

    Args:
        path: Relative file path
        action: "upload" or "download"
        source_size: Size of the source file (bytes)
        source_mtime: Mtime of the source file (milliseconds)
        dest_size: Size of the destination file (bytes)
        dest_mtime: Mtime of the destination file (milliseconds)
        source_newer_label: Label when source is newer (e.g., "local newer" or "remote newer")
        dest_newer_label: Label when dest is newer (e.g., "remote newer" or "local newer")
        ignore_sizes: Only compare mtime
        ignore_times: Only compare size
        ignore_existing: Skip files that exist on receiver
        bucket_file: BucketFile object (for downloads only)

    Returns:
        SyncOperation describing the action to take
    r   )rH   r   r   r   r   z&exists on receiver (--ignore-existing))r   r   )r   r   r   z
same mtimezsize differsz	same size	identicalNr$   )r   r   _SYNC_TIME_WINDOW_MS)rH   r   r   r   r   r   r   r   r   r   r   r   local_mtime_isoremote_mtime_isobase_kwargssize_differssource_newer
dest_newerskip_reasonr   s                       r*   _compare_files_for_syncr     s   H $Fh4F4FLLJWWO$6X3E3EZZ<XX &(	# #K  lkF3[kk_jkkk)+L :-1EEL S 	S r7IWbrrfqrrr$|37KKJ.8J**lK R{RRkRRR	 
S 	S n~S^nnbmnnn R{RRkRRR 	S< 	S'3K^^9KF fv;ffZefff R{RRkRRRr,   Fr7   r   r   existingfilter_matcherstatusc
                    |pt                      }t          |            ot          |          }
t          |           ot          |           }|
s|st          d          t          | |t	          j        t          j                                                            }d}|
rt          j
                            |           }t          |          }|j        |j        }}t          j
                            |          st          d|           i }t!          |          D ]J\  }}}|                    |          r||f||<   |	r&|	                    dt'          |           d           K|	r&|	                    dt'          |           d           i }|	r,	 |                    |          j        }n# t.          $ r Y nw xY w	 t1          |||          D ]V\  }}}}|                    |          r||f||<   |	r1|d| nd}|	                    d	t'          |           | d           Wn.# t2          $ r! t4                              d
| d           Y nw xY w|	r&|	                    d	t'          |           d           t9          |                                          t9          |                                          z  }|	r&|	                    dt'          |           d           t=          |          D ]L}|                    |          }|                    |          }|r|s|rF|j         !                    tE          d||d         dtG          |d                                        y|j         !                    tE          d||d         dtG          |d                                        |r@|r>|\  }}|\  }} |j         !                    tI          |d|||| dd|||                     |sI|rG|rE|j         !                    tE          d||d         dtG          |d                                        Nnt          |           }|j        |j        }}t          j
                            |          }i }i }!|	r,	 |                    |          j        }n# t.          $ r Y nw xY wt1          |||          D ][\  }}}}"|                    |          r||f||<   |"|!|<   |	r1|d| nd}|	                    d	t'          |           | d           \|	r&|	                    d	t'          |           d           i }t          j
                            |          r|r[t!          |          D ]J\  }}}|                    |          r||f||<   |	r&|	                    dt'          |           d           Knd|D ]a}t          j
        %                    ||          }#tM          |#          }$|$4|$||<   |	r&|	                    dt'          |           d           b|	r&|	                    dt'          |           d           t9          |                                          t9          |                                          z  }|	r&|	                    dt'          |           d           t=          |          D ]t}|                    |          }|                    |          }|r|s|rF|j         !                    tE          d||d         dtG          |d                                        y|j         !                    tE          d||d         dtG          |d                   |!                    |                               |rT|rR|\  }} |\  }}|j         !                    tI          |d|| ||dd||||!                    |                               )|sI|rG|rE|j         !                    tE          d||d         dtG          |d                                        v|S )zCompute the sync plan by comparing source and destination.

    Returns:
        SyncPlan with all operations to be performed
    z[One of source or dest must be a bucket path (hf://buckets/...) and the other must be local.r7   r   r   NzSource must be a directory: zScanning local directory (z files)rb   r\   zScanning remote bucket (zBucket 'z' not found, treating as empty.zComparing files (z paths)r   r   znew file (--existing)r   )r   rH   r   r   r   r   znew filezlocal newerzremote newer)rH   r   r   r   r   r   r   r   r   r   r   r   znot in source (--delete))r   rH   r   r   r   r   )r   rH   r   r   r   r   )rH   r   r   r   r   r   r   r   r   r   r   r   )'r   r   rj   r   r   nowr   r   r   rG   rH   r   rh   r   ri   r   r   r   r'   rg   donebucket_infor    	Exceptionr   r   loggerdebugsetkeyssortedgetr   r   r   r   r   r   r   )%r7   r   r   r   r   r   r   r   r   r   	is_uploadis_downloadplanremote_totalr   rm   r_   r   local_filesr   r   r   remote_filesr   	total_str	all_pathsrH   
local_inforemote_info
local_sizer   remote_sizer   bucket_file_mapr   
local_filer   s%                                        r*   _compute_sync_planr  %  sG
   " $6}N#F+++E0E0EI!&))G/$2G2G.GK x[ xvwww,x|,,6688  D  $L SW__V,,
"4(("Iv':6	w}}Z(( 	JHJHHIII (9*(E(E 	V 	V$HdH%%h// 9)-x(8H% VT3{;K;KTTTUUU 	PKKNS5E5ENNNOOO 	"y99E   		P/A#yRX/Y/Y d d+$!!))(33 >.2H-=L* d6B6N 2L 2 2 2TVIMM"bS=N=N"bPY"b"b"bcccd # 	P 	P 	PLLNINNNOOOOO	P  	OKKM3|3D3DMMMNNN ((**++c,2C2C2E2E.F.FF	 	EKKCC	NNCCCDDD9%% 8	 8	D$..J&**400K 4+ 4 O**%#)!%!+A#:(5jm(D(D      O**%#+!%!+A#-(5jm(D(D        *4'
K,7)\&&+!'$.%0"-#/+8)7%1%1(7        
K 
F 
&&!'!(^9%2;q>%B%B    a8	x #6**"Iv':6	W__T**
 *, 	"y99E   5GYX^5_5_ 	` 	`1HdHk%%h// 8*.)9X&,7) `2>2J....PR	^\9J9J^I^^^___ 	OKKM3|3D3DMMMNNN7==$$ 	^ ^0A*0M0M ^ ^,HdH%--h77 A15x0@H- ^&\3{CSCS&\&\&\]]]	^ !- ^ ^H!#j(!C!CJ +J 7 7I ( ,5K) ^&\3{CSCS&\&\&\]]] 	PKKNS5E5ENNNOOO ))++,,s;3C3C3E3E/F/FF	 	EKKCC	NNCCCDDD9%% :	 :	D&**400K$..J 6: 6 O**%#)!%!,Q#:)6{1~)F)F      O**%#-!%!,Q#-)6{1~)F)F(7(;(;D(A(A  	 	 	 	   ,7)\*4'
K&&+!)$/%1",#.+9)6%1%1(7$3$7$7$=$=       ! 
Z 
F 
&&!'!']9$1*Q-$@$@     Ks7   F8 8
GG	A(H2 2(IIR0 0
R=<R=r  c                    d| j         | j        | j        |                                 d}|                    t          j        |          dz              | j        D ]u}d|j        |j	        |j
        d}|j        
|j        |d<   |j        
|j        |d<   |j        
|j        |d	<   |                    t          j        |          dz              vdS )
z1Write a sync plan as JSONL to a file-like object.header)rq   r7   r   r   r   
	operation)rq   r   rH   r   Nr   r   r   )r7   r   r   r   writejsondumpsr   r   rH   r   r   r   r   )r  r   r  r   op_dicts        r*   _write_planr$  !  s     +	^<<>> F GGDJv%&&& o , ,iGi	#
 #
 7 gGFO>%%'^GM"?&&(oGN#	
7##d*++++, ,r,   	plan_filec                 x    t          |d          5 }t          | |           ddd           dS # 1 swxY w Y   dS )z!Save a sync plan to a JSONL file.wN)r   r$  )r  r%  r   s      r*   
_save_planr(  >  s    	i		 D!                 s   /33c                    t          |           5 }|                                }ddd           n# 1 swxY w Y   |st          d|            t          j        |d                   }|                    d          dk    rt          d          t          |d         |d         |d	         
          }|dd         D ]}t          j        |          }|                    d          dk    r0|j                            t          |d         |d         |                    d          |                    dd          |                    d          |                    d                               |S )z#Load a sync plan from a JSONL file.NzEmpty plan file: r   rq   r  z0Invalid plan file: expected header as first liner7   r   r   r  r   r  r   rH   r   r   r\   r   r   )r   rH   r   r   r   r   )
r   	readlinesrj   r!  loadsr  r   r   r   r   )r%  r   linesr  r  r   r#  s          r*   
_load_planr-  D  s   	i A                :8Y88999 Za!!Fzz&X%%KLLLhF^%  D abb	 
 
*T"";;v+--x(V_[[(({{8R00#KK66$[[88  		
 		
 		
 		
 Ks   155verbosec                 
   t          | j                   ot          | j                  }t          | j                  ot          | j                   }|rt          j                            | j                  }t          | j                  }|j        |j        }	}g }
g }| j	        D ]}|j
        xdk    ru t          j                            ||j                  }|	r|	 d|j         n|j        }|r t          d|j         d|j         d           |
                    ||f           xdk    rN |	r|	 d|j         n|j        }|r t          d|j         d|j         d           |                    |           dk    r$|r"t          d	|j         d|j         d           |
s|r|rg }|
r&|                    d
t          |
           d           |r&|                    dt          |           d           |                    d                    |                                                     |                    ||
pd|pd           dS dS |rt          | j                  }|j        |j        }	}t          j                            | j                  }t          j        |d           g }g }| j	        D ]o}|j
        dk    rt          j                            ||j                  }t          j        t          j                            |          d           |r t          d|j         d|j         d           |j        |                    |j        |f           |	r|	 d|j         n|j        }|                    ||f           |j
        dk    r^t          j                            ||j                  }|r t          d|j         d|j         d           |                    |           B|j
        dk    r"|r t          d	|j         d|j         d           qt          |          dk    r>|r&|                    dt          |           d           |                    ||           |r(|r&|                    dt          |           d           |D ]}t          j                            |          rt          j        |           t          j                            |          }||k    rK	 t          j        |           t          j                            |          }n# t4          $ r Y n
w xY w||k    KdS dS )zExecute a sync plan.r   rb   z  Uploading: z ()r   z  Deleting: r   z  Skipping: z
uploading z filesz	deleting z, N)addr   T)exist_okr   z  Downloading: r   zDownloading z	Deleting z local files)r   r7   r   rG   rH   r   rh   r   ri   r   r   r   printr   r   rg   r  
capitalizebatch_bucket_filesmakedirsdirnamer   download_bucket_filesexistsremovermdirr   )r  r   r.  r   r  r  r   rm   r_   r   	add_filesdelete_pathsr   r  remote_pathr   download_filesdelete_files	file_pathparents                       r*   _execute_planrC  o  s   #DK000O_TY5O5OI!$+..Qty7Q7Q3QK \W__T[11
"49--"Iv':6	 ;=	"$/ 	B 	BB)XXXX!#j"'!B!BJ;A"NV"7"7bg"7"7"7rwK GEbgEEEEEFFF$$j+%>????XXXX;A"NV"7"7bg"7"7"7rwK FDRWDD	DDDEEE ''4444VVwV@@@BI@@@AAAA  	 	 ; FLL!Dc)nn!D!D!DEEE HLL!FS->->!F!F!FGGGDIIe,,7799:::""%#+t #     	 	 
 4"4;//"Iv':6	W__TY//
 	J.... EG"$/ 	> 	>ByJ&&W\\*bg>>
BGOOJ77$GGGG ECBGCCryCCCDDD>-"))2>:*FGGGG;A"NV"7"7bg"7"7"7rwK"));
*CDDDDh&&W\\*bg>>
 B@@@BI@@@AAA##J////f$$$<RW<<	<<<=== ~"" HF3~+>+>FFFGGG%%i@@@  	El 	EKKCC$5$5CCCDDD% 
	 
	Iw~~i(( 		)$$$33
**(((!#!8!8"   	 
**_4 4T
	 
	s   3U
UUc                 ,   |                                  }t          d| j         d| j                    t          d|d                     t          d|d                     t          d|d                     t          d	|d
                     dS )z!Print a summary of the sync plan.zSync plan: z -> z  Uploads: r   z  Downloads: r   z  Deletes: r   z	  Skips: r   N)r   r3  r7   r   )r  r   s     r*   _print_plan_summaryrE    s    llnnG	
4
4
4
4
4555	
,	*
,
,---	
0'+.
0
0111	
,	*
,
,---	
(gg&
(
()))))r,   )r   r   r   r   r   includeexcludefilter_fromr  applydry_runr.  quiettokenrF  rG  rH  rI  rJ  rK  rL  c                   |ddl m}  ||          }|r[| s|rt          d          |t          d          |rt          d          |rt          d          |rt          d	          |rt          d
          |	rt          d          |
rt          d          |rt          d          |rt          d          |rt          d          t          |          }t	          |           }|st          |           t          d           |rt                       	 t          ||||           |rt                       n# |rt                       w w xY w|st          d           |S | r|st          d          t          |           }t          |          }|r|rt          d          |s|st          d          |r|rt          d          |r|rt          d          |r|rt          d          |rQt          j                            |          r1t          j                            |          st          d|           n1t          j                            |           st          d|            d}|
rt          |
          }t!          ||	|          }t	          | o|           }t#          | |||||||||
  
        }|rt%          |t&          j                   |S |r5t+          ||           |s!t          |           t          d|            |S |st          |           |                                }|d         d k    r+|d!         d k    r|d"         d k    r|st          d#           |S |st          d$           |rt                       	 t          ||||           |rt                       n# |rt                       w w xY w|st          d           |S )%a  Sync files between a local directory and a bucket.

    This is equivalent to the ``hf buckets sync`` CLI command. One of ``source`` or ``dest`` must be a bucket path
    (``hf://buckets/...``) and the other must be a local directory path.

    Args:
        source (`str`, *optional*):
            Source path: local directory or ``hf://buckets/namespace/bucket_name(/prefix)``.
            Required unless using ``apply``.
        dest (`str`, *optional*):
            Destination path: local directory or ``hf://buckets/namespace/bucket_name(/prefix)``.
            Required unless using ``apply``.
        api ([`HfApi`]):
            The HfApi instance to use for API calls.
        delete (`bool`, *optional*, defaults to `False`):
            Delete destination files not present in source.
        ignore_times (`bool`, *optional*, defaults to `False`):
            Skip files only based on size, ignoring modification times.
        ignore_sizes (`bool`, *optional*, defaults to `False`):
            Skip files only based on modification times, ignoring sizes.
        existing (`bool`, *optional*, defaults to `False`):
            Skip creating new files on receiver (only update existing files).
        ignore_existing (`bool`, *optional*, defaults to `False`):
            Skip updating files that exist on receiver (only create new files).
        include (`list[str]`, *optional*):
            Include files matching patterns (fnmatch-style).
        exclude (`list[str]`, *optional*):
            Exclude files matching patterns (fnmatch-style).
        filter_from (`str`, *optional*):
            Path to a filter file with include/exclude rules.
        plan (`str`, *optional*):
            Save sync plan to this JSONL file instead of executing.
        apply (`str`, *optional*):
            Apply a previously saved plan file. When set, ``source`` and ``dest`` are not needed.
        dry_run (`bool`, *optional*, defaults to `False`):
            Print sync plan to stdout as JSONL without executing.
        verbose (`bool`, *optional*, defaults to `False`):
            Show detailed per-file operations.
        quiet (`bool`, *optional*, defaults to `False`):
            Suppress all output and progress bars.
        token (Union[bool, str, None], optional):
            A valid user access token. If not provided, the locally saved token will be used.

    Returns:
        [`SyncPlan`]: The computed (or loaded) sync plan.

    Raises:
        `ValueError`: If arguments are invalid (e.g., both paths are remote, conflicting options).

    Example:
        ```python
        >>> from huggingface_hub import HfApi
        >>> api = HfApi()

        # Upload local directory to bucket
        >>> api.sync_bucket("./data", "hf://buckets/username/my-bucket")

        # Download bucket to local directory
        >>> api.sync_bucket("hf://buckets/username/my-bucket", "./data")

        # Sync with delete and filtering
        >>> api.sync_bucket(
        ...     "./data",
        ...     "hf://buckets/username/my-bucket",
        ...     delete=True,
        ...     include=["*.safetensors"],
        ... )

        # Dry run: preview what would be synced
        >>> plan = api.sync_bucket("./data", "hf://buckets/username/my-bucket", dry_run=True)
        >>> plan.summary()
        {'uploads': 3, 'downloads': 0, 'deletes': 0, 'skips': 1, 'total_size': 4096}

        # Save plan for review, then apply
        >>> api.sync_bucket("./data", "hf://buckets/username/my-bucket", plan="sync-plan.jsonl")
        >>> api.sync_bucket(apply="sync-plan.jsonl")
        ```
    Nr   r   )rL  z,Cannot specify source/dest when using apply.z#Cannot specify both plan and apply.z'Cannot specify delete when using apply.z-Cannot specify ignore_times when using apply.z-Cannot specify ignore_sizes when using apply.z(Cannot specify include when using apply.z(Cannot specify exclude when using apply.z,Cannot specify filter_from when using apply.z)Cannot specify existing when using apply.z0Cannot specify ignore_existing when using apply.z(Cannot specify dry_run when using apply.)enabledzExecuting plan...)r.  r   zSync completed.z7Both source and dest are required (unless using apply).z?Remote to remote sync is not supported. One path must be local.z?One of source or dest must be a bucket path (hf://buckets/...).z2Cannot specify both ignore_times and ignore_sizes.z1Cannot specify both existing and ignore_existing.z%Cannot specify both dry_run and plan.z!Destination must be a directory: z&Source must be an existing directory: r   )
r7   r   r   r   r   r   r   r   r   r   zPlan saved to: r   r   r   r   zNothing to sync.z
Syncing...)hf_apir   rj   r-  r   rE  r3  r   rC  r   r   rG   rH   r9  r   r   r   r  r$  sysstdoutr(  r   )r7   r   r   r   r   r   r   r   rF  rG  rH  r  rI  rJ  r.  rK  rL  r   	sync_planr   source_is_bucketdest_is_bucketr   r   r   s                            r*   sync_bucket_internalrU    sm   F !!!!!!e%    ) 	MT 	MKLLLBCCC 	HFGGG 	NLMMM 	NLMMM 	IGHHH 	IGHHH 	MKLLL 	JHIII 	QOPPP 	IGHHHu%%	I... 	'	***%&&& 	$!###	')S'&IIII '$&&&  '$&&&&'  	%#$$$  T TRSSS&v..$T**N \N \Z[[[ \N \Z[[[ O OMNNN NO NLMMM B4 B@AAA  P7>>$ 	Id(;(; 	IGGGHHHw}}V$$ 	PNfNNOOO L 7)+66"  !  N E	 9'k:::F"!!'%  I  Isz*** 9d### 	,	****D**+++  'I&&&!!GyQ7;#71#<#<ASWXAXAX 	&$%%% l  #igfEEEE 	# """  	# """"	#  !   s   &E
 
E,O O#)FFFFFNN)FN)NN)Ir0   r   r!  rD   rG   r   rP  rJ   collections.abcr   dataclassesr   r   r   r   pathlibr   typingr	   r
   r   r\   r   r   errorsr   utilsr   r   r   r   r   r   r   rO  r   
get_loggerr-   r  BUCKET_PREFIXr   r   r6   rO   rT   rW   rZ   ro   rw   r1   rh   r3   r   r   r   r   r   r   r   r4   floatr   r   r   r   r   r  r$  r(  r-  rC  rE  rU  r$   r,   r*   <module>r_     s   
       				  



  $ $ $ $ $ $ ( ( ( ( ( ( ( ( ' ' ' ' ' ' ' '       . . . . . . . . . .                 ' ' ' ' ' '                    
	H	%	%    ' ' ' ' ' ' ' '@ 
 
 
 
 
 
 
 
* - - - - - - - -         $         $ $ $ $ $ $ $ $N P P P P P P P P2 
 
 
 
 
 
 
 
6CC CE C C C C$(# ($ ( ( ( (  	* 	* 	* 	* 	* 	* 	* 	* 
 
 
 
 
 
 
 
8. . . . . . . .bC DsCx,A    :*c *eCJ/$6 * * * *7# 7(5c53I*J 7 7 7 7,2G 2 2S 2XeTWY\^cehThNiEj 2 2 2 2DPE Pc P P P P$ #ES ES ES
ES ()ES 	ES
 ES ES ES ES ES ES ES ES tES ES ES ES ESX !+/t tt
t 
t 	t
 t t t t "D(t $Jt t t t tx,h ,d , , , ,:X # $    ## #( # # # #Va a aw a asUYz aei a a a aH*h *4 * * * *  n
 ! $ $"#%n n n$Jn
*n 
	n
 n n n n n #Yn #Yn tn *n :n n  !n" #n$ #:%n& 'n n n n n nr,   