U
    æÀ-^¥  ã                   @   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   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   ZspecsÚappendÚjoinÚsorted)ZrequirementZrequires_distÚopZver© r   ú0/usr/lib/python3/dist-packages/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_resourcesZRequirementÚparser   r	   r
   ÚextrasZproject_name)ZrequirementsZ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   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 5 Q R X tt	 
|¡dd„ d}|D ]6\}}t||iƒD ] \}	}
|	|
f| ¡ kr„|
||	< q„qp|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>U   ó    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_metadataF   s(    

ÿr*   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ÚstrZ	raw_itemsÚencodeÚdecode)r'   ZfieldÚtextÚitemr   r   r   Úpkginfo_unicodec   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&   q   s(    


þýÿ þr&   )Ú__doc__Úos.pathr!   Úrer9   r   Zpkginfor   ÚcompileZEXTRA_REr   r   r   r*   r4   r&   r   r   r   r   Ú<module>   s   ÿ