B
    Uvg                 @   s4  d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZ ddl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m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&m'Z'm(Z(m)Z)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z: ddl;m<Z<m=Z=m>Z>m?Z? ddl@mAZA ddlBmCZCmDZD ddgZEdddddd gZFeGeHZId!d" ZJd6d#d$ZKd7d%d&ZLG d'd deMZNe	Od(e	jPfd)d*ZQd+d, ZRd-d. ZSe	Od/e	jPZTd0d1 ZUG d2d3 d3eMZVed4d5ZWdS )8z!Routines related to PyPI, indexes    )absolute_importN)
namedtuple)html5librequestssix)unescape)
specifiers)canonicalize_name)parse)	HTTPErrorSSLError)request)HAS_TLSis_urlpath_to_urlurl_to_path)BestVersionAlreadyInstalledDistributionNotFoundInvalidWheelFilenameUnsupportedWheel)InstallationCandidate)FormatControl)PyPI)Link)get_supported)	ipaddress)
deprecated)
indent_log)ARCHIVE_EXTENSIONSSUPPORTED_EXTENSIONSnormalize_pathremove_auth_from_url)check_requires_python)Wheel	wheel_extr   PackageFinder)https*r'   )r'   	localhostr'   )r'   z127.0.0.0/8r'   )r'   z::1/128r'   )filer'   N)sshr'   r'   c             C   sD   t | \}}}}}|dkr dS |j| dd}|  |jddS )z;Get the Content-Type of the given url, using a HEAD request>   r&   http T)allow_redirectszContent-Type)urllib_parseurlsplitheadraise_for_statusheadersget)urlsessionschemenetlocpathqueryfragmentresp r<   4/tmp/pip-install-r_9ig3yj/pip/pip/_internal/index.py_get_content_type>   s    r>   c             C   s   |d krt j}|d| | d S )Nz%Could not fetch URL %s: %s - skipping)loggerdebug)linkreasonr4   methr<   r<   r=   _handle_get_page_failL   s    rD   c          
   C   sd  |d krt d| j}|ddd }ddlm} x>|jD ]4}| |r:|t| dkr:t	
d||  d S q:W y| j}xFtD ]>}||rt||d}| d	rP qt	
d
| | d S qW t	
d| t|\}}}	}
}}|dkr0tjt|	r0|ds|d7 }t|d}t	
d| |j|d	ddd}|  |jdd}| d	s|t	
d
| | d S t|j|j|j}W n tk
r } zt| || W d d }~X Y n tk
r } z&d}|t|7 }t| ||t	j d W d d }~X Y n\ t!j"k
r8 } zt| d| | W d d }~X Y n( t!j#k
rZ   t| d| Y nX |S d S )Nz?_get_html_page() missing 1 required keyword argument: 'session'#   r   )
VcsSupportz+:zCannot look at %s URL %s)r5   z	text/htmlz,Skipping page %s because of Content-Type: %szGetting page %sr)   /z
index.htmlz# file: URL is directory, getting %sz	max-age=0)AcceptzCache-Control)r2   zContent-Typeunknownz4There was a problem confirming the ssl certificate: )rC   zconnection error: %sz	timed out)$	TypeErrorr4   splitpip._internal.vcsrG   schemeslower
startswithlenr?   r@   filenamer   endswithr>   r.   urlparseosr8   isdirurllib_requesturl2pathnameurljoinr3   r1   r2   HTMLPagecontentr   rD   r   strinfor   ConnectionErrorTimeout)rA   r5   r4   rG   r6   rR   bad_extcontent_typer7   r8   paramsr9   r:   r;   instexcrB   r<   r<   r=   _get_html_pageR   sl    


" re   c            
   @   s   e Zd ZdZd#ddZdd Zdd	 Zed$d
dZdd Z	dd Z
dd Zdd Zdd Zdd ZedZdd Zdd Zdd Zdd  Zd!d" ZdS )%r%   zThis finds packages.

    This is meant to match easy_install's technique for looking for
    packages, by reading pages and looking for appropriate links.
    FNc             C   s   |dkrt dg | _x:|D ]2}|drBt|}tj|rB|}| j| qW || _g | _	t
 | _|pvtt
 t
 | _dd |r|ng D | _|| _|| _|| _t|	||
|d| _|| _tsx8t| j| jD ]$}t|}|jdkrtd P qW dS )	a  Create a PackageFinder.

        :param format_control: A FormatControl object or None. Used to control
            the selection of source packages / binary packages when consulting
            the index and links.
        :param platform: A string or None. If None, searches for packages
            that are supported by the current system. Otherwise, will find
            packages that can be built on the platform passed in. These
            packages will only be downloaded for distribution: they will
            not be built locally.
        :param versions: A list of strings or None. This is passed directly
            to pep425tags.py in the get_supported() method.
        :param abi: A string or None. This is passed directly
            to pep425tags.py in the get_supported() method.
        :param implementation: A string or None. This is passed directly
            to pep425tags.py in the get_supported() method.
        Nz>PackageFinder() missing 1 required keyword argument: 'session'~c             S   s   g | ]}d |d fqS )r'   r<   ).0hostr<   r<   r=   
<listcomp>   s   z*PackageFinder.__init__.<locals>.<listcomp>)versionsplatformabiimplr&   zipip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.)rK   
find_linksrP   r    rU   r8   existsappend
index_urlsdependency_linkssetlogged_linksr   format_controlsecure_originsallow_all_prereleasesprocess_dependency_linksr5   r   
valid_tagsprefer_binaryr   	itertoolschainr.   rT   r6   r?   warning)selfrn   rq   rw   trusted_hostsrx   r5   ru   rk   rj   rl   implementationrz   rA   new_linkparsedr<   r<   r=   __init__   s@    	




zPackageFinder.__init__c             C   sb   g }| j r:| j tjgkr:|dddd | j D  | jrX|dd| j d|S )NzLooking in indexes: {}z, c             s   s   | ]}t |V  qd S )N)r!   )rg   r4   r<   r<   r=   	<genexpr>  s    z8PackageFinder.get_formatted_locations.<locals>.<genexpr>zLooking in links: {}
)rq   r   
simple_urlrp   formatjoinrn   )r~   linesr<   r<   r=   get_formatted_locations  s    z%PackageFinder.get_formatted_locationsc             C   s&   | j r"tddddd | j| d S )NzXDependency Links processing has been deprecated and will be removed in a future release.zPEP 508 URL dependenciesz18.2i[  )replacementgone_inissue)rx   r   rr   extend)r~   linksr<   r<   r=   add_dependency_links  s    z"PackageFinder.add_dependency_linksc                s   g  g  fdd}x| D ]}t j|}|d}|s>|r|rH|}nt|}t j|r|rt j|}x4t |D ]}|t j|| qxW q|rƈ	| qt j
|r|| qtd| qt|r܈	| qtd| qW  fS )zt
        Sort locations into "files" (archives) and "urls", and return
        a pair of lists (files,urls)
        c                s8   t | }tj|ddd dkr*| n
 | d S )NF)strictr   z	text/html)r   	mimetypes
guess_typerp   )r8   r4   )filesurlsr<   r=   	sort_path7  s    z0PackageFinder._sort_locations.<locals>.sort_pathzfile:z:Url '%s' is ignored: it is neither a file nor a directory.zQUrl '%s' is ignored. It is either a non-existing path or lacks a specific scheme.)rU   r8   ro   rP   r   rV   realpathlistdirr   rp   isfiler?   r}   r   )	locations
expand_dirr   r4   is_local_pathis_file_urlr8   itemr<   )r   r   r=   _sort_locations-  s8    



zPackageFinder._sort_locationsc       	      C   s   t | j}t }d}|jjrt|jj}|| jsBtd|j | j	rLd}|
| j }|jdk	rtd|j}| }t|d |d f}n| }||j||fS )a  
        Function used to generate link sort key for link tuples.
        The greater the return value, the more preferred it is.
        If not finding wheels, then sorted by version only.
        If finding wheels, then the sort order is by version, then:
          1. existing installs
          2. wheels ordered via Wheel.support_index_min(self.valid_tags)
          3. source archives
        If prefer_binary was set, then all wheels are sorted above sources.
        Note: it was considered to embed this logic into the Link
              comparison operators, but then different sdist links
              with the same version, would have to be considered equal
        r   zB%s is not a supported wheel for this platform. It can't be sorted.rF   Nz^(\d+)(.*)$)rQ   ry   tuplelocationis_wheelr#   rR   	supportedr   rz   support_index_min	build_tagrematchgroupsintversion)	r~   	candidatesupport_numr   binary_preferencewheelprir   build_tag_groupsr<   r<   r=   _candidate_sort_keya  s$    


z!PackageFinder._candidate_sort_keyc       	   	   C   sl  t t|}|j|j|jf}|d ddd }x t| j D ]}||d kr`|d dkr`q@yht	
t|d tjs|d d kr|d n|d d}t	t|d tjr|d n|d d}W nJ tk
r   |d r|d  |d  kr|d dkrw@Y nX ||kr q@|d |d krP|d dkrP|d d k	rPq@dS W |d	|j|j d
S )Nr   +rF   r'   utf8   TzThe repository located at %s is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host %s'.F)r.   rT   r\   r6   hostnameportrsplitSECURE_ORIGINSrv   r   
ip_address
isinstancer   	text_typedecode
ip_network
ValueErrorrO   r}   )	r~   r?   r   r   originprotocolsecure_originaddrnetworkr<   r<   r=   _validate_secure_origin  s>    

z%PackageFinder._validate_secure_originc                s    fdd  fdd| j D S )zReturns the locations found via self.index_urls

        Checks the url_name on the main (first in the list) index and
        use this url_name to produce all locations
        c                s,   t | tt }|ds(|d }|S )NrH   )	posixpathr   r.   quoter	   rS   )r4   loc)project_namer<   r=   mkurl_pypi_url  s    
z?PackageFinder._get_index_urls_locations.<locals>.mkurl_pypi_urlc                s   g | ]} |qS r<   r<   )rg   r4   )r   r<   r=   ri     s    z;PackageFinder._get_index_urls_locations.<locals>.<listcomp>)rq   )r~   r   r<   )r   r   r=   _get_index_urls_locations  s    z'PackageFinder._get_index_urls_locationsc          
      s    |} |\}} j jdd\}}  j\}}dd t|||D }	 fddtdd |D dd |D d	d |D D }
td
t|
| x|
D ]}td| qW t	|} j
|}t|||} dd  jD |}g }xL |
|D ]<}td|j t  | | | W dQ R X qW  dd  jD |}|r~tdddd |D   |	|}|r|jdd tdddd |D  || | | S )a  Find all available InstallationCandidate for project_name

        This checks index_urls, find_links and dependency_links.
        All versions found are returned as an InstallationCandidate list.

        See _link_package_versions for details on which files are accepted
        T)r   c             s   s   | ]}t |V  qd S )N)r   )rg   r4   r<   r<   r=   r     s    z4PackageFinder.find_all_candidates.<locals>.<genexpr>c                s   g | ]}  t|r|qS r<   )r   r?   )rg   rA   )r~   r<   r=   ri     s    z5PackageFinder.find_all_candidates.<locals>.<listcomp>c             s   s   | ]}t |V  qd S )N)r   )rg   r4   r<   r<   r=   r     s    c             s   s   | ]}t |V  qd S )N)r   )rg   r4   r<   r<   r=   r     s    c             s   s   | ]}t |V  qd S )N)r   )rg   r4   r<   r<   r=   r     s    z,%d location(s) to search for versions of %s:z* %sc             s   s   | ]}t |d V  qdS )z-fN)r   )rg   r4   r<   r<   r=   r     s    zAnalyzing links from page %sNc             s   s   | ]}t |V  qd S )N)r   )rg   r4   r<   r<   r=   r     s    zdependency_links found: %sz, c             S   s   g | ]}|j jqS r<   )r   r4   )rg   r   r<   r<   r=   ri      s    )reversezLocal files found: %sc             S   s   g | ]}t |jjqS r<   )r   r   r4   )rg   r   r<   r<   r=   ri   *  s   )r   r   rn   rr   r{   r|   r?   r@   rQ   r	   ru   get_allowed_formatsSearch_package_versions
_get_pagesr4   r   r   
iter_linksr   sort)r~   r   index_locationsindex_file_locindex_url_locfl_file_loc
fl_url_locdep_file_locdep_url_locfile_locationsurl_locationsr   canonical_nameformatssearchfind_links_versionspage_versionspagedependency_versionsfile_versionsr<   )r~   r=   find_all_candidates  sX    


"
z!PackageFinder.find_all_candidatesc          
      sb  |  |j}t|jjdd |D | jr,| jndd  fdd|D }|r\t|| jd}nd}|jdk	rxt	|jj
}nd}|dkr|dkrtd|dtd	d
 |D t	d td| d}|r|dks|j
|krd}|s|dk	r|rtd| ntd||j
 dS |r>td|dt t	dp6d ttd|j
dt t	d |jS )zTry to find a Link matching req

        Expects req, an InstallRequirement and upgrade, a boolean
        Returns a Link if found,
        Raises DistributionNotFound or BestVersionAlreadyInstalled otherwise
        c             S   s   g | ]}t |jqS r<   )r\   r   )rg   cr<   r<   r=   ri   H  s    z2PackageFinder.find_requirement.<locals>.<listcomp>N)prereleasesc                s   g | ]}t |j kr|qS r<   )r\   r   )rg   r   )compatible_versionsr<   r=   ri   Q  s    )keyzNCould not find a version that satisfies the requirement %s (from versions: %s)z, c             S   s   h | ]}t |jqS r<   )r\   r   )rg   r   r<   r<   r=   	<setcomp>f  s    z1PackageFinder.find_requirement.<locals>.<setcomp>z%No matching distribution found for %sFTzLExisting installed version (%s) is most up-to-date and satisfies requirementzUExisting installed version (%s) satisfies requirement (most up-to-date version is %s)z=Installed version (%s) is most up-to-date (past versions: %s)nonez)Using version %s (newest of versions: %s))r   namers   	specifierfilterrw   maxr   satisfied_byparse_versionr   r?   criticalr   sortedr   r@   r   r   )r~   requpgradeall_candidatesapplicable_candidatesbest_candidateinstalled_versionbest_installedr<   )r   r=   find_requirement5  sd    


zPackageFinder.find_requirementc             c   sF   t  }x:|D ]2}||krq|| | |}|dkr8q|V  qW dS )zp
        Yields (page, page_url) from the given locations, skipping
        locations that have errors.
        N)rs   add	_get_page)r~   r   r   seenr   r   r<   r<   r=   r     s    


zPackageFinder._get_pagesz-py([123]\.?[0-9]?)$c             C   sT   g g  }}t  }x:|D ]2}||kr|| |jr>|| q|| qW || S )z
        Returns elements of links in order, non-egg links first, egg links
        second, while eliminating duplicates
        )rs   r   egg_fragmentrp   )r~   r   eggsno_eggsr   rA   r<   r<   r=   _sort_links  s    


zPackageFinder._sort_linksc             C   s:   g }x0|  |D ]"}| ||}|d k	r|| qW |S )N)r  _link_package_versionsrp   )r~   r   r   resultrA   vr<   r<   r=   r     s    zPackageFinder._package_versionsc             C   s(   || j kr$td|| | j | d S )NzSkipping link %s; %s)rt   r?   r@   r   )r~   rA   rB   r<   r<   r=   _log_skipped_link  s    
zPackageFinder._log_skipped_linkc       
      C   sJ  d}|j r|j }|j}n| \}}|s:| |d dS |tkrV| |d|  dS d|jkr~|tkr~| |d|j  dS d|jkr|dkr| |d dS |tkr&yt	|j
}W n  tk
r   | |d	 dS X t|j|jkr| |d
|j  dS || js | |d dS |j}d|jkrR|tkrR| |d|j  dS |sft||j|}|dkr| |d|j  dS | j|}|r|d|  }|d}|tjdd kr| |d dS yt|j}	W n. tjk
r   td|j
|j d}	Y nX |	s.td||j dS td|| t|j||S )z'Return an InstallationCandidate or NoneNz
not a filezunsupported archive format: %sbinaryzNo binaries permitted for %smacosx10z.zipzmacosx10 onezinvalid wheel filenamezwrong project name (not %s)z%it is not compatible with this PythonsourcezNo sources permitted for %szMissing project version for %srF      zPython version is incorrectz3Package %s has an invalid Requires-Python entry: %sTz_The package %s is incompatible with the pythonversion in use. Acceptable python versions are:%szFound link %s, version: %s)r   extsplitextr  r   r   r$   suppliedr8   r#   rR   r   r	   r   	canonicalr   ry   r   egg_info_matches_py_version_rer   startgroupsysr"   requires_pythonr   InvalidSpecifierr?   r@   r   )
r~   rA   r   r   egg_infor
  r   r   
py_versionsupport_this_pythonr<   r<   r=   r    s    





z$PackageFinder._link_package_versionsc             C   s   t || jdS )N)r5   )re   r5   )r~   rA   r<   r<   r=   r     s    zPackageFinder._get_page)
FNFNNNNNNF)F)__name__
__module____qualname____doc__r   r   r   staticmethodr   r   r   r   r   r   r   r   compiler  r  r   r  r  r   r<   r<   r<   r=   r%      s,       
T3$GSc
Pz([a-z0-9_.]+)-([a-z0-9_.!+-]+)c             C   s   | | }|std| dS |dkr@|d}|ddd S |d }|dd}| d }||r|dt|d S dS dS )ax  Pull the version part out of a string.

    :param egg_info: The string to parse. E.g. foo-2.1
    :param search_name: The name of the package this belongs to. None to
        infer the name. Note that this cannot unambiguously parse strings
        like foo-2-2 which might be foo, 2-2 or foo-2, 2.
    :param link: The link the string came from, for logging on failure.
    z%Could not parse version from link: %sNr   -rF   r   _)	r   r?   r@   r  rL   rO   replacerP   rQ   )r  search_namerA   _egg_info_rer   
full_matchr   look_forr<   r<   r=   r    s    


r  c             C   s.   x(|  dD ]}|d}|dk	r|S qW |S )a  Determine the HTML document's base URL.

    This looks for a ``<base>`` tag in the HTML document. If present, its href
    attribute denotes the base URL of anchor tags in the document. If there is
    no such tag (or if it does not have a valid href attribute), the HTML
    file's URL is used as the base URL.

    :param document: An HTML document representation. The current
        implementation expects the result of ``html5lib.parse()``.
    :param page_url: The URL of the HTML document.
    z.//basehrefN)findallr3   )documentpage_urlbaser%  r<   r<   r=   _determine_base_url9  s
    
r*  c             C   s2   | r.d| kr.t | d \}}d|kr.|d S dS )zBDetermine if we have any encoding information in our headers.
    zContent-TypecharsetN)cgiparse_header)r2   ra   rb   r<   r<   r=   _get_encoding_from_headersL  s
    r.  z[^a-z0-9$&+,/:;=?@.#%_\\|-]c             C   s   t dd | S )zMakes sure a link is fully encoded.  That is, if a ' ' shows up in
    the link, it will be rewritten to %20 (while not over-quoting
    % or other characters).c             S   s   dt | d S )Nz%%%2xr   )ordr  )r   r<   r<   r=   <lambda>]      z_clean_link.<locals>.<lambda>)_CLEAN_LINK_REsub)r4   r<   r<   r=   _clean_linkY  s    r4  c               @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
rZ   z'Represents one page, along with its URLNc             C   s   || _ || _|| _d S )N)r[   r4   r2   )r~   r[   r4   r2   r<   r<   r=   r   c  s    zHTMLPage.__init__c             C   s   | j S )N)r4   )r~   r<   r<   r=   __str__h  s    zHTMLPage.__str__c             c   s   t j| jt| jdd}t|| j}xb|dD ]T}|dr0|d}t	t
||}|d}|rnt|nd}t|| j|dV  q0W dS )zYields all links in the pageF)transport_encodingnamespaceHTMLElementsz.//ar%  zdata-requires-pythonN)r  )r   r
   r[   r.  r2   r*  r4   r&  r3   r4  r.   rY   r   r   )r~   r'  base_urlanchorr%  r4   	pyrequirer<   r<   r=   r   k  s    


zHTMLPage.iter_links)N)r  r  r  r  r   r5  r   r<   r<   r<   r=   rZ   `  s   
rZ   r   zsupplied canonical formats)N)N)Xr  
__future__r   r,  r{   loggingr   rU   r   r   r  collectionsr   pip._vendorr   r   r   pip._vendor.distlib.compatr   Zpip._vendor.packagingr   pip._vendor.packaging.utilsr	   Zpip._vendor.packaging.versionr
   r   Zpip._vendor.requests.exceptionsr   r   Zpip._vendor.six.moves.urllibr.   r   rW   pip._internal.downloadr   r   r   r   pip._internal.exceptionsr   r   r   r   pip._internal.models.candidater   #pip._internal.models.format_controlr   pip._internal.models.indexr   pip._internal.models.linkr   Zpip._internal.pep425tagsr   pip._internal.utils.compatr   pip._internal.utils.deprecationr   pip._internal.utils.loggingr   pip._internal.utils.miscr   r   r    r!   pip._internal.utils.packagingr"   pip._internal.wheelr#   r$   __all__r   	getLoggerr  r?   r>   rD   re   objectr%   r  Ir  r*  r.  r2  r4  rZ   r   r<   r<   r<   r=   <module>   sn   


`    p

