U
    
Ha                     @   s   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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.
    N)Message)Parser)DictTuple)
BadZipFileZipFile)canonicalize_name)DistInfoDistributionDistribution)UnsupportedWheel)DictMetadata)   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  | || _d S )N)super__init___wheel_name)selfmetadata
wheel_name	__class__ =/tmp/pip-unpacked-wheel-tx790h60/pip/_internal/utils/wheel.pyr      s    zWheelMetadata.__init__c              
      sL   zt  |W S  tk
rF } ztd| j d| W 5 d }~X Y nX d S )NzError decoding metadata for : )r   get_metadataUnicodeDecodeErrorr   r   )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 ]^}|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 )/)
startswith.0pinfo_dirr   r   
<listcomp>3   s      z8pkg_resources_distribution_for_wheel.<locals>.<listcomp>r#   r   {} has an invalid wheel, {}N)locationr   project_name)	parse_wheelnamelistsplitread_wheel_metadata_filer   formatstrr   r	   )
	wheel_zipr   r,   _metadata_filesmetadata_text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.
    r+   N)wheel_dist_info_dirwheel_metadatawheel_versionr   r2   r3   check_compatibility)r4   r   r)   r   versionr   r   r   r   r.   C   s    

&
r.   c                 C   s   dd |   D }dd |D }|s,tdt|dkrLtdd||d	 }t|}t|}||s~td
|||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   h | ]}| d dd qS )r#   r   r   )r0   r%   r   r   r   	<setcomp>^   s     z&wheel_dist_info_dir.<locals>.<setcomp>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})r/   r   lenr2   joinr   r$   )sourcer   subdirs	info_dirsr)   info_dir_namecanonical_namer   r   r   r;   V   s&    
 r;   c              
   C   sN   z|  |W S  tttfk
rH } ztd|d|W 5 d }~X Y nX d S )Nzcould not read z file: )readr   KeyErrorRuntimeErrorr   )rE   r8   r   r   r   r   r1   x   s    r1   c              
   C   sd   | d}t | |}z| }W n6 tk
rV } ztd|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   N)r1   decoder   r   r   parsestr)rE   dist_info_dirr8   wheel_contents
wheel_textr   r   r   r   r<      s    

&r<   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   striptuplemapintr0   
ValueError)
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 piprR   z*Installing from a newer Wheel-Version (%s)N)VERSION_COMPATIBLEr   r2   rD   rU   r3   loggerwarning)r?   r   r   r   r   r>      s     r>   )!r!   loggingemail.messager   email.parserr   typingr   r   zipfiler   r   pip._vendor.packaging.utilsr   Zpip._vendor.pkg_resourcesr	   r
   pip._internal.exceptionsr   !pip._internal.utils.pkg_resourcesr   rZ   	getLoggerr   r[   r   r:   r.   r;   r1   r<   r=   r>   r   r   r   r   <module>   s&   
"
