B
    `’»dŠ  ã               @   s   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ er¨ddlmZ ddlmZmZ ddl
mZ erºddlmZ nddlmZ dZe e¡Z G dd„ deƒZ!dd„ Z"dd„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'd d!„ Z(dS )"z0Support functions for working with wheel files.
é    )Úabsolute_importN)ÚParser)ÚZipFile)Úcanonicalize_name)ÚDistInfoDistribution)ÚPY2Ú
ensure_str)ÚUnsupportedWheel)ÚDictMetadata)ÚMYPY_CHECK_RUNNING)ÚMessage)ÚDictÚTuple)ÚDistribution)Ú
BadZipfile)Ú
BadZipFile)é   r   c                   s,   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡  ZS )ÚWheelMetadatazaMetadata provider that maps metadata decoding exceptions to our
    internal exception type.
    c                s   t t| ƒ |¡ || _d S )N)Úsuperr   Ú__init__Ú_wheel_name)ÚselfÚmetadataÚ
wheel_name)Ú	__class__© ú=/tmp/pip-unpacked-wheel-hu9qqap1/pip/_internal/utils/wheel.pyr   (   s    zWheelMetadata.__init__c          
      sJ   yt t| ƒ |¡S  tk
rD } ztd | j|¡ƒ‚W d d }~X Y nX d S )Nz"Error decoding metadata for {}: {})r   r   Úget_metadataÚUnicodeDecodeErrorr	   Úformatr   )r   ÚnameÚe)r   r   r   r   -   s    zWheelMetadata.get_metadata)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__classcell__r   r   )r   r   r   $   s   r   c                s°   t | |ƒ\‰ }‡ fdd„|  ¡ D ƒ}i }xn|D ]f}t|ƒ}| dd¡\}}yt| |ƒ||< W q. tk
r’ }	 ztd |t|	ƒ¡ƒ‚W dd}	~	X Y q.X q.W t||ƒ}
t	||
|dS )zaGet a pkg_resources distribution given a wheel.

    :raises UnsupportedWheel: on any errors
    c                s    g | ]}|  d  ˆ ¡¡r|‘qS )z{}/)Ú
startswithr   )Ú.0Úp)Úinfo_dirr   r   ú
<listcomp>C   s    z8pkg_resources_distribution_for_wheel.<locals>.<listcomp>ú/r   z{} has an invalid wheel, {}N)Úlocationr   Úproject_name)
Úparse_wheelÚnamelistr   ÚsplitÚread_wheel_metadata_filer	   r   Ústrr   r   )Ú	wheel_zipr    r-   Ú_Úmetadata_filesÚmetadata_textÚpathÚ	full_pathÚmetadata_namer!   r   r   )r*   r   Ú$pkg_resources_distribution_for_wheel:   s    
(
r;   c          
   C   sj   y t | |ƒ}t| |ƒ}t|ƒ}W n6 tk
rV } ztd |t|ƒ¡ƒ‚W dd}~X Y nX t||ƒ ||fS )z©Extract information from the provided wheel, ensuring it meets basic
    standards.

    Returns the name of the .dist-info directory and the parsed WHEEL metadata.
    z{} has an invalid wheel, {}N)Úwheel_dist_info_dirÚwheel_metadataÚwheel_versionr	   r   r3   Úcheck_compatibility)r4   r    r*   r   Úversionr!   r   r   r   r/   _   s    

$
r/   c             C   sŽ   t tdd„ |  ¡ D ƒƒƒ}dd„ |D ƒ}|s4tdƒ‚t|ƒdkrTtd d |¡¡ƒ‚|d	 }t|ƒ}t|ƒ}| |¡s†td
 ||¡ƒ‚t	|ƒS )z®Returns the name of the contained .dist-info directory.

    Raises AssertionError or UnsupportedWheel if not found, >1 found, or
    it doesn't match the provided name.
    c             s   s   | ]}|  d ¡d V  qdS )r,   r   N)r1   )r(   r)   r   r   r   ú	<genexpr>|   s    z&wheel_dist_info_dir.<locals>.<genexpr>c             S   s   g | ]}|  d ¡r|‘qS )z
.dist-info)Úendswith)r(   Úsr   r   r   r+   ~   s    z'wheel_dist_info_dir.<locals>.<listcomp>z.dist-info directory not foundr   z)multiple .dist-info directories found: {}z, r   z2.dist-info directory {!r} does not start with {!r})
ÚlistÚsetr0   r	   Úlenr   Újoinr   r'   r   )Úsourcer    ÚsubdirsÚ	info_dirsr*   Úinfo_dir_nameÚcanonical_namer   r   r   r<   t   s     

r<   c          
   C   sH   y
|   |¡S  tttfk
rB } ztd ||¡ƒ‚W d d }~X Y nX d S )Nzcould not read {!r} file: {!r})Úreadr   ÚKeyErrorÚRuntimeErrorr	   r   )rH   r8   r!   r   r   r   r2   š   s
    
r2   c          
   C   s`   d  |¡}t| |ƒ}yt|ƒ}W n2 tk
rR } ztd  ||¡ƒ‚W dd}~X Y nX tƒ  |¡S )ziReturn the WHEEL metadata of an extracted wheel, if possible.
    Otherwise, raise UnsupportedWheel.
    z{}/WHEELzerror decoding {!r}: {!r}N)r   r2   r   r   r	   r   Úparsestr)rH   Údist_info_dirr8   Úwheel_contentsÚ
wheel_textr!   r   r   r   r=   ¦   s    

"r=   c             C   s\   | d }|dkrt dƒ‚| ¡ }yttt| d¡ƒƒS  tk
rV   t d |¡ƒ‚Y nX dS )zbGiven WHEEL metadata, return the parsed Wheel-Version.
    Otherwise, raise UnsupportedWheel.
    zWheel-VersionNzWHEEL is missing Wheel-VersionÚ.zinvalid Wheel-Version: {!r})r	   ÚstripÚtupleÚmapÚintr1   Ú
ValueErrorr   )Ú
wheel_dataÚversion_textr@   r   r   r   r>   º   s    r>   c          	   C   sR   | d t d kr.td |d tt| ƒ¡¡ƒ‚n | t krNt dd tt| ƒ¡¡ dS )aå  Raises errors or warns if called with an incompatible Wheel-Version.

    pip should refuse to install a Wheel-Version that's a major series
    ahead of what it's compatible with (e.g 2.0 > 1.1); and warn when
    installing a version only minor version ahead (e.g 1.2 > 1.1).

    version: a 2-tuple representing a Wheel-Version (Major, Minor)
    name: name of wheel or package to raise exception about

    :raises UnsupportedWheel: when an incompatible Wheel-Version is given
    r   zB{}'s Wheel-Version ({}) is not compatible with this version of piprT   z*Installing from a newer Wheel-Version (%s)N)ÚVERSION_COMPATIBLEr	   r   rG   rW   r3   ÚloggerÚwarning)r@   r    r   r   r   r?   Ë   s    r?   ))r%   Ú
__future__r   ÚloggingÚemail.parserr   Úzipfiler   Úpip._vendor.packaging.utilsr   Zpip._vendor.pkg_resourcesr   Zpip._vendor.sixr   r   Úpip._internal.exceptionsr	   Ú!pip._internal.utils.pkg_resourcesr
   Úpip._internal.utils.typingr   Úemail.messager   Útypingr   r   r   r   r   r\   Ú	getLoggerr"   r]   r   r;   r/   r<   r2   r=   r>   r?   r   r   r   r   Ú<module>   s6   
%&