3
ûâde  ã               @   s0  d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
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$ZejeƒZG dd„ deƒZeeeedœdd„Zeeeeef dœdd„Zeeedœdd„Zeeedœdd„Zeeedœdd„Z eee!df dœdd „Z"ee!df edd!œd"d#„Z#dS )%z0Support functions for working with wheel files.
é    N)ÚMessage)ÚParser)ÚDictÚTuple)Ú
BadZipFileÚZipFile)Úcanonicalize_name)ÚDistInfoDistributionÚDistribution)ÚUnsupportedWheel)ÚDictMetadataé   c                   sF   e Zd ZdZeeef eddœ‡ fdd„Zeedœ‡ fdd„Z‡  Z	S )	ÚWheelMetadatazaMetadata provider that maps metadata decoding exceptions to our
    internal exception type.
    N)ÚmetadataÚ
wheel_nameÚreturnc                s   t ƒ j|ƒ || _d S )N)ÚsuperÚ__init__Ú_wheel_name)Úselfr   r   )Ú	__class__© ú8/tmp/pip-build-red6nood/pip/pip/_internal/utils/wheel.pyr      s    zWheelMetadata.__init__)Únamer   c                sJ   yt ƒ j|ƒS  tk
rD } ztd| j› d|› �ƒ‚W Y d d }~X nX d S )NzError decoding metadata for z: )r   Úget_metadataÚUnicodeDecodeErrorr   r   )r   r   Úe)r   r   r   r      s
    zWheelMetadata.get_metadata)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚstrÚbytesr   r   Ú__classcell__r   r   )r   r   r      s   r   )Ú	wheel_zipr   Úlocationr   c       
         s¨   t | |ƒ\‰ }‡ fdd„| jƒ D ƒ}i }xf|D ]^}|jddƒ\}}yt| |ƒ||< W q. tk
rŠ } ztdj|t|ƒƒƒ‚W Y dd}~X q.X q.W t||ƒ}	t||	|d�S )zaGet a pkg_resources distribution given a wheel.

    :raises UnsupportedWheel: on any errors
    c                s    g | ]}|j ˆ › d �ƒr|‘qS )ú/)Ú
startswith)Ú.0Úp)Úinfo_dirr   r   ú
<listcomp>2   s    z8pkg_resources_distribution_for_wheel.<locals>.<listcomp>r&   r   z{} has an invalid wheel, {}N)r%   r   Úproject_name)	Úparse_wheelÚnamelistÚsplitÚread_wheel_metadata_filer   Úformatr!   r   r	   )
r$   r   r%   Ú_Zmetadata_filesZmetadata_textÚpathÚmetadata_namer   r   r   )r*   r   Ú$pkg_resources_distribution_for_wheel)   s    
*
r5   )r$   r   r   c             C   sj   y t | |ƒ}t| |ƒ}t|ƒ}W n6 tk
rV } ztdj|t|ƒƒƒ‚W Y dd}~X 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   r1   r!   Úcheck_compatibility)r$   r   r*   r   Úversionr   r   r   r   r-   B   s    

&
r-   )Úsourcer   r   c             C   s‚   dd„ | j ƒ D ƒ}dd„ |D ƒ}|s,tdƒ‚t|ƒdkrLtdjdj|ƒƒƒ‚|d	 }t|ƒ}t|ƒ}|j|ƒs~td
j||ƒƒ‚|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   h | ]}|j d dƒd ’qS )r&   r   r   )r/   )r(   r)   r   r   r   ú	<setcomp>[   s    z&wheel_dist_info_dir.<locals>.<setcomp>c             S   s   g | ]}|j 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})r.   r   Úlenr1   Újoinr   r'   )r;   r   ÚsubdirsZ	info_dirsr*   Zinfo_dir_nameÚcanonical_namer   r   r   r6   T   s    

r6   )r;   r3   r   c             C   sL   y
| j |ƒS  tttfk
rF } ztd|›d|›�ƒ‚W Y d d }~X nX d S )Nzcould not read z file: )Úreadr   ÚKeyErrorÚRuntimeErrorr   )r;   r3   r   r   r   r   r0   u   s    
r0   )r;   Údist_info_dirr   c             C   sd   |› d�}t | |ƒ}y|jƒ }W n6 tk
rV } ztd|›d|›�ƒ‚W Y dd}~X nX tƒ j|ƒS )ziReturn the WHEEL metadata of an extracted wheel, if possible.
    Otherwise, raise UnsupportedWheel.
    z/WHEELzerror decoding z: N)r0   Údecoder   r   r   Úparsestr)r;   rF   r3   Zwheel_contentsZ
wheel_textr   r   r   r   r7   ~   s    

&r7   .)Ú
wheel_datar   c             C   s\   | d }|dkrt dƒ‚|jƒ }yttt|j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   ÚstripÚtupleÚmapÚintr/   Ú
ValueError)rI   Zversion_textr:   r   r   r   r8   ‘   s    r8   )r:   r   r   c             C   sR   | d t d kr.tdj|djtt| ƒƒƒƒ‚n | t krNtjddj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 piprJ   z*Installing from a newer Wheel-Version (%s)N)ÚVERSION_COMPATIBLEr   r1   r@   rM   r!   ÚloggerÚwarning)r:   r   r   r   r   r9   ¡   s    r9   )r   r   )$r    ÚloggingÚemail.messager   Úemail.parserr   Útypingr   r   Úzipfiler   r   Úpip._vendor.packaging.utilsr   Zpip._vendor.pkg_resourcesr	   r
   Zpip._internal.exceptionsr   Z!pip._internal.utils.pkg_resourcesr   rP   Ú	getLoggerr   rQ   r   r!   r5   r-   r6   r"   r0   r7   rN   r8   r9   r   r   r   r   Ú<module>   s(   
!	