o
    0iP                     @   s\  d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	 ddl
mZ ddlmZ dd	lmZmZ er8dd
lmZ e dZG dd de	Zed ZeddG dd dZdedeeef fddZdedee defddZdededefddZdeed f d!eeef ded"edef
d#d$Zd%ed&edee d'ee d(ee deeef fd)d*ZdS )+    N)	dataclass)Path)TYPE_CHECKINGLiteralOptional	TypedDict   )	constants)repo_folder_name   )git_hashsha_fileobj)RepoFilez^[0-9a-f]{40}$c                   @   s.   e Zd ZU eed< eed< eed< eed< dS )Mismatchpathexpectedactual	algorithmN)__name__
__module____qualname__str__annotations__ r   r   ]/home/kim/smarthome/.venv/lib/python3.10/site-packages/huggingface_hub/utils/_verification.pyr      s
   
 r   )sha256git-sha1T)frozenc                   @   sJ   e Zd ZU eed< eed< ee ed< ee ed< ee ed< eed< dS )FolderVerificationrevisionchecked_count
mismatchesmissing_pathsextra_pathsverified_pathN)	r   r   r   r   r   intlistr   r   r   r   r   r   r      s   
 r   rootreturnc                    s    fdd  dD S )z]
    Return a mapping of repo-relative path -> absolute path for all files under `root`.
    c                    s$   i | ]}|  r|  |qS r   )is_filerelative_toas_posix.0pr'   r   r   
<dictcomp>+   s   $ z'collect_local_files.<locals>.<dictcomp>*)rglobr/   r   r/   r   collect_local_files'   s   r3   storage_folderr   c                 C   s   |r	t |r	|S | d }| d }|r.|| }| r#|jdd S td| d| d|d }| r>|jdd S | sJtd	| d
dd | D }t|dkr]|d S td)zR
    Resolve a commit hash from a cache repo folder and an optional revision.
    refs	snapshotszutf-8)encodingz
Revision 'z1' could not be resolved in cache (expected file 'z').mainz+Cache repo is missing snapshots directory: z . Provide --revision explicitly.c                 S   s&   g | ]}|  rt|jr|jqS r   )is_dir_REGEX_COMMIT_HASH	fullmatchnamer,   r   r   r   
<listcomp>F   s   & z3_resolve_commit_hash_from_cache.<locals>.<listcomp>r   r   zhAmbiguous cached revision: multiple snapshots found and no refs to disambiguate. Please pass --revision.)	r:   r;   r)   	read_textstrip
ValueErrorr9   iterdirlen)r4   r   Zrefs_dirZsnapshots_dirZref_pathZmain_ref
candidatesr   r   r   _resolve_commit_hash_from_cache.   s(   rD   r   r   c                 C   sr   |  d*}|dkrt| W  d   S |dkr(t| W  d   S td| 1 s2w   Y  dS )zM
    Compute the checksum of a local file using the requested algorithm.
    rbr   Nr   zUnsupported hash algorithm: )openr   hexr   readr@   )r   r   streamr   r   r   compute_file_hashO   s   

rJ   remote_by_pathr   local_by_pathr$   c              	   C   s   t | }t |}t|| }t|| }t||@ }g }	|D ]T}
| |
 }||
 }t|dd}|dur8t|ddnd}|du rHt|trH|d}|rSd}t| }n|j}d}t| }t	||}||krr|	
t|
|||d qt|t||	|||dS )zHCompare remote entries and local files and return a verification result.lfsNr   r   )r   r   r   r   )r   r    r!   r"   r#   r$   )setsortedgetattr
isinstancedictgetr   lowerblob_idrJ   appendr   r   rB   )rK   rL   r   r$   Zremote_pathsZlocal_pathsmissingextraZbothr!   rel_pathZremote_entryZ
local_pathrM   Zlfs_shar   r   rU   r   r   r   r   verify_maps\   s>   

rZ   repo_id	repo_type	cache_dir	local_dirc           
      C   s   |durt |  }| std| ||ptjfS t |p#tj  }|t| |d }|	 s=td| dt
||}|d | }	|	 sWtd| d|	 d	|	|fS )
zW
    Resolve the root directory to scan locally and the remote revision to verify.
    Nz6Local directory does not exist or is not a directory: )r[   r\   zRepo is not present in cache: z.. Use 'hf download' first or pass --local-dir.r6   z0Snapshot directory does not exist for revision 'z': .)r   
expanduserresolver9   r@   r	   ZDEFAULT_REVISIONZHF_HUB_CACHEr
   existsrD   )
r[   r\   r   r]   r^   r'   Z
cache_rootr4   commitZsnapshot_dirr   r   r   resolve_local_root   s    

rd   ) redataclassesr   pathlibr   typingr   r   r   r    r	   Zfile_downloadr
   shar   r   Zhf_apir   compiler:   r   ZHashAlgor   rR   r   r3   rD   rJ   rZ   tuplerd   r   r   r   r   <module>   sR    
	!


0
