
 X                 @   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 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 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, m- Z- d d	 l m. Z. d d
 l/ m0 Z0 d d l1 m2 Z2 d d l3 m4 Z4 e j5 d  Z6 e j5 d e j7  Z8 e j5 d  Z9 e j5 d e j7  j: Z; d j<   Z= d d d d g Z> d Z? d d   Z@ d d   ZA d d d  ZB d d d  ZC d d d  ZD d e d d d  ZE d d  d!  ZF d" d#   ZG e j5 d$ e j7  ZH eG d% d&    ZI d' e jJ d d(  e d)  d jJ f ZK Gd* d+   d+ eL  ZM Gd, d-   d- eM  ZN Gd. d   d e  ZO e j5 d/  jP ZQ d0 d1   ZR d2 d3   ZS d4 d5   ZT d d6 d7  ZU d8 d9   ZV Gd: d;   d; eL  ZW Gd< d=   d= e- j-  ZX e jY d> d?  ZZ eU e?  eZ  ZZ d@ dA   Z[ dB dC   Z\ d S)Dz#PyPI and direct package downloading    N)wraps)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)urllib2httplibStringIO	HTTPErrorurlparse
urlunparseunquote	splituserurl2pathnamename2codepointunichrurljoinurlsplit
urlunsplitConfigParser)filterfalse)	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 } q | j d d  r |  d d  } |  d d  } d } q | j d
  r |  d d  } d } q | j d d  r |  d d  } |  d d  } d } q n  | | | 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   )NNNiiiiiiir2   i)lowerendswith
startswith)namer3   basepy_verplat r:   J/home/ubuntu/projects/ifolica/build/setuptools/setuptools/package_index.pyr(   .   s$    			c             C   s   t  |   \ } } } } } } t | j d  d  } | d k rk | d k rk t | j d  d  } n  d | k r | j d d  \ } } n  | | f S)	N/   zsourceforge.netdownload   #)r   r   split)urlschemeserverpath
parametersqueryfragmentr7   r:   r:   r;   egg_info_for_urlF   s     rK   c             c   s   t  |   \ } } x t |  | |  D] } | Vq% W| r t j |  } | r x1 t |  | j d  | d t D] } | Vqq Wq n  d S)zEYield egg or source distribution objects that might be found at a URLr=   
precedenceN)rK   distros_for_locationEGG_FRAGMENTmatchr)   groupr   )rD   metadatar7   rJ   distrO   r:   r:   r;   r'   N   s     	"c             C   s   | j  d  r" | d d  } n  | j  d  rS d | k rS t j |  | |  g S| j  d  r t |  \ } } } | d k	 r t |  | | | t |  Sn  xD 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.exer2   )r4   r   from_locationr(   r)   r   
EXTENSIONSlen)locationbasenamerQ   Zwin_baser8   platformextr:   r:   r;   rM   Z   s    rM   c             C   s"   t  t |   t j j |   |  S)zEYield possible egg or source distribution objects based on a filename)rM   r   osrG   rX   )filenamerQ   r:   r:   r;   distros_for_filenameo   s    r]   c       	      c   s   | j  d  } | sd xL t | d d   D]1 \ } } t |  d k r, | j d  r, d Sq, Wn  xm t d t |  d  D]R } t |  | d j | d |   d j | | d   d | d | d	 | Vq~ 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!
    rS   r?   N   zpy2.r=   
py_versionrL   rY   )rC   	enumeraterV   r5   ranger   join)	rW   rX   rQ   r_   rL   rY   partsipr:   r:   r;   r)   v   s    #! 5c             c   s   t    } | j } | d k rM xg t | j |   D] } | |  | Vq1 Wn; x8 |  D]0 } | |  } | | k rT | |  | VqT qT Wd S)zHList unique elements, preserving order. Remember all elements ever seen.N)setaddr"   __contains__)iterablekeyseenZseen_addelementkr:   r:   r;   unique_everseen   s    		

rn   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)rn   )argskwargs)funcr:   r;   wrapper   s    zunique_values.<locals>.wrapper)r   )rq   rr   r:   )rq   r;   unique_values   s    rs   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 x7 t	 j |  D]# } t
 |  t | j d    Vqw Wq q Wxd d D]\ } | j |  } | d	 k r t	 j | |  } | rt
 |  t | j d    Vqq q Wd S)
zEFind rel="homepage" and rel="download" links in `page`, yielding URLs,homepager>   r=   <th>Home Page<th>Download URLN)rv   rw   rA   )RELfinditergroupsrf   mapstrstripr3   rC   HREFr   
htmldecoderP   findsearch)rD   pagerO   tagrelrelsposr:   r:   r;   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.
        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   sL   t  |  d } | s t   S|  j j |  } | s< t   S|  | j     S)z5Construct a (possibly null) ContentChecker from a URLr=   rA   )r   r   patternr   	groupdict)clsrD   rJ   rO   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   re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  |  _ | r t j r | s t j   r t j |  |  _ n t j |  _ d  S)Nr<   |)r	   r   r4   	index_urlscanned_urlsfetched_urlspackage_pagesr   r   rb   r{   r#   rO   allowsto_scanr   Zis_availableZfind_ca_bundleZ
opener_foropenerr   urlopen)r   r   ZhostsZ	ca_bundleZ
verify_sslro   kwr:   r:   r;   r   	  s    !			'	!zPackageIndex.__init__Fc       
      C   sU  | |  j  k r | r d Sd |  j  | <t |  sD |  j |  d St t |   } | r |  j |  so d S|  j d |  n  | 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 rZ| j   d S| j } | j   } t | t  st | t  rd	 } n | j j d
  pd	 } | j | d  } n  | j   xB t j |  D]1 } t | t | j d    }	 |  j |	  qW| j |  j  rQt | d d  d k rQ|  j  | |  } n  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ignorer=   codei  )!r   
URL_SCHEMEprocess_filenamelistr'   url_okdebugr   r{   rg   infoopen_urlrD   headersgetr3   closeread
isinstancer|   r   	get_paramdecoder~   ry   r   r   rP   process_urlr5   r   getattrprocess_index)
r   rD   retrievedistsfr7   r   r   rO   linkr:   r:   r;   r     sN     !
		
*zPackageIndex.process_urlc             C   s   t  j j |  s& |  j d |  d  St  j j |  r | r t  j j |  } x9 t  j |  D]% } |  j t  j j | |  d  qa Wn  t	 |  } | r |  j
 d |  t t |  j |   n  d  S)NzNot found: %sTz	Found: %s)r[   rG   existswarnisdirrealpathlistdirr   rb   r]   r   r   r{   rg   )r   fnnestedrG   itemr   r:   r:   r;   r   H  s    &zPackageIndex.process_filenamec             C   s}   t  |  } | r- | j d  j   d k sF |  j t |  d  rJ d Sd } | ri t | |   n |  j | |  d  S)Nr=   fileTzN
Note: Bypassing %s (disallowed host; see http://bit.ly/1dg9ijs for details).
)r   rP   r3   r   r   r   r   )r   rD   fatalsr   r:   r:   r;   r   X  s    :zPackageIndex.url_okc             C   sf   x_ | D]W } t  j j |  r x< t  j |  D]( } | j d  r/ |  j | |  q/ q/ Wq q Wd  S)Nz	.egg-link)r[   rG   r   r   r4   scan_egg_link)r   search_pathr   entryr:   r:   r;   scan_egg_linksc  s
    zPackageIndex.scan_egg_linksc             C   s   d d   t  t j t t j j | |    D } t |  d k r xX t t j j | | d   D]4 } t j j | |  | _	 t
 | _ |  j |  qf Wn  d  S)Nc             S   s   g  |  ] } | r |  q Sr:   r:   ).0Z_fr:   r:   r;   
<listcomp>k  s   	 z.PackageIndex.scan_egg_link.<locals>.<listcomp>r?   r   )r{   r|   r}   openr[   rG   rb   rV   r
   rW   r   rL   rg   )r   rG   r   linesrR   r:   r:   r;   r   j  s    "&	zPackageIndex.scan_egg_linkc       
         s    f d d   } xR t  j |  D]A } y& | t | t | j d     Wq" t k
 rb Yq" Xq" W| |  \ } } | rxy t | |  D]h } t |  \ } }	 | j d  r |	 r | r | d | | f 7} q   j	 |  n    j
 |  q Wt j d d   |  Sd Sd	 S)
z#Process the contents of a PyPI pagec                s   |  j    j  r t t t |  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 Sn  d S)Nr<   r?   r@   r=   r   T)NN)r5   r   r   r{   r   rV   rC   r   r   r   
setdefaultr3   r   )r   rc   pkgver)r   r:   r;   scanu  s    +"z(PackageIndex.process_index.<locals>.scanr=   z.pyz
#egg=%s-%sc             S   s   d |  j  d d d  S)Nz<a href="%s#md5=%s">%s</a>r=   r   r?   )rP   )mr:   r:   r;   <lambda>  s    z,PackageIndex.process_index.<locals>.<lambda>r   N)r~   ry   r   r   rP   
ValueErrorr   rK   r4   need_version_infoscan_urlPYPI_MD5sub)
r   rD   r   r   rO   r   r   new_urlr7   fragr:   )r   r;   r   s  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   rD   r:   r:   r;   r     s    zPackageIndex.need_version_infoc             G   sO   |  j  |  j k r; | r+ |  j | |  n  |  j d  n  |  j |  j   d  S)Nz6Scanning index of all packages (this may take a while))r   r   r   r   r   )r   r   ro   r:   r:   r;   r     s     
zPackageIndex.scan_allc             C   s   |  j  |  j | j d  |  j j | j  sN |  j  |  j | j d  n  |  j j | j  ss |  j |  n  x3 t |  j j | j f    D] } |  j  |  q Wd  S)Nr<   )	r   r   unsafe_namer   r   rj   project_namenot_found_in_indexr   )r   requirementrD   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   rj   r   superr&   obtain)r   r   	installerrR   )	__class__r:   r;   r     s    
zPackageIndex.obtainc             C   si   | j  |  j d |  | j   se | j   t j |  t d | j j t j	 j
 |  f   n  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   r[   unlinkr   r   r6   rG   rX   )r   checkerr\   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   r5   r   r'   r   append)r   urlsrD   r:   r:   r;   add_find_links  s    zPackageIndex.add_find_linksc             C   s2   |  j  r% t t |  j |  j    n  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?))rj   r   r   r   r   )r   r   methr   r:   r:   r;   r     s    
zPackageIndex.not_found_in_indexc             C   s   t  | t  s t |  } | r| |  j | j d  | |  } t |  \ } } | j d  rx |  j | | |  } n  | St j	 j
 |  r | Sy t j |  } Wq t k
 r t d | f   Yq Xn  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.
        r=   z.pyz1Not a URL, existing file, or requirement spec: %rrW   N)r   r   r   _download_urlrP   rK   r4   	gen_setupr[   rG   r   parser   r   r   fetch_distribution)r   spectmpdirrE   foundr7   rJ   r:   r:   r;   r>     s"    zPackageIndex.downloadc       	         sQ   j  d |  i   d } d      f d d  } | rf  j    j |  | |  } n  | d k	 r | p | | |  } n  | d k r  j d k	 r  j   n  | |  } n  | d k r | r  j |  | |  } n  | 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  } n  x | |  j  D]q } | j t k rn   rn |  k r#  j d |  d  | <q# q# n  | |  k r# | j t k s  r# | Sq# Wd  S)Nz&Skipping development or system egg: %sr=   )rj   rL   r   r   r   )reqenvrR   )
develop_okr   skippedsourcer:   r;   r   1  s    	"z-PackageIndex.fetch_distribution.<locals>.findz2No local packages or download links found for %s%sza source distribution of r   zBest match: %srW   )r   r   r   r   r   cloner>   rW   )	r   r   r  
force_scanr
  r  Zlocal_indexrR   r   r:   )r  r   r	  r
  r;   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  rW   )r   r   r  r  r
  rR   r:   r:   r;   fetch\  s    zPackageIndex.fetchc       
      C   sb  t  j |  } | r= d d   t | | j d  d   D p@ g  } t |  d k r3t j j |  } t j j |  | k r t j j	 | |  } d d l
 m } | | |  s t j | |  | } q n  t t j j	 | d  d  }	 |	 j d | d j | d j t j j |  d f  |	 j   | S| rRt d	 | | f   n t d
   d  S)Nc             S   s   g  |  ] } | j  r |  q Sr:   )version)r   dr:   r:   r;   r   l  s   	 z*PackageIndex.gen_setup.<locals>.<listcomp>r=   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.)rN   rO   r)   rP   rV   r[   rG   rX   dirnamerb   Zsetuptools.command.easy_installr  shutilcopy2r   writer   r  splitextr   r   )
r   r\   rJ   r  rO   r   rX   dstr  r   r:   r:   r;   r   i  s4    	
zPackageIndex.gen_setupi    c             C   s  |  j  d |  d \ } } } zIt j |  } |  j t |   } t | t  rw t d | | j | j	 f   n  | j    } d } |  j
 }	 d	 }
 d | k r t | d  } t t t |   }
 |  j | | | |	 |
  n  t | d  } xZ | j |	  } | rK| j |  | j |  | d 7} |  j | | | |	 |
  q Pq W|  j | | |  | SWd  | r~| j   n  | r| j   n  Xd  S)
NzDownloading %szCan't download %s: %s %sr   r=   zcontent-lengthzContent-Lengthwb)NNNrA   )r   r   r   r   r$   r   r   r   r   r   dl_blocksizer%   maxr{   int
reporthookr   r   r   r  r   r   )r   rD   r\   fpr   r   r   r   blocknumbssizeZsizesr   r:   r:   r;   _download_to  s>    	
  zPackageIndex._download_toc             C   s   d  S)Nr:   )r   rD   r\   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 t j   d } d j	 d d   | j
 D  } | r |  j | |  n t d | | f   Yn.t j k
 r t j   d } | SYnt j k
 r)t j   d } | r|  j | | j  n t d | | j f   Yn t j k
 rt j   d } | re|  j | | j  n t d | | j f   YnT t j k
 rt j   d } | r|  j | |  n t d | | f   Yn Xd  S)	Nzfile:r=    c             S   s   g  |  ] } t  |   q Sr:   )r|   )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)r5   
local_openopen_with_authr   r   r   
InvalidURLsysexc_inforb   ro   r   r   r   r   URLErrorreasonBadStatusLinelineHTTPException)r   rD   warningvr   r:   r:   r;   r     s@    
zPackageIndex.open_urlc             C   sB  t  |  \ } } | rL x7 d | k rH | j d d  j d d  } q Wn d } | j d  rt | d  d  } n  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
 t |  d  S|  j | d  |  j | |  Sd  S)Nz...\_Z__downloaded__z.egg.zipr/   svnzsvn+gitzgit+zhg+r   r?   Tr2   )rK   replacer4   r[   rG   rb   r5   _download_svn_download_git_download_hgr   r   r   _attempt_download)r   rE   rD   r  r6   rJ   r\   r:   r:   r;   r     s$    %zPackageIndex._download_urlc             C   s   |  j  | d  d  S)NT)r   )r   rD   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   r3   _download_html)r   rD   r\   r   r:   r:   r;   r9    s    zPackageIndex._attempt_downloadc             C   s   t  |  } xW | D]O } | j   r t j d |  r^ | j   t j |  |  j | |  SPq q W| j   t j |  t d |   d  S)Nz <title>([^- ]+ - )?Revision \d+:zUnexpected HTML page found at )	r   r}   r   r   r   r[   r   r6  r   )r   rD   r   r\   r   r,  r:   r:   r;   r:  
  s    

zPackageIndex._download_htmlc             C   s`  | j  d d  d } d } | j   j d  r/d | k r/t |  \ } } } } } }	 | r/| j d  r/d | d	 d   k r/| d	 d   j  d d  \ } } t |  \ }
 } |
 r,d
 |
 k r |
 j  d
 d  \ } } d | | f } n
 d |
 } | } t | | | | | |	 f  } q,q/n  |  j d | |  t j d | | | f  | S)Nr@   r=   r   r   zsvn:@z//r<   r?   :z --username=%s --password=%sz --username=z'Doing subversion checkout from %s to %szsvn checkout%s -q %s %s)	rC   r3   r5   r   r   r   r   r[   system)r   rD   r\   credsrE   netlocrG   re   qr   authhostuserpwr:   r:   r;   r6    s"    !,"
'zPackageIndex._download_svnc             C   s   t  |   \ } } } } } | j d d  d } | j d d  d } d  } d | k rt | j d d  \ } } n  t | | | | d f  }  |  | f S)N+r=   r@   r   r;  r   rA   )r   rC   rsplitr    )rD   
pop_prefixrE   r?  rG   rI   r   revr:   r:   r;   _vcs_split_rev_from_url-  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  n  | S)
Nr@   r=   r   rG  TzDoing git clone from %s to %szgit clone --quiet %s %szChecking out %sz"(cd %s && git checkout --quiet %s))rC   rI  r   r[   r=  )r   rD   r\   rH  r:   r:   r;   r7  ?  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  n  | S)
Nr@   r=   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 >&-))rC   rI  r   r[   r=  )r   rD   r\   rH  r:   r:   r;   r8  O  s    	zPackageIndex._download_hgc             G   s   t  j | |  d  S)N)r   r   )r   r   ro   r:   r:   r;   r   _  s    zPackageIndex.debugc             G   s   t  j | |  d  S)N)r   r   )r   r   ro   r:   r:   r;   r   b  s    zPackageIndex.infoc             G   s   t  j | |  d  S)N)r   r   )r   r   ro   r:   r:   r;   r   e  s    zPackageIndex.warn)r   )'r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r>   r  r  r   r  r!  r  r   r   r   r9  r:  r6  staticmethodrI  r7  r8  r   r   r   r:   r:   )r   r;   r&     sL   0	*		)B*$(z!&(#(\d+|x[\da-fA-F]+)|[\w.:-]+);?c             C   s3   t  |  t  s |  S|  d k r) t |   St |   S)N   )r   r  r   chr)cr:   r:   r;   uchrl  s
     
rN  c             C   s   |  j  d  } | j d  r: t | d d   d  } nC | j d  rb t | d d    } n t j | |  j  d   } t |  S)Nr=   z#xr?   r-   r@   r   )rP   r5   r  r   r   rN  )rO   whatr:   r:   r;   decode_entityr  s    rP  c             C   s   t  t |   S)z'Decode HTML entities in the given text.)
entity_subrP  )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)socketgetdefaulttimeoutsetdefaulttimeout)ro   rp   Zold_timeout)rq   timeoutr:   r;   _socket_timeout  s
    z@socket_timeout.<locals>._socket_timeout.<locals>._socket_timeoutr:   )rq   rW  )rV  )rq   r;   rW    s    z'socket_timeout.<locals>._socket_timeoutr:   )rV  rW  r:   )rV  r;   socket_timeout  s    	rX  c             C   sC   t  |   } | 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   encodebase64encodestringr   r5  )rA  Zauth_sZ
auth_bytesZencoded_bytesencodedr:   r:   r;   _encode_auth  s
    r^  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   r`  ra  r:   r:   r;   r     s    	zCredential.__init__c             c   s   |  j  V|  j Vd  S)N)r`  ra  )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   rb  rd  r:   r:   r:   r;   r_    s   r_  c               @   sF   e  Z d  Z d d   Z e d d    Z d d   Z d d   Z d	 S)

PyPIConfigc             C   su   t  j d d d g d  } t j j |  |  t j j t j j d  d  } t j j |  rq |  j	 |  n  d S)z%
        Load from ~/.pypirc
        r`  ra  
repositoryr   ~z.pypircN)
dictfromkeysr!   r   r[   rG   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)rf  )r   r}   )r   section)r   r:   r;   r     s   	 z2PyPIConfig.creds_by_repository.<locals>.<listcomp>)sectionsrh  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)Nrf  r`  ra  )r   r}   r_  )r   rm  repor:   r:   r;   ro    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)rp  itemsr5   )r   rD   rf  credr:   r:   r;   find_credential  s    zPyPIConfig.find_credentialN)r   r   r   r   propertyrp  ro  rt  r:   r:   r:   r;   re    s   	re  c             C   s  t  |   \ } } } } } } | j d  r? t j d   n  | d
 k r` t |  \ } }	 n d } | s t   j |   }
 |
 r t |
  } |
 j |  f } t	 j
 d |  q n  | rd t |  } t | |	 | | | | f  } t j |  } | j d |  n t j |   } | j d	 t  | |  } | rt  | j  \ } } } } } } | | k r| |	 k rt | | | | | | f  | _ qn  | 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)zhttpzhttps)r   r4   r   r&  r   re  rt  r|   r`  r   r   r^  r   r   Request
add_header
user_agentrD   )rD   r   rE   r?  rG   paramsrI   r   rA  rB  rs  r   r   requestr  s2h2Zpath2Zparam2Zquery2Zfrag2r:   r:   r;   r%    s2    !'r%  c             C   s   |  S)Nr:   )rD   r:   r:   r;   
fix_sf_url  s    r  c             C   su  t  |   \ } } } } } } t |  } t j j |  rI t j |   S| j d  r=t j j |  r=g  } x t j	 |  D] }	 |	 d k r t
 t j j | |	  d  }
 |
 j   } |
 j   Pn. t j j t j j | |	   r |	 d 7}	 n  | j d |	 |	 f  q Wd |  d d j |  } d \ } } n d \ } } } i d d 6} t |  | | | t |   S)z7Read a local path, with special support for directoriesr<   z
index.htmlrz<a href=%r>%s</a>z<html><head><title>%s</title>z</head><body>%s</body></html>rY     OK  Path not found	Not foundz	text/htmlzcontent-type)r  zOK)r  r  r  )r   r   r[   rG   isfiler   r   r4   r   r   r   rb   r   r   r   r   r   )rD   rE   rF   rG   paramrI   r   r\   filesr   r  bodystatusmessager   r:   r:   r;   r$  	  s*    !
!r$  )]r   r'  r[   r   r  rS  r[  r   	functoolsr   pkg_resourcesr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   	distutilsr   distutils.errorsr   Zsetuptools.compatr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   fnmatchr#   Zsetuptools.py26compatr$   Zsetuptools.py27compatr%   r   rN   Ir~   r   rO   r   rC   rU   __all__Z_SOCKET_TIMEOUTr(   rK   r'   rM   r]   r)   rn   rs   rx   r   r  r{  objectr   r   r&   r   rQ  rN  rP  r   rX  r^  r_  re  r   r%  r  r$  r:   r:   r:   r;   <module>   sp   Xd			"
'!  f
'+