B
    e¦h\nŸ  ã               @   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	Z	ddl
mZ d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 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, ddl-m.Z. e/Z0e 1d¡Z2e 1dej3¡Z4e 1d¡Z5e 1dej3¡j6Z7d 8¡ Z9ddddgZ:dZ;dZ<e<j=ej>dd… edZ?dd„ Z@dd„ ZAdd„ ZBdGd d„ZCdHd!d"„ZDdId#d$„ZEdedfd%d„ZFdJd&d'„ZGd(d)„ ZHe 1d*ej3¡ZIeHd+d,„ ƒZJG d-d.„ d.ƒZKG d/d0„ d0eKƒZLG d1d„ deƒZMe 1d2¡jNZOd3d4„ ZPd5d6„ ZQdKd7d8„ZRd9d:„ ZSG d;d<„ d<ƒZTG d=d>„ d>ejUƒZVejWjXfd?d@„ZYdAdB„ ZZeRe;ƒeYƒZYdCdD„ Z[dEdF„ Z\dS )Lz#PyPI and direct package downloadingé    N)Úwraps)Ú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ÚEGG_DIST)Ússl_support)Úlog)ÚDistutilsError)Ú	translate)Úget_all_headers)Úunescape)ÚWheelz^egg=([-A-Za-z0-9_.+!]+)$zhref\s*=\s*['"]?([^'"> ]+)zƒ<a href="([^"#]+)">([^<]+)</a>\n\s+\(<a (?:title="MD5 hash"\n\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   s2   y
t  | ¡S  tk
r,   td| f ƒ‚Y nX d S )Nz1Not a URL, existing file, or requirement spec: %r)r   ÚparseÚ
ValueErrorr   )Úspec© r&   ú7lib/python3.7/site-packages/setuptools/package_index.pyÚparse_requirement_arg4   s
    
r(   c             C   s°   |   ¡ }d\}}}| d¡r¦| d¡r8| dd… }d}nn| dd¡rb| d	d
… }| dd… }d}nD| d¡r~| dd… }d}n(| dd¡r¦| d	d
… }| dd… }d}|||fS )z=Return (base,pyversion) or (None,None) for possible .exe name)NNNz.exez
.win32.exeNiöÿÿÿZwin32z	.win32-pyiðÿÿÿiùÿÿÿéüÿÿÿz.win-amd64.exeiòÿÿÿz	win-amd64z.win-amd64-pyiìÿÿÿ)ÚlowerÚendswithÚ
startswith)Únamer*   ÚbaseÚpy_verZplatr&   r&   r'   r   =   s$    



c       	      C   sx   t j | ¡}|\}}}}}}t j | d¡d ¡}|dkrX|dkrXt j | d¡d ¡}d|krp| dd¡\}}||fS )Nú/éÿÿÿÿzsourceforge.netÚdownloadéþÿÿÿú#é   )r   r#   ÚurlparseÚunquoteÚsplit)	ÚurlÚpartsÚschemeÚserverÚpathZ
parametersÚqueryÚfragmentr.   r&   r&   r'   Úegg_info_for_urlU   s    r@   c             c   sd   t | ƒ\}}xt| ||ƒD ]
}|V  qW |r`t |¡}|r`x$t| | d¡|tdD ]
}|V  qRW dS )zEYield egg or source distribution objects that might be found at a URLr5   )Ú
precedenceN)r@   Údistros_for_locationÚEGG_FRAGMENTÚmatchr   Úgroupr   )r9   Úmetadatar.   r?   ÚdistrD   r&   r&   r'   r   `   s    

c             C   sä   |  d¡r|dd… }|  d¡r8d|kr8t | ||¡gS |  d¡rxd|krxt|ƒ}| ¡ s^g S t| |j|jtd dgS |  d	¡rªt|ƒ\}}}|dk	rªt	| |||t
|ƒS x4tD ],}|  |¡r°|dt|ƒ … }t	| ||ƒS q°W g S )
z:Yield egg or source distribution objects based on basenamez.egg.zipNr)   z.eggú-z.whlr5   )ÚlocationÚproject_nameÚversionrA   z.exe)r+   r	   Zfrom_locationr   Zis_compatiblerJ   rK   r   r   r   r
   Ú
EXTENSIONSÚlen)rI   ÚbasenamerF   ZwheelZwin_baser/   ÚplatformZextr&   r&   r'   rB   n   s.    



rB   c             C   s   t t| ƒtj | ¡|ƒS )zEYield possible egg or source distribution objects based on a filename)rB   r   Úosr=   rN   )ÚfilenamerF   r&   r&   r'   Údistros_for_filename   s    rR   c          
   c   s€   |  d¡}|s,tdd„ |dd… D ƒƒr,dS xNtdt|ƒd ƒD ]8}t| |d |d|… ¡d ||d… ¡|||dV  q@W d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!
    rH   c             s   s   | ]}t  d |¡V  qdS )z	py\d\.\d$N)ÚrerD   )Ú.0Úpr&   r&   r'   ú	<genexpr>­   s    z(interpret_distro_name.<locals>.<genexpr>é   Nr5   )Ú
py_versionrA   rO   )r8   ÚanyÚrangerM   r	   Újoin)rI   rN   rF   rX   rA   rO   r:   rU   r&   r&   r'   r   –   s    
$c             c   sn   t ƒ }|j}|dkr>xTtj |j| ¡D ]}||ƒ |V  q&W n,x*| D ]"}||ƒ}||krD||ƒ |V  qDW dS )zHList unique elements, preserving order. Remember all elements ever seen.N)ÚsetÚaddr   ZmovesÚfilterfalseÚ__contains__)ÚiterableÚkeyÚseenZseen_addÚelementÚkr&   r&   r'   Úunique_everseenº   s    
re   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)re   )ÚargsÚkwargs)Úfuncr&   r'   ÚwrapperÒ   s    zunique_values.<locals>.wrapper)r   )rh   ri   r&   )rh   r'   Úunique_valuesÌ   s    rj   z(<([^>]*\srel\s*=\s*['"]?([^'">]+)[^>]*)>c          	   c   sÆ   xvt  |¡D ]h}| ¡ \}}tttj| ¡  d¡ƒƒ}d|ksFd|krx,t	 |¡D ]}t
j | t| d¡ƒ¡V  qRW qW xHdD ]@}| |¡}|dkr~t	 ||¡}|r~t
j | t| d¡ƒ¡V  q~W dS )zEFind rel="homepage" and rel="download" links in `page`, yielding URLsú,Zhomepager2   r5   )z<th>Home Pagez<th>Download URLr1   N)ÚRELÚfinditerÚgroupsr\   r   ÚstrÚstripr*   r8   ÚHREFr   r#   ÚurljoinÚ
htmldecoderE   ÚfindÚsearch)r9   ÚpagerD   ÚtagZrelZrelsÚposr&   r&   r'   Úfind_external_linksÝ   s    "

ry   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'   rz   ð   s   rz   c               @   sB   e Zd Ze 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 |¡| _|| _d S )N)Ú	hash_nameÚhashlibÚnewÚhashÚexpected)r{   r‡   r‹   r&   r&   r'   Ú__init__  s    zHashChecker.__init__c             C   s>   t j |¡d }|stƒ S | j |¡}|s0tƒ S | f | ¡ ŽS )z5Construct a (possibly null) ContentChecker from a URLr1   )r   r#   r6   rz   Úpatternru   Ú	groupdict)Úclsr9   r?   rD   r&   r&   r'   Úfrom_url  s    zHashChecker.from_urlc             C   s   | j  |¡ d S )N)rŠ   Úupdate)r{   r|   r&   r&   r'   r}     s    zHashChecker.feedc             C   s   | j  ¡ | jkS )N)rŠ   Z	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„   rS   Úcompiler   rŒ   Úclassmethodr   r}   r~   r   r&   r&   r&   r'   r†   	  s   r†   c                   s<  e Zd ZdZdJdd„ZdKd	d
„ZdLdd„ZdMdd„Zdd„ Zdd„ Z	dd„ Z
dd„ ZdNdd„Zdd„ ZdO‡ fdd„	Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ ZdPd'd(„ZdQd)d*„Zd+d,„ Zd-Zd.d/„ Zd0d1„ ZdRd2d3„Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Zd<d=„ Ze dSd>d?„ƒZ!d@dA„ Z"dBdC„ Z#dDdE„ Z$dFdG„ Z%dHdI„ Z&‡  Z'S )Tr   z;A distribution index that scans web pages for download URLsúhttps://pypi.org/simple/©Ú*NTc             O   s”   t j| f|ž|Ž |dd | d¡ …  | _i | _i | _i | _t d 	t
t|ƒ¡¡j| _g | _|ortjor|prt ¡ }|r†t |¡| _n
tjj| _d S )Nr0   ú|)r   rŒ   r+   Ú	index_urlÚscanned_urlsÚfetched_urlsÚpackage_pagesrS   r“   r[   r   r   rD   ÚallowsÚto_scanr   Zis_availableZfind_ca_bundleZ
opener_forÚopenerr   ÚrequestÚurlopen)r{   r™   ZhostsZ	ca_bundleZ
verify_sslrf   ÚkwZuse_sslr&   r&   r'   rŒ   -  s    zPackageIndex.__init__Fc             C   sÔ  t  d¡rtd| ƒ‚|| jkr(|s(dS d| j|< t|ƒsH|  |¡ dS tt|ƒƒ}|rr|  |¡sfdS |  	d|¡ |s„|r„|| j
kr˜tt| j|ƒƒ dS |  |¡s°d| j
|< dS |  d|¡ d| j
|< d}|  ||| ¡}|dkrædS d| j
|j< d|j d	d
¡ ¡ kr| ¡  dS |j}| ¡ }t|tƒsdt|tjjƒrFd}n|j d¡pVd}| |d¡}| ¡  x6t |¡D ](}	tj |t |	 !d¡ƒ¡}
|  "|
¡ qxW | #| j$¡rÐt%|ddƒdkrÐ|  &||¡}dS )z<Evaluate a URL as a possible download, and maybe retrieve itZCONDA_BUILDzkSetuptools downloading is disabled in conda build. Be sure to add all dependencies in the meta.yaml  url=%sNTzFound link: %sz
Reading %sz<Download error on %s: %%s -- Some packages may not be found!Úhtmlzcontent-typeÚ zlatin-1ÚcharsetÚignorer5   Úcodei”  )'rP   ÚgetenvÚRuntimeErrorrš   Ú
URL_SCHEMEÚprocess_filenameÚlistr   Úurl_okÚdebugr›   r   r]   ÚinfoÚopen_urlr9   ÚheadersÚgetr*   ÚcloseÚreadÚ
isinstancero   r   ÚerrorÚ	HTTPErrorZ	get_paramÚdecoderq   rm   r#   rr   rs   rE   Úprocess_urlr,   r™   ÚgetattrÚprocess_index)r{   r9   ZretrieveÚdistsÚtmplÚfr.   rv   r¥   rD   Úlinkr&   r&   r'   r¹   B  sV    






 zPackageIndex.process_urlc             C   sŽ   t j |¡s|  d|¡ d S t j |¡rb|sbt j |¡}x(t  |¡D ]}|  t j ||¡d¡ qDW t	|ƒ}|rŠ|  
d|¡ tt| j|ƒƒ d S )NzNot found: %sTz	Found: %s)rP   r=   ÚexistsÚwarnÚisdirÚrealpathÚlistdirr«   r[   rR   r®   r¬   r   r]   )r{   ÚfnÚnestedr=   Úitemr¼   r&   r&   r'   r«   y  s    zPackageIndex.process_filenamec             C   sb   t |ƒ}|o| d¡ ¡ dk}|s8|  tj |¡d ¡r<dS d}|rRt|| ƒ‚n|  ||¡ d S )Nr5   ÚfileTzN
Note: Bypassing %s (disallowed host; see http://bit.ly/2hrImnY for details).
)	rª   rE   r*   r   r   r#   r6   r   rÁ   )r{   r9   ZfatalÚsÚis_filer’   r&   r&   r'   r­   ‰  s    zPackageIndex.url_okc             C   s2   t tjj|ƒ}dd„ |D ƒ}tt | j|¡ƒ d S )Nc             s   s0   | ](}t  |¡D ]}| d ¡r||fV  qqdS )z	.egg-linkN)rP   rÄ   r+   )rT   r=   Úentryr&   r&   r'   rV   ™  s   z.PackageIndex.scan_egg_links.<locals>.<genexpr>)ÚfilterrP   r=   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 ||¡ƒ}ttd ttj|ƒƒƒ}W d Q R X t	|ƒdkrDd S |\}}x>t
tj ||¡ƒD ](}tjj|f|žŽ |_t|_|  |¡ q`W d S )NrW   )ÚopenrP   r=   r[   r¬   rÌ   r   ro   rp   rM   r   rI   r   rA   r]   )r{   r=   rË   Z	raw_linesÚlinesZegg_pathZ
setup_pathrG   r&   r&   r'   rÏ      s     zPackageIndex.scan_egg_linkc       
   	      sÖ   ‡ fdd„}xHt  |¡D ]:}y |tj |t| d¡ƒ¡ƒ W q tk
rP   Y qX qW ||ƒ\}}|rÎxVt||ƒD ]H}t	|ƒ\}}	| 
d¡r°|	s°|r¦|d||f 7 }n
ˆ  |¡ ˆ  |¡ qrW t dd„ |¡S dS d	S )
z#Process the contents of a PyPI pagec                sŒ   |   ˆ j¡rˆtttjj| tˆ jƒd …  d¡ƒƒ}t|ƒdkrˆd|d krˆt	|d ƒ}t
|d ƒ}dˆ j | ¡ i ¡| < t|ƒt|ƒfS dS )Nr0   rW   r4   r5   r   T)NN)r,   r™   r¬   r   r   r#   r7   rM   r8   r   r   rœ   Ú
setdefaultr*   r   )r¿   r:   ÚpkgÚver)r{   r&   r'   Úscan³  s    "z(PackageIndex.process_index.<locals>.scanr5   z.pyz
#egg=%s-%sc             S   s   d|   ddd¡ S )Nz<a href="%s#md5=%s">%s</a>r5   r!   rW   )rE   )Úmr&   r&   r'   Ú<lambda>Ö  s    z,PackageIndex.process_index.<locals>.<lambda>r¤   N)rq   rm   r   r#   rr   rs   rE   r$   ry   r@   r+   Úneed_version_infoÚscan_urlÚPYPI_MD5Úsub)
r{   r9   rv   rØ   rD   rÖ   r×   Únew_urlr.   Úfragr&   )r{   r'   r»   °  s$     

zPackageIndex.process_indexc             C   s   |   d|¡ d S )NzPPage at %s links to .py file(s) without version info; an index scan is required.)Úscan_all)r{   r9   r&   r&   r'   rÛ   Û  s    zPackageIndex.need_version_infoc             G   s:   | j | jkr*|r | j|f|žŽ  |  d¡ |  | j ¡ d S )Nz6Scanning index of all packages (this may take a while))r™   r›   rÁ   r¯   rÜ   )r{   r’   rf   r&   r&   r'   rá   á  s    zPackageIndex.scan_allc             C   s~   |   | j|j d ¡ | j |j¡s:|   | j|j d ¡ | j |j¡sR|  |¡ x&t| j |jd¡ƒD ]}|   |¡ qhW d S )Nr0   r&   )	rÜ   r™   Úunsafe_namerœ   r²   ra   rJ   Únot_found_in_indexr¬   )r{   Úrequirementr9   r&   r&   r'   Úfind_packagesê  s    
zPackageIndex.find_packagesc                sR   |   ¡  |  |¡ x,| |j D ]}||kr.|S |  d||¡ qW tt| ƒ ||¡S )Nz%s does not match %s)Úprescanrå   ra   r®   Úsuperr   Úobtain)r{   rä   Z	installerrG   )Ú	__class__r&   r'   rè   ù  s    
zPackageIndex.obtainc             C   sL   |  | jd| ¡ | ¡ sH| ¡  t |¡ td|jjt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³   rP   Úunlinkr   rŠ   r-   r=   rN   )r{   ÚcheckerrQ   Útfpr&   r&   r'   Ú
check_hash  s    

zPackageIndex.check_hashc             C   sR   xL|D ]D}| j dks2t|ƒr2| d¡s2tt|ƒƒr>|  |¡ q| j  |¡ qW dS )z;Add `urls` to the list that will be prescanned for searchesNzfile:)rž   rª   r,   r¬   r   rÜ   Úappend)r{   Zurlsr9   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   s<   | |j  r| jd }}n| jd }}|||jƒ |  ¡  d S )Nz#Couldn't retrieve index page for %rz3Couldn't find index page for %r (maybe misspelled?))ra   r¯   rÁ   râ   rá   )r{   rä   Úmethr’   r&   r&   r'   rã   '  s    
zPackageIndex.not_found_in_indexc             C   s~   t |tƒsjt|ƒ}|rR|  | d¡||¡}t|ƒ\}}| d¡rN|  |||¡}|S tj	 
|¡rb|S t|ƒ}t|  ||¡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.
        r5   z.pyrI   N)rµ   r   rª   Ú_download_urlrE   r@   r+   Ú	gen_setuprP   r=   rÀ   r(   rº   Úfetch_distribution)r{   r%   Útmpdirr;   Úfoundr.   r?   r&   r&   r'   r2   1  s    

zPackageIndex.downloadc       	         sÜ   ˆ  d|¡ i ‰d}d
‡ ‡‡‡‡fdd„	}|rHˆ ¡  ˆ |¡ ||ƒ}|s^|dk	r^|||ƒ}|dkr€ˆjdk	rxˆ ¡  ||ƒ}|dkrž|sžˆ |¡ ||ƒ}|dkr¾ˆ dˆr´dp¶d|¡ nˆ  d|¡ |j|jd	S d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 ]r}|jtkrHˆ sH|ˆkrˆ d|¡ dˆ|< q|| ko^|jtkp^ˆ }|rˆ |jˆ¡}||_tj	 
|j¡r|S qW d S )Nz&Skipping development or system egg: %sr5   )ra   rA   r   rÁ   r   r2   rI   Údownload_locationrP   r=   rÀ   )ZreqÚenvrG   ZtestZloc)Ú
develop_okr{   ÚskippedÚsourcerô   r&   r'   rt   k  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: %s)rI   )N)r¯   ræ   rå   rž   rÁ   Zclonerö   )	r{   rä   rô   Ú
force_scanrú   rø   Zlocal_indexrG   rt   r&   )rø   r{   rù   rú   rô   r'   ró   S  s0    




zPackageIndex.fetch_distributionc             C   s"   |   ||||¡}|dk	r|jS d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ó   rI   )r{   rä   rô   rû   rú   rG   r&   r&   r'   ÚfetchŸ  s    zPackageIndex.fetchc       
   	   C   sü   t  |¡}|r*dd„ t|| d¡d ƒD ƒp,g }t|ƒdkrÚtj |¡}tj |¡|krŠtj 	||¡}ddl
m} |||ƒsŠt ||¡ |}ttj 	|d¡dƒ2}	|	 d|d j|d jtj |¡d f ¡ W d Q R X |S |rðtd	||f ƒ‚ntd
ƒ‚d S )Nc             S   s   g | ]}|j r|‘qS r&   )rK   )rT   Údr&   r&   r'   ú
<listcomp>¯  s    z*PackageIndex.gen_setup.<locals>.<listcomp>r5   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.)rC   rD   r   rE   rM   rP   r=   rN   Údirnamer[   Zsetuptools.command.easy_installrÿ   ÚshutilZcopy2rÓ   ÚwriterJ   rK   Úsplitextr   )
r{   rQ   r?   rô   rD   r¼   rN   Údstrÿ   rÈ   r&   r&   r'   rò   ¬  s2    

 zPackageIndex.gen_setupi    c          	   C   s  |   d|¡ d }zît |¡}|  |¡}t|tjjƒrJtd||j	|j
f ƒ‚|  ¡ }d}| j}d}d|kr’t|dƒ}	ttt|	ƒƒ}|  |||||¡ t|dƒZ}
xD| |¡}|rÞ| |¡ |
 |¡ |d7 }|  |||||¡ q P q W |  |||
¡ W d Q R X |S |r| ¡  X d S )	NzDownloading %szCan't download %s: %s %sr   r1   zcontent-lengthzContent-LengthÚwbr5   )r¯   r†   r   r°   rµ   r   r¶   r·   r   r§   r’   Údl_blocksizer   Úmaxr   ÚintÚ
reporthookrÓ   r´   r}   r  rí   r³   )r{   r9   rQ   Úfprë   r±   ÚblocknumZbsÚsizeZsizesrì   r|   r&   r&   r'   Ú_download_to×  s:    





zPackageIndex._download_toc             C   s   d S )Nr&   )r{   r9   rQ   r  Zblksizer  r&   r&   r'   r
  û  s    zPackageIndex.reporthookc          
   C   sŽ  |  d¡rt|ƒS yt|| jƒS  ttjfk
r€ } z<d dd„ |jD ƒ¡}|r^|  	||¡ nt
d||f ƒ‚W d d }~X Y n
 tjjk
r¤ } z|S d }~X Y næ tjjk
rî } z*|rÌ|  	||j¡ nt
d||jf ƒ‚W d d }~X Y nœ tjk
r: } z,|r|  	||j¡ nt
d||jf ƒ‚W d d }~X Y nP tjtjfk
rˆ } z(|rh|  	||¡ nt
d||f ƒ‚W d d }~X Y nX d S )Nzfile:ú c             S   s   g | ]}t |ƒ‘qS r&   )ro   )rT   Ú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)r,   Ú
local_openÚopen_with_authrŸ   r$   r   Ú
InvalidURLr[   rf   rÁ   r   r   r¶   r·   ZURLErrorÚreasonZBadStatusLineÚlineZHTTPExceptionÚsocket)r{   r9   ZwarningÚvr’   r&   r&   r'   r°   þ  s6    
"zPackageIndex.open_urlc             C   sê   t |ƒ\}}|r4x&d|kr0| dd¡ dd¡}qW nd}| d¡rN|d d… }tj ||¡}|dksn| d	¡rz|  ||¡S |d
ksŒ| d¡r˜|  ||¡S | d¡r®|  	||¡S |dkrÎt
j t
j |¡d ¡S |  |d¡ |  ||¡S d S )Nz..Ú.ú\Ú_Z__downloaded__z.egg.zipr)   Zsvnzsvn+Zgitzgit+zhg+rÈ   rW   T)r@   Úreplacer+   rP   r=   r[   r,   Ú_download_svnÚ_download_gitÚ_download_hgr   r    Úurl2pathnamer#   r6   r­   Ú_attempt_download)r{   r;   r9   rô   r-   r?   rQ   r&   r&   r'   rñ   !  s$    


zPackageIndex._download_urlc             C   s   |   |d¡ d S )NT)r¹   )r{   r9   r&   r&   r'   rÜ   >  s    zPackageIndex.scan_urlc             C   s6   |   ||¡}d| dd¡ ¡ kr.|  |||¡S |S d S )Nr£   zcontent-typer¤   )r  r²   r*   Ú_download_html)r{   r9   rQ   r±   r&   r&   r'   r   A  s    zPackageIndex._attempt_downloadc             C   sl   t |ƒ}x@|D ]8}| ¡ rt d|¡rD| ¡  t |¡ |  ||¡S P qW | ¡  t |¡ td| ƒ‚d S )Nz <title>([^- ]+ - )?Revision \d+:zUnexpected HTML page found at )	rÓ   rp   rS   ru   r³   rP   rê   r  r   )r{   r9   r±   rQ   rÈ   r  r&   r&   r'   r!  H  s    


zPackageIndex._download_htmlc             C   s  t  dt¡ | dd¡d }d}| ¡  d¡râd|krâtj |¡\}}}}}}	|sâ| d¡râd	|d
d … krâ|d
d …  d	d¡\}}t	|ƒ\}
}|
râd|
krº|
 dd¡\}}d||f }nd|
 }|}||||||	f}tj 
|¡}|  d||¡ t d|||f ¡ |S )Nz"SVN download support is deprecatedr4   r5   r   r¤   zsvn:ú@z//r0   rW   ú:z --username=%s --password=%sz --username=z'Doing subversion checkout from %s to %szsvn checkout%s -q %s %s)ÚwarningsrÁ   ÚUserWarningr8   r*   r,   r   r#   r6   Ú
_splituserÚ
urlunparser¯   rP   Úsystem)r{   r9   rQ   Zcredsr;   Únetlocr=   rU   Úqr¾   ÚauthÚhostÚuserZpwr:   r&   r&   r'   r  W  s&    zPackageIndex._download_svnc             C   sp   t j | ¡\}}}}}| dd¡d }| dd¡d }d }d|krR| dd¡\}}t j ||||df¡} | |fS )Nú+r5   r1   r4   r   r"  r¤   )r   r#   Zurlsplitr8   ÚrsplitZ
urlunsplit)r9   Ú
pop_prefixr;   r)  r=   r>   rà   Úrevr&   r&   r'   Ú_vcs_split_rev_from_urlm  s    z$PackageIndex._vcs_split_rev_from_urlc             C   sl   |  dd¡d }| j|dd\}}|  d||¡ t d||f ¡ |d k	rh|  d|¡ t d	||f ¡ |S )
Nr4   r5   r   T)r0  zDoing git clone from %s to %szgit clone --quiet %s %szChecking out %sz"(cd %s && git checkout --quiet %s))r8   r2  r¯   rP   r(  )r{   r9   rQ   r1  r&   r&   r'   r    s    
zPackageIndex._download_gitc             C   sl   |  dd¡d }| j|dd\}}|  d||¡ t d||f ¡ |d k	rh|  d|¡ t d	||f ¡ |S )
Nr4   r5   r   T)r0  zDoing hg clone from %s to %szhg clone --quiet %s %szUpdating to %sz(cd %s && hg up -C -r %s -q))r8   r2  r¯   rP   r(  )r{   r9   rQ   r1  r&   r&   r'   r    s    
zPackageIndex._download_hgc             G   s   t j|f|žŽ  d S )N)r   r®   )r{   r’   rf   r&   r&   r'   r®   Ÿ  s    zPackageIndex.debugc             G   s   t j|f|žŽ  d S )N)r   r¯   )r{   r’   rf   r&   r&   r'   r¯   ¢  s    zPackageIndex.infoc             G   s   t j|f|žŽ  d S )N)r   rÁ   )r{   r’   rf   r&   r&   r'   rÁ   ¥  s    zPackageIndex.warn)r•   r–   NT)F)F)F)N)N)FFFN)FF)N)F)(r‚   rƒ   r„   r…   rŒ   r¹   r«   r­   rÒ   rÏ   r»   rÛ   rá   rå   rè   rí   rï   ræ   rã   r2   ró   rü   rò   r  r  r
  r°   rñ   rÜ   r   r!  r  Ústaticmethodr2  r  r  r®   r¯   rÁ   Ú__classcell__r&   r&   )ré   r'   r   *  sL    

7


+
		
# 
J
)$
#z!&(#(\d+|x[\da-fA-F]+)|[\w.:-]+);?c             C   s   |   d¡}t|ƒS )Nr   )rE   r   )rD   Zwhatr&   r&   r'   Údecode_entity®  s    
r5  c             C   s
   t t| ƒS )a  
    Decode HTML entities in the given text.

    >>> htmldecode(
    ...     'https://../package_name-0.1.2.tar.gz'
    ...     '?tokena=A&amp;tokenb=B">package_name-0.1.2.tar.gz')
    'https://../package_name-0.1.2.tar.gz?tokena=A&tokenb=B">package_name-0.1.2.tar.gz'
    )Ú
entity_subr5  )Útextr&   r&   r'   rs   ³  s    	rs   c                s   ‡ fdd„}|S )Nc                s   ‡ ‡fdd„}|S )Nc           	      s.   t  ¡ }t  ˆ¡ z
ˆ | |ŽS t  |¡ X d S )N)r  ZgetdefaulttimeoutZsetdefaulttimeout)rf   rg   Zold_timeout)rh   Útimeoutr&   r'   Ú_socket_timeoutÁ  s
    

z@socket_timeout.<locals>._socket_timeout.<locals>._socket_timeoutr&   )rh   r9  )r8  )rh   r'   r9  À  s    z'socket_timeout.<locals>._socket_timeoutr&   )r8  r9  r&   )r8  r'   Úsocket_timeout¿  s    r:  c             C   s2   t j | ¡}| ¡ }t |¡}| ¡ }| 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#   r7   ÚencodeÚbase64Z	b64encoder¸   r  )r+  Zauth_sZ
auth_bytesZencoded_bytesZ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               @   s0   e Zd Zdd„ Zedd„ ƒZdd„ Zdd„ Zd	S )
Ú
PyPIConfigc             C   sP   t  dddgd¡}tj | |¡ tj tj d¡d¡}tj 	|¡rL|  
|¡ dS )z%
        Load from ~/.pypirc
        r@  rA  Ú
repositoryr¤   ú~z.pypircN)ÚdictÚfromkeysr   ÚRawConfigParserrŒ   rP   r=   r[   Ú
expanduserrÀ   r´   )r{   ÚdefaultsZrcr&   r&   r'   rŒ   ö  s
    zPyPIConfig.__init__c                s&   ‡ fdd„ˆ   ¡ D ƒ}ttˆ j|ƒƒS )Nc                s    g | ]}ˆ   |d ¡ ¡ r|‘qS )rF  )r²   rp   )rT   Úsection)r{   r&   r'   rþ     s    z2PyPIConfig.creds_by_repository.<locals>.<listcomp>)ZsectionsrH  r   Ú_get_repo_cred)r{   Zsections_with_repositoriesr&   )r{   r'   Úcreds_by_repository  s    zPyPIConfig.creds_by_repositoryc             C   s6   |   |d¡ ¡ }|t|   |d¡ ¡ |   |d¡ ¡ ƒfS )NrF  r@  rA  )r²   rp   r?  )r{   rM  Zrepor&   r&   r'   rN  
  s    zPyPIConfig._get_repo_credc             C   s*   x$| j  ¡ D ]\}}| |¡r|S qW dS )zŒ
        If the URL indicated appears to be a repository defined in this
        config, return the credential for that repository.
        N)rO  Úitemsr,   )r{   r9   rF  Úcredr&   r&   r'   Úfind_credential  s    
zPyPIConfig.find_credentialN)r‚   rƒ   r„   rŒ   ÚpropertyrO  rN  rR  r&   r&   r&   r'   rE  õ  s   	rE  c             C   s:  t j | ¡}|\}}}}}}| d¡r0t d¡‚|dkrFt|ƒ\}	}
nd}	|	s~tƒ  | ¡}|r~t	|ƒ}	|j
| f}tjd	|žŽ  |	rÄdt|	ƒ }	||
||||f}t j |¡}t j |¡}| d|	¡ nt j | ¡}| dt¡ ||ƒ}|	r6t j |j¡\}}}}}}||kr6||
kr6||||||f}t j |¡|_|S )
z4Open a urllib2 request, handling HTTP authenticationr#  znonnumeric port: '')ZhttpZhttpsNú*Authenticating as %s for %s (from .pypirc)zBasic ZAuthorizationz
User-Agent)rT  )r   r#   r6   r+   r   r  r&  rE  rR  ro   r@  r   r¯   r>  r'  r    ZRequestZ
add_headerÚ
user_agentr9   )r9   rŸ   Zparsedr;   r)  r=   Zparamsr>   rà   r+  ZaddressrQ  r¯   r:   rß   r    r  Ús2Zh2Zpath2Zparam2Zquery2Zfrag2r&   r&   r'   r    s8    


r  c             C   s    |   d¡\}}} |r|nd| fS )zJsplituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]'.r"  N)Ú
rpartition)r,  r-  Zdelimr&   r&   r'   r&  J  s    r&  c             C   s   | S )Nr&   )r9   r&   r&   r'   Ú
fix_sf_urlT  s    rX  c          
   C   s  t j | ¡\}}}}}}t j |¡}tj |¡r<t j | ¡S | 	d¡rêtj 
|¡rêg }xˆt |¡D ]b}	tj ||	¡}
|	dkržt|
dƒ}| ¡ }W dQ R X P ntj 
|
¡r²|	d7 }	| dj|	d¡ qbW d}|j| d |¡d	}d
\}}n
d\}}}ddi}t |¡}t j | ||||¡S )z7Read a local path, with special support for directoriesr0   z
index.htmlÚrNz<a href="{name}">{name}</a>)r-   zB<html><head><title>{url}</title></head><body>{files}</body></html>r;  )r9   Úfiles)éÈ   ZOK)i”  zPath not foundz	Not foundzcontent-typez	text/html)r   r#   r6   r    r  rP   r=   Úisfiler¡   r+   rÂ   rÄ   r[   rÓ   r´   rî   Úformatr   ÚStringIOr¶   r·   )r9   r;   r<   r=   Zparamr>   rà   rQ   rZ  r¾   Úfilepathr  Zbodyr½   ZstatusÚmessager±   Zbody_streamr&   r&   r'   r  X  s,    


r  )N)N)N)N)r    )]r…   ÚsysrP   rS   r  r  r=  rˆ   rÍ   r$  Ú	functoolsr   Zsetuptools.externr   Zsetuptools.extern.six.movesr   r   r   r   r"   Zpkg_resourcesr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   Z	distutilsr   Zdistutils.errorsr   Zfnmatchr   Zsetuptools.py27compatr   Zsetuptools.py33compatr   Zsetuptools.wheelr   ÚtypeZ__metaclass__r“   rC   ÚIrq   rÝ   rD   rª   r8   rL   Ú__all__Z_SOCKET_TIMEOUTZ_tmplr]  rK   rU  r(   r   r@   r   rB   rR   r   re   rj   rl   ry   rz   r†   r   rÞ   r6  r5  rs   r:  r>  r?  rJ  rE  r    r¡   r  r&  rX  r  r&   r&   r&   r'   Ú<module>   s‚   <
	

!
"
!     
&/