o
    ix                     @   sh  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	m
Z
 ddlmZ ddlmZ dd	lmZmZ er<dd
lmZ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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Union   )	constants)repo_folder_name   )git_hashsha_fileobj)RepoFile
RepoFolderz^[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   i/var/www/html/karishye-ai-python/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>*)rglobr1   r   r1   r   collect_local_files'   s   r5   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
ValueErrorr;   iterdirlen)r6   r!   refs_dirsnapshots_dirref_pathmain_ref
candidatesr   r   r   _resolve_commit_hash_from_cache.   s(   rJ   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   readrB   )r   r   streamr   r   r   compute_file_hashO   s   

rP   remote_by_path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_idrP   appendr   r    rD   )rQ   rR   r!   r&   remote_pathslocal_pathsmissingextrabothr#   rel_pathremote_entry
local_pathrS   lfs_shar   r   r[   r   r   r   r   verify_maps\   s>   

rf   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: )rg   rh   zRepo is not present in cache: z.. Use 'hf download' first or pass --local-dir.r8   z0Snapshot directory does not exist for revision 'z': .)r   
expanduserresolver;   rB   r
   DEFAULT_REVISIONHF_HUB_CACHEr   existsrJ   )
rg   rh   r!   ri   rj   r)   
cache_rootr6   commitsnapshot_dirr   r   r   resolve_local_root   s    

rt   )"redataclassesr   pathlibr   typingr   r   r   r   r    r
   file_downloadr   shar   r   hf_apir   r   compiler<   r   HashAlgor    rX   r   r5   rJ   rP   rf   tuplert   r   r   r   r   <module>   sR    
	!

0
