
`@_B                 @   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 m Z 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 j0 d  Z1 e j0 d e j2  Z3 e j0 d  Z4 e j0 d e j2  j5 Z6 d j7   Z8 d d d d g Z9 d Z: d d   Z; d d   Z< d d d  Z= d d d  Z> d d d  Z? d e d d  d  Z@ d d! d"  ZA d# d$   ZB e j0 d% e j2  ZC eB d& d'    ZD d( e jE d d)  e d*  d jE f ZF Gd+ d,   d, eG  ZH Gd- d.   d. eH  ZI Gd/ d   d e  ZJ e j0 d0  jK ZL d1 d2   ZM d3 d4   ZN d5 d6   ZO d d7 d8  ZP d9 d:   ZQ Gd; d<   d< eG  ZR Gd= d>   d> e jS  ZT e jU jV d? d@  ZW eP e:  eW  ZW dA dB   ZX dC dD   ZY d S)Ez#PyPI and direct package downloading    N)wraps)	splituser)six)urllibhttp_clientconfigparsermap)CHECKOUT_DISTDistributionBINARY_DISTnormalize_pathSOURCE_DISTrequire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   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   )NNNiiiiiiir(   i)lowerendswith
startswith)namer)   basepy_verplat r0   >/tmp/pip-build-tw3_dw7d/setuptools/setuptools/package_index.pyr   1   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   parseurlparseunquotesplit)	urlpartsschemeserverpathZ
parametersqueryfragmentr-   r0   r0   r1   egg_info_for_urlI   s     rD   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 URLr3   
precedenceN)rD   distros_for_locationEGG_FRAGMENTmatchr   groupr	   )r=   metadatar-   rC   distrH   r0   r0   r1   r   R   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(   )r*   r
   from_locationr   r   r   
EXTENSIONSlen)locationbasenamerJ   Zwin_baser.   platformextr0   r0   r1   rF   ^   s    rF   c             C   s"   t  t |   t j j |   |  S)zEYield possible egg or source distribution objects based on a filename)rF   r   osrA   rQ   )filenamerJ   r0   r0   r1   distros_for_filenames   s    rV   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)
zGenerate 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!
    rL   c             s   s!   |  ] } t  j d  |  Vq d S)z	py\d\.\d$N)rerH   ).0pr0   r0   r1   	<genexpr>   s    z(interpret_distro_name.<locals>.<genexpr>r5   Nr3   
py_versionrE   rR   )r<   anyrangerO   r
   join)rP   rQ   rJ   r[   rE   rR   r>   rY   r0   r0   r1   r   z   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kr0   r0   r1   unique_everseen   s    		

ri   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)ri   )argskwargs)funcr0   r1   wrapper   s    zunique_values.<locals>.wrapper)r   )rl   rm   r0   )rl   r1   unique_values   s    rn   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homepager4   r3   <th>Home Page<th>Download URLN)rp   rq   r7   )RELfinditergroupsr_   r   strstripr)   r<   HREFr   r9   urljoin
htmldecoderI   findsearch)r=   pagerH   tagrelZrelsposr0   r0   r1   find_external_links   s    '+r   zPython-urllib/%s setuptools/%s   
setuptoolsc               @   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.
        Nr0   )selfblockr0   r0   r1   feed   s    zContentChecker.feedc             C   s   d S)zC
        Check the hash. Return False if validation fails.
        Tr0   )r   r0   r0   r1   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.
        Nr0   )r   reportertemplater0   r0   r1   report   s    zContentChecker.reportN)__name__
__module____qualname____doc__r   r   r   r0   r0   r0   r1   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   r0   r0   r1   __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 URLr3   r7   )r   r9   r:   r   patternr{   	groupdict)clsr=   rC   rH   r0   r0   r1   from_url   s    zHashChecker.from_urlc             C   s   |  j  j |  d  S)N)r   update)r   r   r0   r0   r1   r      s    zHashChecker.feedc             C   s   |  j  j   |  j k S)N)r   	hexdigestr   )r   r0   r0   r1   r     s    zHashChecker.is_validc             C   s   | |  j  } | |  S)N)r   )r   r   r   msgr0   r0   r1   r     s    zHashChecker.reportN)r   r   r   rW   compiler   r   classmethodr   r   r   r   r0   r0   r0   r1   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  |  _ | r t j r | s t j   r t j |  |  _ n t j j |  _ d  S)Nr2   |)r   r   r*   	index_urlscanned_urlsfetched_urlspackage_pagesrW   r   r^   r   r   rH   allowsto_scanr   Zis_availableZfind_ca_bundleZ
opener_foropenerr   requesturlopen)r   r   hostsZ	ca_bundleZ
verify_sslrj   kwr0   r0   r1   r     s    !			'	!zPackageIndex.__init__Fc       
      C   sX  | |  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 | <|  j | d |  } | d k rd Sd |  j | j <d | j j d d  j   k rW| 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!  rTt" | d d  d k rT|  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ignorer3   codei  )$r   
URL_SCHEMEprocess_filenamelistr   url_okdebugr   r   r`   infoopen_urlr=   headersgetr)   closeread
isinstanceru   r   error	HTTPError	get_paramdecoderw   rs   r9   rx   ry   rI   process_urlr+   r   getattrprocess_index)
r   r=   retrievedistsfr-   r|   r   rH   linkr0   r0   r1   r     sN     !
		
$*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)rT   rA   existswarnisdirrealpathlistdirr   r^   rV   r   r   r   r`   )r   fnnestedrA   itemr   r0   r0   r1   r   K  s    #zPackageIndex.process_filenamec             C   s   t  |  } | r- | j d  j   d k sL |  j t j j |  d  rP d Sd } | ro t | |   n |  j | |  d  S)Nr3   fileTzN
Note: Bypassing %s (disallowed host; see http://bit.ly/1dg9ijs for details).
)	r   rI   r)   r   r   r9   r:   r   r   )r   r=   fatalsr   r0   r0   r1   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)rT   r   r*   )rX   rA   entryr0   r0   r1   rZ   i  s   z.PackageIndex.scan_egg_links.<locals>.<genexpr>)filterrT   rA   r   r   	itertoolsstarmapscan_egg_link)r   search_pathdirsZ	egg_linksr0   r0   r1   scan_egg_linksf  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)Nr5   )openrT   rA   r^   r   r   r   ru   rv   rO   r   rP   r   rE   r`   )r   rA   r   Z	raw_lineslinesZegg_pathZ
setup_pathrK   r0   r0   r1   r   p  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)Nr2   r5   r6   r3   r   T)NN)r+   r   r   r   r   r9   r;   rO   r<   r   r   r   
setdefaultr)   r   )r   r>   pkgver)r   r0   r1   scan  s    1"z(PackageIndex.process_index.<locals>.scanr3   z.pyz
#egg=%s-%sc             S   s   d |  j  d d d  S)Nz<a href="%s#md5=%s">%s</a>r3   r   r5   )rI   )mr0   r0   r1   <lambda>  s    z,PackageIndex.process_index.<locals>.<lambda>r   N)rw   rs   r   r9   rx   ry   rI   
ValueErrorr   rD   r*   need_version_infoscan_urlPYPI_MD5sub)
r   r=   r|   r   rH   r   r   new_urlr-   fragr0   )r   r1   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   r=   r0   r0   r1   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   rj   r0   r0   r1   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)Nr2   )	r   r   unsafe_namer   r   re   project_namenot_found_in_indexr   )r   requirementr=   r0   r0   r1   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   re   r   superr   obtain)r   r   	installerrK   )	__class__r0   r1   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   rT   unlinkr   r   r,   rA   rQ   )r   checkerrU   tfpr0   r0   r1   
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   r+   r   r   r   append)r   urlsr=   r0   r0   r1   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   r0   r0   r1   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?))re   r   r   r   r   )r   r   methr   r0   r0   r1   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 | Sy t j |  } Wn% t k
 r t d | f   Yn Xt |  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.
        r3   z.pyz1Not a URL, existing file, or requirement spec: %rrP   N)r   r   r   _download_urlrI   rD   r*   	gen_setuprT   rA   r   r9   r   r   r   fetch_distribution)r   spectmpdirr?   foundr-   rC   r0   r0   r1   r4     s"    zPackageIndex.downloadc       	         sB   j  d |  i   d } d      f d d  } | rc  j    j |  | |  } | d k	 r | p | | |  } | 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 | 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  } xz | |  j  D]k } | j t k re   re |  k r   j d |  d  | <q  | |  k r  | j t k s  r  | Sq  Wd  S)Nz&Skipping development or system egg: %sr3   )re   rE   r   r   r   )reqenvrK   )
develop_okr   skippedsourcer0   r1   rz   >  s    
"z-PackageIndex.fetch_distribution.<locals>.findz2No local packages or download links found for %s%sza source distribution of r   zBest match: %srP   )r   r   r   r   r   cloner4   rP   )	r   r   r  
force_scanr  r  Zlocal_indexrK   rz   r0   )r  r   r  r  r1   r   %  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   rP   )r   r   r  r	  r  rK   r0   r0   r1   fetchi  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 Sr0   )version)rX   dr0   r0   r1   
<listcomp>y  s   	 z*PackageIndex.gen_setup.<locals>.<listcomp>r3   r   )samefilezsetup.pywzIfrom setuptools import setup
setup(name=%r, version=%r, py_modules=[%r])
zCan'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.)rG   rH   r   rI   rO   rT   rA   rQ   dirnamer^   Zsetuptools.command.easy_installr  shutilcopy2r   writer   r  splitextr   )
r   rU   rC   r  rH   r   rQ   dstr  r   r0   r0   r1   r   v  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   r3   zcontent-lengthzContent-Lengthwb)NNr7   )r   r   r   r   r   r   r   r   r   r   r   r   dl_blocksizer   maxr   int
reporthookr   r   r   r  r   r   )r   r=   rU   fpr   r   r   blocknumbssizeZsizesr   r   r0   r0   r1   _download_to  s:    	
 zPackageIndex._download_toc             C   s   d  S)Nr0   )r   r=   rU   r  Zblksizer  r0   r0   r1   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  } ~ Xn@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  } ~ XnW t j k
 r} z4 | r|  j	 | |  n t
 d | | f   WYd  d  } ~ Xn Xd  S)Nzfile: c             S   s   g  |  ] } t  |   q Sr0   )ru   )rX   argr0   r0   r1   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)r+   
local_openopen_with_authr   r   r   
InvalidURLr^   rj   r   r   r   r   r   URLErrorreasonBadStatusLinelineHTTPException)r   r=   warningvr   r0   r0   r1   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   r5   Tr(   )rD   replacer*   rT   rA   r^   r+   _download_svn_download_git_download_hgr   r   url2pathnamer9   r:   r   _attempt_download)r   r?   r=   r  r,   rC   rU   r0   r0   r1   r     s$    % zPackageIndex._download_urlc             C   s   |  j  | d  d  S)NT)r   )r   r=   r0   r0   r1   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   r)   _download_html)r   r=   rU   r   r0   r0   r1   r6  	  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   rv   rW   r{   r   rT   r   r2  r   )r   r=   r   rU   r   r(  r0   r0   r1   r7    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)Nr6   r3   r   r   zsvn:@z//r2   r5   :z --username=%s --password=%sz --username=z'Doing subversion checkout from %s to %szsvn checkout%s -q %s %s)r<   r)   r+   r   r9   r:   r   
urlunparser   rT   system)r   r=   rU   credsr?   netlocrA   rY   qr   authhostuserpwr>   r0   r0   r1   r2    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+r3   r6   r   r8  r   r7   )r   r9   urlsplitr<   rsplit
urlunsplit)r=   
pop_prefixr?   r=  rA   rB   r   revr0   r0   r1   _vcs_split_rev_from_url4  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)
Nr6   r3   r   rG  TzDoing git clone from %s to %szgit clone --quiet %s %szChecking out %sz"(cd %s && git checkout --quiet %s))r<   rI  r   rT   r;  )r   r=   rU   rH  r0   r0   r1   r3  F  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)
Nr6   r3   r   rG  TzDoing hg clone from %s to %szhg clone --quiet %s %szUpdating to %sz(cd %s && hg up -C -r %s >&-))r<   rI  r   rT   r;  )r   r=   rU   rH  r0   r0   r1   r4  V  s    	zPackageIndex._download_hgc             G   s   t  j | |  d  S)N)r   r   )r   r   rj   r0   r0   r1   r   f  s    zPackageIndex.debugc             G   s   t  j | |  d  S)N)r   r   )r   r   rj   r0   r0   r1   r   i  s    zPackageIndex.infoc             G   s   t  j | |  d  S)N)r   r   )r   r   rj   r0   r0   r1   r   l  s    zPackageIndex.warn)r   )'r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r4   r   r
  r   r  r  r  r   r   r   r6  r7  r2  staticmethodrI  r3  r4  r   r   r   r0   r0   )r   r1   r   	  sL   0
*		)B)##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)cr0   r0   r1   uchrs  s
     rO  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)Nr3   z#xr5   r#   r6   r   )	rI   r+   r  r   ra   html_entitiesname2codepointr   rO  )rH   whatr0   r0   r1   decode_entityy  s    $rS  c             C   s   t  t |   S)z'Decode HTML entities in the given text.)
entity_subrS  )textr0   r0   r1   ry     s    ry   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)socketgetdefaulttimeoutsetdefaulttimeout)rj   rk   Zold_timeout)rl   timeoutr0   r1   _socket_timeout  s
    z@socket_timeout.<locals>._socket_timeout.<locals>._socket_timeoutr0   )rl   rZ  )rY  )rl   r1   rZ    s    z'socket_timeout.<locals>._socket_timeoutr0   )rY  rZ  r0   )rY  r1   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   r9   r;   encodebase64encodestringr   r1  )r?  Zauth_sZ
auth_bytesZencoded_bytesencodedr0   r0   r1   _encode_auth  s
    ra  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   rc  rd  r0   r0   r1   r     s    	zCredential.__init__c             c   s   |  j  V|  j Vd  S)N)rc  rd  )r   r0   r0   r1   __iter__  s    zCredential.__iter__c             C   s   d t  |   S)Nz%(username)s:%(password)s)vars)r   r0   r0   r1   __str__  s    zCredential.__str__N)r   r   r   r   r   re  rg  r0   r0   r0   r1   rb    s   rb  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
        rc  rd  
repositoryr   ~z.pypircN)dictfromkeysr   RawConfigParserr   rT   rA   r^   
expanduserr   r   )r   defaultsrcr0   r0   r1   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)ri  )r   rv   )rX   section)r   r0   r1   r    s   	 z2PyPIConfig.creds_by_repository.<locals>.<listcomp>)sectionsrk  r   _get_repo_cred)r   Zsections_with_repositoriesr0   )r   r1   creds_by_repository  s    zPyPIConfig.creds_by_repositoryc             C   sO   |  j  | d  j   } | t |  j  | d  j   |  j  | d  j    f S)Nri  rc  rd  )r   rv   rb  )r   rq  repor0   r0   r1   rs    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)rt  itemsr+   )r   r=   ri  credr0   r0   r1   find_credential  s    zPyPIConfig.find_credentialN)r   r   r   r   propertyrt  rs  rx  r0   r0   r0   r1   rh    s   	rh  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 authenticationr9  znonnumeric port: ''httphttpsNz*Authenticating as %s for %s (from .pypirc)zBasic Authorizationz
User-Agent)zhttpzhttps)r   r9   r:   r*   r   r$  r   rh  rx  ru   rc  r   r   ra  r:  r   Request
add_header
user_agentr=   )r=   r   r?   r=  rA   paramsrB   r   r?  r@  rw  r   r>   r   r   r  s2h2path2Zparam2Zquery2Zfrag2r0   r0   r1   r#    s6    $'r#  c             C   s   |  S)Nr0   )r=   r0   r0   r1   
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 directoriesr2   z
index.htmlrNz<a href="{name}">{name}</a>r,   zB<html><head><title>{url}</title></head><body>{files}</body></html>r=   filesr\     OK  Path not found	Not foundzcontent-typez	text/html)r  zOK)r  r  r  )r   r9   r:   r   r5  rT   rA   isfiler   r*   r   r   r^   r   r   r   formatr   StringIOr   r   )r=   r?   r@   rA   paramrB   r   rU   r  r   filepathr  bodytmplstatusmessager   Zbody_streamr0   r0   r1   r"    s,    $!
!r"  )Zr   sysrT   rW   r  rV  r^  r   r   	functoolsr   urllib.parser   ImportErrorurllib2Zsetuptools.externr   Zsetuptools.extern.six.movesr   r   r   r   pkg_resourcesr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   	distutilsr   Zdistutils.errorsr   fnmatchr   Zsetuptools.py26compatr   Zsetuptools.py27compatr   r   rG   Irw   r   rH   r   r<   rN   __all__Z_SOCKET_TIMEOUTr   rD   r   rF   rV   r   ri   rn   rr   r   r  r  objectr   r   r   r   rT  rO  rS  ry   r[  ra  rb  rm  rh  r   r   r#  r  r"  r0   r0   r0   r1   <module>   sz   "X				!
'!  j
'-