o 7]h @sddZddlZddlZddlZddlZddlmZmZmZm Z m Z m Z m Z ddl mZddlmZddlmZddlmZddlmZdd lmZmZmZmZdd lmZmZdd lm Z dd l!m"Z"dd l#m$Z$ddl%m&Z&ddl'm(Z(ddl)m*Z*ddl+m,Z,ddl-m.Z.ddl/m0Z0ddl1m2Z2ddl3m4Z4ddl5m6Z6ddl7m8Z8ddl9m:Z:ddl;mZ?e e de e@eAffZBe e@e@e@ee e@eBfZC d8de$de e@e@e@fdeDd eDfd!d"ZEGd#d$d$ZFd%ee d&e4d'eAd ee fd(d)ZGGd*d+d+ZHGd,d-d-ZIGd.d/d/ZJGd0d1d1ZKd2eAd3eAd e@fd4d5ZLd2eAd3eAd e eAfd6d7ZMdS)9z!Routines related to PyPI, indexesN) FrozenSetIterableListOptionalSetTupleUnion) specifiers)Tag)canonicalize_name) _BaseVersion)parse)BestVersionAlreadyInstalledDistributionNotFoundInvalidWheelFilenameUnsupportedWheel) LinkCollector parse_links)InstallationCandidate) FormatControl)Link) SearchScope)SelectionPreferences) TargetPython)Wheel)InstallRequirement) getLogger)WHEEL_EXTENSION)Hashes) indent_log) build_netloc)check_requires_python)SUPPORTED_EXTENSIONS)rBestCandidateResult PackageFinderFlink version_infoignore_requires_pythonreturncCs~z t|j|d}Wntjytd|j|YdSw|s=dtt|}|s4t d||j|dStd||j|dS)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. )r'z2Ignoring 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 InvalidSpecifierloggerdebugjoinmapstrverbose)r&r'r( is_compatibleversionr%r%D/usr/lib/python3/dist-packages/pip/_internal/index/package_finder.py_check_link_requires_python2s< r6c@sjeZdZdZedZ ddededeede de d e e d dfd d Z d e d ee e effddZdS) LinkEvaluatorzD Responsible for evaluating links for a particular project. z-py([123]\.?[0-9]?)$N project_namecanonical_nameformats target_python allow_yankedr(r)cCs4|durd}||_||_||_||_||_||_dS)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_pythonr8)selfr8r9r:r;r<r(r%r%r5__init__ms zLinkEvaluator.__init__r&c Csd}|jr|js|jp d}dd|fS|jr|j}|j}nx|\}}|s(dS|tvr3dd|fSd|jvrF|tkrFd |j }d|fSd |j vrQ|d krQd S|tkrzt |j }Wn tyfYd Swt|j|jkryd |j }d|fS|j}||s|}d d|}d|fS|j}d|jvr|tkrd|j }d|fS|st||j}|sd|j }d|fS|j|} | r|d| }| d} | |jjkrdSt||jj|j d} | sdSt!"d||d|fS)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 Fzyanked for reason: )Fz not a filezunsupported archive format: binaryzNo binaries permitted for {}macosx10z.zip)Fz macosx10 one)Fzinvalid wheel filenamezwrong project name (not {})z^none of the wheel's tags ({}) are compatible (run pip debug --verbose to show compatible tags), sourcezNo sources permitted for zMissing project version for )FzPython version is incorrect)r'r()FNzFound link %s, version: %sT)# is_yankedr= yanked_reason egg_fragmentextsplitextr"r@rformatr8pathrfilenamerr namer>rAget_tags supportedget_formatted_file_tagsr/r4_extract_version_from_fragment_py_version_researchstartgroup py_versionr6py_version_infor?r-r.) rBr&r4reasonegg_inforLwheelsupported_tags file_tagsmatchrZsupports_pythonr%r%r5 evaluate_links|             zLinkEvaluator.evaluate_linkN)__name__ __module__ __qualname____doc__recompilerVr1rrboolrrCrrrcr%r%r%r5r7as(   "%r7 candidateshashesr8c Cs|stdt||t|Sg}g}d}|D]}|j}|js n|j|dr+|d7}n||q||q|r<|}nt|}t|t|krKd} ndt|d dd |D} td 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)rmrHzdiscarding no candidateszdiscarding {} non-matches: {}z cs|]}t|jVqdSrd)r1r&).0 candidater%r%r5 #z*filter_unallowed_hashes..zPChecked %s links for project %r against %s hashes (%s matches, %s no digest): %s) r-r.lenlistr&has_hashis_hash_allowedappendrNr/ digest_count) rlrmr8matches_or_no_digest non_matches match_countrpr&filtereddiscard_messager%r%r5filter_unallowed_hashessL      r~c@s,eZdZdZ  d dededdfddZdS) CandidatePreferenceszk Encapsulates some of the preferences for filtering and sorting InstallationCandidate objects. F prefer_binaryallow_all_prereleasesr)NcCs||_||_dS)zR :param allow_all_prereleases: Whether to allow all pre-releases. N)rr)rBrrr%r%r5rC;s zCandidatePreferences.__init__)FF)rerfrgrhrkrCr%r%r%r5r4src@sZeZdZdZdeedeedeeddfddZdeefd d Z deefd d Z dS) r#zA collection of candidates, returned by `PackageFinder.find_best_candidate`. This class is only intended to be instantiated by CandidateEvaluator's `compute_best_candidate()` method. rlapplicable_candidatesbest_candidater)NcCsHt|t|ks J|dur|rJn||vsJ||_||_||_dS)a :param candidates: A sequence of all available candidates found. :param applicable_candidates: The applicable candidates. :param best_candidate: The most preferred candidate found, or None if no applicable candidates were found. N)set_applicable_candidates _candidatesrrBrlrrr%r%r5rCNs    zBestCandidateResult.__init__cC t|jS)zIterate through all candidates.)iterrrBr%r%r5iter_allf zBestCandidateResult.iter_allcCr)z*Iterate through the applicable candidates.)rrrr%r%r5iter_applicablejrz#BestCandidateResult.iter_applicable) rerfrgrhrrrrCrrrr%r%r%r5r#Gs r#c@seZdZdZe     ddedeedededee j d ee d dfd d Z   dded e ede j deded ee d dfddZde ed e efddZded efddZde ed eefddZde ed efddZdS)CandidateEvaluatorzm Responsible for filtering and sorting candidates for installation based on what tags are valid. NFr8r;rr specifierrmr)cCs:|durt}|durt}|}|||||||dS)aCreate 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 specifier: An optional object implementing `filter` (e.g. `packaging.specifiers.SpecifierSet`) to filter applicable versions. :param hashes: An optional collection of allowed hashes. N)r8r_rrrrm)rr SpecifierSetrR)clsr8r;rrrrmr_r%r%r5createvszCandidateEvaluator.creater_cCs<||_||_||_||_||_||_ddt|D|_dS)z :param supported_tags: The PEP 425 tags supported by the target Python in order of preference (most preferred first). cSsi|]\}}||qSr%r%)roidxtagr%r%r5 s z/CandidateEvaluator.__init__..N)_allow_all_prereleases_hashes_prefer_binary _project_name _specifier_supported_tags enumerate_wheel_tag_preferences)rBr8r_rrrrmr%r%r5rCs  zCandidateEvaluator.__init__rlcsd|jpd}|j}dd|jdd|D|dDfdd|D}t||j|jd }t||jd S) zM Return the applicable candidates from a list of candidates. NcSsh|]}t|qSr%)r1)rovr%r%r5 sz?CandidateEvaluator.get_applicable_candidates..csrnrdr1r4rocr%r%r5rqrrz?CandidateEvaluator.get_applicable_candidates..) prereleasescsg|] }t|jvr|qSr%rrversionsr%r5 sz@CandidateEvaluator.get_applicable_candidates..)rlrmr8key)rrfilterr~rrsorted _sort_key)rBrlallow_prereleasesrrfiltered_applicable_candidatesr%rr5get_applicable_candidatess  z,CandidateEvaluator.get_applicable_candidatesrpc Cs|j}t|}d}d}|j}|jrPt|j}z |||j }Wnty/t d |jw|j r5d}|j durOt d|j } | } t| d| df}n| }t||j} dt|j} | | ||j||fS)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%rzB{} is not a supported wheel for this platform. It can't be sorted.rHNz ^(\d+)(.*)$)rrsr&is_wheelrrPfind_most_preferred_tagr ValueErrorrrNr build_tagriragroupsintrvrrIr4) rBrp valid_tags support_numrbinary_preferencer&r^prirabuild_tag_groupshas_allowed_hash yank_valuer%r%r5rsF   zCandidateEvaluator._sort_keycCs|sdSt||jd}|S)zy Return the best candidate per the instance's sort order, or None if no candidate is acceptable. Nr)maxr)rBrlrr%r%r5sort_best_candidatesz&CandidateEvaluator.sort_best_candidatecCs"||}||}t|||dS)zF Compute and return a `BestCandidateResult` instance. )rr)rrr#rr%r%r5compute_best_candidate)s  z)CandidateEvaluator.compute_best_candidate)NFFNN)FFN)rerfrgrh classmethodr1rrrkr BaseSpecifierrrrr rCrrCandidateSortingKeyrrr#rr%r%r%r5rosr (  $D  rc@s^eZdZdZ   dCdededededeedee d eed dfd d Z e dDded e deeded df ddZ ed efddZed efddZejded dfddZed eefddZed eefddZed eefddZed efddZdEddZed efd d!ZdEd"d#Zd$ed efd%d&Zd'eed eefd(d)Z d*ed+ed dfd,d-Z!d.ed*ed ee"fd/d0Z#d.ed'eed ee"fd1d2Z$d3ed.ed ee"fd4d5Z%e&j'dd6d$ed ee"fd7d8Z(  dFd$ed9ee)j*d:ee+d e,fd;d<Z-e&j'dd6  dFd$ed9ee)j*d:ee+d e.fd=d>Z/d?e0d@ed ee"fdAdBZ1dS)Gr$zThis finds packages. This is meant to match easy_install's technique for looking for packages, by reading pages and looking for appropriate links. Nlink_collectorr;r<use_deprecated_html5libformat_controlcandidate_prefsr(r)cCsV|durt}|pttt}||_||_||_||_||_||_||_ t|_ dS)a This constructor is primarily meant to be used by the create() class method and from tests. :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) rrrr=_candidate_prefsr?_link_collectorrA_use_deprecated_html5libr _logged_links)rBrr;r<rrrr(r%r%r5rCBs zPackageFinder.__init__selection_prefsc Cs:|durt}t|j|jd}|||||j|j|j|dS)afCreate a PackageFinder. :param selection_prefs: The candidate selection preferences, as a SelectionPreferences 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. N)rr)rrr;r<rr(r)rrrrr<rr()rrrr;rrr%r%r5rkszPackageFinder.createcCs|jSrd)rArr%r%r5r;szPackageFinder.target_pythoncC|jjSrdr search_scoperr%r%r5rzPackageFinder.search_scopercCs ||j_dSrdr)rBrr%r%r5rs cCrrd)r find_linksrr%r%r5rrzPackageFinder.find_linkscCrrd)r index_urlsrr%r%r5rrzPackageFinder.index_urlsccs |jjjD]}t|VqdSrd)rsessionpip_trusted_originsr )rB host_portr%r%r5 trusted_hostss zPackageFinder.trusted_hostscCrrdrrrr%r%r5rrz#PackageFinder.allow_all_prereleasescC d|j_dSNTrrr%r%r5set_allow_all_prereleases z'PackageFinder.set_allow_all_prereleasescCrrdrrrr%r%r5rrzPackageFinder.prefer_binarycCrrrrr%r%r5set_prefer_binaryrzPackageFinder.set_prefer_binaryr8cCs.t|}|j|}t||||j|j|jdS)N)r8r9r:r;r<r()r rget_allowed_formatsr7rAr=r?)rBr8r9r:r%r%r5make_link_evaluators z!PackageFinder.make_link_evaluatorlinkscCsPgg}}t}|D]}||vr#|||jr||q ||q ||S)z Returns elements of links in order, non-egg links first, egg links second, while eliminating duplicates )raddrKrw)rBreggsno_eggsseenr&r%r%r5 _sort_linkss    zPackageFinder._sort_linksr&r\cCs,||jvrtd|||j|dSdS)NzSkipping link: %s: %s)rr-r.r)rBr&r\r%r%r5_log_skipped_links zPackageFinder._log_skipped_linklink_evaluatorcCs8||\}}|s|r|j||ddSt|j||dS)z If the link is a candidate for install, convert it to an InstallationCandidate and return it. Otherwise, return None. )r\N)rQr&r4)rcrrr8)rBrr& is_candidateresultr%r%r5get_install_candidatesz#PackageFinder.get_install_candidatecCs6g}||D]}|||}|dur||q|S)zU Convert links that are candidates to InstallationCandidate objects. N)rrrw)rBrrrlr&rpr%r%r5evaluate_linkss  zPackageFinder.evaluate_links project_urlcCsntd||j|}|durgStt||j}t|j||d}Wd|S1s0wY|S)Nz-Fetching project page and analyzing links: %s)r) r-r.r fetch_pagertrrrr)rBrr html_page page_links package_linksr%r%r5process_project_urls"  z!PackageFinder.process_project_url)maxsizec Cs||}|jj|tj|j|dd}tjdd|D}t |}tjdd|D}| |t |dd}t tjrn|rng}|D]!} | jjsKJz || jjWqCtyd|| jjYqCwt dd |||S) aFind 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. )r)r8candidates_from_pagecs*|]}|D] }|dur|VqqdSrd)page_candidatesrosourcesrGr%r%r5rq#z4PackageFinder.find_all_candidates..csrrd) file_linksrr%r%r5rq+rT)reversezLocal files found: %srF)rrcollect_sources functoolspartialr itertoolschain from_iterablertrrr- isEnabledForloggingDEBUGr&urlrw file_path Exceptionr.r/) rBr8rcollected_sourcespage_candidates_itr file_links_itfile_candidatespathsrpr%r%r5find_all_candidatess<      z!PackageFinder.find_all_candidatesrrmcCs"|j}tj||j|j|j||dS)z*Create a CandidateEvaluator object to use.)r8r;rrrrm)rrrrArr)rBr8rrmrr%r%r5make_candidate_evaluatorDsz&PackageFinder.make_candidate_evaluatorcCs$||}|j|||d}||S)aFind 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 `BestCandidateResult` instance. )r8rrm)r r r)rBr8rrmrlcandidate_evaluatorr%r%r5find_best_candidateUs  z!PackageFinder.find_best_candidaterequpgradec Cs|jdd}|j|j|j|d}|j}d}|jdur|jj}dttdt fdd}|durC|durCt d ||| t d |d}|rR|dusP|j|krRd }|sl|durl|rbt d |dSt d ||jdS|r{t d|||tt d|j|||S)zTry to find a Link matching req Expects req, an InstallRequirement and upgrade, a boolean Returns a InstallationCandidate if found, Raises DistributionNotFound or BestVersionAlreadyInstalled otherwise F)trust_internet)rrmN cand_iterr)cSs dtdd|DtdpdS)NrFcSsh|]}t|jqSr%rrr%r%r5rszKPackageFinder.find_requirement.._format_versions..rnone)r/r parse_version)rr%r%r5_format_versionss z8PackageFinder.find_requirement.._format_versionszNCould not find a version that satisfies the requirement %s (from versions: %s)z%No matching distribution found for {}TzLExisting 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))rmrrQrr satisfied_byr4rrr1r-criticalrrrNr.rr) rBrrrmbest_candidate_resultrinstalled_versionrbest_installedr%r%r5find_requirementlsd       zPackageFinder.find_requirement)NNNrd)r)N)NN)2rerfrgrhrrrkrrrrCrrrpropertyr;rrsetterrr1rrrrrrrrr7rrrrrrrrr lru_cacher r rrrr r#rrrr%r%r%r5r$;s  ) "      7  r$fragmentr9cCsJt|D]\}}|dkr qt|d||kr|Sqt|d|)aFind the separator's index based on the package's canonical name. :param fragment: A + 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 )rr r)rr9irr%r%r5_find_name_version_sepsr"cCs@z t||d}Wn tyYdSw||d}|sdS|S)zParse the version string from a + 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. rHN)r"r)rr9 version_startr4r%r%r5rUs  rU)F)Nrhrrrritypingrrrrrrrpip._vendor.packagingr pip._vendor.packaging.tagsr pip._vendor.packaging.utilsr pip._vendor.packaging.versionr r rpip._internal.exceptionsrrrrpip._internal.index.collectorrrpip._internal.models.candidater#pip._internal.models.format_controlrpip._internal.models.linkr!pip._internal.models.search_scoper$pip._internal.models.selection_prefsr"pip._internal.models.target_pythonrpip._internal.models.wheelrpip._internal.reqrpip._internal.utils._logrpip._internal.utils.filetypesrpip._internal.utils.hashesrpip._internal.utils.loggingrpip._internal.utils.miscr pip._internal.utils.packagingr!pip._internal.utils.unpackingr"__all__rer-rr1BuildTagrrkr6r7r~rr#rr$r"rUr%r%r%r5s~$                      /  J(M