
    g                        d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZmZmZmZmZmZmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ  ej        e          Zed         ZdgZ ed           G d d                      Z  ed           G d d                      Z! ed           G d d                      Z" ed           G d d                      Z# ed           G d d                      Z$d(deee%e	f                  de$fdZ&de	de"fdZ'd e(de%fd!Z)d"Z*d#e+de%fd$Z,d%e	d&e%ddfd'Z-dS ))z4Contains utilities to manage the HF cache directory.    N)defaultdict)	dataclass)Path)Dict	FrozenSetListLiteralOptionalSetUnion)CacheNotFoundCorruptedCacheException   )tabulate)HF_HUB_CACHE   )logging)modeldatasetspacez	.DS_StoreT)frozenc                       e Zd ZU dZeed<   eed<   eed<   eed<   eed<   eed<   e	defd	            Z
e	defd
            Ze	defd            ZdS )CachedFileInfoa  Frozen data structure holding information about a single cached file.

    Args:
        file_name (`str`):
            Name of the file. Example: `config.json`.
        file_path (`Path`):
            Path of the file in the `snapshots` directory. The file path is a symlink
            referring to a blob in the `blobs` folder.
        blob_path (`Path`):
            Path of the blob file. This is equivalent to `file_path.resolve()`.
        size_on_disk (`int`):
            Size of the blob file in bytes.
        blob_last_accessed (`float`):
            Timestamp of the last time the blob file has been accessed (from any
            revision).
        blob_last_modified (`float`):
            Timestamp of the last time the blob file has been modified/created.

    <Tip warning={true}>

    `blob_last_accessed` and `blob_last_modified` reliability can depend on the OS you
    are using. See [python documentation](https://docs.python.org/3/library/os.html#os.stat_result)
    for more details.

    </Tip>
    	file_name	file_path	blob_pathsize_on_diskblob_last_accessedblob_last_modifiedreturnc                 *    t          | j                  S )z
        (property) Timestamp of the last time the blob file has been accessed (from any
        revision), returned as a human-readable string.

        Example: "2 weeks ago".
        )_format_timesincer   selfs    `/var/www/html/ai-engine/env/lib/python3.11/site-packages/huggingface_hub/utils/_cache_manager.pyblob_last_accessed_strz%CachedFileInfo.blob_last_accessed_strM        !!8999    c                 *    t          | j                  S )z
        (property) Timestamp of the last time the blob file has been modified, returned
        as a human-readable string.

        Example: "2 weeks ago".
        )r"   r   r#   s    r%   blob_last_modified_strz%CachedFileInfo.blob_last_modified_strW   r'   r(   c                 *    t          | j                  S )zi
        (property) Size of the blob file as a human-readable string.

        Example: "42.2K".
        _format_sizer   r#   s    r%   size_on_disk_strzCachedFileInfo.size_on_disk_stra        D-...r(   N)__name__
__module____qualname____doc__str__annotations__r   intfloatpropertyr&   r*   r.    r(   r%   r   r   (   s          6 NNNOOOOOO: : : : X: : : : : X: /# / / / X/ / /r(   r   c                       e Zd ZU dZeed<   eed<   eed<   ee	         ed<   ee         ed<   e
ed<   edefd	            Zedefd
            Zedefd            ZdS )CachedRevisionInfoaN  Frozen data structure holding information about a revision.

    A revision correspond to a folder in the `snapshots` folder and is populated with
    the exact tree structure as the repo on the Hub but contains only symlinks. A
    revision can be either referenced by 1 or more `refs` or be "detached" (no refs).

    Args:
        commit_hash (`str`):
            Hash of the revision (unique).
            Example: `"9338f7b671827df886678df2bdd7cc7b4f36dffd"`.
        snapshot_path (`Path`):
            Path to the revision directory in the `snapshots` folder. It contains the
            exact tree structure as the repo on the Hub.
        files: (`FrozenSet[CachedFileInfo]`):
            Set of [`~CachedFileInfo`] describing all files contained in the snapshot.
        refs (`FrozenSet[str]`):
            Set of `refs` pointing to this revision. If the revision has no `refs`, it
            is considered detached.
            Example: `{"main", "2.4.0"}` or `{"refs/pr/1"}`.
        size_on_disk (`int`):
            Sum of the blob file sizes that are symlink-ed by the revision.
        last_modified (`float`):
            Timestamp of the last time the revision has been created/modified.

    <Tip warning={true}>

    `last_accessed` cannot be determined correctly on a single revision as blob files
    are shared across revisions.

    </Tip>

    <Tip warning={true}>

    `size_on_disk` is not necessarily the sum of all file sizes because of possible
    duplicated files. Besides, only blobs are taken into account, not the (negligible)
    size of folders and symlinks.

    </Tip>
    commit_hashsnapshot_pathr   filesrefslast_modifiedr    c                 *    t          | j                  S )z
        (property) Timestamp of the last time the revision has been modified, returned
        as a human-readable string.

        Example: "2 weeks ago".
        r"   r@   r#   s    r%   last_modified_strz$CachedRevisionInfo.last_modified_str        !!3444r(   c                 *    t          | j                  S zn
        (property) Sum of the blob file sizes as a human-readable string.

        Example: "42.2K".
        r,   r#   s    r%   r.   z#CachedRevisionInfo.size_on_disk_str   r/   r(   c                 *    t          | j                  S )zC
        (property) Total number of files in the revision.
        )lenr>   r#   s    r%   nb_fileszCachedRevisionInfo.nb_files   s    
 4:r(   N)r0   r1   r2   r3   r4   r5   r   r6   r   r   r7   r8   rC   r.   rI   r9   r(   r%   r;   r;   k   s         & &P ^$$$$
C.53 5 5 5 X5 /# / / / X/ #    X  r(   r;   c                       e Zd ZU dZeed<   eed<   eed<   eed<   eed<   e	e
         ed<   eed<   eed	<   ed
efd            Zed
efd            Zed
efd            Zed
eee
f         fd            ZdS )CachedRepoInfoad  Frozen data structure holding information about a cached repository.

    Args:
        repo_id (`str`):
            Repo id of the repo on the Hub. Example: `"google/fleurs"`.
        repo_type (`Literal["dataset", "model", "space"]`):
            Type of the cached repo.
        repo_path (`Path`):
            Local path to the cached repo.
        size_on_disk (`int`):
            Sum of the blob file sizes in the cached repo.
        nb_files (`int`):
            Total number of blob files in the cached repo.
        revisions (`FrozenSet[CachedRevisionInfo]`):
            Set of [`~CachedRevisionInfo`] describing all revisions cached in the repo.
        last_accessed (`float`):
            Timestamp of the last time a blob file of the repo has been accessed.
        last_modified (`float`):
            Timestamp of the last time a blob file of the repo has been modified/created.

    <Tip warning={true}>

    `size_on_disk` is not necessarily the sum of all revisions sizes because of
    duplicated files. Besides, only blobs are taken into account, not the (negligible)
    size of folders and symlinks.

    </Tip>

    <Tip warning={true}>

    `last_accessed` and `last_modified` reliability can depend on the OS you are using.
    See [python documentation](https://docs.python.org/3/library/os.html#os.stat_result)
    for more details.

    </Tip>
    repo_id	repo_type	repo_pathr   rI   	revisionslast_accessedr@   r    c                 *    t          | j                  S )z
        (property) Last time a blob file of the repo has been accessed, returned as a
        human-readable string.

        Example: "2 weeks ago".
        )r"   rP   r#   s    r%   last_accessed_strz CachedRepoInfo.last_accessed_str   rD   r(   c                 *    t          | j                  S )z
        (property) Last time a blob file of the repo has been modified, returned as a
        human-readable string.

        Example: "2 weeks ago".
        rB   r#   s    r%   rC   z CachedRepoInfo.last_modified_str   rD   r(   c                 *    t          | j                  S rF   r,   r#   s    r%   r.   zCachedRepoInfo.size_on_disk_str   r/   r(   c                 $    d | j         D             S )zQ
        (property) Mapping between `refs` and revision data structures.
        c                 (    i | ]}|j         D ]}||S r9   )r?   ).0revisionrefs      r%   
<dictcomp>z'CachedRepoInfo.refs.<locals>.<dictcomp>  s)    VVV(VVXVVVVr(   )rO   r#   s    r%   r?   zCachedRepoInfo.refs  s    
 WVdnVVVVr(   N)r0   r1   r2   r3   r4   r5   REPO_TYPE_Tr   r6   r   r;   r7   r8   rR   rC   r.   r   r?   r9   r(   r%   rK   rK      s,        # #J LLLOOOMMM+,,,,53 5 5 5 X5 53 5 5 5 X5 /# / / / X/ Wd3 223 W W W XW W Wr(   rK   c                       e Zd ZU dZeed<   ee         ed<   ee         ed<   ee         ed<   ee         ed<   ede	fd            Z
dd
Zd	S )DeleteCacheStrategya  Frozen data structure holding the strategy to delete cached revisions.

    This object is not meant to be instantiated programmatically but to be returned by
    [`~utils.HFCacheInfo.delete_revisions`]. See documentation for usage example.

    Args:
        expected_freed_size (`float`):
            Expected freed size once strategy is executed.
        blobs (`FrozenSet[Path]`):
            Set of blob file paths to be deleted.
        refs (`FrozenSet[Path]`):
            Set of reference file paths to be deleted.
        repos (`FrozenSet[Path]`):
            Set of entire repo paths to be deleted.
        snapshots (`FrozenSet[Path]`):
            Set of snapshots to be deleted (directory of symlinks).
    expected_freed_sizeblobsr?   repos	snapshotsr    c                 *    t          | j                  S )zt
        (property) Expected size that will be freed as a human-readable string.

        Example: "42.2K".
        )r-   r^   r#   s    r%   expected_freed_size_strz+DeleteCacheStrategy.expected_freed_size_str(  s     D4555r(   Nc                 $   | j         D ]}t          |d           | j        D ]}t          |d           | j        D ]}t          |d           | j        D ]}t          |d           t
                              d| j         d           dS )	a  Execute the defined strategy.

        <Tip warning={true}>

        If this method is interrupted, the cache might get corrupted. Deletion order is
        implemented so that references and symlinks are deleted before the actual blob
        files.

        </Tip>

        <Tip warning={true}>

        This method is irreversible. If executed, cached files are erased and must be
        downloaded again.

        </Tip>
        repo)	path_typesnapshotrY   blobzCache deletion done. Saved .N)r`   _try_delete_pathra   r?   r_   loggerinforc   )r$   paths     r%   executezDeleteCacheStrategy.execute1  s    . J 	5 	5DTV44444 N 	9 	9DTZ88888 I 	4 	4DTU33333 J 	5 	5DTV44444Q$2NQQQRRRRRr(   )r    N)r0   r1   r2   r3   r6   r5   r   r   r8   r4   rc   rn   r9   r(   r%   r]   r]     s          $ T?
D/T?6 6 6 6 X6&S &S &S &S &S &Sr(   r]   c                       e Zd ZU dZeed<   ee         ed<   ee	         ed<   e
defd            ZdedefdZd	d
dedefdZdS )HFCacheInfoa  Frozen data structure holding information about the entire cache-system.

    This data structure is returned by [`scan_cache_dir`] and is immutable.

    Args:
        size_on_disk (`int`):
            Sum of all valid repo sizes in the cache-system.
        repos (`FrozenSet[CachedRepoInfo]`):
            Set of [`~CachedRepoInfo`] describing all valid cached repos found on the
            cache-system while scanning.
        warnings (`List[CorruptedCacheException]`):
            List of [`~CorruptedCacheException`] that occurred while scanning the cache.
            Those exceptions are captured so that the scan can continue. Corrupted repos
            are skipped from the scan.

    <Tip warning={true}>

    Here `size_on_disk` is equal to the sum of all repo sizes (only blobs). However if
    some cached repos are corrupted, their sizes are not taken into account.

    </Tip>
    r   r`   warningsr    c                 *    t          | j                  S )z
        (property) Sum of all valid repo sizes in the cache-system as a human-readable
        string.

        Example: "42.2K".
        r,   r#   s    r%   r.   zHFCacheInfo.size_on_disk_strw  s     D-...r(   rO   c                    t          |          }t          t                     }| j        D ]J}|j        D ]@}|j        |v r5||                             |           |                    |j                   AKt          |          dk    r0t          	                    dd
                    |                      t                      }t                      }t                      }t                      }	d}
|                                D ]\  }}|j        |z
  }t          |          dk    r%|                    |j                   |
|j        z  }
G|D ]}|	                    |j                   |j        D ]"}|                    |j        dz  |z             #|j        D ]Z}|j        |vrOd}|D ]$}|j        D ]}|j        |j        k    rd} n|s n%|r$|                    |j                   |
|j        z  }
[t%          t'          |          t'          |          t'          |          t'          |	          |
          S )a  Prepare the strategy to delete one or more revisions cached locally.

        Input revisions can be any revision hash. If a revision hash is not found in the
        local cache, a warning is thrown but no error is raised. Revisions can be from
        different cached repos since hashes are unique across repos,

        Examples:
        ```py
        >>> from huggingface_hub import scan_cache_dir
        >>> cache_info = scan_cache_dir()
        >>> delete_strategy = cache_info.delete_revisions(
        ...     "81fd1d6e7847c99f5862c9fb81387956d99ec7aa"
        ... )
        >>> print(f"Will free {delete_strategy.expected_freed_size_str}.")
        Will free 7.9K.
        >>> delete_strategy.execute()
        Cache deletion done. Saved 7.9K.
        ```

        ```py
        >>> from huggingface_hub import scan_cache_dir
        >>> scan_cache_dir().delete_revisions(
        ...     "81fd1d6e7847c99f5862c9fb81387956d99ec7aa",
        ...     "e2983b237dccf3ab4937c97fa717319a9ca1a96d",
        ...     "6c0e6080953db56375760c0471a8c5f2929baf11",
        ... ).execute()
        Cache deletion done. Saved 8.6G.
        ```

        <Tip warning={true}>

        `delete_revisions` returns a [`~utils.DeleteCacheStrategy`] object that needs to
        be executed. The [`~utils.DeleteCacheStrategy`] is not meant to be modified but
        allows having a dry run before actually executing the deletion.

        </Tip>
        r   z,Revision(s) not found - cannot delete them: , r?   TF)r_   r?   r`   ra   r^   )setr   r`   rO   r<   addremoverH   rk   warningjoinitemsrN   r   r=   r?   r>   r   r]   	frozenset)r$   rO   hashes_to_deleterepos_with_revisionsre   rX   delete_strategy_blobsdelete_strategy_refsdelete_strategy_reposdelete_strategy_snapshots#delete_strategy_expected_freed_sizeaffected_reporevisions_to_deleteother_revisionsrevision_to_deleterY   fileis_file_alonerev_files                      r%   delete_revisionszHFCacheInfo.delete_revisions  s   L &)^^NYZ]N^N^J 	B 	BD N B B'+;;;(.228<<<$++H,@AAAB
   1$$NNg$))TdJeJegghhh+.55*-%%+.55/2uu!./+2F2L2L2N2N #	U #	U.M.+58KKO ?##q((%))-*ABBB3}7QQ3 ': U U")--.@.NOOO .2 U UC(,,]-Dv-MPS-STTTT /4 U UD~-BBB(,(7 & &H,4N * *#'>X5G#G#G49M$)E $H $1 & %& ) U155dnEEE?4CTT?UU4 #122/00122 9:: C
 
 
 	
r(   r   )	verbosityr   c                    |dk    r2t          d t          | j        d           D             g d          S t          d t          | j        d           D             g d	          S )
a}  Generate a table from the [`HFCacheInfo`] object.

        Pass `verbosity=0` to get a table with a single row per repo, with columns
        "repo_id", "repo_type", "size_on_disk", "nb_files", "last_accessed", "last_modified", "refs", "local_path".

        Pass `verbosity=1` to get a table with a row per repo and revision (thus multiple rows can appear for a single repo), with columns
        "repo_id", "repo_type", "revision", "size_on_disk", "nb_files", "last_modified", "refs", "local_path".

        Example:
        ```py
        >>> from huggingface_hub.utils import scan_cache_dir

        >>> hf_cache_info = scan_cache_dir()
        HFCacheInfo(...)

        >>> print(hf_cache_info.export_as_table())
        REPO ID                                             REPO TYPE SIZE ON DISK NB FILES LAST_ACCESSED LAST_MODIFIED REFS LOCAL PATH
        --------------------------------------------------- --------- ------------ -------- ------------- ------------- ---- --------------------------------------------------------------------------------------------------
        roberta-base                                        model             2.7M        5 1 day ago     1 week ago    main ~/.cache/huggingface/hub/models--roberta-base
        suno/bark                                           model             8.8K        1 1 week ago    1 week ago    main ~/.cache/huggingface/hub/models--suno--bark
        t5-base                                             model           893.8M        4 4 days ago    7 months ago  main ~/.cache/huggingface/hub/models--t5-base
        t5-large                                            model             3.0G        4 5 weeks ago   5 months ago  main ~/.cache/huggingface/hub/models--t5-large

        >>> print(hf_cache_info.export_as_table(verbosity=1))
        REPO ID                                             REPO TYPE REVISION                                 SIZE ON DISK NB FILES LAST_MODIFIED REFS LOCAL PATH
        --------------------------------------------------- --------- ---------------------------------------- ------------ -------- ------------- ---- -----------------------------------------------------------------------------------------------------------------------------------------------------
        roberta-base                                        model     e2da8e2f811d1448a5b465c236feacd80ffbac7b         2.7M        5 1 week ago    main ~/.cache/huggingface/hub/models--roberta-base/snapshots/e2da8e2f811d1448a5b465c236feacd80ffbac7b
        suno/bark                                           model     70a8a7d34168586dc5d028fa9666aceade177992         8.8K        1 1 week ago    main ~/.cache/huggingface/hub/models--suno--bark/snapshots/70a8a7d34168586dc5d028fa9666aceade177992
        t5-base                                             model     a9723ea7f1b39c1eae772870f3b547bf6ef7e6c1       893.8M        4 7 months ago  main ~/.cache/huggingface/hub/models--t5-base/snapshots/a9723ea7f1b39c1eae772870f3b547bf6ef7e6c1
        t5-large                                            model     150ebc2c4b72291e770f58e6057481c8d2ed331a         3.0G        4 5 months ago  main ~/.cache/huggingface/hub/models--t5-large/snapshots/150ebc2c4b72291e770f58e6057481c8d2ed331a
        ```

        Args:
            verbosity (`int`, *optional*):
                The verbosity level. Defaults to 0.

        Returns:
            `str`: The table as a string.
        r   c                     g | ]t}|j         |j        d                     |j                  |j        |j        |j        d                    t          |j	                            t          |j                  guS ){:>12}rt   )rL   rM   formatr.   rI   rR   rC   ry   sortedr?   r4   rN   rW   re   s     r%   
<listcomp>z/HFCacheInfo.export_as_table.<locals>.<listcomp>  s          (=>>..		&"3"344DN++	  r(   c                     | j         S NrN   re   s    r%   <lambda>z-HFCacheInfo.export_as_table.<locals>.<lambda>      DN r(   key)REPO ID	REPO TYPESIZE ON DISKNB FILESLAST_ACCESSEDLAST_MODIFIEDREFS
LOCAL PATH)rowsheadersc                 &   g | ]}t          |j        d            D ]t}|j        |j        |j        d                    |j                  |j        |j        d	                    t          |j
                            t          |j                  guS )c                     | j         S r   )r<   )rX   s    r%   r   z8HFCacheInfo.export_as_table.<locals>.<listcomp>.<lambda>8  s
    PXPd r(   r   r   rt   )r   rO   rL   rM   r<   r   r.   rI   rC   ry   r?   r4   r=   )rW   re   rX   s      r%   r   z/HFCacheInfo.export_as_table.<locals>.<listcomp>,  s        $*4>?d?d$e$e$e  !  , (ABB ) 2		&"7"788H233	   r(   c                     | j         S r   r   r   s    r%   r   z-HFCacheInfo.export_as_table.<locals>.<lambda>7  r   r(   )r   r   REVISIONr   r   r   r   r   )r   r   r`   )r$   r   s     r%   export_as_tablezHFCacheInfo.export_as_table  s    P >>  !'tz7R7R S S S  	 	 	   4   !'tz7R7R S S S  	 	 	   r(   N)r0   r1   r2   r3   r6   r5   r   rK   r   r   r8   r4   r.   r]   r   r   r9   r(   r%   rp   rp   Z  s          . ^$$$$*++++/# / / / X/e
3 e
3F e
 e
 e
 e
N 34 \ \ \C \ \ \ \ \ \ \r(   rp   	cache_dirr    c                    | t           } t          |                                                                           } |                                 st          d|  d|           |                                 rt          d|  d          t                      }g }| 	                                D ]]}|j
        dk    r	 |                    t          |                     2# t          $ r}|                    |           Y d}~Vd}~ww xY wt          t!          |          t#          d |D                       |          S )	at  Scan the entire HF cache-system and return a [`~HFCacheInfo`] structure.

    Use `scan_cache_dir` in order to programmatically scan your cache-system. The cache
    will be scanned repo by repo. If a repo is corrupted, a [`~CorruptedCacheException`]
    will be thrown internally but captured and returned in the [`~HFCacheInfo`]
    structure. Only valid repos get a proper report.

    ```py
    >>> from huggingface_hub import scan_cache_dir

    >>> hf_cache_info = scan_cache_dir()
    HFCacheInfo(
        size_on_disk=3398085269,
        repos=frozenset({
            CachedRepoInfo(
                repo_id='t5-small',
                repo_type='model',
                repo_path=PosixPath(...),
                size_on_disk=970726914,
                nb_files=11,
                revisions=frozenset({
                    CachedRevisionInfo(
                        commit_hash='d78aea13fa7ecd06c29e3e46195d6341255065d5',
                        size_on_disk=970726339,
                        snapshot_path=PosixPath(...),
                        files=frozenset({
                            CachedFileInfo(
                                file_name='config.json',
                                size_on_disk=1197
                                file_path=PosixPath(...),
                                blob_path=PosixPath(...),
                            ),
                            CachedFileInfo(...),
                            ...
                        }),
                    ),
                    CachedRevisionInfo(...),
                    ...
                }),
            ),
            CachedRepoInfo(...),
            ...
        }),
        warnings=[
            CorruptedCacheException("Snapshots dir doesn't exist in cached repo: ..."),
            CorruptedCacheException(...),
            ...
        ],
    )
    ```

    You can also print a detailed report directly from the `huggingface-cli` using:
    ```text
    > huggingface-cli scan-cache
    REPO ID                     REPO TYPE SIZE ON DISK NB FILES REFS                LOCAL PATH
    --------------------------- --------- ------------ -------- ------------------- -------------------------------------------------------------------------
    glue                        dataset         116.3K       15 1.17.0, main, 2.4.0 /Users/lucain/.cache/huggingface/hub/datasets--glue
    google/fleurs               dataset          64.9M        6 main, refs/pr/1     /Users/lucain/.cache/huggingface/hub/datasets--google--fleurs
    Jean-Baptiste/camembert-ner model           441.0M        7 main                /Users/lucain/.cache/huggingface/hub/models--Jean-Baptiste--camembert-ner
    bert-base-cased             model             1.9G       13 main                /Users/lucain/.cache/huggingface/hub/models--bert-base-cased
    t5-base                     model            10.1K        3 main                /Users/lucain/.cache/huggingface/hub/models--t5-base
    t5-small                    model           970.7M       11 refs/pr/1, main     /Users/lucain/.cache/huggingface/hub/models--t5-small

    Done in 0.0s. Scanned 6 repo(s) for a total of 3.4G.
    Got 1 warning(s) while scanning. Use -vvv to print details.
    ```

    Args:
        cache_dir (`str` or `Path`, `optional`):
            Cache directory to cache. Defaults to the default HF cache directory.

    <Tip warning={true}>

    Raises:

        `CacheNotFound`
          If the cache directory does not exist.

        [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
          If the cache directory is a file, instead of a directory.

    </Tip>

    Returns: a [`~HFCacheInfo`] object.
    NzCache directory not found: zM. Please use `cache_dir` argument or set `HF_HUB_CACHE` environment variable.)r   z1Scan cache expects a directory but found a file: z.locksc              3   $   K   | ]}|j         V  d S r   )r   r   s     r%   	<genexpr>z!scan_cache_dir.<locals>.<genexpr>  s%      ==t*======r(   )r`   r   rq   )r   r   
expanduserresolveexistsr   is_file
ValueErrorru   iterdirnamerv   _scan_cached_repor   appendrp   r{   sum)r   r`   rq   rN   es        r%   scan_cache_dirr   G  s   l  	Y**,,4466I 
 C)  C  C  C
 
 
 	

  
 Y	  Y  Y  Y
 
 	
 "%E.0H&&((  	>X%%	II'	223333& 	 	 	OOA	 ==u=====   s    "C##
D-DDrN   c                 B
   |                                  st          d|            d| j        vrt          d|            | j                            dd          \  }}|dd         }|                    dd          }|d	vrt          d
| d|  d          i | dz  }| dz  }|                                r|                                 st          d|           t          t                    }|                                r|                                rt          d|           |	                    d          D ]}|                                 rt          |                    |                    }|                                5 }|                                }	ddd           n# 1 swxY w Y   ||	                             |           t                      }
|                                D ]}|j        t           v r|                                rt          d|           t                      }|	                    d          D ]}|                                 rt#          |                                          }|                                st          d|           |vr|                                |<   |                    t)          |j        ||         j        ||         j        |         j                             t1          |          dk    rt3          fd|D                       }n|                                j        }|
                    t5          |j        t7          |          t7          |                    |j        t                                          t;          fdt          d |D                       D                       ||                     	t1          |          dk    r#t          dt=          |           d|  d          t1                    dk    rWt3          d                                 D                       }t3          d                                 D                       }n"|                                 }|j        }|j        }tA          t1                    || |t7          |
          t;          d                                 D                       ||          S ) zScan a single cache repo and return information about it.

    Any unexpected behavior will raise a [`~CorruptedCacheException`].
    zRepo path is not a directory: z--z6Repo path is not a valid HuggingFace cache directory: r   )maxsplitN/>   r   r   r   z8Repo type must be `dataset`, `model` or `space`, found `z` (z).ra   r?   z,Snapshots dir doesn't exist in cached repo: z!Refs directory cannot be a file: z**/*z*Snapshots folder corrupted. Found a file: zBlob missing (broken symlink): )r   r   r   r   r   r   r   c              3   <   K   | ]}|j                  j        V  d S r   )r   st_mtime)rW   r   
blob_statss     r%   r   z$_scan_cached_repo.<locals>.<genexpr>  s.      (f(fQUDN)C)L(f(f(f(f(f(fr(   c              3   2   K   | ]}|         j         V  d S r   st_size)rW   r   r   s     r%   r   z$_scan_cached_repo.<locals>.<genexpr>  s<       ! !6?Jy)1! ! ! ! ! !r(   c              3   $   K   | ]}|j         V  d S r   )r   )rW   r   s     r%   r   z$_scan_cached_repo.<locals>.<genexpr>   s%      FoFoZ^t~FoFoFoFoFoFor(   )r<   r>   r?   r   r=   r@   z-Reference(s) refer to missing commit hashes: z (c              3   $   K   | ]}|j         V  d S r   )st_atimerW   stats     r%   r   z$_scan_cached_repo.<locals>.<genexpr>0  $       O O4 O O O O O Or(   c              3   $   K   | ]}|j         V  d S r   )r   r   s     r%   r   z$_scan_cached_repo.<locals>.<genexpr>1  r   r(   c              3   $   K   | ]}|j         V  d S r   r   r   s     r%   r   z$_scan_cached_repo.<locals>.<genexpr>>  s$      FF$FFFFFFr(   )rI   rL   rN   rM   rO   r   rP   r@   )!is_dirr   r   splitreplacer   r   ru   r   globr4   relative_toopenreadrv   r   FILES_TO_IGNOREr   r   r   r   r   r   r   rH   maxr;   r{   popr   dictvaluesrK   )rN   rM   rL   snapshots_path	refs_pathrefs_by_hashref_pathref_namefr<   cached_revisionsrevision_pathcached_filesr   r   revision_last_modifiedrepo_last_accessedrepo_last_modified
repo_statsr   s                      @r%   r   r     s   
  T%&Ry&R&RSSS9>!!%&j_h&j&jkkk"--dQ-??Iw#2#IoodC((G555%bybbU^bbb
 
 	
 .0J,NF"I  "" g.*?*?*A*A g%&eUc&e&efff
 )4C(8(8L 4  	[)*Yi*Y*YZZZ!v.. 		4 		4H   8//	::;;H 'Affhh' ' ' ' ' ' ' ' ' ' ' ' ' ' ' %))(3333 14'//11 1
 1
00  "" 	h)*fWd*f*fggguu&++F33 	 	I!! Y//11I##%% ]-.[PY.[.[\\\
**(1(8(8
9%'n'!+I!6!>''1)'<'E'1)'<'E  	 	 	 	 |q  %((f(f(f(fYe(f(f(f%f%f""%2%7%7%9%9%B").--|//0BCEEJJKK  ! ! ! !CFFoFobnFoFoFoCoCo! ! !   ,4	 	 		
 	
 	
 	
 <1%_D<N<N__R[___
 
 	
 :  O O:;L;L;N;N O O OOO  O O:;L;L;N;N O O OOO^^%%
'0'0 Z,--FF*2C2C2E2EFFFFF((	 	 	 	s   F33F7	:F7	numc                 x    t          |           }dD ]#}t          |          dk     r	|d| c S |dz  }$|ddS )zkFormat size in bytes into a human-readable string.

    Taken from https://stackoverflow.com/a/1094933
    ) KMGTPEZg     @@z3.1fz.1fY)r7   abs)r   num_funits      r%   r-   r-   D  sb    
 #JJE7  u::(($(((((????r(   ))secondr   <   )minuter   r   )houri     )dayiQ    )weeki:	 r   )monthi '    )yeari3Ntsc                     t          j                     | z
  }|dk     rdS t          D ]"\  }}}t          ||z            }|||k    r n#| d| |dk    rdnd dS )	zFormat timestamp in seconds into a human-readable string, relative to now.

    Vaguely inspired by Django's `timesince` formatter.
       za few seconds agoN r   sr   z ago)time_TIMESINCE_CHUNKSround)r   deltalabeldivider	max_valuevalues         r%   r"   r"   ]  s    
 IKK"Erzz""%6  !w	ego&& Ui%7%7E;;e;EAIISS2;;;;r(   rm   rf   c                    t                               d| d|             	 |                                 rt          j        |            d	S t          j        |            d	S # t          $ r' t                               d| d|  dd           Y d	S t          $ r' t                               d| d|  dd           Y d	S w xY w)
aE  Try to delete a local file or folder.

    If the path does not exists, error is logged as a warning and then ignored.

    Args:
        path (`Path`)
            Path to delete. Can be a file or a folder.
        path_type (`str`)
            What path are we deleting ? Only for logging purposes. Example: "snapshot".
    zDelete z: zCouldn't delete z: file not found ()T)exc_infoz: permission denied (N)
rk   rl   r   osrw   shutilrmtreeFileNotFoundErrorrx   PermissionError)rm   rf   s     r%   rj   rj   l  s     KK-)--t--...b<<>> 	 IdOOOOOM$ _ _ _N)NNtNNNY]^^^^^^ b b bQ)QQ$QQQ\`aaaaaabs   (A" A" "-C,CCr   ).r3   r  r  r  collectionsr   dataclassesr   pathlibr   typingr   r   r   r	   r
   r   r   huggingface_hub.errorsr   r   commands._cli_utilsr   	constantsr   r   r   
get_loggerr0   rk   r[   r   r   r;   rK   r]   rp   r4   r   r   r6   r-   r  r7   r"   rj   r9   r(   r%   <module>r     sw   ; : 				   # # # # # # ! ! ! ! ! !       G G G G G G G G G G G G G G G G G G I I I I I I I I * * * * * * $ $ $ $ $ $       
	H	%	%12 - $?/ ?/ ?/ ?/ ?/ ?/ ?/ ?/D $I I I I I I I IX $RW RW RW RW RW RW RW RWj $HS HS HS HS HS HS HS HSV $i i i i i i i iXs shuS$Y'78 sK s s s slD D. D D D DN
c 
c 
 
 
 
	 <% <C < < < <b4 bC bD b b b b b br(   