U
    @v¨g†  ã                   @   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-gw11q0wt/pip/_internal/utils/wheel.pyr   (   s    zWheelMetadata.__init__c              
      sL   zt t| ƒ |¡W S  tk
rF } ztd | j|¡ƒ‚W 5 d }~X Y nX d S )Nz"Error decoding metadata for {}: {})r   r   Úget_metadataÚUnicodeDecodeErrorr	   Úformatr   )r   ÚnameÚer   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 }|D ]f}t|ƒ}| dd¡\}}zt| |ƒ||< W q, tk
r }	 ztd |t|	ƒ¡ƒ‚W 5 d}	~	X Y q,X q,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>B   s     z8pkg_resources_distribution_for_wheel.<locals>.<listcomp>ú/r   ú{} has an invalid wheel, {}N)Úlocationr   Úproject_name)
Úparse_wheelÚnamelistr   ÚsplitÚread_wheel_metadata_filer	   r    Ústrr   r   )Ú	wheel_zipr!   r1   Ú_Úmetadata_filesÚmetadata_textÚpathÚ	full_pathÚmetadata_namer"   r   r   r,   r   Ú$pkg_resources_distribution_for_wheel:   s.    
ÿ ÿÿ
  ÿr?   c              
   C   sj   z t | |ƒ}t| |ƒ}t|ƒ}W n6 tk
rV } ztd |t|ƒ¡ƒ‚W 5 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.
    r0   N)Úwheel_dist_info_dirÚwheel_metadataÚwheel_versionr	   r    r7   Úcheck_compatibility)r8   r!   r-   r   Úversionr"   r   r   r   r3   _   s    

ÿ
r3   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)r5   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Úsetr4   r	   Úlenr    Újoinr   r(   r   )Úsourcer!   ÚsubdirsÚ	info_dirsr-   Úinfo_dir_nameÚcanonical_namer   r   r   r@   t   s*    ÿÿ
 ÿÿr@   c              
   C   sJ   z|   |¡W S  tttfk
rD } ztd ||¡ƒ‚W 5 d }~X Y nX d S )Nzcould not read {!r} file: {!r})Úreadr   ÚKeyErrorÚRuntimeErrorr	   r    )rL   r<   r"   r   r   r   r6   š   s    
ÿr6   c              
   C   s`   d  |¡}t| |ƒ}zt|ƒ}W n2 tk
rR } ztd  ||¡ƒ‚W 5 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    r6   r   r   r	   r   Úparsestr)rL   Údist_info_dirr<   Úwheel_contentsÚ
wheel_textr"   r   r   r   rA   ¦   s    

"rA   c                 C   s^   | d }|dkrt dƒ‚| ¡ }zttt| d¡ƒƒW S  tk
rX   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Úintr5   Ú
ValueErrorr    )Ú
wheel_dataÚversion_textrD   r   r   r   rB   º   s    rB   c                 C   sR   | d t d kr.td|d tt| ƒ¡f ƒ‚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's Wheel-Version (%s) is not compatible with this version of piprX   z*Installing from a newer Wheel-Version (%s)N)ÚVERSION_COMPATIBLEr	   rK   r[   r7   ÚloggerÚwarning)rD   r!   r   r   r   rC   Ë   s    ÿÿþrC   ))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#   ra   r   r?   r3   r@   r6   rA   rB   rC   r   r   r   r   Ú<module>   s6   
%&