o
    |ŸŽiÔ  ã                   @   sŽ   d dl Z d dlmZmZmZmZmZ d dlmZm	Z	m
Z
 d dlmZ er0d dl mZ d dlmZ dZg d¢ZG d	d
„ d
ƒZG dd„ deƒZdS )é    N)ÚTYPE_CHECKINGÚBinaryIOÚDictÚIteratorÚList)ÚHashMismatchÚHashMissingÚInstallationError)Úread_chunks)Ú_Hash)ÚNoReturnÚsha256)r   Úsha384Úsha512c                   @   sv   e Zd ZdZddd„Zdd„ Zedd„ ƒZd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚHasheszaA wrapper that builds multiple hashes at once and checks them against
    known-good values

    Nc                 C   s4   i }|dur|  ¡ D ]
\}}t|ƒ||< q
|| _dS )zo
        :param hashes: A dict of algorithm names pointing to lists of allowed
            hex digests
        N)ÚitemsÚsortedÚ_allowed)ÚselfÚhashesÚallowedÚalgÚkeys© r   údC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\pip/_internal/utils/hashes.pyÚ__init__   s
   
zHashes.__init__c                    sb   t |tƒstS |sˆS ˆs|S i }|j ¡ D ]\‰ }ˆ ˆjvr q‡ ‡fdd„|D ƒ|ˆ < qt|ƒS )Nc                    s   g | ]}|ˆj ˆ  v r|‘qS r   )r   )Ú.0Úv©r   r   r   r   Ú
<listcomp>=   s    z"Hashes.__and__.<locals>.<listcomp>)Ú
isinstancer   ÚNotImplementedr   r   )r   ÚotherÚnewÚvaluesr   r   r   Ú__and__,   s   

zHashes.__and__c                 C   s   t dd„ | j ¡ D ƒƒS )Nc                 s   s   � | ]}t |ƒV  qd S ©N)Úlen)r   Údigestsr   r   r   Ú	<genexpr>C   s   € z&Hashes.digest_count.<locals>.<genexpr>)Úsumr   r$   ©r   r   r   r   Údigest_count@   s   zHashes.digest_countc                 C   s   || j  |g ¡v S )z/Return whether the given hex digest is allowed.N)r   Úget)r   Ú	hash_nameÚ
hex_digestr   r   r   Úis_hash_allowedE   s   zHashes.is_hash_allowedc              
   C   s    i }| j  ¡ D ]}z	t |¡||< W q ttfy"   td|› �ƒ‚w |D ]}| ¡ D ]}| |¡ q+q%| 	¡ D ]\}}| 
¡ | j | v rH dS q8|  |¡ dS )z‚Check good hashes against ones built from iterable of chunks of
        data.

        Raise HashMismatch if none match.

        zUnknown hash name: N)r   r   Úhashlibr#   Ú
ValueErrorÚ	TypeErrorr	   r$   Úupdater   Ú	hexdigestÚ_raise)r   ÚchunksÚgotsr.   ÚchunkÚhashÚgotr   r   r   Úcheck_against_chunksN   s    ÿÿÿzHashes.check_against_chunksc                 C   s   t | j|ƒ‚r&   )r   r   ©r   r8   r   r   r   r6   f   s   zHashes._raisec                 C   s   |   t|ƒ¡S )zaCheck good hashes against a file-like object

        Raise HashMismatch if none match.

        N)r<   r
   )r   Úfiler   r   r   Úcheck_against_filej   s   zHashes.check_against_filec                 C   s8   t |dƒ�}|  |¡W  d   ƒ S 1 sw   Y  d S )NÚrb)Úopenr?   )r   Úpathr>   r   r   r   Úcheck_against_paths   s   $ÿzHashes.check_against_pathc                 C   s
   t | jƒS )z,Return whether I know any known-good hashes.N)Úboolr   r+   r   r   r   Ú__nonzero__x   s   
zHashes.__nonzero__c                 C   s   |   ¡ S r&   )rE   r+   r   r   r   Ú__bool__}   s   zHashes.__bool__c                 C   s   t |tƒstS | j|jkS r&   )r    r   r!   r   )r   r"   r   r   r   Ú__eq__�   s   
zHashes.__eq__c                 C   s"   t d tdd„ | j ¡ D ƒƒ¡ƒS )Nú,c                 s   s,   � | ]\}}|D ]
}d   ||f¡V  qqdS )ú:N)Újoin)r   r   Údigest_listÚdigestr   r   r   r)   ‹   s   € ýþÿz"Hashes.__hash__.<locals>.<genexpr>)r:   rJ   r   r   r   r+   r   r   r   Ú__hash__‡   s   þÿÿzHashes.__hash__r&   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   Úpropertyr,   r0   r<   r6   r?   rC   rE   rF   rG   rM   r   r   r   r   r      s    

		r   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚMissingHasheszÀA workalike for Hashes used when we're missing a hash for a requirement

    It computes the actual hash of the requirement and raises a HashMissing
    exception showing it to the user.

    c                    s   t ƒ jtg id� dS )z!Don't offer the ``hashes`` kwarg.)r   N)Úsuperr   ÚFAVORITE_HASHr+   ©Ú	__class__r   r   r   œ   s   zMissingHashes.__init__c                 C   s   t |t  ¡ ƒ‚r&   )r   rU   r5   r=   r   r   r   r6   £   s   zMissingHashes._raise)rN   rO   rP   rQ   r   r6   Ú__classcell__r   r   rV   r   rS   ”   s    rS   )r1   Útypingr   r   r   r   r   Úpip._internal.exceptionsr   r   r	   Úpip._internal.utils.miscr
   r   r   rU   ÚSTRONG_HASHESr   rS   r   r   r   r   Ú<module>   s    {