
§|jœ  ã               @   sæ  d  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 d l Z d d l Z d d l	 m
 Z
 y d d l m Z Wn" e k
 r® d d l m Z Yn Xd d l m Z d d l m Z m Z m Z m Z d d l Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m  Z  m! Z! m" Z" m# Z# d d l m$ Z$ d d	 l% m& Z& d d
 l' m( Z( d d l) m* Z* d d l+ m, Z, d d l- m. Z. e j/ d ƒ Z0 e j/ d e j1 ƒ Z2 e j/ d ƒ Z3 e j/ d e j1 ƒ j4 Z5 d j6 ƒ  Z7 d d d d g Z8 d Z9 d Z: e: j; d e j< d d … d e ƒ Z= d d „  Z> d d „  Z? d d  „  Z@ d d! d „ ZA d d" d# „ ZB d d$ d% „ ZC d e d d& d „ ZD d d' d( „ ZE d) d* „  ZF e j/ d+ e j1 ƒ ZG eF d, d- „  ƒ ZH Gd. d/ „  d/ eI ƒ ZJ Gd0 d1 „  d1 eJ ƒ ZK Gd2 d „  d e ƒ ZL e j/ d3 ƒ jM ZN d4 d5 „  ZO d6 d7 „  ZP d8 d9 „  ZQ d d: d; „ ZR d< d= „  ZS Gd> d? „  d? eI ƒ ZT Gd@ dA „  dA e jU ƒ ZV e jW jX dB dC „ ZY eR e9 ƒ eY ƒ ZY dD dE „  ZZ dF dG „  Z[ d S)Hz#PyPI and direct package downloadingé    N)Úwraps)Ú	splituser)Úsix)ÚurllibÚhttp_clientÚconfigparserÚmap)ÚCHECKOUT_DISTÚDistributionÚBINARY_DISTÚnormalize_pathÚSOURCE_DISTÚEnvironmentÚfind_distributionsÚ	safe_nameÚsafe_versionÚto_filenameÚRequirementÚDEVELOP_DIST)Ússl_support)Úlog)ÚDistutilsError)Ú	translate)Ústrip_fragment)Úget_all_headersz^egg=([-A-Za-z0-9_.]+)$zhref\s*=\s*['"]?([^'"> ]+)z�<a href="([^"#]+)">([^<]+)</a>
\s+\(<a (?:title="MD5 hash"
\s+)href="[^?]+\?:action=show_md5&amp;digest=([0-9a-f]{32})">md5</a>\)z([-+.a-z0-9]{2,}):z.tar.gz .tar.bz2 .tar .zip .tgzÚPackageIndexÚdistros_for_urlÚparse_bdist_wininstÚinterpret_distro_nameé   z<setuptools/{setuptools.__version__} Python-urllib/{py_major}Zpy_majoré   Ú
setuptoolsc             C   s=   y t  j |  ƒ SWn% t k
 r8 t d |  f ƒ ‚ Yn Xd  S)Nz1Not a URL, existing file, or requirement spec: %r)r   ÚparseÚ
ValueErrorr   )Úspec© r%   ú>/tmp/pip-build-5c7ija6t/setuptools/setuptools/package_index.pyÚparse_requirement_arg6   s
    r'   c             C   sú   |  j  ƒ  } d \ } } } | j d ƒ rí | j d ƒ rR |  d d … } d } n› | j d d ƒ r� |  d d … } |  d d … } d } n` | j d
 ƒ rµ |  d d … } d } n8 | j d d ƒ rí |  d d … } |  d d … } d } | | | f S)z=Return (base,pyversion) or (None,None) for possible .exe nameNz.exez
.win32.exeé
   Úwin32z	.win32-pyé   é   é   z.win-amd64.exeé   z	win-amd64z.win-amd64-pyé   )NNNiöÿÿÿiðÿÿÿiùÿÿÿéüÿÿÿiðÿÿÿiòÿÿÿiìÿÿÿiùÿÿÿr/   iìÿÿÿ)ÚlowerÚendswithÚ
startswith)Únamer0   ÚbaseÚpy_verÚplatr%   r%   r&   r   ?   s$    			c       	      C   s®   t  j j |  ƒ } | \ } } } } } } t  j j | j d ƒ d ƒ } | d k r€ | d k r€ t  j j | j d ƒ d ƒ } d | k r¤ | j d d ƒ \ } } | | f S)	Nú/é   zsourceforge.netÚdownloadé   ú#éÿÿÿÿéþÿÿÿ)r   r"   ÚurlparseÚunquoteÚsplit)	ÚurlÚpartsÚschemeÚserverÚpathZ
parametersÚqueryÚfragmentr4   r%   r%   r&   Úegg_info_for_urlW   s    rH   c             c   s„   t  |  ƒ \ } } x t |  | | ƒ D] } | Vq% W| r€ t j | ƒ } | r€ x. t |  | j d ƒ | d t ƒD] } | Vqq Wd S)zEYield egg or source distribution objects that might be found at a URLr8   Ú
precedenceN)rH   Údistros_for_locationÚEGG_FRAGMENTÚmatchr   Úgroupr	   )rA   Úmetadatar4   rG   ÚdistrL   r%   r%   r&   r   b   s    	"c             C   sä   | j  d ƒ r | d d … } | j  d ƒ rP d | k rP t j |  | | ƒ g S| j  d ƒ r™ t | ƒ \ } } } | d k	 r™ t |  | | | t | ƒ SxD t D]< } | j  | ƒ r  | d t | ƒ … } t |  | | ƒ Sq  Wg  S)z:Yield egg or source distribution objects based on basenamez.egg.zipNr,   z.eggú-z.exer/   )r1   r
   Úfrom_locationr   r   r   Ú
EXTENSIONSÚlen)ÚlocationÚbasenamerN   Zwin_baser5   ÚplatformÚextr%   r%   r&   rJ   p   s    rJ   c             C   s"   t  t |  ƒ t j j |  ƒ | ƒ S)zEYield possible egg or source distribution objects based on a filename)rJ   r   ÚosrE   rU   )ÚfilenamerN   r%   r%   r&   Údistros_for_filename†   s    rZ   c             c   s±   | j  d ƒ } | r= t d d „  | d d … Dƒ ƒ r= d Sxm t d t | ƒ d ƒ D]R } t |  | d j | d | … ƒ d j | | d … ƒ d | d | d	 | ƒVqW Wd S)
zØGenerate alternative interpretations of a source distro name

    Note: if `location` is a filesystem filename, you should call
    ``pkg_resources.normalize_path()`` on it before passing it to this
    routine!
    rP   c             s   s!   |  ] } t  j d  | ƒ Vq d S)z	py\d\.\d$N)ÚrerL   )Ú.0Úpr%   r%   r&   ú	<genexpr>¤   s    z(interpret_distro_name.<locals>.<genexpr>r:   Nr8   Ú
py_versionrI   rV   )r@   ÚanyÚrangerS   r
   Újoin)rT   rU   rN   r_   rI   rV   rB   r]   r%   r%   r&   r   �   s    * 5c             c   s�   t  ƒ  } | j } | d k rS xj t j j | j |  ƒ D] } | | ƒ | Vq7 Wn8 x5 |  D]- } | | ƒ } | | k rZ | | ƒ | VqZ Wd S)zHList unique elements, preserving order. Remember all elements ever seen.N)ÚsetÚaddr   ÚmovesÚfilterfalseÚ__contains__)ÚiterableÚkeyÚseenZseen_addÚelementÚkr%   r%   r&   Úunique_everseen±   s    		

rm   c                s"   t  ˆ  ƒ ‡  f d d †  ƒ } | S)zs
    Wrap a function returning an iterable such that the resulting iterable
    only ever yields unique items.
    c                 s   t  ˆ  |  | Ž  ƒ S)N)rm   )ÚargsÚkwargs)Úfuncr%   r&   ÚwrapperÉ   s    zunique_values.<locals>.wrapper)r   )rp   rq   r%   )rp   r&   Úunique_valuesÃ   s    rr   z(<([^>]*\srel\s*=\s*['"]?([^'">]+)[^>]*)>c             c   s  x¥ t  j | ƒ D]” } | j ƒ  \ } } t t t j | j ƒ  j d ƒ ƒ ƒ } d | k sg d | k r x: t	 j | ƒ D]) } t
 j j |  t | j d ƒ ƒ ƒ Vqw Wq Wxd d D]\ } | j | ƒ } | d	 k r¯ t	 j | | ƒ } | r¯ t
 j j |  t | j d ƒ ƒ ƒ Vq¯ Wd S)
zEFind rel="homepage" and rel="download" links in `page`, yielding URLsú,Zhomepager9   r8   ú<th>Home Pageú<th>Download URLN)rt   ru   r<   )ÚRELÚfinditerÚgroupsrc   r   ÚstrÚstripr0   r@   ÚHREFr   r"   ÚurljoinÚ
htmldecoderM   ÚfindÚsearch)rA   ÚpagerL   ÚtagÚrelZrelsÚposr%   r%   r&   Úfind_external_linksÔ   s    '+r„   c               @   s:   e  Z d  Z d Z d d „  Z d d „  Z d d „  Z d S)	ÚContentCheckerzP
    A null content checker that defines the interface for checking content
    c             C   s   d S)z3
        Feed a block of data to the hash.
        Nr%   )ÚselfÚblockr%   r%   r&   Úfeedì   s    zContentChecker.feedc             C   s   d S)zC
        Check the hash. Return False if validation fails.
        Tr%   )r†   r%   r%   r&   Úis_validò   s    zContentChecker.is_validc             C   s   d S)zu
        Call reporter with information about the checker (hash name)
        substituted into the template.
        Nr%   )r†   ÚreporterÚtemplater%   r%   r&   Úreportø   s    zContentChecker.reportN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rˆ   r‰   rŒ   r%   r%   r%   r&   r…   ç   s   r…   c               @   sa   e  Z d  Z e j d ƒ Z d d „  Z e d d „  ƒ Z d d „  Z	 d d	 „  Z
 d
 d „  Z d S)ÚHashCheckerzK(?P<hash_name>sha1|sha224|sha384|sha256|sha512|md5)=(?P<expected>[a-f0-9]+)c             C   s(   | |  _  t j | ƒ |  _ | |  _ d  S)N)Ú	hash_nameÚhashlibÚnewÚhashÚexpected)r†   r’   r–   r%   r%   r&   Ú__init__  s    	zHashChecker.__init__c             C   sR   t  j j | ƒ d } | s# t ƒ  S|  j j | ƒ } | sB t ƒ  S|  | j ƒ  �  S)z5Construct a (possibly null) ContentChecker from a URLr8   r<   )r   r"   r>   r…   Úpatternr   Ú	groupdict)ÚclsrA   rG   rL   r%   r%   r&   Úfrom_url  s    zHashChecker.from_urlc             C   s   |  j  j | ƒ d  S)N)r•   Úupdate)r†   r‡   r%   r%   r&   rˆ     s    zHashChecker.feedc             C   s   |  j  j ƒ  |  j k S)N)r•   Ú	hexdigestr–   )r†   r%   r%   r&   r‰     s    zHashChecker.is_validc             C   s   | |  j  } | | ƒ S)N)r’   )r†   rŠ   r‹   Úmsgr%   r%   r&   rŒ     s    zHashChecker.reportN)r�   rŽ   r�   r[   Úcompiler˜   r—   Úclassmethodr›   rˆ   r‰   rŒ   r%   r%   r%   r&   r‘      s   	r‘   c                   sç  e  Z d  Z d Z d dJ d d d d „ Z d d	 d
 „ Z d d d „ Z d d d „ Z d d „  Z d d „  Z	 d d „  Z
 d d „  Z d d d „ Z d d „  Z d ‡  f d d † Z d d „  Z d d  „  Z d! d" „  Z d# d$ „  Z d% d& „  Z d d d d d' d( „ Z d d d) d* „ Z d+ d, „  Z d- Z d. d/ „  Z d0 d1 „  Z d d2 d3 „ Z d4 d5 „  Z d6 d7 „  Z d8 d9 „  Z d: d; „  Z d< d= „  Z e  d d> d? „ ƒ Z! d@ dA „  Z" dB dC „  Z# dD dE „  Z$ dF dG „  Z% dH dI „  Z& ‡  S)Kr   z;A distribution index that scans web pages for download URLszhttps://pypi.python.org/simpleÚ*NTc             O   sÎ   t  j |  | | Ž | d d  | j d ƒ … |  _ i  |  _ i  |  _ i  |  _ t j d j	 t
 t | ƒ ƒ ƒ j |  _ g  |  _ | o� t j o� | p� t j ƒ  } | r» t j | ƒ |  _ n t j j |  _ d  S)Nr7   ú|)r   r—   r1   Ú	index_urlÚscanned_urlsÚfetched_urlsÚpackage_pagesr[   rŸ   rb   r   r   rL   ÚallowsÚto_scanr   Zis_availableZfind_ca_bundleZ
opener_forÚopenerr   ÚrequestÚurlopen)r†   r£   ÚhostsZ	ca_bundleZ
verify_sslrn   ÚkwZuse_sslr%   r%   r&   r—   $  s    !			'		zPackageIndex.__init__Fc             C   s^  | |  j  k r | r d Sd |  j  | <t | ƒ sD |  j | ƒ d St t | ƒ ƒ } | r |  j | ƒ so d S|  j d | ƒ | s› | s› | |  j k rµ t t |  j	 | ƒ ƒ d S|  j | ƒ sÕ d |  j | <d S|  j
 d | ƒ d |  j | <d } |  j | | | ƒ } | d k rd Sd |  j | j <d | j j d d ƒ j ƒ  k r]| j ƒ  d S| j } | j ƒ  } t | t ƒ sÉt | t j j ƒ rŸd	 } n | j j d
 ƒ p´d	 } | j | d ƒ } | j ƒ  xH t j | ƒ D]7 }	 t j j | t |	 j d ƒ ƒ ƒ }
 |  j |
 ƒ qãW| j  |  j! ƒ rZt" | d d ƒ d k rZ|  j# | | ƒ } d S)z<Evaluate a URL as a possible download, and maybe retrieve itNTzFound link: %sz
Reading %sz<Download error on %s: %%s -- Some packages may not be found!Úhtmlzcontent-typeÚ zlatin-1ÚcharsetÚignorer8   Úcodei”  )$r¤   Ú
URL_SCHEMEÚprocess_filenameÚlistr   Úurl_okÚdebugr¥   r   rd   ÚinfoÚopen_urlrA   ÚheadersÚgetr0   ÚcloseÚreadÚ
isinstancery   r   ÚerrorÚ	HTTPErrorÚ	get_paramÚdecoder{   rw   r"   r|   r}   rM   Úprocess_urlr2   r£   ÚgetattrÚprocess_index)r†   rA   ÚretrieveÚdistsÚtmplÚfr4   r€   r°   rL   Úlinkr%   r%   r&   rÃ   9  sP    !
		
$*zPackageIndex.process_urlc             C   sÆ   t  j j | ƒ s& |  j d | ƒ d  St  j j | ƒ rŠ | rŠ t  j j | ƒ } x6 t  j | ƒ D]% } |  j t  j j | | ƒ d ƒ qa Wt	 | ƒ } | rÂ |  j
 d | ƒ t t |  j | ƒ ƒ d  S)NzNot found: %sTz	Found: %s)rX   rE   ÚexistsÚwarnÚisdirÚrealpathÚlistdirr´   rb   rZ   r·   rµ   r   rd   )r†   ÚfnÚnestedrE   ÚitemrÇ   r%   r%   r&   r´   k  s    #zPackageIndex.process_filenamec             C   s‰   t  | ƒ } | o* | j d ƒ j ƒ  d k } | sR |  j t j j | ƒ d ƒ rV d Sd } | ru t | | ƒ ‚ n |  j | | ƒ d  S)Nr8   ÚfileTzN
Note: Bypassing %s (disallowed host; see http://bit.ly/1dg9ijs for details).
)	r³   rM   r0   r§   r   r"   r>   r   rÌ   )r†   rA   ÚfatalÚsÚis_filerž   r%   r%   r&   r¶   {  s    !%zPackageIndex.url_okc             C   sE   t  t j j | ƒ } d d „  | Dƒ } t t j |  j | ƒ ƒ d  S)Nc             s   s@   |  ]6 } t  j | ƒ D]  } | j d  ƒ r | | f Vq q d S)z	.egg-linkN)rX   rÏ   r1   )r\   rE   Úentryr%   r%   r&   r^   Š  s   z.PackageIndex.scan_egg_links.<locals>.<genexpr>)ÚfilterrX   rE   rÍ   rµ   Ú	itertoolsÚstarmapÚscan_egg_link)r†   Úsearch_pathÚdirsZ	egg_linksr%   r%   r&   Úscan_egg_links‡  s    	
zPackageIndex.scan_egg_linksc             C   sÀ   t  t j j | | ƒ ƒ �( } t t d  t t j | ƒ ƒ ƒ } Wd  QRXt	 | ƒ d k r\ d  S| \ } } xQ t
 t j j | | ƒ ƒ D]4 } t j j | | Œ | _ t | _ |  j | ƒ q„ Wd  S)Nr:   )ÚopenrX   rE   rb   rµ   rØ   r   ry   rz   rS   r   rT   r   rI   rd   )r†   rE   r×   Z	raw_linesÚlinesZegg_pathZ
setup_pathrO   r%   r%   r&   rÛ   ‘  s    ("	zPackageIndex.scan_egg_linkc       
         s  ‡  f d d †  } xX t  j | ƒ D]G } y, | t j j | t | j d ƒ ƒ ƒ ƒ Wq" t k
 rh Yq" Xq" W| | ƒ \ } } | rxv t | | ƒ D]e } t	 | ƒ \ } }	 | j
 d ƒ rí |	 rí | rà | d | | f 7} n ˆ  j | ƒ ˆ  j | ƒ q• Wt j d d „  | ƒ Sd Sd	 S)
z#Process the contents of a PyPI pagec                sÄ   |  j  ˆ  j ƒ rÀ t t t j j |  t ˆ  j ƒ d  … j d ƒ ƒ ƒ } t | ƒ d k rÀ d | d k rÀ t	 | d ƒ } t
 | d ƒ } d ˆ  j j | j ƒ  i  ƒ |  <t | ƒ t | ƒ f Sd S)Nr7   r:   r;   r8   r   T)NN)r2   r£   rµ   r   r   r"   r?   rS   r@   r   r   r¦   Ú
setdefaultr0   r   )rÊ   rB   ÚpkgÚver)r†   r%   r&   Úscan¤  s    1"z(PackageIndex.process_index.<locals>.scanr8   z.pyz
#egg=%s-%sc             S   s   d |  j  d d d ƒ S)Nz<a href="%s#md5=%s">%s</a>r8   r    r:   )rM   )Úmr%   r%   r&   Ú<lambda>Ç  s    z,PackageIndex.process_index.<locals>.<lambda>r¯   N)r{   rw   r   r"   r|   r}   rM   r#   r„   rH   r1   Úneed_version_infoÚscan_urlÚPYPI_MD5Úsub)
r†   rA   r€   rä   rL   râ   rã   Únew_urlr4   Úfragr%   )r†   r&   rÅ   ¡  s$    ,	zPackageIndex.process_indexc             C   s   |  j  d | ƒ d  S)NzPPage at %s links to .py file(s) without version info; an index scan is required.)Úscan_all)r†   rA   r%   r%   r&   rç   Ì  s    zPackageIndex.need_version_infoc             G   sI   |  j  |  j k r5 | r( |  j | | Œ |  j d ƒ |  j |  j  ƒ d  S)Nz6Scanning index of all packages (this may take a while))r£   r¥   rÌ   r¸   rè   )r†   rž   rn   r%   r%   r&   rí   Ò  s    zPackageIndex.scan_allc             C   s§   |  j  |  j | j d ƒ |  j j | j ƒ sK |  j  |  j | j d ƒ |  j j | j ƒ sm |  j | ƒ x3 t |  j j | j f  ƒ ƒ D] } |  j  | ƒ qŒ Wd  S)Nr7   )	rè   r£   Úunsafe_namer¦   r»   ri   Úproject_nameÚnot_found_in_indexrµ   )r†   ÚrequirementrA   r%   r%   r&   Úfind_packagesÛ  s    %zPackageIndex.find_packagesc                sk   |  j  ƒ  |  j | ƒ x8 |  | j D]) } | | k r; | S|  j d | | ƒ q% Wt t |  ƒ j | | ƒ S)Nz%s does not match %s)Úprescanrò   ri   r·   Úsuperr   Úobtain)r†   rñ   Ú	installerrO   )Ú	__class__r%   r&   rõ   ê  s    
zPackageIndex.obtainc             C   sf   | j  |  j d | ƒ | j ƒ  sb | j ƒ  t j | ƒ t d | j j t j	 j
 | ƒ f ƒ ‚ d S)z-
        checker is a ContentChecker
        zValidating %%s checksum for %sz7%s validation failed for %s; possible download problem?N)rŒ   r·   r‰   r¼   rX   Úunlinkr   r•   r3   rE   rU   )r†   ÚcheckerrY   Útfpr%   r%   r&   Ú
check_hashó  s    
zPackageIndex.check_hashc             C   sr   xk | D]c } |  j  d k sJ t | ƒ sJ | j d ƒ sJ t t | ƒ ƒ rZ |  j | ƒ q |  j  j | ƒ q Wd S)z;Add `urls` to the list that will be prescanned for searchesNzfile:)r¨   r³   r2   rµ   r   rè   Úappend)r†   ÚurlsrA   r%   r%   r&   Úadd_find_links  s    zPackageIndex.add_find_linksc             C   s/   |  j  r" t t |  j |  j  ƒ ƒ d |  _  d S)z7Scan urls scheduled for prescanning (e.g. --find-links)N)r¨   rµ   r   rè   )r†   r%   r%   r&   ró     s    	zPackageIndex.prescanc             C   sN   |  | j  r  |  j d } } n |  j d } } | | | j ƒ |  j ƒ  d  S)Nz#Couldn't retrieve index page for %rz3Couldn't find index page for %r (maybe misspelled?))ri   r¸   rÌ   rî   rí   )r†   rñ   Úmethrž   r%   r%   r&   rð     s    
zPackageIndex.not_found_in_indexc             C   s·   t  | t ƒ s› t | ƒ } | ry |  j | j d ƒ | | ƒ } t | ƒ \ } } | j d ƒ ru |  j | | | ƒ } | St j	 j
 | ƒ r� | St | ƒ } t |  j | | ƒ d d ƒ S)a  Locate and/or download `spec` to `tmpdir`, returning a local path

        `spec` may be a ``Requirement`` object, or a string containing a URL,
        an existing local filename, or a project/version requirement spec
        (i.e. the string form of a ``Requirement`` object).  If it is the URL
        of a .py file with an unambiguous ``#egg=name-version`` tag (i.e., one
        that escapes ``-`` as ``_`` throughout), a trivial ``setup.py`` is
        automatically created alongside the downloaded file.

        If `spec` is a ``Requirement`` object or a string containing a
        project/version requirement spec, this method returns the location of
        a matching distribution (possibly after downloading it to `tmpdir`).
        If `spec` is a locally existing file or directory name, it is simply
        returned unchanged.  If `spec` is a URL, it is downloaded to a subpath
        of `tmpdir`, and the local filename is returned.  Various errors may be
        raised if a problem occurs during downloading.
        r8   z.pyrT   N)r¾   r   r³   Ú_download_urlrM   rH   r1   Ú	gen_setuprX   rE   rË   r'   rÄ   Úfetch_distribution)r†   r$   ÚtmpdirrC   Úfoundr4   rG   r%   r%   r&   r9      s    zPackageIndex.downloadc       	         s:  ˆ j  d | ƒ i  ‰ d } d ‡  ‡ ‡ ‡ ‡ f d d † } | rf ˆ j ƒ  ˆ j | ƒ | | ƒ } | rˆ | d k	 rˆ | | | ƒ } | d k r¹ ˆ j d k	 r­ ˆ j ƒ  | | ƒ } | d k rå | rå ˆ j | ƒ | | ƒ } | d k rˆ j d ˆ rd p	d | ƒ n# ˆ j  d | ƒ | j d	 | j ƒ Sd S)
a|  Obtain a distribution suitable for fulfilling `requirement`

        `requirement` must be a ``pkg_resources.Requirement`` instance.
        If necessary, or if the `force_scan` flag is set, the requirement is
        searched for in the (online) package index as well as the locally
        installed packages.  If a distribution matching `requirement` is found,
        the returned distribution's ``location`` is the value you would have
        gotten from calling the ``download()`` method with the matching
        distribution's URL or filename.  If no matching distribution is found,
        ``None`` is returned.

        If the `source` flag is set, only source distributions and source
        checkout links will be considered.  Unless the `develop_ok` flag is
        set, development and system eggs (i.e., those using the ``.egg-info``
        format) will be ignored.
        zSearching for %sNc                sÀ   | d  k r ˆ } x§ | |  j  D]˜ } | j t k re ˆ  re | ˆ k r  ˆ j d | ƒ d ˆ | <q  | |  k r  | j t k s‡ ˆ r  ˆ j | j ˆ ƒ | _ t j	 j
 | j ƒ r  | Sq  Wd  S)Nz&Skipping development or system egg: %sr8   )ri   rI   r   rÌ   r   r9   rT   Údownload_locationrX   rE   rË   )ÚreqÚenvrO   )Ú
develop_okr†   ÚskippedÚsourcer  r%   r&   r~   [  s    
"z-PackageIndex.fetch_distribution.<locals>.findz:No local packages or working download links found for %s%sza source distribution of r¯   zBest match: %srT   )r¸   ró   rò   r¨   rÌ   Úcloner  )	r†   rñ   r  Ú
force_scanr
  r  Zlocal_indexrO   r~   r%   )r  r†   r	  r
  r  r&   r  B  s0    !


zPackageIndex.fetch_distributionc             C   s/   |  j  | | | | ƒ } | d k	 r+ | j Sd S)a3  Obtain a file suitable for fulfilling `requirement`

        DEPRECATED; use the ``fetch_distribution()`` method now instead.  For
        backward compatibility, this routine is identical but returns the
        ``location`` of the downloaded distribution instead of a distribution
        object.
        N)r  rT   )r†   rñ   r  r  r
  rO   r%   r%   r&   Úfetchˆ  s    zPackageIndex.fetchc       
      C   s\  t  j | ƒ } | r= d d „  t | | j d ƒ d  ƒ Dƒ p@ g  } t | ƒ d k r-t j j | ƒ } t j j | ƒ | k rÉ t j j	 | | ƒ } d d l
 m } | | | ƒ sÉ t j | | ƒ | } t t j j	 | d ƒ d ƒ �? }	 |	 j d | d j | d j t j j | ƒ d f ƒ Wd  QRX| S| rLt d	 | | f ƒ ‚ n t d
 ƒ ‚ d  S)Nc             S   s   g  |  ] } | j  r | ‘ q Sr%   )Úversion)r\   Údr%   r%   r&   ú
<listcomp>˜  s   	 z*PackageIndex.gen_setup.<locals>.<listcomp>r8   r   )Úsamefilezsetup.pyÚwzIfrom setuptools import setup
setup(name=%r, version=%r, py_modules=[%r])
z†Can't unambiguously interpret project/version identifier %r; any dashes in the name or version should be escaped using underscores. %rzpCan't process plain .py files without an '#egg=name-version' suffix to enable automatic setup script generation.)rK   rL   r   rM   rS   rX   rE   rU   Údirnamerb   Zsetuptools.command.easy_installr  ÚshutilÚcopy2rß   Úwriterï   r  Úsplitextr   )
r†   rY   rG   r  rL   rÇ   rU   Údstr  rÓ   r%   r%   r&   r  •  s2    	!"zPackageIndex.gen_setupi    c             C   s‡  |  j  d | ƒ d \ } } zSt j | ƒ } |  j t | ƒ ƒ } t | t j j ƒ rw t	 d | | j
 | j f ƒ ‚ | j  ƒ  } d } |  j } d	 }	 d | k rá t | d ƒ }
 t t t |
 ƒ ƒ }	 |  j | | | | |	 ƒ t | d ƒ �w } xZ | j | ƒ } | rK| j | ƒ | j | ƒ | d 7} |  j | | | | |	 ƒ qö Pqö W|  j | | | ƒ Wd  QRX| SWd  | r‚| j ƒ  Xd  S)
NzDownloading %szCan't download %s: %s %sr   r8   zcontent-lengthzContent-LengthÚwb)NNr<   )r¸   r‘   r›   r¹   r   r¾   r   r¿   rÀ   r   r²   rž   Údl_blocksizer   Úmaxr   ÚintÚ
reporthookrß   r½   rˆ   r  rû   r¼   )r†   rA   rY   Úfpr¸   rù   rº   ÚblocknumÚbsÚsizeÚsizesrú   r‡   r%   r%   r&   Ú_download_toÀ  s:    	
zPackageIndex._download_toc             C   s   d  S)Nr%   )r†   rA   rY   r  Zblksizer!  r%   r%   r&   r  ä  s    zPackageIndex.reporthookc             C   sø  | j  d ƒ r t | ƒ Sy t | |  j ƒ SWnÄt t j f k
 r« } zS d j d d „  | j Dƒ ƒ } | rƒ |  j	 | | ƒ n t
 d | | f ƒ ‚ WYd  d  } ~ XnIt j j k
 rÙ } z	 | SWYd  d  } ~ Xnt j j k
 r8} z: | r|  j	 | | j ƒ n t
 d | | j f ƒ ‚ WYd  d  } ~ Xn¼ t j k
 r”} z: | ri|  j	 | | j ƒ n t
 d | | j f ƒ ‚ WYd  d  } ~ Xn` t j t j f k
 ró} z4 | rË|  j	 | | ƒ n t
 d | | f ƒ ‚ WYd  d  } ~ Xn Xd  S)Nzfile:ú c             S   s   g  |  ] } t  | ƒ ‘ q Sr%   )ry   )r\   Úargr%   r%   r&   r  í  s   	 z)PackageIndex.open_url.<locals>.<listcomp>z%s %szDownload error for %s: %sz;%s returned a bad status line. The server might be down, %s)r2   Ú
local_openÚopen_with_authr©   r#   r   Ú
InvalidURLrb   rn   rÌ   r   r   r¿   rÀ   ÚURLErrorÚreasonÚBadStatusLineÚlineÚHTTPExceptionÚsocket)r†   rA   ÚwarningÚvrž   r%   r%   r&   r¹   ç  s6    
(%%zPackageIndex.open_urlc             C   sK  t  | ƒ \ } } | rL x7 d | k rH | j d d ƒ j d d ƒ } q Wn d } | j d ƒ rq | d  d … } t j j | | ƒ } | d k s¡ | j d	 ƒ r± |  j | | ƒ S| d
 k sÌ | j d ƒ rÜ |  j | | ƒ S| j d ƒ rû |  j	 | | ƒ S| d k r't
 j j t
 j j | ƒ d ƒ S|  j | d ƒ |  j | | ƒ Sd  S)Nz..Ú.ú\Ú_Z__downloaded__z.egg.zipr,   Úsvnzsvn+Úgitzgit+zhg+rÓ   r:   Tr/   )rH   Úreplacer1   rX   rE   rb   r2   Ú_download_svnÚ_download_gitÚ_download_hgr   rª   Úurl2pathnamer"   r>   r¶   Ú_attempt_download)r†   rC   rA   r  r3   rG   rY   r%   r%   r&   r   
  s$    % zPackageIndex._download_urlc             C   s   |  j  | d ƒ d  S)NT)rÃ   )r†   rA   r%   r%   r&   rè   '  s    zPackageIndex.scan_urlc             C   sK   |  j  | | ƒ } d | j d d ƒ j ƒ  k rC |  j | | | ƒ S| Sd  S)Nr®   zcontent-typer¯   )r#  r»   r0   Ú_download_html)r†   rA   rY   rº   r%   r%   r&   r;  *  s    zPackageIndex._attempt_downloadc             C   sŽ   t  | ƒ } xT | D]L } | j ƒ  r t j d | ƒ r^ | j ƒ  t j | ƒ |  j | | ƒ SPq W| j ƒ  t j | ƒ t d | ƒ ‚ d  S)Nz <title>([^- ]+ - )?Revision \d+:zUnexpected HTML page found at )	rß   rz   r[   r   r¼   rX   rø   r7  r   )r†   rA   rº   rY   rÓ   r,  r%   r%   r&   r<  1  s    

zPackageIndex._download_htmlc             C   si  | j  d d ƒ d } d } | j ƒ  j d ƒ r8d | k r8t j j | ƒ \ } } } } } }	 | r8| j d ƒ r8d | d	 d  … k r8| d	 d  … j  d d ƒ \ } } t | ƒ \ }
 } |
 r8d
 |
 k rþ |
 j  d
 d ƒ \ } } d | | f } n
 d |
 } | } | | | | | |	 f } t j j | ƒ } |  j d | | ƒ t	 j
 d | | | f ƒ | S)Nr;   r8   r   r¯   zsvn:ú@z//r7   r:   ú:z --username=%s --password=%sz --username=z'Doing subversion checkout from %s to %szsvn checkout%s -q %s %s)r@   r0   r2   r   r"   r>   r   Ú
urlunparser¸   rX   Úsystem)r†   rA   rY   ÚcredsrC   ÚnetlocrE   r]   ÚqrÉ   ÚauthÚhostÚuserÚpwrB   r%   r%   r&   r7  @  s$    !$,"
zPackageIndex._download_svnc             C   s¢   t  j j |  ƒ \ } } } } } | j d d ƒ d } | j d d ƒ d } d  } d | k rw | j d d ƒ \ } } t  j j | | | | d f ƒ }  |  | f S)Nú+r8   r;   r   r=  r¯   r<   )r   r"   Úurlsplitr@   ÚrsplitÚ
urlunsplit)rA   Ú
pop_prefixrC   rB  rE   rF   rì   Úrevr%   r%   r&   Ú_vcs_split_rev_from_urlU  s    !!z$PackageIndex._vcs_split_rev_from_urlc             C   s’   | j  d d ƒ d } |  j | d d ƒ\ } } |  j d | | ƒ t j d | | f ƒ | d  k	 rŽ |  j d | ƒ t j d	 | | f ƒ | S)
Nr;   r8   r   rL  TzDoing git clone from %s to %szgit clone --quiet %s %szChecking out %sz"(cd %s && git checkout --quiet %s))r@   rN  r¸   rX   r@  )r†   rA   rY   rM  r%   r%   r&   r8  g  s    	zPackageIndex._download_gitc             C   s’   | j  d d ƒ d } |  j | d d ƒ\ } } |  j d | | ƒ t j d | | f ƒ | d  k	 rŽ |  j d | ƒ t j d	 | | f ƒ | S)
Nr;   r8   r   rL  TzDoing hg clone from %s to %szhg clone --quiet %s %szUpdating to %sz(cd %s && hg up -C -r %s >&-))r@   rN  r¸   rX   r@  )r†   rA   rY   rM  r%   r%   r&   r9  w  s    	zPackageIndex._download_hgc             G   s   t  j | | Œ d  S)N)r   r·   )r†   rž   rn   r%   r%   r&   r·   ‡  s    zPackageIndex.debugc             G   s   t  j | | Œ d  S)N)r   r¸   )r†   rž   rn   r%   r%   r&   r¸   Š  s    zPackageIndex.infoc             G   s   t  j | | Œ d  S)N)r   rÌ   )r†   rž   rn   r%   r%   r&   rÌ   �  s    zPackageIndex.warn)r¡   )'r�   rŽ   r�   r�   r—   rÃ   r´   r¶   rÞ   rÛ   rÅ   rç   rí   rò   rõ   rû   rþ   ró   rð   r9   r  r  r  r  r#  r  r¹   r   rè   r;  r<  r7  ÚstaticmethodrN  r8  r9  r·   r¸   rÌ   r%   r%   )r÷   r&   r   !  sL   2
+			#D)$#z!&(#(\d+|x[\da-fA-F]+)|[\w.:-]+);?c             C   s6   t  |  t ƒ s |  S|  d k r, t j |  ƒ St |  ƒ S)Néÿ   )r¾   r  r   ÚunichrÚchr)Úcr%   r%   r&   Úuchr–  s
    rT  c             C   s�   |  j  d ƒ } | j d ƒ r: t | d d  … d ƒ } nL | j d ƒ rb t | d d  … ƒ } n$ t j j j j | |  j  d ƒ ƒ } t | ƒ S)Nr8   z#xr:   r*   r;   r   )	rM   r2   r  r   re   Úhtml_entitiesÚname2codepointr»   rT  )rL   Úwhatr%   r%   r&   Údecode_entityž  s    $rX  c             C   s   t  t |  ƒ S)z'Decode HTML entities in the given text.)Ú
entity_subrX  )Útextr%   r%   r&   r}   ©  s    r}   c                s   ‡  f d d †  } | S)Nc                s   ‡  ‡ f d d †  } | S)Nc                 s?   t  j ƒ  } t  j ˆ ƒ z ˆ  |  | Ž  SWd  t  j | ƒ Xd  S)N)r.  ÚgetdefaulttimeoutÚsetdefaulttimeout)rn   ro   Zold_timeout)rp   Útimeoutr%   r&   Ú_socket_timeout°  s
    z@socket_timeout.<locals>._socket_timeout.<locals>._socket_timeoutr%   )rp   r^  )r]  )rp   r&   r^  ¯  s    z'socket_timeout.<locals>._socket_timeoutr%   )r]  r^  r%   )r]  r&   Úsocket_timeout®  s    r_  c             C   sI   t  j j |  ƒ } | j ƒ  } t j | ƒ } | j ƒ  } | j d d ƒ S)aq  
    A function compatible with Python 2.3-3.3 that will encode
    auth from a URL suitable for an HTTP header.
    >>> str(_encode_auth('username%3Apassword'))
    'dXNlcm5hbWU6cGFzc3dvcmQ='

    Long auth strings should not cause a newline to be inserted.
    >>> long_auth = 'username:' + 'password'*10
    >>> chr(10) in str(_encode_auth(long_auth))
    False
    Ú
r¯   )r   r"   r?   ÚencodeÚbase64ÚencodestringrÂ   r6  )rD  Zauth_sZ
auth_bytesZencoded_bytesÚencodedr%   r%   r&   Ú_encode_auth½  s
    re  c               @   s:   e  Z d  Z d Z d d „  Z d d „  Z d d „  Z d S)	Ú
Credentialz:
    A username/password pair. Use like a namedtuple.
    c             C   s   | |  _  | |  _ d  S)N)ÚusernameÚpassword)r†   rg  rh  r%   r%   r&   r—   Ù  s    	zCredential.__init__c             c   s   |  j  V|  j Vd  S)N)rg  rh  )r†   r%   r%   r&   Ú__iter__Ý  s    zCredential.__iter__c             C   s   d t  |  ƒ S)Nz%(username)s:%(password)s)Úvars)r†   r%   r%   r&   Ú__str__á  s    zCredential.__str__N)r�   rŽ   r�   r�   r—   ri  rk  r%   r%   r%   r&   rf  Ô  s   rf  c               @   sF   e  Z d  Z d d „  Z e d d „  ƒ Z d d „  Z d d „  Z d	 S)
Ú
PyPIConfigc             C   sr   t  j d d d g d ƒ } t j j |  | ƒ t j j t j j d ƒ d ƒ } t j j	 | ƒ rn |  j
 | ƒ d S)z%
        Load from ~/.pypirc
        rg  rh  Ú
repositoryr¯   ú~z.pypircN)ÚdictÚfromkeysr   ÚRawConfigParserr—   rX   rE   rb   Ú
expanduserrË   r½   )r†   ÚdefaultsÚrcr%   r%   r&   r—   æ  s
    !zPyPIConfig.__init__c                s5   ‡  f d d †  ˆ  j  ƒ  Dƒ } t t ˆ  j | ƒ ƒ S)Nc                s.   g  |  ]$ } ˆ  j  | d  ƒ j ƒ  r | ‘ q S)rm  )r»   rz   )r\   Úsection)r†   r%   r&   r  ô  s   	 z2PyPIConfig.creds_by_repository.<locals>.<listcomp>)Úsectionsro  r   Ú_get_repo_cred)r†   Zsections_with_repositoriesr%   )r†   r&   Úcreds_by_repositoryñ  s    zPyPIConfig.creds_by_repositoryc             C   sO   |  j  | d ƒ j ƒ  } | t |  j  | d ƒ j ƒ  |  j  | d ƒ j ƒ  ƒ f S)Nrm  rg  rh  )r»   rz   rf  )r†   ru  Úrepor%   r%   r&   rw  ú  s    zPyPIConfig._get_repo_credc             C   s7   x0 |  j  j ƒ  D] \ } } | j | ƒ r | Sq Wd S)zŒ
        If the URL indicated appears to be a repository defined in this
        config, return the credential for that repository.
        N)rx  Úitemsr2   )r†   rA   rm  Úcredr%   r%   r&   Úfind_credential  s    zPyPIConfig.find_credentialN)r�   rŽ   r�   r—   Úpropertyrx  rw  r|  r%   r%   r%   r&   rl  å  s   	rl  c             C   s»  t  j j |  ƒ \ } } } } } } | j d ƒ rB t j d ƒ ‚ | d
 k rc t | ƒ \ } }	 n d } | s² t ƒ  j |  ƒ }
 |
 r² t	 |
 ƒ } |
 j
 |  f } t j d | Œ | rd t | ƒ } | |	 | | | | f } t  j j | ƒ } t  j j | ƒ } | j d | ƒ n t  j j |  ƒ } | j d	 t ƒ | | ƒ } | r·t  j j | j ƒ \ } } } } } } | | k r·| |	 k r·| | | | | | f } t  j j | ƒ | _ | S)z4Open a urllib2 request, handling HTTP authenticationr>  znonnumeric port: ''ÚhttpÚhttpsNz*Authenticating as %s for %s (from .pypirc)zBasic ÚAuthorizationz
User-Agent)r~  r  )r   r"   r>   r1   r   r(  r   rl  r|  ry   rg  r   r¸   re  r?  rª   ÚRequestÚ
add_headerÚ
user_agentrA   )rA   r©   rC   rB  rE   ÚparamsrF   rì   rD  rE  r{  r¸   rB   rë   rª   r  Ús2Úh2Úpath2Zparam2Zquery2Zfrag2r%   r%   r&   r'    s6    $'r'  c             C   s   |  S)Nr%   )rA   r%   r%   r&   Ú
fix_sf_url<  s    rˆ  c             C   s”  t  j j |  ƒ \ } } } } } } t  j j | ƒ } t j j | ƒ rX t  j j |  ƒ S| j	 d ƒ rNt j j
 | ƒ rNg  } x½ t j | ƒ D]… }	 t j j | |	 ƒ }
 |	 d k rß t |
 d ƒ � } | j ƒ  } Wd QRXPn t j j
 |
 ƒ rû |	 d 7}	 | j d j d |	 ƒ ƒ q� Wd } | j d |  d	 d
 j | ƒ ƒ } d \ } } n d \ } } } d d i } t j | ƒ } t  j j |  | | | | ƒ S)z7Read a local path, with special support for directoriesr7   z
index.htmlÚrNz<a href="{name}">{name}</a>r3   zB<html><head><title>{url}</title></head><body>{files}</body></html>rA   Úfilesr`  éÈ   ÚOKé”  úPath not foundú	Not foundzcontent-typez	text/html)r‹  rŒ  )r�  rŽ  r�  )r   r"   r>   rª   r:  rX   rE   Úisfiler«   r1   rÍ   rÏ   rb   rß   r½   rü   Úformatr   ÚStringIOr¿   rÀ   )rA   rC   rD   rE   ÚparamrF   rì   rY   rŠ  rÉ   Úfilepathr  ÚbodyrÈ   ÚstatusÚmessagerº   Zbody_streamr%   r%   r&   r&  @  s,    $!
!r&  )\r�   ÚsysrX   r[   r  r.  rb  r“   rÙ   Ú	functoolsr   Úurllib.parser   ÚImportErrorÚurllib2Zsetuptools.externr   Zsetuptools.extern.six.movesr   r   r   r   r!   Úpkg_resourcesr	   r
   r   r   r   r   r   r   r   r   r   r   r   Ú	distutilsr   Zdistutils.errorsr   Úfnmatchr   Zsetuptools.py26compatr   Zsetuptools.py27compatr   rŸ   rK   ÚIr{   ré   rL   r³   r@   rR   Ú__all__Z_SOCKET_TIMEOUTZ_tmplr‘  r  rƒ  r'   r   rH   r   rJ   rZ   r   rm   rr   rv   r„   Úobjectr…   r‘   r   rê   rY  rT  rX  r}   r_  re  rf  rq  rl  rª   r«   r'  rˆ  r&  r%   r%   r%   r&   Ú<module>   s~   "R			%	"!ÿ ÿ t&.