o
    )j                      @   sh   d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	Z	d dl
mZ e ZdZG dd dZdS )    N)Path)DictOptionalUnion)
get_loggerz.ms_upload_cachec                	   @   s   e Zd ZdZdeeef fddZedede	de
defd	d
Zdede	de
dee fddZdede	de
defddZdd Zdd ZdS )UploadHashCachear  Persistent local hash cache for upload_folder resume.

    Stores SHA256 hashes keyed by (relative_path, mtime, size) to skip
    re-hashing unchanged files on retry/resume. Thread-safe for concurrent
    put() calls from multiple upload threads.

    Cache is stored as JSON at {folder_path}/.ms_upload_cache, co-located
    with the upload source for portability.
    
cache_pathc                 C   s&   t || _i | _t | _|   dS )z}Initialize cache.

        Args:
            cache_path: Path to the cache file (typically folder/.ms_upload_cache).
        N)r   _cache_path_cache	threadingLock_lock_load)selfr    r   \/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/hub/upload_cache.py__init__   s   

zUploadHashCache.__init__rel_pathmtimesizereturnc                 C   s   |  d| d| S )z*Build cache lookup key from file metadata.|r   )r   r   r   r   r   r   	_make_key(   s   zUploadHashCache._make_keyc                 C   s`   |  |||}| j | j|}W d   n1 sw   Y  |du r&dS ||d |d dS )aF  Return cached hash info or None if not cached / stale.

        Args:
            rel_path: Relative path of the file within the upload folder.
            mtime: File modification time (os.stat st_mtime).
            size: File size in bytes.

        Returns:
            Dict with file_hash and file_size, or None.
        N	file_hash	file_size)Zfile_path_or_objr   r   )r   r   r
   get)r   r   r   r   keyentryr   r   r   r   -   s   zUploadHashCache.get	hash_infoc                 C   sV   |  |||}|d |d d}| j || j|< W d   dS 1 s$w   Y  dS )a  Store hash info for a file. Thread-safe.

        Args:
            rel_path: Relative path of the file.
            mtime: File modification time.
            size: File size in bytes.
            hash_info: Dict from compute_file_hash with file_hash and file_size.
        r   r   )r   r   N)r   r   r
   )r   r   r   r   r   r   r   r   r   r   putD   s   	"zUploadHashCache.putc              
   C   sN  z| j jjddd | j t| j}W d   n1 sw   Y  tjt| j jdd\}}z)t	j
|ddd}t|| W d   n1 sIw   Y  t	|t| j  W n tye   t	|  w td	t| d
| j   | j  std| j   W dS W dS  ty } ztd| j  d|  W Y d}~dS d}~ww )zPersist cache to disk via atomic write (temp file + rename).

        Safe against crashes -- either the old or new file is present,
        never a partial write.
        T)parentsexist_okNz.ms_upload_cache_tmp_)dirprefixwzutf-8)encodingzHash cache saved: z entries -> z&Hash cache file not found after save: zFailed to save hash cache to z: )r	   parentmkdirr   dictr
   tempfilemkstempstrosfdopenjsondumpreplaceBaseExceptionunlinkloggerinfolenexistswarning	Exception)r   datafdZtmp_pathfer   r   r   saveU   s@   





zUploadHashCache.savec              
   C   s   | j  sdS z.t| j d}t|| _W d   n1 sw   Y  tdt| j d| j   W dS  t	yS } zt
d|  i | _W Y d}~dS d}~ww )z8Load cache from disk. Tolerates missing or corrupt file.NrzHash cache loaded: z entries from z+Failed to load hash cache, starting fresh: )r	   r6   openr.   loadr
   r3   r4   r5   r8   r7   )r   r;   r<   r   r   r   r   s   s   

zUploadHashCache._loadN)__name__
__module____qualname____doc__r   r+   r   r   staticmethodfloatintr   r   r(   r   r   r=   r   r   r   r   r   r      s    
r   )r,   r)   r   pathlibr   typingr   r   r   r.   Zmodelscope.utils.loggerr   r3   ZUPLOAD_HASH_CACHE_FILEr   r   r   r   r   <module>   s   