3
ÀÞL]ÑÚ  ã               @   s|  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	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mZ ddlmZ dd
lmZ ddl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*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? e=�rVddlm@Z@ ddlAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZN ddlOZPddlmQZQ ddlRmSZS ddlTmUZU ddlVmWZW ddlmXZX ddlYmZZZ dd l[m\Z\ eMeBd!f Z]eMe^e^e^eQe]eIe^ f Z_ePj`jajbZceMededeIed f Zed"d#d$gZfdYdZd[d\d]d^gZgejheiƒZjd,d-„ Zkd.d/„ ZlG d0d1„ d1emƒZnd2d3„ ZoG d4d5„ d5emƒZpd6d7„ Zqd8d9„ Zrd_d:d;„Zsd`d<d=„Ztdad?d@„ZuG dAdB„ dBevƒZwdCdD„ ZxG dEdF„ dFevƒZyG dGdH„ dHevƒZzG dId#„ d#evƒZ{G dJd$„ d$evƒZ|dKdL„ Z}dMdN„ Z~dOdP„ ZdQdR„ Z€dSdT„ Z�dUdV„ Z‚G dWdX„ dXevƒZƒdS )bz!Routines related to PyPI, indexesé    )Úabsolute_importN)Úhtml5libÚrequestsÚsix)Úunescape)Ú
specifiers)Úcanonicalize_name)Úparse)Ú	HTTPErrorÚ
RetryErrorÚSSLError)Úrequest)Úis_urlÚurl_to_path)ÚBestVersionAlreadyInstalledÚDistributionNotFoundÚInvalidWheelFilenameÚUnsupportedWheel)ÚInstallationCandidate)ÚFormatControl)ÚLink)ÚSelectionPreferences)ÚTargetPython)Ú	ipaddress)Ú
indent_log)ÚARCHIVE_EXTENSIONSÚSUPPORTED_EXTENSIONSÚWHEEL_EXTENSIONÚpath_to_urlÚredact_password_from_url)Úcheck_requires_python)ÚMYPY_CHECK_RUNNING)ÚWheel)ÚLogger)ÚAnyÚCallableÚ	FrozenSetÚIterableÚIteratorÚListÚMutableMappingÚOptionalÚSequenceÚSetÚTextÚTupleÚUnion)Ú_BaseVersion)ÚResponse)ÚSearchScope)ÚInstallRequirement)Ú
PipSession)Ú	Pep425Tag)ÚHashes.r   ÚFoundCandidatesÚPackageFinderÚhttpsÚ*Ú	localhostú127.0.0.0/8ú::1/128ÚfileÚsshc             C   sB   ddl m} x0|jD ]&}| jƒ j|ƒr| t|ƒ dkr|S qW dS )zgLook for VCS schemes in the URL.

    Returns the matched VCS scheme, or None if there's no match.
    r   )Úvcsz+:N)Zpip._internal.vcsrA   ÚschemesÚlowerÚ
startswithÚlen)ÚurlrA   Úscheme© rH   ú2/tmp/pip-build-69lrc5qx/pip/pip/_internal/index.pyÚ_match_vcs_schemeR   s
    rJ   c             C   s*   t | ƒj}xtD ]}|j|ƒrdS qW dS )z2Return whether the URL looks like an archive.
    TF)r   Úfilenamer   Úendswith)rF   rK   Úbad_extrH   rH   rI   Ú_is_url_like_archive_   s
    


rN   c                   s   e Zd Z‡ fdd„Z‡  ZS )Ú_NotHTMLc                s"   t t| ƒj||ƒ || _|| _d S )N)ÚsuperrO   Ú__init__Úcontent_typeÚrequest_desc)ÚselfrR   rS   )Ú	__class__rH   rI   rQ   k   s    z_NotHTML.__init__)Ú__name__Ú
__module__Ú__qualname__rQ   Ú__classcell__rH   rH   )rU   rI   rO   j   s   rO   c             C   s.   | j jddƒ}|jƒ jdƒs*t|| jjƒ‚dS )z…Check the Content-Type header to ensure the response contains HTML.

    Raises `_NotHTML` if the content type is not text/html.
    zContent-TypeÚ z	text/htmlN)ÚheadersÚgetrC   rD   rO   r   Úmethod)ÚresponserR   rH   rH   rI   Ú_ensure_html_headerr   s    r_   c               @   s   e Zd ZdS )Ú_NotHTTPN)rV   rW   rX   rH   rH   rH   rI   r`   }   s   r`   c             C   sD   t j| ƒ\}}}}}|dkr"tƒ ‚|j| dd�}|jƒ  t|ƒ dS )zÊSend a HEAD request to the URL, and ensure the response contains HTML.

    Raises `_NotHTTP` if the URL is not available for a HEAD request, or
    `_NotHTML` if the content type is not text/html.
    Úhttpr:   T)Úallow_redirectsN>   r:   ra   )Úurllib_parseÚurlsplitr`   ÚheadÚraise_for_statusr_   )rF   ÚsessionrG   ÚnetlocÚpathÚqueryÚfragmentÚresprH   rH   rI   Ú_ensure_html_response�   s    rm   c             C   sL   t | ƒrt| |d� tjdt| ƒƒ |j| dddœd�}|jƒ  t|ƒ |S )a  Access an HTML page with GET, and return the response.

    This consists of three parts:

    1. If the URL looks suspiciously like an archive, send a HEAD first to
       check the Content-Type is HTML, to avoid downloading a large file.
       Raise `_NotHTTP` if the content type cannot be determined, or
       `_NotHTML` if it is not HTML.
    2. Actually perform the request. Raise HTTP exceptions on network failures.
    3. Check the Content-Type header to make sure we got HTML, and raise
       `_NotHTML` otherwise.
    )rg   zGetting page %sz	text/htmlz	max-age=0)ÚAcceptzCache-Control)r[   )rN   rm   ÚloggerÚdebugr   r\   rf   r_   )rF   rg   rl   rH   rH   rI   Ú_get_html_response’   s    rq   c             C   s   |d krt j}|d| |ƒ d S )Nz%Could not fetch URL %s: %s - skipping)ro   rp   )ÚlinkÚreasonÚmethrH   rH   rI   Ú_handle_get_page_failÅ   s    ru   c       
      C   s
  |d krt dƒ‚| jjddƒd }t|ƒ}|r@tjd|| ƒ d S tj|ƒ\}}}}}}|dkrštj	j
tj|ƒƒrš|jdƒs‚|d7 }tj|dƒ}tjd	|ƒ yt||d
�}W �nF tk
rÎ   tjd| ƒ Y �n8 tk
�r } ztjd| |j|jƒ W Y d d }~X �n  tk
�r2 } zt| |ƒ W Y d d }~X nÔ tk
�r^ } zt| |ƒ W Y d d }~X n¨ tk
�r  } z&d}	|	t|ƒ7 }	t| |	tjd� W Y d d }~X nf tjk
�rÒ } zt| d| ƒ W Y d d }~X n4 tjk
�rò   t| dƒ Y nX t|j|j|jƒS d S )Nz?_get_html_page() missing 1 required keyword argument: 'session'ú#é   r   zCannot look at %s URL %sr?   ú/z
index.htmlz# file: URL is directory, getting %s)rg   zQSkipping page %s because it looks like an archive, and cannot be checked by HEAD.z<Skipping page %s because the %s request got Content-Type: %sz4There was a problem confirming the ssl certificate: )rt   zconnection error: %sz	timed out) Ú	TypeErrorrF   ÚsplitrJ   ro   rp   rc   ÚurlparseÚosri   ÚisdirÚurllib_requestÚurl2pathnamerL   Úurljoinrq   r`   rO   rS   rR   r
   ru   r   r   ÚstrÚinfor   ÚConnectionErrorÚTimeoutÚHTMLPageÚcontentr[   )
rr   rg   rF   Z
vcs_schemerG   Ú_ri   rl   Úexcrs   rH   rH   rI   Ú_get_html_pageÐ   sL    
  r‰   Fc             C   s~   yt | j|d�}W n& tjk
r8   tjd| j| ƒ Y nBX |szdjtt|ƒƒ}|shtjd|| j| ƒ dS tjd|| j| ƒ dS )aa  
    Return whether the given Python version is compatible with a link's
    "Requires-Python" value.

    :param version_info: A 3-tuple of ints representing the Python
        major-minor-micro version to check.
    :param ignore_requires_python: Whether to ignore the "Requires-Python"
        value if the given Python version isn't compatible.
    )Úversion_infoz2Ignoring invalid Requires-Python (%r) for link: %sÚ.z4Link requires a different Python (%s not in: %r): %sFzBIgnoring failed Requires-Python check (%s not in: %r) for link: %sT)	r    Úrequires_pythonr   ÚInvalidSpecifierro   rp   ÚjoinÚmapr�   )rr   rŠ   Úignore_requires_pythonZis_compatibleÚversionrH   rH   rI   Ú_check_link_requires_python  s$    r’   c               @   s,   e Zd ZdZejdƒZddd„Zdd„ ZdS )	ÚLinkEvaluatorzD
    Responsible for evaluating links for a particular project.
    z-py([123]\.?[0-9]?)$Nc             C   s4   |dkrd}|| _ || _|| _|| _|| _|| _dS )aÕ  
        :param project_name: The user supplied package name.
        :param canonical_name: The canonical package name.
        :param formats: The formats allowed for this package. Should be a set
            with 'binary' or 'source' or both in it.
        :param target_python: The target Python interpreter to use when
            evaluating link compatibility. This is used, for example, to
            check wheel compatibility, as well as when checking the Python
            version, e.g. the Python version embedded in a link filename
            (or egg fragment) and against an HTML link's optional PEP 503
            "data-requires-python" attribute.
        :param allow_yanked: Whether files marked as yanked (in the sense
            of PEP 592) are permitted to be candidates for install.
        :param ignore_requires_python: Whether to ignore incompatible
            PEP 503 "data-requires-python" values in HTML links. Defaults
            to False.
        NF)Ú_allow_yankedÚ_canonical_nameÚ_ignore_requires_pythonÚ_formatsÚ_target_pythonÚproject_name)rT   r™   Úcanonical_nameÚformatsÚtarget_pythonÚallow_yankedr�   rH   rH   rI   rQ   =  s    zLinkEvaluator.__init__c             C   sì  d}|j r*| j r*|jpd}ddj|ƒfS |jr@|j}|j}� nð|jƒ \}}|sTdS |tkrhdd| fS d| jkrŒ|t	krŒd| j
 }d|fS d	|jkr¢|d
kr¢dS |t	k�r0yt|jƒ}W n tk
rÎ   dS X t|jƒ| jk� rôd| j
 }d|fS | jjƒ }|j|ƒ�s*|jƒ }djdj|ƒƒ}d|fS |j}d| jk�rT|t	k�rTdd| j
 fS |�sft|| jƒ}|�szdd| j
 fS | jj|ƒ}	|	�r¸|d|	jƒ … }|	jdƒ}
|
| jjk�r¸dS t|| jj| j d�}|�sÖdS t!j"d||ƒ d|fS )aG  
        Determine whether a link is a candidate for installation.

        :return: A tuple (is_candidate, result), where `result` is (1) a
            version string if `is_candidate` is True, and (2) if
            `is_candidate` is False, an optional string to log the reason
            the link fails to qualify.
        Nz<none given>Fzyanked for reason: {}ú
not a filezunsupported archive format: %sÚbinaryzNo binaries permitted for %sÚmacosx10z.zipúmacosx10 oneúinvalid wheel filenamezwrong project name (not %s)z"none of the wheel's tags match: {}z, ÚsourcezNo sources permitted for %szMissing project version for %srw   úPython version is incorrect)rŠ   r�   zFound link %s, version: %sT)Frž   )Fr¡   )Fr¢   )Fr¤   )FN)#Ú	is_yankedr”   Úyanked_reasonÚformatÚegg_fragmentÚextÚsplitextr   r—   r   r™   ri   r"   rK   r   r   Únamer•   r˜   Úget_tagsÚ	supportedZget_formatted_file_tagsrŽ   r‘   Ú_extract_version_from_fragmentÚ_py_version_reÚsearchÚstartÚgroupÚ
py_versionr’   Zpy_version_infor–   ro   rp   )rT   rr   r‘   rs   Úegg_infor©   ÚwheelÚsupported_tagsÚ	file_tagsÚmatchr³   Zsupports_pythonrH   rH   rI   Úevaluate_linkc  sh    









zLinkEvaluator.evaluate_link)N)	rV   rW   rX   Ú__doc__ÚreÚcompiler¯   rQ   r¹   rH   rH   rH   rI   r“   1  s   

r“   c       
      C   sâ   |st jdt| ƒ|ƒ t| ƒS g }g }d}xF| D ]>}|j}|jsBn"|j|d�rX|d7 }n|j|ƒ q0|j|ƒ q0W |r||}nt| ƒ}t|ƒt| ƒkršd}	n djt|ƒdj	dd	„ |D ƒƒƒ}	t jd
t| ƒ||j
|t|ƒ| |	ƒ |S )aÆ  
    Filter out candidates whose hashes aren't allowed, and return a new
    list of candidates.

    If at least one candidate has an allowed hash, then all candidates with
    either an allowed hash or no hash specified are returned.  Otherwise,
    the given candidates are returned.

    Including the candidates with no hash specified when there is a match
    allows a warning to be logged if there is a more preferred candidate
    with no hash specified.  Returning all candidates in the case of no
    matches lets pip report the hash of the candidate that would otherwise
    have been installed (e.g. permitting the user to more easily update
    their requirements file with the desired hash).
    zJGiven no hashes to check %s links for project %r: discarding no candidatesr   )Úhashesrw   zdiscarding no candidateszdiscarding {} non-matches:
  {}z
  c             s   s   | ]}t |jƒV  qd S )N)r�   rr   )Ú.0Ú	candidaterH   rH   rI   ú	<genexpr>ö  s    z*filter_unallowed_hashes.<locals>.<genexpr>zPChecked %s links for project %r against %s hashes (%s matches, %s no digest): %s)ro   rp   rE   Úlistrr   Zhas_hashÚis_hash_allowedÚappendr§   rŽ   Zdigest_count)
Ú
candidatesr½   r™   Zmatches_or_no_digestZnon_matchesZmatch_countr¿   rr   ÚfilteredZdiscard_messagerH   rH   rI   Úfilter_unallowed_hashes¼  sF    



rÆ   c               @   s   e Zd ZdZddd„ZdS )ÚCandidatePreferenceszk
    Encapsulates some of the preferences for filtering and sorting
    InstallationCandidate objects.
    Fc             C   s   || _ || _dS )zR
        :param allow_all_prereleases: Whether to allow all pre-releases.
        N)Úallow_all_prereleasesÚprefer_binary)rT   rÉ   rÈ   rH   rH   rI   rQ     s    	zCandidatePreferences.__init__N)FF)rV   rW   rX   rº   rQ   rH   rH   rH   rI   rÇ     s    rÇ   c               @   sH   e Zd ZdZeddd„ƒZddd„Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚCandidateEvaluatorzm
    Responsible for filtering and sorting candidates for installation based
    on what tags are valid.
    NFc             C   s:   |dkrt ƒ }|dkrtjƒ }|jƒ }| ||||||d�S )a@  Create a CandidateEvaluator object.

        :param target_python: The target Python interpreter to use when
            checking compatibility. If None (the default), a TargetPython
            object will be constructed from the running Python.
        :param hashes: An optional collection of allowed hashes.
        N)r™   r¶   Ú	specifierrÉ   rÈ   r½   )r   r   ÚSpecifierSetr¬   )Úclsr™   rœ   rÉ   rÈ   rË   r½   r¶   rH   rH   rI   Úcreate"  s    zCandidateEvaluator.createc             C   s(   || _ || _|| _|| _|| _|| _dS )z“
        :param supported_tags: The PEP 425 tags supported by the target
            Python in order of preference (most preferred first).
        N)Ú_allow_all_prereleasesÚ_hashesÚ_prefer_binaryÚ_project_nameÚ
_specifierÚ_supported_tags)rT   r™   r¶   rË   rÉ   rÈ   r½   rH   rH   rI   rQ   D  s    zCandidateEvaluator.__init__c                sV   | j pd}| j}dd„ |jdd„ |D ƒ|d�D ƒ‰ ‡ fdd„|D ƒ}t|| j| jd	�S )
zM
        Return the applicable candidates from a list of candidates.
        Nc             S   s   h | ]}t |ƒ’qS rH   )r�   )r¾   ÚvrH   rH   rI   ú	<setcomp>d  s   z?CandidateEvaluator.get_applicable_candidates.<locals>.<setcomp>c             s   s   | ]}t |jƒV  qd S )N)r�   r‘   )r¾   ÚcrH   rH   rI   rÀ   m  s    z?CandidateEvaluator.get_applicable_candidates.<locals>.<genexpr>)Úprereleasesc                s   g | ]}t |jƒˆ kr|‘qS rH   )r�   r‘   )r¾   r×   )ÚversionsrH   rI   ú
<listcomp>t  s    z@CandidateEvaluator.get_applicable_candidates.<locals>.<listcomp>)rÄ   r½   r™   )rÏ   rÓ   ÚfilterrÆ   rÐ   rÒ   )rT   rÄ   Zallow_prereleasesrË   Úapplicable_candidatesrH   )rÙ   rI   Úget_applicable_candidatesY  s    	
z,CandidateEvaluator.get_applicable_candidatesc             C   s   | j |ƒ}t||| d�S )zç
        Create and return a `FoundCandidates` instance.

        :param specifier: An optional object implementing `filter`
            (e.g. `packaging.specifiers.SpecifierSet`) to filter applicable
            versions.
        )rÜ   Ú	evaluator)rÝ   r8   )rT   rÄ   rÜ   rH   rH   rI   Úmake_found_candidates}  s
    
z(CandidateEvaluator.make_found_candidatesc             C   sÈ   | j }t|ƒ}tƒ }d}|j}|jr’t|jƒ}|j|ƒsFtd|j ƒ‚| j	rPd}|j
|ƒ }|jdk	r˜tjd|jƒ}	|	jƒ }
t|
d ƒ|
d f}n| }t|j| jƒƒ}dt|jƒ }||||j||fS )a)  
        Function to pass as the `key` argument to a call to sorted() to sort
        InstallationCandidates by preference.

        Returns a tuple such that tuples sorting as greater using Python's
        default comparison operator are more preferred.

        The preference is as follows:

        First and foremost, candidates with allowed (matching) hashes are
        always preferred over candidates without matching hashes. This is
        because e.g. if the only candidate with an allowed hash is yanked,
        we still want to use that candidate.

        Second, excepting hash considerations, candidates that have been
        yanked (in the sense of PEP 592) are always less preferred than
        candidates that haven't been yanked. Then:

        If not finding wheels, they are 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._supported_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.rw   Nz^(\d+)(.*)$éÿÿÿÿ)rÔ   rE   Útuplerr   Úis_wheelr"   rK   r­   r   rÑ   Úsupport_index_minÚ	build_tagr»   r¸   ÚgroupsÚintrÂ   rÐ   r¥   r‘   )rT   r¿   Ú
valid_tagsÚsupport_numrä   Zbinary_preferencerr   rµ   Úprir¸   Zbuild_tag_groupsZhas_allowed_hashZ
yank_valuerH   rH   rI   Ú	_sort_key‘  s.    




zCandidateEvaluator._sort_keyc             C   sH   |sdS t || jd�}|j}|jrD|jp*d}dj||d�}tj|ƒ |S )zy
        Return the best candidate per the instance's sort order, or None if
        no candidate is acceptable.
        N)Úkeyz<none given>zqThe candidate selected for download or install is a yanked version: {candidate}
Reason for being yanked: {reason})r¿   rs   )Úmaxrê   rr   r¥   r¦   r§   ro   Úwarning)rT   rÄ   Úbest_candidaterr   rs   ÚmsgrH   rH   rI   Úget_best_candidateÍ  s    	


z%CandidateEvaluator.get_best_candidate)NFFNN)FFN)rV   rW   rX   rº   ÚclassmethodrÎ   rQ   rÝ   rß   rê   rð   rH   rH   rH   rI   rÊ     s         
$<rÊ   c               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )r8   z¿A collection of candidates, returned by `PackageFinder.find_candidates`.

    This class is only intended to be instantiated by CandidateEvaluator's
    `make_found_candidates()` method.
    c             C   s   || _ || _|| _dS )a  
        :param candidates: A sequence of all available candidates found.
        :param applicable_candidates: The applicable candidates.
        :param evaluator: A CandidateEvaluator object to sort applicable
            candidates by order of preference.
        N)Ú_applicable_candidatesÚ_candidatesÚ
_evaluator)rT   rÄ   rÜ   rÞ   rH   rH   rI   rQ   ó  s    zFoundCandidates.__init__c             C   s
   t | jƒS )z(Iterate through all candidates.
        )Úiterró   )rT   rH   rH   rI   Úiter_all  s    zFoundCandidates.iter_allc             C   s
   t | jƒS )z3Iterate through the applicable candidates.
        )rõ   rò   )rT   rH   rH   rI   Úiter_applicable
  s    zFoundCandidates.iter_applicablec             C   s   t | jƒ ƒ}| jj|ƒS )zdReturn the best candidate available, or None if no applicable
        candidates are found.
        )rÁ   r÷   rô   rð   )rT   rÄ   rH   rH   rI   Úget_best  s    zFoundCandidates.get_bestN)rV   rW   rX   rº   rQ   rö   r÷   rø   rH   rH   rH   rI   r8   ì  s
   c               @   sÐ   e Zd ZdZd,dd„Zed-dd„ƒZedd„ ƒZed	d
„ ƒZ	edd„ ƒZ
dd„ Zd.dd„Zdd„ Zed/dd„ƒZdd„ Zdd„ Zdd„ Zd0dd„Zd1dd„Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ ZdS )2r9   z This finds packages.

    This is meant to match easy_install's technique for looking for
    packages, by reading pages and looking for appropriate links.
    Nc	       	      C   sh   |dkrg }|dkrt ƒ }|p*ttƒ tƒ ƒ}|| _|| _|| _|| _|| _|| _|| _	|| _
tƒ | _dS )aÐ  
        This constructor is primarily meant to be used by the create() class
        method and from tests.

        :param session: The Session to use to make requests.
        :param format_control: A FormatControl object, used to control
            the selection of source packages / binary packages when consulting
            the index and links.
        :param candidate_prefs: Options to use when creating a
            CandidateEvaluator object.
        N)rÇ   r   Úsetr”   Ú_candidate_prefsr–   r˜   Úsearch_scoperg   Úformat_controlÚtrusted_hostsÚ_logged_links)	rT   rû   rg   rœ   r�   rü   rý   Úcandidate_prefsr�   rH   rH   rI   rQ      s    zPackageFinder.__init__c          
   C   sL   |dkrt dƒ‚|dkrtƒ }t|j|jd�}| |||||j|j||jd�S )a  Create a PackageFinder.

        :param selection_prefs: The candidate selection preferences, as a
            SelectionPreferences object.
        :param trusted_hosts: Domains not to emit warnings for when not using
            HTTPS.
        :param session: The Session to use to make requests.
        :param target_python: The target Python interpreter to use when
            checking compatibility. If None (the default), a TargetPython
            object will be constructed from the running Python.
        NzEPackageFinder.create() missing 1 required keyword argument: 'session')rÉ   rÈ   )rÿ   rû   rg   rœ   r�   rü   rý   r�   )ry   r   rÇ   rÉ   rÈ   r�   rü   r�   )rÍ   rû   Zselection_prefsrý   rg   rœ   rÿ   rH   rH   rI   rÎ   O  s"    
zPackageFinder.createc             C   s   | j jS )N)rû   Ú
find_links)rT   rH   rH   rI   r   |  s    zPackageFinder.find_linksc             C   s   | j jS )N)rû   Ú
index_urls)rT   rH   rH   rI   r  �  s    zPackageFinder.index_urlsc             C   s   | j jS )N)rú   rÈ   )rT   rH   rH   rI   rÈ   †  s    z#PackageFinder.allow_all_prereleasesc             C   s   d| j _d S )NT)rú   rÈ   )rT   rH   rH   rI   Úset_allow_all_prereleases‹  s    z'PackageFinder.set_allow_all_prereleasesc             C   sT   dj |ƒ}|dk	r |dj |ƒ7 }tj|ƒ | jj|ƒ || jkrDdS | jj|ƒ dS )zt
        :param source: An optional source string, for logging where the host
            string came from.
        zadding trusted host: {!r}Nz
 (from {}))r§   ro   r‚   rg   Zadd_insecure_hostrý   rÃ   )rT   Úhostr£   rï   rH   rH   rI   Úadd_trusted_host�  s    


zPackageFinder.add_trusted_hostc             c   s4   xt D ]
}|V  qW x| jD ]}d|dfV  qW d S )Nr;   )ÚSECURE_ORIGINSrý   )rT   Úsecure_originr  rH   rH   rI   Úiter_secure_origins¡  s    

z!PackageFinder.iter_secure_originsFc                s  g ‰ g ‰‡ ‡fdd„}xæ| D ]Þ}t jj|ƒ}|jdƒ}|s>|rÚ|rH|}nt|ƒ}t jj|ƒr¶|r”t jj|ƒ}xFt j|ƒD ]}|t jj||ƒƒ qxW qØ|r¤ˆj	|ƒ qØt
jdj|ƒƒ qút jj|ƒrÌ||ƒ qút
jd|ƒ qt|ƒrîˆj	|ƒ qt
j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*ˆj|ƒ n
ˆ j|ƒ d S )NF)Ústrictr   z	text/html)r   Ú	mimetypesÚ
guess_typerÃ   )ri   rF   )ÚfilesÚurlsrH   rI   Ú	sort_path³  s    z0PackageFinder._sort_locations.<locals>.sort_pathzfile:z)Path '{0}' is ignored: it is a directory.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.)r|   ri   ÚexistsrD   r   r}   ÚrealpathÚlistdirrŽ   rÃ   ro   rí   r§   Úisfiler   )Ú	locationsÚ
expand_dirr  rF   Úis_local_pathÚis_file_urlri   ÚitemrH   )r  r  rI   Ú_sort_locations¨  s>    




zPackageFinder._sort_locationsc       	      C   sj  t jt|ƒƒ}|j|j|jf}|d jddƒd
 }�x| jƒ D �]}||d kr^|d dkr^q>yhtj	t
|d tjƒs€|d d krˆ|d n|d jdƒƒ}tjt
|d tjƒr´|d n|d jdƒƒ}W nJ tk
�r   |d �r|d jƒ |d jƒ k�r|d dk�rw>Y nX ||k�rq>|d |d k�rN|d dk�rN|d d k	�rNq>dS W |jd|j|jƒ d	S )Nr   ú+rw   r;   Úutf8é   TzõThe 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'.Frà   )rc   r{   r�   rG   ÚhostnameÚportÚrsplitr  r   Ú
ip_addressÚ
isinstancer   Ú	text_typeÚdecodeÚ
ip_networkÚ
ValueErrorrC   rí   )	rT   ro   ÚlocationÚparsedÚoriginÚprotocolr  ÚaddrÚnetworkrH   rH   rI   Ú_validate_secure_originâ  s>    

z%PackageFinder._validate_secure_originc             C   s.   t |ƒ}| jj|ƒ}t|||| j| j| jd�S )N)r™   rš   r›   rœ   r�   r�   )r   rü   Zget_allowed_formatsr“   r˜   r”   r–   )rT   r™   rš   r›   rH   rH   rI   Úmake_link_evaluator,  s    z!PackageFinder.make_link_evaluatorc                sZ  ˆ j }|j|ƒ}ˆ j|ƒ\}}ˆ jˆ jdd�\}}dd„ tj||ƒD ƒ}‡ fdd„tjdd„ |D ƒdd„ |D ƒƒD ƒ}	tjd	t|	ƒ|ƒ x|	D ]}
tjd
|
ƒ qŽW ˆ j	|ƒ}ˆ j
|dd„ ˆ jD ƒƒ}g }xJˆ j|	|ƒD ]:}tjd|jƒ tƒ � |jˆ j
||jƒ ƒƒ W dQ R X qÖW ˆ j
||ƒ}|�rN|jdd� tjddjdd„ |D ƒƒƒ || | S )a  Find all available InstallationCandidate for project_name

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

        See LinkEvaluator.evaluate_link() for details on which files
        are accepted.
        T)r  c             s   s   | ]}t |ƒV  qd S )N)r   )r¾   rF   rH   rH   rI   rÀ   K  s    z4PackageFinder.find_all_candidates.<locals>.<genexpr>c                s   g | ]}ˆ j t|ƒr|‘qS rH   )r*  ro   )r¾   rr   )rT   rH   rI   rÚ   S  s    z5PackageFinder.find_all_candidates.<locals>.<listcomp>c             s   s   | ]}t |ƒV  qd S )N)r   )r¾   rF   rH   rH   rI   rÀ   T  s    c             s   s   | ]}t |ƒV  qd S )N)r   )r¾   rF   rH   rH   rI   rÀ   U  s    z,%d location(s) to search for versions of %s:z* %sc             s   s   | ]}t |d ƒV  qdS )z-fN)r   )r¾   rF   rH   rH   rI   rÀ   d  s    zAnalyzing links from page %sN)ÚreversezLocal files found: %sz, c             S   s   g | ]}t |jjƒ‘qS rH   )r   rr   rF   )r¾   r¿   rH   rH   rI   rÚ   u  s   )rû   Zget_index_urls_locationsr  r   Ú	itertoolsÚchainro   rp   rE   r+  Ú_package_versionsÚ
_get_pagesrF   r   ÚextendÚ
iter_linksÚsortrŽ   )rT   r™   rû   Úindex_locationsÚindex_file_locÚindex_url_locÚfl_file_locÚ
fl_url_locÚfile_locationsÚurl_locationsr$  Úlink_evaluatorÚfind_links_versionsÚpage_versionsÚpageÚfile_versionsrH   )rT   rI   Úfind_all_candidates:  sB    




 z!PackageFinder.find_all_candidatesc             C   s"   | j }tj|| j|j|j||d�S )z3Create a CandidateEvaluator object to use.
        )r™   rœ   rÉ   rÈ   rË   r½   )rú   rÊ   rÎ   r˜   rÉ   rÈ   )rT   r™   rË   r½   rÿ   rH   rH   rI   Úmake_candidate_evaluator}  s    	z&PackageFinder.make_candidate_evaluatorc             C   s$   | j |ƒ}| j|||d�}|j|ƒS )a  Find matches for the given project and specifier.

        :param specifier: An optional object implementing `filter`
            (e.g. `packaging.specifiers.SpecifierSet`) to filter applicable
            versions.

        :return: A `FoundCandidates` instance.
        )r™   rË   r½   )r@  rA  rß   )rT   r™   rË   r½   rÄ   Zcandidate_evaluatorrH   rH   rI   Úfind_candidates�  s    
zPackageFinder.find_candidatesc       	      C   s
  |j dd�}| j|j|j|d�}|jƒ }d}|jdk	rBt|jjƒ}dd„ }|dkr||dkr|tj	d|||j
ƒ ƒƒ td| ƒ‚d}|rš|dks–|j|kršd	}| rÎ|dk	rÎ|rºtjd
|ƒ ntjd||jƒ dS |rìtjd|||jƒ ƒƒ t‚tjd|j||jƒ ƒƒ |jS )zÙTry to find a Link matching req

        Expects req, an InstallRequirement and upgrade, a boolean
        Returns a Link if found,
        Raises DistributionNotFound or BestVersionAlreadyInstalled otherwise
        F)Útrust_internet)rË   r½   Nc             S   s    dj tdd„ | D ƒtd�ƒpdS )Nz, c             S   s   h | ]}t |jƒ’qS rH   )r�   r‘   )r¾   r×   rH   rH   rI   rÖ   ¿  s    zKPackageFinder.find_requirement.<locals>._format_versions.<locals>.<setcomp>)rë   Únone)rŽ   ÚsortedÚparse_version)Z	cand_iterrH   rH   rI   Ú_format_versions¹  s    
z8PackageFinder.find_requirement.<locals>._format_versionszNCould not find a version that satisfies the requirement %s (from versions: %s)z%No matching distribution found for %sTzLExisting 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)z)Using version %s (newest of versions: %s))r½   rB  r«   rË   rø   Úsatisfied_byrF  r‘   ro   Úcriticalrö   r   rp   r÷   r   rr   )	rT   ÚreqÚupgrader½   rÄ   rî   Úinstalled_versionrG  Úbest_installedrH   rH   rI   Úfind_requirement§  sR    



zPackageFinder.find_requirementc             c   sJ   t ƒ }x>|D ]6}||krq|j|ƒ t|| jd�}|dkr<q|V  qW dS )zp
        Yields (page, page_url) from the given locations, skipping
        locations that have errors.
        )rg   N)rù   Úaddr‰   rg   )rT   r  r™   Úseenr$  r>  rH   rH   rI   r0  ö  s    

zPackageFinder._get_pagesc             C   sT   g g  }}t ƒ }x:|D ]2}||kr|j|ƒ |jr>|j|ƒ q|j|ƒ qW || S )z�
        Returns elements of links in order, non-egg links first, egg links
        second, while eliminating duplicates
        )rù   rO  r¨   rÃ   )rT   ÚlinksÚeggsÚno_eggsrP  rr   rH   rH   rI   Ú_sort_links  s    


zPackageFinder._sort_linksc             C   s(   || j kr$tjd||ƒ | j j|ƒ d S )NzSkipping link: %s: %s)rþ   ro   rp   rO  )rT   rr   rs   rH   rH   rI   Ú_log_skipped_link  s    
zPackageFinder._log_skipped_linkc             C   s<   |j |ƒ\}}|s(|r$| j||d� dS t|j|t|ƒd�S )z�
        If the link is a candidate for install, convert it to an
        InstallationCandidate and return it. Otherwise, return None.
        )rs   N)Úprojectrr   r‘   )r¹   rU  r   r™   r�   )rT   r;  rr   Zis_candidateÚresultrH   rH   rI   Úget_install_candidate$  s    z#PackageFinder.get_install_candidatec             C   s:   g }x0| j |ƒD ]"}| j||ƒ}|d k	r|j|ƒ qW |S )N)rT  rX  rÃ   )rT   r;  rQ  rW  rr   r¿   rH   rH   rI   r/  8  s    zPackageFinder._package_versions)NNNN)NNN)N)F)NN)NN)rV   rW   rX   rº   rQ   rñ   rÎ   Úpropertyr   r  rÈ   r  r  r  Ústaticmethodr  r*  r+  r@  rA  rB  rN  r0  rT  rU  rX  r/  rH   rH   rH   rI   r9     s<      
&  &
9JF 
 
Oc             C   sL   x6t | ƒD ]*\}}|dkrq
t| d|… ƒ|kr
|S q
W tdj| |ƒƒ‚dS )aö  Find the separator's index based on the package's canonical name.

    :param fragment: A <package>+<version> filename "fragment" (stem) or
        egg fragment.
    :param canonical_name: The package's canonical name.

    This function is needed since the canonicalized name does not necessarily
    have the same length as the egg info's name part. An example::

    >>> fragment = 'foo__bar-1.0'
    >>> canonical_name = 'foo-bar'
    >>> _find_name_version_sep(fragment, canonical_name)
    8
    ú-Nz{} does not match {})Ú	enumerater   r#  r§   )rk   rš   Úir×   rH   rH   rI   Ú_find_name_version_sepB  s    r^  c             C   s@   yt | |ƒd }W n tk
r&   dS X | |d… }|s<dS |S )zúParse the version string from a <package>+<version> filename
    "fragment" (stem) or egg fragment.

    :param fragment: The string to parse. E.g. foo-2.1
    :param canonical_name: The canonicalized name of the package this
        belongs to.
    rw   N)r^  r#  )rk   rš   Zversion_startr‘   rH   rH   rI   r®   ]  s    	r®   c             C   s.   x(| j dƒD ]}|j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)Úfindallr\   )ÚdocumentÚpage_urlÚbaser_  rH   rH   rI   Ú_determine_base_urlp  s
    
rd  c             C   s2   | r.d| kr.t j| 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)r[   rR   ÚparamsrH   rH   rI   Ú_get_encoding_from_headersƒ  s
    ri  c             C   sP   t j| ƒ}|jdkr(tjtj|jƒƒ}nt jt j|jƒdd�}t j	|j
|d�ƒS )z Makes 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).rZ   z/@)Úsafe)ri   )rc   r{   rh   r~   Úpathname2urlr   ri   ÚquoteÚunquoteÚ
urlunparseÚ_replace)rF   rW  ri   rH   rH   rI   Ú_clean_link�  s    	

rp  c             C   sf   | j dƒ}|sdS ttj||ƒƒ}| j dƒ}|r8t|ƒnd}| j dƒ}|rRt|ƒ}t||||d�}|S )zJ
    Convert an anchor element in a simple repository page to a Link.
    r_  Nzdata-requires-pythonzdata-yanked)Ú
comes_fromrŒ   r¦   )r\   rp  rc   r€   r   r   )Úanchorrb  Úbase_urlr_  rF   Ú	pyrequirer¦   rr   rH   rH   rI   Ú_create_link_from_element§  s    	


ru  c               @   s*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
r…   z'Represents one page, along with its URLNc             C   s   || _ || _|| _d S )N)r†   rF   r[   )rT   r†   rF   r[   rH   rH   rI   rQ   Ê  s    zHTMLPage.__init__c             C   s
   t | jƒS )N)r   rF   )rT   rH   rH   rI   Ú__str__Ð  s    zHTMLPage.__str__c             c   s\   t j| jt| jƒdd�}t|| jƒ}x2|jdƒD ]$}t|| j|d�}|dkrNq0|V  q0W dS )zYields all links in the pageF)Útransport_encodingÚnamespaceHTMLElementsz.//a)rb  rs  N)	r   r	   r†   ri  r[   rd  rF   r`  ru  )rT   ra  rs  rr  rr   rH   rH   rI   r2  Ó  s    zHTMLPage.iter_links)N)rV   rW   rX   rº   rQ   rv  r2  rH   rH   rH   rI   r…   Ç  s   
r…   )r:   r;   r;   )r;   r<   r;   )r;   r=   r;   )r;   r>   r;   )r?   r;   N)r@   r;   r;   )N)N)F)„rº   Ú
__future__r   rf  r-  Úloggingr	  r|   r»   Úpip._vendorr   r   r   Zpip._vendor.distlib.compatr   Zpip._vendor.packagingr   Úpip._vendor.packaging.utilsr   Zpip._vendor.packaging.versionr	   rF  Zpip._vendor.requests.exceptionsr
   r   r   Zpip._vendor.six.moves.urllibrc   r   r~   Zpip._internal.downloadr   r   Zpip._internal.exceptionsr   r   r   r   Zpip._internal.models.candidater   Z#pip._internal.models.format_controlr   Zpip._internal.models.linkr   Z$pip._internal.models.selection_prefsr   Z"pip._internal.models.target_pythonr   Zpip._internal.utils.compatr   Zpip._internal.utils.loggingr   Zpip._internal.utils.miscr   r   r   r   r   Zpip._internal.utils.packagingr    Zpip._internal.utils.typingr!   Zpip._internal.wheelr"   r#   Útypingr$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   Úxml.etree.ElementTreeÚxmlr1   Zpip._vendor.requestsr2   Z!pip._internal.models.search_scoper3   Zpip._internal.reqr4   r5   Zpip._internal.pep425tagsr6   Zpip._internal.utils.hashesr7   ZBuildTagræ   ZCandidateSortingKeyÚetreeÚElementTreeÚElementZHTMLElementr�   ZSecureOriginÚ__all__r  Ú	getLoggerrV   ro   rJ   rN   Ú	ExceptionrO   r_   r`   rm   rq   ru   r‰   r’   Úobjectr“   rÆ   rÇ   rÊ   r8   r9   r^  r®   rd  ri  rp  ru  r…   rH   rH   rH   rI   Ú<module>   s    <


6

9
( K R-    -
 