
0c@_                 @   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 j e  Z  Gd 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   c                   s:   e  Z d  Z d Z   f d d   Z   f d d   Z   S)WheelMetadatazaMetadata provider that maps metadata decoding exceptions to our
    internal exception type.
    c                s#   t  t |   j |  | |  _ d  S)N)superr   __init___wheel_name)selfmetadata
wheel_name)	__class__ 8/tmp/pip-build-jynh7p1z/pip/pip/_internal/utils/wheel.pyr   (   s    zWheelMetadata.__init__c                sa   y t  t |   j |  SWn@ t k
 r\ } z  t d j |  j |    WYd  d  } ~ Xn Xd  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   r   r   )r   r   r   $   s   r   c                s   t  |  |  \   }   f d d   |  j   D } i  } x | D] } t |  } | j d d  \ } } y t |  |  | | <WqA t k
 r }	 z# t d j | t |	     WYd d }	 ~	 XqA XqA Wt | |  }
 t	 d | d |
 d	 |  S)
zaGet a pkg_resources distribution given a wheel.

    :raises UnsupportedWheel: on any errors
    c                s.   g  |  ]$ } | j  d  j     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,   _Zmetadata_filesZmetadata_textpath	full_pathmetadata_namer!   r   r   )r)   r   $pkg_resources_distribution_for_wheel:   s    2r8   c             C   s   y. t  |  |  } t |  |  } t |  } WnC t k
 rs } z# t d j | t |     WYd d } ~ Xn Xt | |  | | 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.
    z{} has an invalid wheel, {}N)wheel_dist_info_dirwheel_metadatawheel_versionr	   r   r2   check_compatibility)r3   r    r)   r   versionr!   r   r   r   r.   _   s    .r.   c             C   s   t  d d   |  j   D  } d d   | D } | sD t d   t |  d k rt t d j d j |     | d	 } t |  } t |  } | j |  s t d
 j | |    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%   |  ] } | j  d  d  d Vq d S)r+   r   r   N)r0   )r'   r(   r   r   r   	<genexpr>|   s    z&wheel_dist_info_dir.<locals>.<genexpr>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})	setr/   r	   lenr   joinr   r&   r   )sourcer    subdirsZ	info_dirsr)   Zinfo_dir_namecanonical_namer   r   r   r9   t   s     
r9   c             C   s^   y |  j  |  SWnF t t t f k
 rY } z t d j | |    WYd  d  } ~ Xn Xd  S)Nzcould not read {!r} file: {!r})readr   KeyErrorRuntimeErrorr	   r   )rD   r5   r!   r   r   r   r1      s
    r1   c             C   s~   d j  |  } t |  |  } y t |  } Wn= t k
 rm } z t d j  | |    WYd d } ~ Xn Xt   j |  S)ziReturn the WHEEL metadata of an extracted wheel, if possible.
    Otherwise, raise UnsupportedWheel.
    z{}/WHEELzerror decoding {!r}: {!r}N)r   r1   r   r   r	   r   parsestr)rD   Zdist_info_dirr5   Zwheel_contentsZ
wheel_textr!   r   r   r   r:      s    +r:   c             C   s|   |  d } | d k r" t  d   | j   } y  t t t | j d    SWn' t k
 rw t  d j |    Yn Xd 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intr0   
ValueErrorr   )
wheel_dataZversion_textr=   r   r   r   r;      s    
 r;   c             C   ss   |  d t  d k rA t d j | d j t t |       n. |  t  k ro t 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 piprK   z*Installing from a newer Wheel-Version (%s)N)VERSION_COMPATIBLEr	   r   rC   rN   r2   loggerwarning)r=   r    r   r   r   r<      s    $r<   )r   r   ))r%   
__future__r   loggingemail.parserr   zipfiler   pip._vendor.packaging.utilsr   Zpip._vendor.pkg_resourcesr   Zpip._vendor.sixr   r   Zpip._internal.exceptionsr	   Z!pip._internal.utils.pkg_resourcesr
   Zpip._internal.utils.typingr   email.messager   Ztypingr   r   r   r   r   rR   	getLoggerr"   rS   r   r8   r.   r9   r1   r:   r;   r<   r   r   r   r   <module>   s6   %&