B
    ülc  ã               @  sž   d Z ddlmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZmZmZ dd	d
œdd„Zdddœdd„Zdddœdd„Zd	d	ddœdd„ZdS )z2
Tools for converting old- to new-style metadata.
é    )ÚannotationsN)ÚMessage)ÚParser)ÚIterator)ÚRequirementÚ
safe_extraÚsplit_sectionsr   Ústr)ÚrequirementÚreturnc             C  s^   t | ddƒrd| j S g }x | jD ]\}}| || ¡ q"W |rVdd t|ƒ¡ d S dS dS )zFReturn the version specifier for a requirement in PEP 345/566 fashion.ÚurlNz @ z (ú,ú)Ú )Úgetattrr   ZspecsÚappendÚjoinÚsorted)r
   Zrequires_distÚopZver© r   ú-lib/python3.7/site-packages/wheel/metadata.pyÚrequires_to_requires_dist   s    
r   z	list[str]zIterator[str])Úrequirementsr   c             c  sT   xN| D ]F}t  |¡}t|ƒ}d t|jƒ¡}|r<d|› d}|j| | V  qW dS )z=Yield Requires-Dist: strings for parsed requirements strings.r   ú[ú]N)r   Úparser   r   r   ÚextrasZproject_name)r   ZreqZparsed_requirementÚspecr   r   r   r   Úconvert_requirements   s    

r   zdict[str, list[str]]zIterator[tuple[str, str]])Úextras_requirer   c             c  sœ   x–|   ¡ D ]Š\}}d}|pd}d|kr6| dd¡\}}t|ƒ}|rhd|fV  |r\d| d }|d| 7 }|rtd| }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   ú:é   zProvides-Extraú(z) and zextra == '%s'z ; zRequires-DistN)ÚitemsÚsplitr   r   )r   ÚextraZdependsZ	conditionZnew_reqr   r   r   Úgenerate_requirements*   s    

r&   r   )Úegg_info_pathÚpkginfo_pathr   c          
   C  s$  t |dd}tƒ  |¡}W dQ R X | 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 qŒW |d }|r |d  ¡ }d |d  ¡ t d |dd… ¡¡df¡}| |¡ |d= |S )zN
    Convert .egg-info directory with PKG-INFO to the Metadata 2.1 format
    zutf-8)ÚencodingNzMetadata-Versionz2.1zProvides-ExtrazRequires-Distzrequires.txtc             S  s   | d p
dS )Nr   r   r   )Úxr   r   r   Ú<lambda>X   ó    z%pkginfo_to_metadata.<locals>.<lambda>)ÚkeyZDescriptionÚ
r   r!   )Úopenr   r   Zreplace_headerÚosÚpathr   ÚexistsÚreadr   r   r&   r#   Ú
splitlinesÚlstripÚtextwrapÚdedentZset_payload)r'   r(   ZheadersZpkg_infoZrequires_pathZrequires_fileZrequiresZparsed_requirementsr%   Zreqsr-   ÚvalueÚdescriptionZdescription_linesZdedented_descriptionr   r   r   Úpkginfo_to_metadataH   s0    


r:   )Ú__doc__Z
__future__r   Zos.pathr0   r6   Zemail.messager   Zemail.parserr   Útypingr   Zpkg_resourcesr   r   r   r   r   r&   r:   r   r   r   r   Ú<module>   s   