U
    Ha                     @   s\   d Z ddlZddlZddlZddlmZ dd Zdd Zd	d
 Z	dd Z
dd Zdd ZdS )z2
Tools for converting old- to new-style metadata.
    N   )read_pkg_infoc                 C   sR   t | ddrd| j S g }| jD ]\}}|||  q |s@dS ddt| S )zFReturn the version specifier for a requirement in PEP 345/566 fashion.urlNz @  z (%s),)getattrr   specsappendjoinsorted)requirementZrequires_distopver r   2/tmp/pip-unpacked-wheel-67hhwib2/wheel/metadata.pyrequires_to_requires_dist   s    
r   c                 c   sN   | D ]D}t j|}t|}dt|j}|r8d| }|j| | V  qdS )z=Yield Requires-Dist: strings for parsed requirements strings.r   z[%s]N)pkg_resourcesRequirementparser   r
   r   extrasproject_name)requirementsreqZparsed_requirementspecr   r   r   r   convert_requirements   s    r   c                 c   s   |   D ]\}}d}|pd}d|kr4|dd\}}t|}|rhd|fV  |r\d| d }|d| 7 }|rtd| }t|D ]}d	|| fV  q|qd
S )a,  
    Convert requirements from a setup()-style dictionary to ('Requires-Dist', 'requirement')
    and ('Provides-Extra', 'extra') tuples.

    extras_require is a dictionary of {extra: [requirements]} as passed to setup(),
    using the empty extra {'': [requirements]} to hold install_requires.
    r   :r   Provides-Extra(z) and zextra == '%s'z ; Requires-DistN)itemssplitr   
safe_extrar   )Zextras_requireextradepends	conditionZnew_reqr   r   r   generate_requirements%   s    

r%   c              	   C   s   t |}|dd |d= |d= tj| d}tj|rt|}| }W 5 Q R X tt	
|dd d}|D ]6\}}t||iD ] \}	}
|	|
f| kr|
||	< qqp|d	 }|r|t| |d	= |S )
zN
    Convert .egg-info directory with PKG-INFO to the Metadata 2.1 format
    zMetadata-Versionz2.1r   r   zrequires.txtc                 S   s   | d p
dS )Nr   r   r   )xr   r   r   <lambda>P       z%pkginfo_to_metadata.<locals>.<lambda>)keyDescription)r   replace_headerospathr
   existsopenreadr   r   split_sectionsr%   r   set_payloaddedent_description)Zegg_info_pathZpkginfo_pathpkg_infoZrequires_pathZrequires_filerequiresZparsed_requirementsr"   reqsr)   valuedescriptionr   r   r   pkginfo_to_metadataA   s(    

r9   c                 C   sV   | | }|  }t|tsR|  D ].}|d   |kr"|d ddd} qRq"|S )z<Hack to coax Unicode out of an email Message() - Python 3.3+r   r   asciisurrogateescapezutf-8)lower
isinstancestr	raw_itemsencodedecode)r4   fieldtextitemr   r   r   pkginfo_unicode^   s    
rE   c              
   C   sr   | d }d}t |ts$d}t| d}| }d|d  td|dd df}|rn|d	d	d
}|S )z@
    Dedent and convert pkg_info['Description'] to Unicode.
    r*   FT
r   r   Nutf8r:   r;   )
r=   r>   rE   
splitlinesr
   lstriptextwrapdedentr@   rA   )r4   r8   Z
surrogatesZdescription_linesZdescription_dedentr   r   r   r3   l   s(    


 r3   )__doc__os.pathr,   rJ   r   Zpkginfor   r   r   r%   r9   rE   r3   r   r   r   r   <module>   s   