B
    pQaø  ã               @   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   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   ÚspecsÚappendÚjoinÚsorted)ÚrequirementZrequires_distÚopÚver© r   ú1/tmp/pip-install-s1dd8p2m/wheel/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_resourcesÚRequirementÚparser   r
   r   ÚextrasÚproject_name)ÚrequirementsÚ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   Ú
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 |¡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>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)Úegg_info_pathZpkginfo_pathÚpkg_infoZrequires_pathZrequires_fileÚrequiresZparsed_requirementsr    Úreqsr'   ÚvalueÚdescriptionr   r   r   Úpkginfo_to_metadataA   s&    

r8   c             C   sX   | | }|  ¡ }t|tƒsTx8|  ¡ D ],}|d   ¡ |kr$|d  dd¡ d¡}P q$W |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)r3   ÚfieldÚtextÚitemr   r   r   Úpkginfo_unicode^   s    
rD   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Úutf8r9   r:   )
r<   r=   rD   Ú
splitlinesr
   ÚlstripÚtextwrapÚdedentr?   r@   )r3   r7   Z
surrogatesZdescription_linesZdescription_dedentr   r   r   r1   l   s    


r1   )Ú__doc__Úos.pathr*   rI   r   Zpkginfor   r   r   r#   r8   rD   r1   r   r   r   r   Ú<module>   s   