3
zYði2  ã               @   sz   d dl mZ d dlZd dlmZmZmZ d dlmZ d dl	m
Z
mZmZ dZdddgZG d	d
„ d
eƒZG dd„ deƒZdS )é    )Úabsolute_importN)ÚHashMismatchÚHashMissingÚInstallationError)Úread_chunks)Ú	iteritemsÚiterkeysÚ
itervaluesÚsha256Úsha384Úsha512c               @   sJ   e Zd ZdZd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   s   |dkri n|| _ dS )zo
        :param hashes: A dict of algorithm names pointing to lists of allowed
            hex digests
        N)Ú_allowed)ÚselfÚhashes© r   ú//tmp/pip-build-ue6tjl93/pip/pip/utils/hashes.pyÚ__init__   s    zHashes.__init__c             C   s´   i }xJt | jƒD ]<}ytj|ƒ||< W q ttfk
rJ   td| ƒ‚Y qX qW x(|D ] }xt|ƒD ]}|j|ƒ qdW qVW x*t	|ƒ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: %sN)r   r   ÚhashlibÚnewÚ
ValueErrorÚ	TypeErrorr   r	   Úupdater   Ú	hexdigestÚ_raise)r   ÚchunksÚgotsÚ	hash_nameÚchunkÚhashÚgotr   r   r   Úcheck_against_chunks    s    
zHashes.check_against_chunksc             C   s   t | j|ƒ‚d S )N)r   r   )r   r   r   r   r   r   7   s    zHashes._raisec             C   s   | j t|ƒƒS )zaCheck good hashes against a file-like object

        Raise HashMismatch if none match.

        )r!   r   )r   Úfiler   r   r   Úcheck_against_file:   s    zHashes.check_against_filec          	   C   s    t |dƒ�}| j|ƒS Q R X d S )NÚrb)Úopenr#   )r   Úpathr"   r   r   r   Úcheck_against_pathB   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__F   s    zHashes.__nonzero__c             C   s   | j ƒ S )N)r)   )r   r   r   r   Ú__bool__J   s    zHashes.__bool__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r   r#   r'   r)   r*   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 t| ƒjtg id� dS )z!Don't offer the ``hashes`` kwarg.)r   N)Úsuperr/   r   ÚFAVORITE_HASH)r   )Ú	__class__r   r   r   U   s    zMissingHashes.__init__c             C   s   t |t jƒ ƒ‚d S )N)r   r1   r   )r   r   r   r   r   r   [   s    zMissingHashes._raise)r+   r,   r-   r.   r   r   Ú__classcell__r   r   )r2   r   r/   N   s   r/   )Ú
__future__r   r   Úpip.exceptionsr   r   r   Ú	pip.utilsr   Zpip._vendor.sixr   r   r	   r1   ÚSTRONG_HASHESÚobjectr   r/   r   r   r   r   Ú<module>   s   
: