U
    
Ha/                     @   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r`d dl mZ d dlmZ dZddd	g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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k	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   >/tmp/pip-unpacked-wheel-tx790h60/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kr@q, fdd|D | < q,t|S )Nc                    s   g | ]}|j   k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 kS )z/Return whether the given hex digest is allowed.)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k
rJ   td| Y qX q|D ]}| D ]}|| q^qR|	 D ] \}}|
 | j | krx dS qx| | dS )zCheck 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|d S 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.

        )r<   r
   )r   filer   r   r   check_against_filej   s    zHashes.check_against_filec              
   C   s,   t |d}| |W  5 Q R  S Q R X 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.)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   Z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__)N)__name__
__module____qualname____doc__r   r%   propertyr,   r0   r<   r6   r?   rC   rE   rF   rG   rL   r   r   r   r   r      s   

		r   c                       s(   e Zd ZdZ fddZdd Z  ZS )MissingHasheszA 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  d S r&   )r   rT   r5   r=   r   r   r   r6      s    zMissingHashes._raise)rM   rN   rO   rP   r   r6   __classcell__r   r   rU   r   rR      s   rR   )r1   typingr   r   r   r   r   pip._internal.exceptionsr   r   r	   pip._internal.utils.miscr
   r   r   rT   STRONG_HASHESr   rR   r   r   r   r   <module>   s   
{