o
    h@                  
   @   sV  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mZ ddlm	Z	 ddl
mZ ddlmZ eeZeG dd	 d	Zed
dG dd dZeG dd dZeG dd dZde	dedefddZde	dedefddZde	dedee fddZde	dedefddZde	dedededdf
dd Zde	de	fd!d"Zdedefd#d$ZdS )%u$  Contains utilities to handle the `../.cache/huggingface` folder in local directories.

First discussed in https://github.com/huggingface/huggingface_hub/issues/1738 to store
download metadata when downloading files from the hub to a local directory (without
using the cache).

./.cache/huggingface folder structure:
[4.0K]  data
├── [4.0K]  .cache
│   └── [4.0K]  huggingface
│       └── [4.0K]  download
│           ├── [  16]  file.parquet.metadata
│           ├── [  16]  file.txt.metadata
│           └── [4.0K]  folder
│               └── [  16]  file.parquet.metadata
│
├── [6.5G]  file.parquet
├── [1.5K]  file.txt
└── [4.0K]  folder
    └── [   16]  file.parquet


Download metadata file structure:
```
# file.txt.metadata
11c5a3d5811f50298f278a704980280950aedb10
a16a55fda99d2f2e7b69cce5cf93ff4ad3049930
1712656091.123

# file.parquet.metadata
11c5a3d5811f50298f278a704980280950aedb10
7c5d3f4b8b76583b422fcb9189ad6c89d5d97a094541ce8932dce3ecabde1421
1712656091.123
}
```
    N)	dataclass)Path)Optional   )WeakFileLockc                   @   s<   e Zd ZU dZeed< eed< eed< dedefddZd	S )
LocalDownloadFilePathsa  
    Paths to the files related to a download process in a local dir.

    Returned by [`get_local_download_paths`].

    Attributes:
        file_path (`Path`):
            Path where the file will be saved.
        lock_path (`Path`):
            Path to the lock file used to ensure atomicity when reading/writing metadata.
        metadata_path (`Path`):
            Path to the metadata file.
    	file_path	lock_pathmetadata_pathetagreturnc                 C   s    | j jt| j j d| d S )z^Return the path where a file will be temporarily downloaded before being moved to `file_path`..z.incomplete)r
   parent_short_hashname)selfr    r   q/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/huggingface_hub/_local_folder.pyincomplete_pathW   s    z&LocalDownloadFilePaths.incomplete_pathN)__name__
__module____qualname____doc__r   __annotations__strr   r   r   r   r   r   C   s   
 r   T)frozenc                   @   s2   e Zd ZU dZeed< eed< eed< eed< dS )LocalUploadFilePathsa  
    Paths to the files related to an upload process in a local dir.

    Returned by [`get_local_upload_paths`].

    Attributes:
        path_in_repo (`str`):
            Path of the file in the repo.
        file_path (`Path`):
            Path where the file will be saved.
        lock_path (`Path`):
            Path to the lock file used to ensure atomicity when reading/writing metadata.
        metadata_path (`Path`):
            Path to the metadata file.
    path_in_repor   r	   r
   N)r   r   r   r   r   r   r   r   r   r   r   r   \   s   
 r   c                   @   s2   e Zd ZU dZeed< eed< eed< eed< dS )LocalDownloadFileMetadataaG  
    Metadata about a file in the local directory related to a download process.

    Attributes:
        filename (`str`):
            Path of the file in the repo.
        commit_hash (`str`):
            Commit hash of the file in the repo.
        etag (`str`):
            ETag of the file in the repo. Used to check if the file has changed.
            For LFS files, this is the sha256 of the file. For regular files, it corresponds to the git hash.
        timestamp (`int`):
            Unix timestamp of when the metadata was saved i.e. when the metadata was accurate.
    filenamecommit_hashr   	timestampN)r   r   r   r   r   r   floatr   r   r   r   r   t   s   
 r   c                   @   s   e Zd ZU dZeed< dZee ed< dZ	ee
 ed< dZee ed< dZee ed< dZe
ed	< dZe
ed
< deddfddZdS )LocalUploadFileMetadatazT
    Metadata about a file in the local directory related to an upload process.
    sizeNr!   should_ignoresha256upload_modeFis_uploadedis_committedpathsr   c              	   C   s.  t |j |jdn}t }|t|d  |t| j |d | jdur6|tt	| j |d | j
durF|| j
 |d | jdurV|| j |d |tt	| jd  |tt	| jd  W d   n1 s}w   Y  || _W d   dS 1 sw   Y  dS )zSave the metadata to disk.w
N)r   r	   r
   opentimewriter   r$   r%   intr&   r'   r(   r)   r!   )r   r*   fnew_timestampr   r   r   save   s(   






"zLocalUploadFileMetadata.save)r   r   r   r   r0   r   r!   r   r"   r%   boolr&   r   r'   r(   r)   r   r3   r   r   r   r   r#      s   
 r#   	local_dirr   r   c                 C   s   t jj|d }t jdkr|dsd|v rtd| d| | }t| d | d }|d	}t jdkrgt	| d
sgt
t j|dkrgtd
t j| }td
t j| }td
t j| }|jjddd |jjddd t|||dS )a  Compute paths to the files related to a download process.

    Folders containing the paths are all guaranteed to exist.

    Args:
        local_dir (`Path`):
            Path to the local directory in which files are downloaded.
        filename (`str`):
            Path of the file in the repo.

    Return:
        [`LocalDownloadFilePaths`]: the paths to the files (file_path, lock_path, metadata_path, incomplete_path).
    /nt..\\..\*Invalid filename: cannot handle filename 'B' on Windows. Please ask the repository owner to rename this file.download	.metadata.lock\\?\   Tparentsexist_ok)r   r	   r
   )ospathjoinsplitr   
startswith
ValueError_huggingface_dirwith_suffixr   lenabspathr   r   mkdirr   r5   r   sanitized_filenamer   r
   r	   r   r   r   get_local_download_paths   s"   



"rQ   c                 C   s   t jj|d }t jdkr|dsd|v rtd| d| | }t| d | d }|d	}t jdkrgt	| d
sgt
t j|dkrgtd
t j| }td
t j| }td
t j| }|jjddd |jjddd t||||dS )a  Compute paths to the files related to an upload process.

    Folders containing the paths are all guaranteed to exist.

    Args:
        local_dir (`Path`):
            Path to the local directory that is uploaded.
        filename (`str`):
            Path of the file in the repo.

    Return:
        [`LocalUploadFilePaths`]: the paths to the files (file_path, lock_path, metadata_path).
    r6   r7   r8   r9   r:   r;   uploadr=   r>   r?   r@   TrA   )r   r   r	   r
   )rD   rE   rF   rG   r   rH   rI   rJ   rK   r   rL   rM   r   r   rN   r   rO   r   r   r   get_local_upload_paths   s&   



"rS   c           
      C   s  t | |}t|j |j rz3|j $}|  }|  }t|  }t	||||d}W d   n1 s=w   Y  W nE t
y } z9td|j d| d z|j  W n t
y} } ztd|j d|  W Y d}~nd}~ww W Y d}~nd}~ww z"|j }	|	jd |jkr|W W  d   S td| d	 W n ty   Y W d   dS w W d   dS W d   dS 1 sw   Y  dS )
ar  Read metadata about a file in the local directory related to a download process.

    Args:
        local_dir (`Path`):
            Path to the local directory in which files are downloaded.
        filename (`str`):
            Path of the file in the repo.

    Return:
        `[LocalDownloadFileMetadata]` or `None`: the metadata if it exists, `None` otherwise.
    )r   r    r   r!   NInvalid metadata file : %. Removing it from disk and continue.)Could not remove corrupted metadata file r   Ignored metadata for '#' (outdated). Will re-compute hash.)rQ   r   r	   r
   existsr-   readlinestripr"   r   	Exceptionloggerwarningunlinkr   statst_mtimer!   infoFileNotFoundError)
r5   r   r*   r1   r    r   r!   metadataera   r   r   r   read_download_metadata	  s\   


$


"
""rg   c                 C   sv  t | |}t|j |j r"z|j t}t|  }t	|  }|  }|dkr5dnt
t	|}|  }|dkrGdn|}	|  }
|
dkrUdn|
}|dvrftd|j d| t
t	|  }t
t	|  }t||||	|||d}W d   n1 sw   Y  W nE ty } z9td|j d| d z|j  W n ty } ztd	|j d|  W Y d}~nd}~ww W Y d}~nd}~ww |jdur|jr|jst |j d
krd|_z%|jdur|j j|jkr|W W  d   S td| d W n
 ty!   Y nw W d   n	1 s-w   Y  t|j jdS )a  Read metadata about a file in the local directory related to an upload process.

    TODO: factorize logic with `read_download_metadata`.

    Args:
        local_dir (`Path`):
            Path to the local directory in which files are downloaded.
        filename (`str`):
            Path of the file in the repo.

    Return:
        `[LocalUploadFileMetadata]` or `None`: the metadata if it exists, `None` otherwise.
     N)Nregularlfsz Invalid upload mode in metadata rU   )r!   r$   r%   r&   r'   r(   r)   rT   rV   rW   i@ FrX   rY   )r$   )rS   r   r	   r
   rZ   r-   r"   r[   r\   r0   r4   rI   r   r#   r]   r^   r_   r`   r!   r(   r)   r.   r   ra   rb   rc   rd   st_size)r5   r   r*   r1   r!   r$   _should_ignorer%   _sha256r&   _upload_moder'   r(   r)   re   rf   r   r   r   read_upload_metadata;  st   
$
 6<ro   r    r   c              
   C   s   t | |}t|j7 |jd}|| d| dt  d W d   n1 s,w   Y  W d   dS W d   dS 1 sDw   Y  dS )zWrite metadata about a file in the local directory related to a download process.

    Args:
        local_dir (`Path`):
            Path to the local directory in which files are downloaded.
    r+   r,   N)rQ   r   r	   r
   r-   r/   r.   )r5   r   r    r   r*   r1   r   r   r   write_download_metadata  s   
""rp   c                 C   s   | d d }|j ddd |d }|d }| sZzt|dd |d	 W d
   n1 s0w   Y  W n ty?   Y n	 tyG   Y nw z|  W |S  tyY   Y |S w |S )zKReturn the path to the `.cache/huggingface` directory in a local directory.z.cachehuggingfaceT)rC   rB   z
.gitignorez.gitignore.lockg?)timeout*N)rN   rZ   r   
write_text
IndexErrorOSErrorr`   )r5   rE   	gitignoregitignore_lockr   r   r   rJ     s.   
rJ   c                 C   s   t t|    S )N)base64urlsafe_b64encodehashlibsha1encodedigestdecode)r   r   r   r   r     s   r   )r   ry   r{   loggingrD   r.   dataclassesr   pathlibr   typingr   utilsr   	getLoggerr   r^   r   r   r   r#   r   rQ   rS   rg   ro   rp   rJ   r   r   r   r   r   <module>   s4   %
+(*2N