3
ûâdeË  ã               @   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               @   sÚ   e Zd ZdZd#eeee f ddœdd„Zd d dœdd„Ze	e
d	œd
d„ƒZeeedœdd„Zee ddœdd„Zeedf ddœdd„Zeddœdd„Zeddœdd„Zed	œdd„Zeedœdd „Ze
d	œd!d"„ZdS )$ÚHasheszaA wrapper that builds multiple hashes at once and checks them against
    known-good values

    N)ÚhashesÚreturnc             C   s8   i }|dk	r.x |j ƒ D ]\}}t|ƒ||< qW || _dS )zo
        :param hashes: A dict of algorithm names pointing to lists of allowed
            hex digests
        N)ÚitemsÚsortedÚ_allowed)Úselfr   ÚallowedÚalgÚkeys© r   ú9/tmp/pip-build-red6nood/pip/pip/_internal/utils/hashes.pyÚ__init__   s
    zHashes.__init__)Úotherr   c                sf   t |tƒstS |sˆS ˆs|S i }x:|jjƒ D ],\‰ }ˆ ˆjkrBq.‡ ‡fdd„|D ƒ|ˆ < q.W 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   r   ÚnewÚvaluesr   )r   r   r   Ú__and__+   s    

zHashes.__and__)r   c             C   s   t dd„ | jjƒ D ƒƒS )Nc             s   s   | ]}t |ƒV  qd S )N)Úlen)r   Zdigestsr   r   r   ú	<genexpr>@   s    z&Hashes.digest_count.<locals>.<genexpr>)Úsumr   r$   )r   r   r   r   Údigest_count>   s    zHashes.digest_count)Ú	hash_nameÚ
hex_digestr   c             C   s   || j j|g ƒkS )z/Return whether the given hex digest is allowed.)r   Úget)r   r*   r+   r   r   r   Úis_hash_allowedB   s    zHashes.is_hash_allowed)Úchunksr   c             C   s¶   i }xL| j jƒ D ]>}ytj|ƒ||< W q ttfk
rL   td|› �ƒ‚Y qX qW x(|D ] }x|jƒ D ]}|j|ƒ qfW qXW x*|j	ƒ D ]\}}|j
ƒ | j | kr†dS q†W | j|ƒ 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   r.   Úgotsr*   ÚchunkÚhashÚgotr   r   r   Úcheck_against_chunksF   s    
zHashes.check_against_chunksr   r   )r5   r   c             C   s   t | j|ƒ‚d S )N)r   r   )r   r5   r   r   r   r4   ]   s    zHashes._raise)Úfiler   c             C   s   | j t|ƒƒS )zaCheck good hashes against a file-like object

        Raise HashMismatch if none match.

        )r9   r
   )r   r:   r   r   r   Úcheck_against_file`   s    zHashes.check_against_file)Úpathr   c          	   C   s    t |dƒ�}| j|ƒS Q R X d S )NÚrb)Úopenr;   )r   r<   r:   r   r   r   Úcheck_against_pathh   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   Ú__bool__l   s    zHashes.__bool__c             C   s   t |tƒstS | j|jkS )N)r!   r   r"   r   )r   r   r   r   r   Ú__eq__p   s    
zHashes.__eq__c             C   s"   t djtdd„ | jjƒ D ƒƒƒƒS )Nú,c             s   s*   | ]"\}}|D ]}d j ||fƒV  qqdS )ú:N)Újoin)r   r   Zdigest_listÚdigestr   r   r   r'   y   s   z"Hashes.__hash__.<locals>.<genexpr>)r7   rE   r   r   r   )r   r   r   r   Ú__hash__u   s
    zHashes.__hash__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr   r   r%   ÚpropertyÚintr)   r@   r-   r   Úbytesr9   r4   r   r;   r?   rA   ÚobjectrB   rG   r   r   r   r   r      s   r   c                   s>   e Zd ZdZddœ‡ fdd„Zeedf ddœ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.

    N)r   c                s   t ƒ jtg id� dS )z!Don't offer the ``hashes`` kwarg.)r   N)Úsuperr   ÚFAVORITE_HASH)r   )Ú	__class__r   r   r   ‰   s    zMissingHashes.__init__r   r   )r5   r   c             C   s   t |t jƒ ƒ‚d S )N)r   rS   r3   )r   r5   r   r   r   r4   �   s    zMissingHashes._raise)	rH   rI   rJ   rK   r   r   rL   r4   Ú__classcell__r   r   )rT   r   rQ   �   s   rQ   )r/   Útypingr   r   r   r   r   Zpip._internal.exceptionsr   r   r	   Zpip._internal.utils.miscr
   r   r   rS   ÚSTRONG_HASHESr   rQ   r   r   r   r   Ú<module>   s   
h