3
ÕZð][  ã               @   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j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 ]\}}|j|| ƒ q"W |sDdS ddj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   ú//tmp/pip-build-uspjrw0y/wheel/wheel/metadata.pyÚrequires_to_requires_dist   s    
r   c             c   sR   xL| D ]D}t jj|ƒ}t|ƒ}dj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˜| j ƒ D ]Œ\}}d}|pd}d|kr6|jddƒ\}}tj|ƒ}|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 |ƒ}|jddƒ |d= |d= tjj| dƒ}tjj|ƒr°t|ƒ�}|jƒ }W dQ R X tt	j
|ƒdd„ d	�}xB|D ]:\}}x0t||iƒD ] \}	}
|	|
f|jƒ krˆ|
||	< qˆW qrW |d
 }|rÐ|j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   s    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_metadataG   s&    

r7   c             C   sj   | | }|j ƒ }t|tƒsft| dƒs,t|ƒS x8| jƒ D ],}|d j ƒ |kr6|d jddƒj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Úhasattrr8   ÚencodeÚdecode)r2   ÚfieldÚtextÚitemr   r   r   Úpkginfo_unicoded   s    

rD   c             C   sr   | d }d}t |tƒs$d}t| dƒ}|jƒ }dj|d jƒ tjdj|dd… ƒƒdfƒ}|rn|jdƒj	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@   )r2   r6   Z
surrogatesZdescription_linesZdescription_dedentr   r   r   r0   t   s    


r0   )Ú__doc__Úos.pathr)   ÚrerI   r   Zpkginfor   ÚcompileZEXTRA_REr   r   r#   r7   rD   r0   r   r   r   r   Ú<module>   s   