B
    UvgT                 @   sz   d dl mZ d dlZd dlmZmZmZ d dlmZm	Z	m
Z
 d dl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)	iteritemsiterkeys
itervalues)HashMismatchHashMissingInstallationError)read_chunks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-install-r_9ig3yj/pip/pip/_internal/utils/hashes.py__init__   s    zHashes.__init__c          
   C   s   i }xJt | jD ]<}yt|||< W q ttfk
rJ   td| Y qX qW x(|D ] }xt|D ]}|| qdW qVW x*t	|D ]\}}|
 | j| krdS qW | | dS )zCheck 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   9   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_file<   s    zHashes.check_against_filec          	   C   s    t |d}| |S Q R X d S )Nrb)openr#   )r   pathr"   r   r   r   check_against_pathD   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__H   s    zHashes.__nonzero__c             C   s   |   S )N)r)   )r   r   r   r   __bool__L   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 )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 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   W   s    zMissingHashes.__init__c             C   s   t |t  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/   P   s   r/   )
__future__r   r   Zpip._vendor.sixr   r   r   pip._internal.exceptionsr   r   r   pip._internal.utils.miscr	   r1   STRONG_HASHESobjectr   r/   r   r   r   r   <module>   s   
: