
!dV(                 @   s  d  Z  d d l m Z m Z d d l m Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l Z d Z i d d 6d	 d
 6d d 6Z e   Z i d d 6d d 6d f i d d 6d d 6d f f Z e d,  Z e j d  Z e j d  Z e d d-  Z d d   Z d d   Z d d d   Z d! d"   Z d# d$   Z d% d&   Z d' d(   Z d) d*   Z e  d+ k rd d l! Z! d d l" Z" e" j" e e! j# d   n  d S).z2
Tools for converting old- to new-style metadata.
    )defaultdict
namedtuple   )read_pkg_infoNz2.0classifiers
classifierprovidesZprovides_distextrasprovides_extraauthor_emailemailauthornamemaintainer_email
maintainerplatform	home_pagelicensezF^(?P<package>.*?)(;\s*(?P<condition>.*?)(extra == '(?P<extra>.*?)')?)$z[ -,]+MayRequiresKey	conditionextrac             c   s?   t    } x/ |  D]' } | | k r | j |  | Vq q Wd S)z<
    Yield unique values in iterable, preserving order.
    N)setadd)iterableseenvalue r   //tmp/pip-build-0jahl3lb/wheel/wheel/metadata.pyunique'   s
    	r   c             C   s}  t  t  } x | j |  D] } t j |  } | r | j   } | d } | d } | d }	 | j d  r | d d  } q n d \ } } | }	 t | |  } | | j |	  q W| ryg  |  d <xj | j	   D]\ \ } } i | d 6}
 | j
 r| j
 |
 d <n  | j r$| j |
 d	 <n  |  d j |
  q Wd
 |  k rRg  |  d
 <n  |  d
 j d d   | j   D  n  d S)zE
    Place the runtime requirements from pkg_info into metadata.
    r   r   packagez and N   Zrun_requiresrequiresenvironmentr	   c             S   s"   g  |  ] } | j  r | j   q Sr   )r   ).0keyr   r   r   
<listcomp>R   s   	 z#handle_requires.<locals>.<listcomp>)NN)r   listget_allEXTRA_REsearch	groupdictendswithr   appenditemsr   r   extendkeys)metadatapkg_infor$   Zmay_requiresr   Zextra_matchr+   r   r   r   Zmay_requirementr   r   r   handle_requires2   s4    



		r3   c             C   s  t  d d    } d t j d | d <y t t |   } WnB t k
 r t j j   j	 t
 |  d  j   j d   } Yn Xd } | d	 r t | d	  | d
 <| d	 =n  | d r t |  } | d =n< | j   } t | t  r | j d  } n  | r	| } n  | r| | d <n  xYt d d   | j   D  D]8} | j d d  } | t k rfq<n  | t k r| j |  d k rq<n  | t k r| j |  | t | <q<| d k rt | | |  q<| d k rd | k rg  | d <n  | d j | j |   q<| d k r@i | | d 6| d d d <q<| d k rft j | |  | d <q<| | | | <q<Wt | d <d | k rt t  | d   | d <n  | r+xw d, D]l \ } }	 yK t! | |	  }
 t |
 t"  rt" t# |
   } i | d 6g | | <n  Wqt$ k
 r#YqXqWn  g  } xu t% D]m \ } } i  } x8 | D]0 } | | | k rQ| j& | |  | | <qQqQW| r8| | d <| j' |  q8q8W| r| | d d d  <n  y t
 t( j) j* t( j) j+ |   d!  d"   } t, j- j. | j    } Wd QXi  } xg | j/   D]Y \ } } i  | | <x@ | j0   D]2 } t1 |  j d# d$  \ } } | | | | <qHWq%W| r| | d d% <n  Wn t2 k
 rYn Xd% | d k rxL d/ D]A \ } } | | d d% k r| d d% | | d d* | <qqWn  | S)0a  
    Convert PKG-INFO to a prototype Metadata 2.0 (PEP 426) dict.

    The description is included under the key ['description'] rather than
    being written to a separate file.

    path: path to PKG-INFO file
    distribution: optional distutils Distribution()
    c               S   s   t  d d    S)Nc               S   s
   t  t  S)N)r   dictr   r   r   r   <lambda>`   s    z3pkginfo_to_dict.<locals>.<lambda>.<locals>.<lambda>)r   r   r   r   r   r5   `   s    z!pkginfo_to_dict.<locals>.<lambda>zbdist_wheel ()	generatorrbzutf-8NZSummarysummaryDescriptiondescriptionc             s   s   |  ] } | j    Vq d  S)N)lower)r#   kr   r   r   	<genexpr>|   s    z"pkginfo_to_dict.<locals>.<genexpr>-_UNKNOWNrequires_distr
   r	   r   ZHome
extensionszpython.detailsZproject_urlskeywordsmetadata_versiontest_requirestests_requirer!   rolecontactszentry_points.txtrz = r   zpython.exportsconsole_scriptswrap_consolegui_scriptswrap_guizpython.commandsrF   rG   )rO   zconsole_scriptsrL   zgui_scriptsrN   )rP   rQ   )3r   wheel__version__unicoder   	NameErrorr   parserParserparsestropenreaddecodepkginfo_unicodededent_descriptionget_payload
isinstancebytesr   r0   replaceSKIP_FIELDSUNKNOWN_FIELDSgetPLURAL_FIELDSr(   r3   r/   KEYWORDS_REsplitMETADATA_VERSIONsortedr   getattrr'   convert_requirementsAttributeErrorCONTACT_FIELDSpopr-   ospathjoindirnamepkg_resources
EntryPoint	parse_mapr.   valuesstrIOError)rp   distributionr1   r2   r;   payloadr$   Zlow_keyr!   attrrequirementsnew_requirementsrI   Zcontact_typerH   contactep_fileep_mapexportsgroupr.   itemr   exportZ	ep_scriptZwrap_scriptr   r   r   pkginfo_to_dictU   s    5



	&! 

-
(r   c             C   sL   g  } x( |  j  D] \ } } | j | |  q W| s; d Sd d j |  S)zBCompose the version predicates for requirement in PEP 345 fashion. z (%s),)specsr-   rq   )requirementrB   opverr   r   r   requires_to_requires_dist   s    r   c             c   sh   xa |  D]Y } t  j j |  } t |  } d j | j  } | rP d | } n  | j | | Vq Wd S)z=Yield Requires-Dist: strings for parsed requirements strings.r   z[%s]N)rs   Requirementparser   rq   r	   project_name)r|   reqZparsed_requirementspecr	   r   r   r   rk      s    rk   c       
      C   sN  t  |  } | j d d  t j j |  d  } t j j |  rt |  j   } x t j	 |  D] \ } } d } | r d | k r | j
 d d  \ } } n  | r | | d <| r | d 7} n  | d	 t |  7} n  | r d
 | } n  x" t |  D] } | | | d <q Wqe Wn  | d }	 |	 rJ| j t |   | d =n  | S)zn
    Convert .egg-info directory with PKG-INFO to the Metadata 1.3 aka
    old-draft Metadata 2.0 format.
    zMetadata-Versionz2.0zrequires.txtr   :r   zProvides-Extraz and zextra == %sz; zRequires-Distr:   )r   replace_headerro   rp   rq   existsrY   rZ   rs   split_sectionsrg   reprrk   set_payloadr]   )
egg_info_pathZpkginfo_pathr2   Zrequires_pathr!   r   reqsr   Znew_reqr;   r   r   r   pkginfo_to_metadata   s.    


r   c             C   s   |  | } | j    } t | t  s t |  d  s> t |  SxP |  j   D]? } | d j    | k rK | d j d d  j d  } PqK qK Wn  | S)z<Hack to coax Unicode out of an email Message() - Python 3.3+	raw_itemsr   r   asciisurrogateescapezutf-8)r<   r_   rw   hasattrr   encoder[   )r2   fieldtextr   r   r   r   r\     s    

	r\   c             C   s   |  d } d } t  | t  s7 d } t |  d  } n  | j   } d j | d j   t j d j | d d    d f  } | r | j d  j	 d	 d
  } n  | S)z@
    Dedent and convert pkg_info['Description'] to Unicode.
    r:   FT
r   r   Nutf8r   r   )
r_   rw   r\   
splitlinesrq   lstriptextwrapdedentr   r[   )r2   r;   Z
surrogatesZdescription_linesZdescription_dedentr   r   r   r]     s    
	r]   __main__)zauthorzauthor_emailzplatformz	home_pagezlicense)z	conditionzextra)$__doc__collectionsr   r   Zpkginfor   reos.pathro   r   rs   email.parserr   rR   rh   re   r   rb   rm   rc   compiler)   rf   r   r   r3   r   r   rk   r   r\   r]   __name__syspprintargvr   r   r   r   <module>   sB   

		#z	
 