B
    Kkd                 @   s   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 dlmZ erd dlmZmZmZmZmZ d dlmZ erd d	lmZ n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)MYPY_CHECK_RUNNING)DictListBinaryIONoReturnIterator)PY3)_Hash)_hash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-gxxfd9b7/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#   G   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_fileK   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_pathT   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__Y   s    zHashes.__nonzero__c             C   s   |   S )N)r2   )r   r   r   r   __bool__^   s    zHashes.__bool__)N)__name__
__module____qualname____doc__r   r*   r#   r,   r0   r2   r3   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)superr8   r   FAVORITE_HASH)r   )	__class__r   r   r   j   s    zMissingHashes.__init__c             C   s   t |t  d S )N)r   r:   r"   )r   r%   r   r   r   r#   q   s    zMissingHashes._raise)r4   r5   r6   r7   r   r#   __classcell__r   r   )r;   r   r8   c   s   r8   )
__future__r   r   pip._vendor.sixr   r   r   pip._internal.exceptionsr   r   r   pip._internal.utils.miscr	   pip._internal.utils.typingr
   typingr   r   r   r   r   r   r   r   r:   STRONG_HASHESobjectr   r8   r   r   r   r   <module>   s   
A