U
    Ȃe                     @   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rddlmZ ddlmZmZ ddl
mZ erd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-vuk8uv8f/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 )zExtract 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|}||std
||t	|S )zReturns 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   
%&