U
    d                     @  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  sZ   t | ddrd| j S g }| jD ]\}}|||  q |rRddt| d S dS dS )zFReturn the version specifier for a requirement in PEP 345/566 fashion.urlNz @ z (,) )getattrr   specsappendjoinsorted)r
   Zrequires_distopver r   2/tmp/pip-unpacked-wheel-v7gjrrul/wheel/metadata.pyrequires_to_requires_dist   s    
r   z	list[str]zIterator[str])requirementsr   c                 c  sP   | D ]F}t |}t|}dt|j}|r:d| d}|j| | V  qdS )z=Yield Requires-Dist: strings for parsed requirements strings.r   []N)r   parser   r   r   extrasproject_name)r   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   |   D ]\}}d}|pd}d|kr4|dd\}}t|}|rfd|fV  |rZd| d }|d| 7 }|rrd| }t|D ]}d	|| fV  qzq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   :   Provides-Extra(z) and zextra == '%s'z ; Requires-DistN)itemssplitr   r"   )r#   extradepends	conditionZnew_reqr   r   r   generate_requirements*   s    

r.   r   )egg_info_pathpkginfo_pathr   c              
   C  s  t |dd}t |}W 5 Q R X |dd |d= |d= tj| d}tj|rt |}| }W 5 Q R X t	t
|dd	 d
}|D ]6\}}	t||	iD ] \}
}|
|f| kr|||
< qq|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)encodingzMetadata-Versionz2.1r&   r(   zrequires.txtc                 S  s   | d p
dS )Nr   r   r   )xr   r   r   <lambda>X       z%pkginfo_to_metadata.<locals>.<lambda>)keyDescription
r   r%   N)openr   r   replace_headerospathr   existsreadr   r   r.   r)   
splitlineslstriptextwrapdedentset_payload)r/   r0   headerspkg_infoZrequires_pathZrequires_filerequiresZparsed_requirementsr+   reqsr5   valuedescriptionZdescription_linesZdedented_descriptionr   r   r   pkginfo_to_metadataH   s4    

	
rI   )__doc__
__future__r   os.pathr:   r@   email.messager   email.parserr   typingr   pkg_resourcesr   r   r   r   r"   r.   rI   r   r   r   r   <module>   s   