B
    ˜#Y][  ã               @   sn   d Z ddlZddlZddlZddlZddlmZ e d¡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_infozF^(?P<package>.*?)(;\s*(?P<condition>.*?)(extra == '(?P<extra>.*?)')?)$c             C   sV   t | ddƒrd| j S g }x | jD ]\}}| || ¡ q"W |sDdS dd t|ƒ¡ S )zFReturn the version specifier for a requirement in PEP 345/566 fashion.ÚurlNz @ Ú z (%s)ú,)Úgetattrr   ZspecsÚappendÚjoinÚsorted)ZrequirementZrequires_distÚopZver© r   ú-lib/python3.7/site-packages/wheel/metadata.pyÚrequires_to_requires_dist   s    
r   c             c   sR   xL| D ]D}t j |¡}t|ƒ}d t|jƒ¡}|r:d| }|j| | V  qW dS )z=Yield Requires-Dist: strings for parsed requirements strings.r   z[%s]N)Úpkg_resourcesZRequirementÚparser   r	   r
   ÚextrasZproject_name)ZrequirementsZreqZparsed_requirementÚspecr   r   r   r   Úconvert_requirements    s    
r   c             c   sž   x˜|   ¡ D ]Œ\}}d}|pd}d|kr6| dd¡\}}t |¡}|rjd|fV  |r^d| d }|d| 7 }|rvd| }xt|ƒD ]}d	|| fV  q€W q
W 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   zProvides-Extraú(z) and zextra == '%s'z ; zRequires-DistN)ÚitemsÚsplitr   Z
safe_extrar   )Zextras_requireÚextraZdependsZ	conditionZnew_reqr   r   r   Úgenerate_requirements+   s    

r   c          	   C   sÔ   t |ƒ}| dd¡ |d= |d= tj | d¡}tj |¡r°t|ƒ}| ¡ }W dQ R X tt	 
|¡dd„ d	}xB|D ]:\}}x0t||iƒD ] \}	}
|	|
f| ¡ krˆ|
||	< qˆW qrW |d
 }|rÐ| t|ƒ¡ |d
= |S )zN
    Convert .egg-info directory with PKG-INFO to the Metadata 2.1 format
    zMetadata-Versionz2.1zProvides-ExtrazRequires-Distzrequires.txtNc             S   s   | d p
dS )Nr   r   r   )Úxr   r   r   Ú<lambda>V   ó    z%pkginfo_to_metadata.<locals>.<lambda>)ÚkeyÚDescription)r   Zreplace_headerÚosÚpathr	   ÚexistsÚopenÚreadr
   r   Zsplit_sectionsr   r   Zset_payloadÚdedent_description)Zegg_info_pathZpkginfo_pathÚpkg_infoZrequires_pathZrequires_fileZrequiresZparsed_requirementsr   Zreqsr   ÚvalueÚdescriptionr   r   r   Úpkginfo_to_metadataG   s&    

r(   c             C   sj   | | }|  ¡ }t|tƒsft| dƒs,t|ƒS x8|  ¡ D ],}|d   ¡ |kr6|d  dd¡ d¡}P q6W |S )z<Hack to coax Unicode out of an email Message() - Python 3.3+Ú	raw_itemsr   r   ÚasciiÚsurrogateescapezutf-8)ÚlowerÚ
isinstanceÚstrÚhasattrr)   ÚencodeÚdecode)r%   ZfieldÚtextÚitemr   r   r   Úpkginfo_unicoded   s    

r4   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.   r4   Ú
splitlinesr	   ÚlstripÚtextwrapÚdedentr0   r1   )r%   r'   Z
surrogatesZdescription_linesZdescription_dedentr   r   r   r$   t   s    


r$   )Ú__doc__Úos.pathr   Úrer9   r   Zpkginfor   ÚcompileZEXTRA_REr   r   r   r(   r4   r$   r   r   r   r   Ú<module>   s   