first commit
This commit is contained in:
Binary file not shown.
Binary file not shown.
20
venv/Lib/site-packages/pip/_internal/resolution/base.py
Normal file
20
venv/Lib/site-packages/pip/_internal/resolution/base.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from typing import Callable, List, Optional
|
||||
|
||||
from pip._internal.req.req_install import InstallRequirement
|
||||
from pip._internal.req.req_set import RequirementSet
|
||||
|
||||
InstallRequirementProvider = Callable[
|
||||
[str, Optional[InstallRequirement]], InstallRequirement
|
||||
]
|
||||
|
||||
|
||||
class BaseResolver:
|
||||
def resolve(
|
||||
self, root_reqs: List[InstallRequirement], check_supported_wheels: bool
|
||||
) -> RequirementSet:
|
||||
raise NotImplementedError()
|
||||
|
||||
def get_installation_order(
|
||||
self, req_set: RequirementSet
|
||||
) -> List[InstallRequirement]:
|
||||
raise NotImplementedError()
|
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,141 @@
|
||||
from typing import FrozenSet, Iterable, Optional, Tuple, Union
|
||||
|
||||
from pip._vendor.packaging.specifiers import SpecifierSet
|
||||
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
|
||||
from pip._vendor.packaging.version import LegacyVersion, Version
|
||||
|
||||
from pip._internal.models.link import Link, links_equivalent
|
||||
from pip._internal.req.req_install import InstallRequirement
|
||||
from pip._internal.utils.hashes import Hashes
|
||||
|
||||
CandidateLookup = Tuple[Optional["Candidate"], Optional[InstallRequirement]]
|
||||
CandidateVersion = Union[LegacyVersion, Version]
|
||||
|
||||
|
||||
def format_name(project: str, extras: FrozenSet[str]) -> str:
|
||||
if not extras:
|
||||
return project
|
||||
canonical_extras = sorted(canonicalize_name(e) for e in extras)
|
||||
return "{}[{}]".format(project, ",".join(canonical_extras))
|
||||
|
||||
|
||||
class Constraint:
|
||||
def __init__(
|
||||
self, specifier: SpecifierSet, hashes: Hashes, links: FrozenSet[Link]
|
||||
) -> None:
|
||||
self.specifier = specifier
|
||||
self.hashes = hashes
|
||||
self.links = links
|
||||
|
||||
@classmethod
|
||||
def empty(cls) -> "Constraint":
|
||||
return Constraint(SpecifierSet(), Hashes(), frozenset())
|
||||
|
||||
@classmethod
|
||||
def from_ireq(cls, ireq: InstallRequirement) -> "Constraint":
|
||||
links = frozenset([ireq.link]) if ireq.link else frozenset()
|
||||
return Constraint(ireq.specifier, ireq.hashes(trust_internet=False), links)
|
||||
|
||||
def __bool__(self) -> bool:
|
||||
return bool(self.specifier) or bool(self.hashes) or bool(self.links)
|
||||
|
||||
def __and__(self, other: InstallRequirement) -> "Constraint":
|
||||
if not isinstance(other, InstallRequirement):
|
||||
return NotImplemented
|
||||
specifier = self.specifier & other.specifier
|
||||
hashes = self.hashes & other.hashes(trust_internet=False)
|
||||
links = self.links
|
||||
if other.link:
|
||||
links = links.union([other.link])
|
||||
return Constraint(specifier, hashes, links)
|
||||
|
||||
def is_satisfied_by(self, candidate: "Candidate") -> bool:
|
||||
# Reject if there are any mismatched URL constraints on this package.
|
||||
if self.links and not all(_match_link(link, candidate) for link in self.links):
|
||||
return False
|
||||
# We can safely always allow prereleases here since PackageFinder
|
||||
# already implements the prerelease logic, and would have filtered out
|
||||
# prerelease candidates if the user does not expect them.
|
||||
return self.specifier.contains(candidate.version, prereleases=True)
|
||||
|
||||
|
||||
class Requirement:
|
||||
@property
|
||||
def project_name(self) -> NormalizedName:
|
||||
"""The "project name" of a requirement.
|
||||
|
||||
This is different from ``name`` if this requirement contains extras,
|
||||
in which case ``name`` would contain the ``[...]`` part, while this
|
||||
refers to the name of the project.
|
||||
"""
|
||||
raise NotImplementedError("Subclass should override")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""The name identifying this requirement in the resolver.
|
||||
|
||||
This is different from ``project_name`` if this requirement contains
|
||||
extras, where ``project_name`` would not contain the ``[...]`` part.
|
||||
"""
|
||||
raise NotImplementedError("Subclass should override")
|
||||
|
||||
def is_satisfied_by(self, candidate: "Candidate") -> bool:
|
||||
return False
|
||||
|
||||
def get_candidate_lookup(self) -> CandidateLookup:
|
||||
raise NotImplementedError("Subclass should override")
|
||||
|
||||
def format_for_error(self) -> str:
|
||||
raise NotImplementedError("Subclass should override")
|
||||
|
||||
|
||||
def _match_link(link: Link, candidate: "Candidate") -> bool:
|
||||
if candidate.source_link:
|
||||
return links_equivalent(link, candidate.source_link)
|
||||
return False
|
||||
|
||||
|
||||
class Candidate:
|
||||
@property
|
||||
def project_name(self) -> NormalizedName:
|
||||
"""The "project name" of the candidate.
|
||||
|
||||
This is different from ``name`` if this candidate contains extras,
|
||||
in which case ``name`` would contain the ``[...]`` part, while this
|
||||
refers to the name of the project.
|
||||
"""
|
||||
raise NotImplementedError("Override in subclass")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""The name identifying this candidate in the resolver.
|
||||
|
||||
This is different from ``project_name`` if this candidate contains
|
||||
extras, where ``project_name`` would not contain the ``[...]`` part.
|
||||
"""
|
||||
raise NotImplementedError("Override in subclass")
|
||||
|
||||
@property
|
||||
def version(self) -> CandidateVersion:
|
||||
raise NotImplementedError("Override in subclass")
|
||||
|
||||
@property
|
||||
def is_installed(self) -> bool:
|
||||
raise NotImplementedError("Override in subclass")
|
||||
|
||||
@property
|
||||
def is_editable(self) -> bool:
|
||||
raise NotImplementedError("Override in subclass")
|
||||
|
||||
@property
|
||||
def source_link(self) -> Optional[Link]:
|
||||
raise NotImplementedError("Override in subclass")
|
||||
|
||||
def iter_dependencies(self, with_requires: bool) -> Iterable[Optional[Requirement]]:
|
||||
raise NotImplementedError("Override in subclass")
|
||||
|
||||
def get_install_requirement(self) -> Optional[InstallRequirement]:
|
||||
raise NotImplementedError("Override in subclass")
|
||||
|
||||
def format_for_error(self) -> str:
|
||||
raise NotImplementedError("Subclass should override")
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,155 @@
|
||||
"""Utilities to lazily create and visit candidates found.
|
||||
|
||||
Creating and visiting a candidate is a *very* costly operation. It involves
|
||||
fetching, extracting, potentially building modules from source, and verifying
|
||||
distribution metadata. It is therefore crucial for performance to keep
|
||||
everything here lazy all the way down, so we only touch candidates that we
|
||||
absolutely need, and not "download the world" when we only need one version of
|
||||
something.
|
||||
"""
|
||||
|
||||
import functools
|
||||
from collections.abc import Sequence
|
||||
from typing import TYPE_CHECKING, Any, Callable, Iterator, Optional, Set, Tuple
|
||||
|
||||
from pip._vendor.packaging.version import _BaseVersion
|
||||
|
||||
from .base import Candidate
|
||||
|
||||
IndexCandidateInfo = Tuple[_BaseVersion, Callable[[], Optional[Candidate]]]
|
||||
|
||||
if TYPE_CHECKING:
|
||||
SequenceCandidate = Sequence[Candidate]
|
||||
else:
|
||||
# For compatibility: Python before 3.9 does not support using [] on the
|
||||
# Sequence class.
|
||||
#
|
||||
# >>> from collections.abc import Sequence
|
||||
# >>> Sequence[str]
|
||||
# Traceback (most recent call last):
|
||||
# File "<stdin>", line 1, in <module>
|
||||
# TypeError: 'ABCMeta' object is not subscriptable
|
||||
#
|
||||
# TODO: Remove this block after dropping Python 3.8 support.
|
||||
SequenceCandidate = Sequence
|
||||
|
||||
|
||||
def _iter_built(infos: Iterator[IndexCandidateInfo]) -> Iterator[Candidate]:
|
||||
"""Iterator for ``FoundCandidates``.
|
||||
|
||||
This iterator is used when the package is not already installed. Candidates
|
||||
from index come later in their normal ordering.
|
||||
"""
|
||||
versions_found: Set[_BaseVersion] = set()
|
||||
for version, func in infos:
|
||||
if version in versions_found:
|
||||
continue
|
||||
candidate = func()
|
||||
if candidate is None:
|
||||
continue
|
||||
yield candidate
|
||||
versions_found.add(version)
|
||||
|
||||
|
||||
def _iter_built_with_prepended(
|
||||
installed: Candidate, infos: Iterator[IndexCandidateInfo]
|
||||
) -> Iterator[Candidate]:
|
||||
"""Iterator for ``FoundCandidates``.
|
||||
|
||||
This iterator is used when the resolver prefers the already-installed
|
||||
candidate and NOT to upgrade. The installed candidate is therefore
|
||||
always yielded first, and candidates from index come later in their
|
||||
normal ordering, except skipped when the version is already installed.
|
||||
"""
|
||||
yield installed
|
||||
versions_found: Set[_BaseVersion] = {installed.version}
|
||||
for version, func in infos:
|
||||
if version in versions_found:
|
||||
continue
|
||||
candidate = func()
|
||||
if candidate is None:
|
||||
continue
|
||||
yield candidate
|
||||
versions_found.add(version)
|
||||
|
||||
|
||||
def _iter_built_with_inserted(
|
||||
installed: Candidate, infos: Iterator[IndexCandidateInfo]
|
||||
) -> Iterator[Candidate]:
|
||||
"""Iterator for ``FoundCandidates``.
|
||||
|
||||
This iterator is used when the resolver prefers to upgrade an
|
||||
already-installed package. Candidates from index are returned in their
|
||||
normal ordering, except replaced when the version is already installed.
|
||||
|
||||
The implementation iterates through and yields other candidates, inserting
|
||||
the installed candidate exactly once before we start yielding older or
|
||||
equivalent candidates, or after all other candidates if they are all newer.
|
||||
"""
|
||||
versions_found: Set[_BaseVersion] = set()
|
||||
for version, func in infos:
|
||||
if version in versions_found:
|
||||
continue
|
||||
# If the installed candidate is better, yield it first.
|
||||
if installed.version >= version:
|
||||
yield installed
|
||||
versions_found.add(installed.version)
|
||||
candidate = func()
|
||||
if candidate is None:
|
||||
continue
|
||||
yield candidate
|
||||
versions_found.add(version)
|
||||
|
||||
# If the installed candidate is older than all other candidates.
|
||||
if installed.version not in versions_found:
|
||||
yield installed
|
||||
|
||||
|
||||
class FoundCandidates(SequenceCandidate):
|
||||
"""A lazy sequence to provide candidates to the resolver.
|
||||
|
||||
The intended usage is to return this from `find_matches()` so the resolver
|
||||
can iterate through the sequence multiple times, but only access the index
|
||||
page when remote packages are actually needed. This improve performances
|
||||
when suitable candidates are already installed on disk.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
get_infos: Callable[[], Iterator[IndexCandidateInfo]],
|
||||
installed: Optional[Candidate],
|
||||
prefers_installed: bool,
|
||||
incompatible_ids: Set[int],
|
||||
):
|
||||
self._get_infos = get_infos
|
||||
self._installed = installed
|
||||
self._prefers_installed = prefers_installed
|
||||
self._incompatible_ids = incompatible_ids
|
||||
|
||||
def __getitem__(self, index: Any) -> Any:
|
||||
# Implemented to satisfy the ABC check. This is not needed by the
|
||||
# resolver, and should not be used by the provider either (for
|
||||
# performance reasons).
|
||||
raise NotImplementedError("don't do this")
|
||||
|
||||
def __iter__(self) -> Iterator[Candidate]:
|
||||
infos = self._get_infos()
|
||||
if not self._installed:
|
||||
iterator = _iter_built(infos)
|
||||
elif self._prefers_installed:
|
||||
iterator = _iter_built_with_prepended(self._installed, infos)
|
||||
else:
|
||||
iterator = _iter_built_with_inserted(self._installed, infos)
|
||||
return (c for c in iterator if id(c) not in self._incompatible_ids)
|
||||
|
||||
def __len__(self) -> int:
|
||||
# Implemented to satisfy the ABC check. This is not needed by the
|
||||
# resolver, and should not be used by the provider either (for
|
||||
# performance reasons).
|
||||
raise NotImplementedError("don't do this")
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def __bool__(self) -> bool:
|
||||
if self._prefers_installed and self._installed:
|
||||
return True
|
||||
return any(self)
|
@@ -0,0 +1,255 @@
|
||||
import collections
|
||||
import math
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Dict,
|
||||
Iterable,
|
||||
Iterator,
|
||||
Mapping,
|
||||
Sequence,
|
||||
TypeVar,
|
||||
Union,
|
||||
)
|
||||
|
||||
from pip._vendor.resolvelib.providers import AbstractProvider
|
||||
|
||||
from .base import Candidate, Constraint, Requirement
|
||||
from .candidates import REQUIRES_PYTHON_IDENTIFIER
|
||||
from .factory import Factory
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pip._vendor.resolvelib.providers import Preference
|
||||
from pip._vendor.resolvelib.resolvers import RequirementInformation
|
||||
|
||||
PreferenceInformation = RequirementInformation[Requirement, Candidate]
|
||||
|
||||
_ProviderBase = AbstractProvider[Requirement, Candidate, str]
|
||||
else:
|
||||
_ProviderBase = AbstractProvider
|
||||
|
||||
# Notes on the relationship between the provider, the factory, and the
|
||||
# candidate and requirement classes.
|
||||
#
|
||||
# The provider is a direct implementation of the resolvelib class. Its role
|
||||
# is to deliver the API that resolvelib expects.
|
||||
#
|
||||
# Rather than work with completely abstract "requirement" and "candidate"
|
||||
# concepts as resolvelib does, pip has concrete classes implementing these two
|
||||
# ideas. The API of Requirement and Candidate objects are defined in the base
|
||||
# classes, but essentially map fairly directly to the equivalent provider
|
||||
# methods. In particular, `find_matches` and `is_satisfied_by` are
|
||||
# requirement methods, and `get_dependencies` is a candidate method.
|
||||
#
|
||||
# The factory is the interface to pip's internal mechanisms. It is stateless,
|
||||
# and is created by the resolver and held as a property of the provider. It is
|
||||
# responsible for creating Requirement and Candidate objects, and provides
|
||||
# services to those objects (access to pip's finder and preparer).
|
||||
|
||||
|
||||
D = TypeVar("D")
|
||||
V = TypeVar("V")
|
||||
|
||||
|
||||
def _get_with_identifier(
|
||||
mapping: Mapping[str, V],
|
||||
identifier: str,
|
||||
default: D,
|
||||
) -> Union[D, V]:
|
||||
"""Get item from a package name lookup mapping with a resolver identifier.
|
||||
|
||||
This extra logic is needed when the target mapping is keyed by package
|
||||
name, which cannot be directly looked up with an identifier (which may
|
||||
contain requested extras). Additional logic is added to also look up a value
|
||||
by "cleaning up" the extras from the identifier.
|
||||
"""
|
||||
if identifier in mapping:
|
||||
return mapping[identifier]
|
||||
# HACK: Theoretically we should check whether this identifier is a valid
|
||||
# "NAME[EXTRAS]" format, and parse out the name part with packaging or
|
||||
# some regular expression. But since pip's resolver only spits out three
|
||||
# kinds of identifiers: normalized PEP 503 names, normalized names plus
|
||||
# extras, and Requires-Python, we can cheat a bit here.
|
||||
name, open_bracket, _ = identifier.partition("[")
|
||||
if open_bracket and name in mapping:
|
||||
return mapping[name]
|
||||
return default
|
||||
|
||||
|
||||
class PipProvider(_ProviderBase):
|
||||
"""Pip's provider implementation for resolvelib.
|
||||
|
||||
:params constraints: A mapping of constraints specified by the user. Keys
|
||||
are canonicalized project names.
|
||||
:params ignore_dependencies: Whether the user specified ``--no-deps``.
|
||||
:params upgrade_strategy: The user-specified upgrade strategy.
|
||||
:params user_requested: A set of canonicalized package names that the user
|
||||
supplied for pip to install/upgrade.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
factory: Factory,
|
||||
constraints: Dict[str, Constraint],
|
||||
ignore_dependencies: bool,
|
||||
upgrade_strategy: str,
|
||||
user_requested: Dict[str, int],
|
||||
) -> None:
|
||||
self._factory = factory
|
||||
self._constraints = constraints
|
||||
self._ignore_dependencies = ignore_dependencies
|
||||
self._upgrade_strategy = upgrade_strategy
|
||||
self._user_requested = user_requested
|
||||
self._known_depths: Dict[str, float] = collections.defaultdict(lambda: math.inf)
|
||||
|
||||
def identify(self, requirement_or_candidate: Union[Requirement, Candidate]) -> str:
|
||||
return requirement_or_candidate.name
|
||||
|
||||
def get_preference(
|
||||
self,
|
||||
identifier: str,
|
||||
resolutions: Mapping[str, Candidate],
|
||||
candidates: Mapping[str, Iterator[Candidate]],
|
||||
information: Mapping[str, Iterable["PreferenceInformation"]],
|
||||
backtrack_causes: Sequence["PreferenceInformation"],
|
||||
) -> "Preference":
|
||||
"""Produce a sort key for given requirement based on preference.
|
||||
|
||||
The lower the return value is, the more preferred this group of
|
||||
arguments is.
|
||||
|
||||
Currently pip considers the following in order:
|
||||
|
||||
* Prefer if any of the known requirements is "direct", e.g. points to an
|
||||
explicit URL.
|
||||
* If equal, prefer if any requirement is "pinned", i.e. contains
|
||||
operator ``===`` or ``==``.
|
||||
* If equal, calculate an approximate "depth" and resolve requirements
|
||||
closer to the user-specified requirements first. If the depth cannot
|
||||
by determined (eg: due to no matching parents), it is considered
|
||||
infinite.
|
||||
* Order user-specified requirements by the order they are specified.
|
||||
* If equal, prefers "non-free" requirements, i.e. contains at least one
|
||||
operator, such as ``>=`` or ``<``.
|
||||
* If equal, order alphabetically for consistency (helps debuggability).
|
||||
"""
|
||||
try:
|
||||
next(iter(information[identifier]))
|
||||
except StopIteration:
|
||||
# There is no information for this identifier, so there's no known
|
||||
# candidates.
|
||||
has_information = False
|
||||
else:
|
||||
has_information = True
|
||||
|
||||
if has_information:
|
||||
lookups = (r.get_candidate_lookup() for r, _ in information[identifier])
|
||||
candidate, ireqs = zip(*lookups)
|
||||
else:
|
||||
candidate, ireqs = None, ()
|
||||
|
||||
operators = [
|
||||
specifier.operator
|
||||
for specifier_set in (ireq.specifier for ireq in ireqs if ireq)
|
||||
for specifier in specifier_set
|
||||
]
|
||||
|
||||
direct = candidate is not None
|
||||
pinned = any(op[:2] == "==" for op in operators)
|
||||
unfree = bool(operators)
|
||||
|
||||
try:
|
||||
requested_order: Union[int, float] = self._user_requested[identifier]
|
||||
except KeyError:
|
||||
requested_order = math.inf
|
||||
if has_information:
|
||||
parent_depths = (
|
||||
self._known_depths[parent.name] if parent is not None else 0.0
|
||||
for _, parent in information[identifier]
|
||||
)
|
||||
inferred_depth = min(d for d in parent_depths) + 1.0
|
||||
else:
|
||||
inferred_depth = math.inf
|
||||
else:
|
||||
inferred_depth = 1.0
|
||||
self._known_depths[identifier] = inferred_depth
|
||||
|
||||
requested_order = self._user_requested.get(identifier, math.inf)
|
||||
|
||||
# Requires-Python has only one candidate and the check is basically
|
||||
# free, so we always do it first to avoid needless work if it fails.
|
||||
requires_python = identifier == REQUIRES_PYTHON_IDENTIFIER
|
||||
|
||||
# Prefer the causes of backtracking on the assumption that the problem
|
||||
# resolving the dependency tree is related to the failures that caused
|
||||
# the backtracking
|
||||
backtrack_cause = self.is_backtrack_cause(identifier, backtrack_causes)
|
||||
|
||||
return (
|
||||
not requires_python,
|
||||
not direct,
|
||||
not pinned,
|
||||
not backtrack_cause,
|
||||
inferred_depth,
|
||||
requested_order,
|
||||
not unfree,
|
||||
identifier,
|
||||
)
|
||||
|
||||
def find_matches(
|
||||
self,
|
||||
identifier: str,
|
||||
requirements: Mapping[str, Iterator[Requirement]],
|
||||
incompatibilities: Mapping[str, Iterator[Candidate]],
|
||||
) -> Iterable[Candidate]:
|
||||
def _eligible_for_upgrade(identifier: str) -> bool:
|
||||
"""Are upgrades allowed for this project?
|
||||
|
||||
This checks the upgrade strategy, and whether the project was one
|
||||
that the user specified in the command line, in order to decide
|
||||
whether we should upgrade if there's a newer version available.
|
||||
|
||||
(Note that we don't need access to the `--upgrade` flag, because
|
||||
an upgrade strategy of "to-satisfy-only" means that `--upgrade`
|
||||
was not specified).
|
||||
"""
|
||||
if self._upgrade_strategy == "eager":
|
||||
return True
|
||||
elif self._upgrade_strategy == "only-if-needed":
|
||||
user_order = _get_with_identifier(
|
||||
self._user_requested,
|
||||
identifier,
|
||||
default=None,
|
||||
)
|
||||
return user_order is not None
|
||||
return False
|
||||
|
||||
constraint = _get_with_identifier(
|
||||
self._constraints,
|
||||
identifier,
|
||||
default=Constraint.empty(),
|
||||
)
|
||||
return self._factory.find_candidates(
|
||||
identifier=identifier,
|
||||
requirements=requirements,
|
||||
constraint=constraint,
|
||||
prefers_installed=(not _eligible_for_upgrade(identifier)),
|
||||
incompatibilities=incompatibilities,
|
||||
)
|
||||
|
||||
def is_satisfied_by(self, requirement: Requirement, candidate: Candidate) -> bool:
|
||||
return requirement.is_satisfied_by(candidate)
|
||||
|
||||
def get_dependencies(self, candidate: Candidate) -> Sequence[Requirement]:
|
||||
with_requires = not self._ignore_dependencies
|
||||
return [r for r in candidate.iter_dependencies(with_requires) if r is not None]
|
||||
|
||||
@staticmethod
|
||||
def is_backtrack_cause(
|
||||
identifier: str, backtrack_causes: Sequence["PreferenceInformation"]
|
||||
) -> bool:
|
||||
for backtrack_cause in backtrack_causes:
|
||||
if identifier == backtrack_cause.requirement.name:
|
||||
return True
|
||||
if backtrack_cause.parent and identifier == backtrack_cause.parent.name:
|
||||
return True
|
||||
return False
|
@@ -0,0 +1,80 @@
|
||||
from collections import defaultdict
|
||||
from logging import getLogger
|
||||
from typing import Any, DefaultDict
|
||||
|
||||
from pip._vendor.resolvelib.reporters import BaseReporter
|
||||
|
||||
from .base import Candidate, Requirement
|
||||
|
||||
logger = getLogger(__name__)
|
||||
|
||||
|
||||
class PipReporter(BaseReporter):
|
||||
def __init__(self) -> None:
|
||||
self.reject_count_by_package: DefaultDict[str, int] = defaultdict(int)
|
||||
|
||||
self._messages_at_reject_count = {
|
||||
1: (
|
||||
"pip is looking at multiple versions of {package_name} to "
|
||||
"determine which version is compatible with other "
|
||||
"requirements. This could take a while."
|
||||
),
|
||||
8: (
|
||||
"pip is still looking at multiple versions of {package_name} to "
|
||||
"determine which version is compatible with other "
|
||||
"requirements. This could take a while."
|
||||
),
|
||||
13: (
|
||||
"This is taking longer than usual. You might need to provide "
|
||||
"the dependency resolver with stricter constraints to reduce "
|
||||
"runtime. See https://pip.pypa.io/warnings/backtracking for "
|
||||
"guidance. If you want to abort this run, press Ctrl + C."
|
||||
),
|
||||
}
|
||||
|
||||
def rejecting_candidate(self, criterion: Any, candidate: Candidate) -> None:
|
||||
self.reject_count_by_package[candidate.name] += 1
|
||||
|
||||
count = self.reject_count_by_package[candidate.name]
|
||||
if count not in self._messages_at_reject_count:
|
||||
return
|
||||
|
||||
message = self._messages_at_reject_count[count]
|
||||
logger.info("INFO: %s", message.format(package_name=candidate.name))
|
||||
|
||||
msg = "Will try a different candidate, due to conflict:"
|
||||
for req_info in criterion.information:
|
||||
req, parent = req_info.requirement, req_info.parent
|
||||
# Inspired by Factory.get_installation_error
|
||||
msg += "\n "
|
||||
if parent:
|
||||
msg += f"{parent.name} {parent.version} depends on "
|
||||
else:
|
||||
msg += "The user requested "
|
||||
msg += req.format_for_error()
|
||||
logger.debug(msg)
|
||||
|
||||
|
||||
class PipDebuggingReporter(BaseReporter):
|
||||
"""A reporter that does an info log for every event it sees."""
|
||||
|
||||
def starting(self) -> None:
|
||||
logger.info("Reporter.starting()")
|
||||
|
||||
def starting_round(self, index: int) -> None:
|
||||
logger.info("Reporter.starting_round(%r)", index)
|
||||
|
||||
def ending_round(self, index: int, state: Any) -> None:
|
||||
logger.info("Reporter.ending_round(%r, state)", index)
|
||||
|
||||
def ending(self, state: Any) -> None:
|
||||
logger.info("Reporter.ending(%r)", state)
|
||||
|
||||
def adding_requirement(self, requirement: Requirement, parent: Candidate) -> None:
|
||||
logger.info("Reporter.adding_requirement(%r, %r)", requirement, parent)
|
||||
|
||||
def rejecting_candidate(self, criterion: Any, candidate: Candidate) -> None:
|
||||
logger.info("Reporter.rejecting_candidate(%r, %r)", criterion, candidate)
|
||||
|
||||
def pinning(self, candidate: Candidate) -> None:
|
||||
logger.info("Reporter.pinning(%r)", candidate)
|
@@ -0,0 +1,165 @@
|
||||
from pip._vendor.packaging.specifiers import SpecifierSet
|
||||
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
|
||||
|
||||
from pip._internal.req.req_install import InstallRequirement
|
||||
|
||||
from .base import Candidate, CandidateLookup, Requirement, format_name
|
||||
|
||||
|
||||
class ExplicitRequirement(Requirement):
|
||||
def __init__(self, candidate: Candidate) -> None:
|
||||
self.candidate = candidate
|
||||
|
||||
def __str__(self) -> str:
|
||||
return str(self.candidate)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "{class_name}({candidate!r})".format(
|
||||
class_name=self.__class__.__name__,
|
||||
candidate=self.candidate,
|
||||
)
|
||||
|
||||
@property
|
||||
def project_name(self) -> NormalizedName:
|
||||
# No need to canonicalize - the candidate did this
|
||||
return self.candidate.project_name
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
# No need to canonicalize - the candidate did this
|
||||
return self.candidate.name
|
||||
|
||||
def format_for_error(self) -> str:
|
||||
return self.candidate.format_for_error()
|
||||
|
||||
def get_candidate_lookup(self) -> CandidateLookup:
|
||||
return self.candidate, None
|
||||
|
||||
def is_satisfied_by(self, candidate: Candidate) -> bool:
|
||||
return candidate == self.candidate
|
||||
|
||||
|
||||
class SpecifierRequirement(Requirement):
|
||||
def __init__(self, ireq: InstallRequirement) -> None:
|
||||
assert ireq.link is None, "This is a link, not a specifier"
|
||||
self._ireq = ireq
|
||||
self._extras = frozenset(ireq.extras)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return str(self._ireq.req)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "{class_name}({requirement!r})".format(
|
||||
class_name=self.__class__.__name__,
|
||||
requirement=str(self._ireq.req),
|
||||
)
|
||||
|
||||
@property
|
||||
def project_name(self) -> NormalizedName:
|
||||
assert self._ireq.req, "Specifier-backed ireq is always PEP 508"
|
||||
return canonicalize_name(self._ireq.req.name)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return format_name(self.project_name, self._extras)
|
||||
|
||||
def format_for_error(self) -> str:
|
||||
# Convert comma-separated specifiers into "A, B, ..., F and G"
|
||||
# This makes the specifier a bit more "human readable", without
|
||||
# risking a change in meaning. (Hopefully! Not all edge cases have
|
||||
# been checked)
|
||||
parts = [s.strip() for s in str(self).split(",")]
|
||||
if len(parts) == 0:
|
||||
return ""
|
||||
elif len(parts) == 1:
|
||||
return parts[0]
|
||||
|
||||
return ", ".join(parts[:-1]) + " and " + parts[-1]
|
||||
|
||||
def get_candidate_lookup(self) -> CandidateLookup:
|
||||
return None, self._ireq
|
||||
|
||||
def is_satisfied_by(self, candidate: Candidate) -> bool:
|
||||
assert candidate.name == self.name, (
|
||||
f"Internal issue: Candidate is not for this requirement "
|
||||
f"{candidate.name} vs {self.name}"
|
||||
)
|
||||
# We can safely always allow prereleases here since PackageFinder
|
||||
# already implements the prerelease logic, and would have filtered out
|
||||
# prerelease candidates if the user does not expect them.
|
||||
assert self._ireq.req, "Specifier-backed ireq is always PEP 508"
|
||||
spec = self._ireq.req.specifier
|
||||
return spec.contains(candidate.version, prereleases=True)
|
||||
|
||||
|
||||
class RequiresPythonRequirement(Requirement):
|
||||
"""A requirement representing Requires-Python metadata."""
|
||||
|
||||
def __init__(self, specifier: SpecifierSet, match: Candidate) -> None:
|
||||
self.specifier = specifier
|
||||
self._candidate = match
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"Python {self.specifier}"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "{class_name}({specifier!r})".format(
|
||||
class_name=self.__class__.__name__,
|
||||
specifier=str(self.specifier),
|
||||
)
|
||||
|
||||
@property
|
||||
def project_name(self) -> NormalizedName:
|
||||
return self._candidate.project_name
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._candidate.name
|
||||
|
||||
def format_for_error(self) -> str:
|
||||
return str(self)
|
||||
|
||||
def get_candidate_lookup(self) -> CandidateLookup:
|
||||
if self.specifier.contains(self._candidate.version, prereleases=True):
|
||||
return self._candidate, None
|
||||
return None, None
|
||||
|
||||
def is_satisfied_by(self, candidate: Candidate) -> bool:
|
||||
assert candidate.name == self._candidate.name, "Not Python candidate"
|
||||
# We can safely always allow prereleases here since PackageFinder
|
||||
# already implements the prerelease logic, and would have filtered out
|
||||
# prerelease candidates if the user does not expect them.
|
||||
return self.specifier.contains(candidate.version, prereleases=True)
|
||||
|
||||
|
||||
class UnsatisfiableRequirement(Requirement):
|
||||
"""A requirement that cannot be satisfied."""
|
||||
|
||||
def __init__(self, name: NormalizedName) -> None:
|
||||
self._name = name
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self._name} (unavailable)"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "{class_name}({name!r})".format(
|
||||
class_name=self.__class__.__name__,
|
||||
name=str(self._name),
|
||||
)
|
||||
|
||||
@property
|
||||
def project_name(self) -> NormalizedName:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
def format_for_error(self) -> str:
|
||||
return str(self)
|
||||
|
||||
def get_candidate_lookup(self) -> CandidateLookup:
|
||||
return None, None
|
||||
|
||||
def is_satisfied_by(self, candidate: Candidate) -> bool:
|
||||
return False
|
@@ -0,0 +1,299 @@
|
||||
import functools
|
||||
import logging
|
||||
import os
|
||||
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, cast
|
||||
|
||||
from pip._vendor.packaging.utils import canonicalize_name
|
||||
from pip._vendor.resolvelib import BaseReporter, ResolutionImpossible
|
||||
from pip._vendor.resolvelib import Resolver as RLResolver
|
||||
from pip._vendor.resolvelib.structs import DirectedGraph
|
||||
|
||||
from pip._internal.cache import WheelCache
|
||||
from pip._internal.index.package_finder import PackageFinder
|
||||
from pip._internal.operations.prepare import RequirementPreparer
|
||||
from pip._internal.req.req_install import InstallRequirement
|
||||
from pip._internal.req.req_set import RequirementSet
|
||||
from pip._internal.resolution.base import BaseResolver, InstallRequirementProvider
|
||||
from pip._internal.resolution.resolvelib.provider import PipProvider
|
||||
from pip._internal.resolution.resolvelib.reporter import (
|
||||
PipDebuggingReporter,
|
||||
PipReporter,
|
||||
)
|
||||
|
||||
from .base import Candidate, Requirement
|
||||
from .factory import Factory
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pip._vendor.resolvelib.resolvers import Result as RLResult
|
||||
|
||||
Result = RLResult[Requirement, Candidate, str]
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Resolver(BaseResolver):
|
||||
_allowed_strategies = {"eager", "only-if-needed", "to-satisfy-only"}
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
preparer: RequirementPreparer,
|
||||
finder: PackageFinder,
|
||||
wheel_cache: Optional[WheelCache],
|
||||
make_install_req: InstallRequirementProvider,
|
||||
use_user_site: bool,
|
||||
ignore_dependencies: bool,
|
||||
ignore_installed: bool,
|
||||
ignore_requires_python: bool,
|
||||
force_reinstall: bool,
|
||||
upgrade_strategy: str,
|
||||
py_version_info: Optional[Tuple[int, ...]] = None,
|
||||
):
|
||||
super().__init__()
|
||||
assert upgrade_strategy in self._allowed_strategies
|
||||
|
||||
self.factory = Factory(
|
||||
finder=finder,
|
||||
preparer=preparer,
|
||||
make_install_req=make_install_req,
|
||||
wheel_cache=wheel_cache,
|
||||
use_user_site=use_user_site,
|
||||
force_reinstall=force_reinstall,
|
||||
ignore_installed=ignore_installed,
|
||||
ignore_requires_python=ignore_requires_python,
|
||||
py_version_info=py_version_info,
|
||||
)
|
||||
self.ignore_dependencies = ignore_dependencies
|
||||
self.upgrade_strategy = upgrade_strategy
|
||||
self._result: Optional[Result] = None
|
||||
|
||||
def resolve(
|
||||
self, root_reqs: List[InstallRequirement], check_supported_wheels: bool
|
||||
) -> RequirementSet:
|
||||
collected = self.factory.collect_root_requirements(root_reqs)
|
||||
provider = PipProvider(
|
||||
factory=self.factory,
|
||||
constraints=collected.constraints,
|
||||
ignore_dependencies=self.ignore_dependencies,
|
||||
upgrade_strategy=self.upgrade_strategy,
|
||||
user_requested=collected.user_requested,
|
||||
)
|
||||
if "PIP_RESOLVER_DEBUG" in os.environ:
|
||||
reporter: BaseReporter = PipDebuggingReporter()
|
||||
else:
|
||||
reporter = PipReporter()
|
||||
resolver: RLResolver[Requirement, Candidate, str] = RLResolver(
|
||||
provider,
|
||||
reporter,
|
||||
)
|
||||
|
||||
try:
|
||||
limit_how_complex_resolution_can_be = 200000
|
||||
result = self._result = resolver.resolve(
|
||||
collected.requirements, max_rounds=limit_how_complex_resolution_can_be
|
||||
)
|
||||
|
||||
except ResolutionImpossible as e:
|
||||
error = self.factory.get_installation_error(
|
||||
cast("ResolutionImpossible[Requirement, Candidate]", e),
|
||||
collected.constraints,
|
||||
)
|
||||
raise error from e
|
||||
|
||||
req_set = RequirementSet(check_supported_wheels=check_supported_wheels)
|
||||
for candidate in result.mapping.values():
|
||||
ireq = candidate.get_install_requirement()
|
||||
if ireq is None:
|
||||
continue
|
||||
|
||||
# Check if there is already an installation under the same name,
|
||||
# and set a flag for later stages to uninstall it, if needed.
|
||||
installed_dist = self.factory.get_dist_to_uninstall(candidate)
|
||||
if installed_dist is None:
|
||||
# There is no existing installation -- nothing to uninstall.
|
||||
ireq.should_reinstall = False
|
||||
elif self.factory.force_reinstall:
|
||||
# The --force-reinstall flag is set -- reinstall.
|
||||
ireq.should_reinstall = True
|
||||
elif installed_dist.version != candidate.version:
|
||||
# The installation is different in version -- reinstall.
|
||||
ireq.should_reinstall = True
|
||||
elif candidate.is_editable or installed_dist.editable:
|
||||
# The incoming distribution is editable, or different in
|
||||
# editable-ness to installation -- reinstall.
|
||||
ireq.should_reinstall = True
|
||||
elif candidate.source_link and candidate.source_link.is_file:
|
||||
# The incoming distribution is under file://
|
||||
if candidate.source_link.is_wheel:
|
||||
# is a local wheel -- do nothing.
|
||||
logger.info(
|
||||
"%s is already installed with the same version as the "
|
||||
"provided wheel. Use --force-reinstall to force an "
|
||||
"installation of the wheel.",
|
||||
ireq.name,
|
||||
)
|
||||
continue
|
||||
|
||||
# is a local sdist or path -- reinstall
|
||||
ireq.should_reinstall = True
|
||||
else:
|
||||
continue
|
||||
|
||||
link = candidate.source_link
|
||||
if link and link.is_yanked:
|
||||
# The reason can contain non-ASCII characters, Unicode
|
||||
# is required for Python 2.
|
||||
msg = (
|
||||
"The candidate selected for download or install is a "
|
||||
"yanked version: {name!r} candidate (version {version} "
|
||||
"at {link})\nReason for being yanked: {reason}"
|
||||
).format(
|
||||
name=candidate.name,
|
||||
version=candidate.version,
|
||||
link=link,
|
||||
reason=link.yanked_reason or "<none given>",
|
||||
)
|
||||
logger.warning(msg)
|
||||
|
||||
req_set.add_named_requirement(ireq)
|
||||
|
||||
reqs = req_set.all_requirements
|
||||
self.factory.preparer.prepare_linked_requirements_more(reqs)
|
||||
for req in reqs:
|
||||
req.prepared = True
|
||||
req.needs_more_preparation = False
|
||||
return req_set
|
||||
|
||||
def get_installation_order(
|
||||
self, req_set: RequirementSet
|
||||
) -> List[InstallRequirement]:
|
||||
"""Get order for installation of requirements in RequirementSet.
|
||||
|
||||
The returned list contains a requirement before another that depends on
|
||||
it. This helps ensure that the environment is kept consistent as they
|
||||
get installed one-by-one.
|
||||
|
||||
The current implementation creates a topological ordering of the
|
||||
dependency graph, giving more weight to packages with less
|
||||
or no dependencies, while breaking any cycles in the graph at
|
||||
arbitrary points. We make no guarantees about where the cycle
|
||||
would be broken, other than it *would* be broken.
|
||||
"""
|
||||
assert self._result is not None, "must call resolve() first"
|
||||
|
||||
if not req_set.requirements:
|
||||
# Nothing is left to install, so we do not need an order.
|
||||
return []
|
||||
|
||||
graph = self._result.graph
|
||||
weights = get_topological_weights(graph, set(req_set.requirements.keys()))
|
||||
|
||||
sorted_items = sorted(
|
||||
req_set.requirements.items(),
|
||||
key=functools.partial(_req_set_item_sorter, weights=weights),
|
||||
reverse=True,
|
||||
)
|
||||
return [ireq for _, ireq in sorted_items]
|
||||
|
||||
|
||||
def get_topological_weights(
|
||||
graph: "DirectedGraph[Optional[str]]", requirement_keys: Set[str]
|
||||
) -> Dict[Optional[str], int]:
|
||||
"""Assign weights to each node based on how "deep" they are.
|
||||
|
||||
This implementation may change at any point in the future without prior
|
||||
notice.
|
||||
|
||||
We first simplify the dependency graph by pruning any leaves and giving them
|
||||
the highest weight: a package without any dependencies should be installed
|
||||
first. This is done again and again in the same way, giving ever less weight
|
||||
to the newly found leaves. The loop stops when no leaves are left: all
|
||||
remaining packages have at least one dependency left in the graph.
|
||||
|
||||
Then we continue with the remaining graph, by taking the length for the
|
||||
longest path to any node from root, ignoring any paths that contain a single
|
||||
node twice (i.e. cycles). This is done through a depth-first search through
|
||||
the graph, while keeping track of the path to the node.
|
||||
|
||||
Cycles in the graph result would result in node being revisited while also
|
||||
being on its own path. In this case, take no action. This helps ensure we
|
||||
don't get stuck in a cycle.
|
||||
|
||||
When assigning weight, the longer path (i.e. larger length) is preferred.
|
||||
|
||||
We are only interested in the weights of packages that are in the
|
||||
requirement_keys.
|
||||
"""
|
||||
path: Set[Optional[str]] = set()
|
||||
weights: Dict[Optional[str], int] = {}
|
||||
|
||||
def visit(node: Optional[str]) -> None:
|
||||
if node in path:
|
||||
# We hit a cycle, so we'll break it here.
|
||||
return
|
||||
|
||||
# Time to visit the children!
|
||||
path.add(node)
|
||||
for child in graph.iter_children(node):
|
||||
visit(child)
|
||||
path.remove(node)
|
||||
|
||||
if node not in requirement_keys:
|
||||
return
|
||||
|
||||
last_known_parent_count = weights.get(node, 0)
|
||||
weights[node] = max(last_known_parent_count, len(path))
|
||||
|
||||
# Simplify the graph, pruning leaves that have no dependencies.
|
||||
# This is needed for large graphs (say over 200 packages) because the
|
||||
# `visit` function is exponentially slower then, taking minutes.
|
||||
# See https://github.com/pypa/pip/issues/10557
|
||||
# We will loop until we explicitly break the loop.
|
||||
while True:
|
||||
leaves = set()
|
||||
for key in graph:
|
||||
if key is None:
|
||||
continue
|
||||
for _child in graph.iter_children(key):
|
||||
# This means we have at least one child
|
||||
break
|
||||
else:
|
||||
# No child.
|
||||
leaves.add(key)
|
||||
if not leaves:
|
||||
# We are done simplifying.
|
||||
break
|
||||
# Calculate the weight for the leaves.
|
||||
weight = len(graph) - 1
|
||||
for leaf in leaves:
|
||||
if leaf not in requirement_keys:
|
||||
continue
|
||||
weights[leaf] = weight
|
||||
# Remove the leaves from the graph, making it simpler.
|
||||
for leaf in leaves:
|
||||
graph.remove(leaf)
|
||||
|
||||
# Visit the remaining graph.
|
||||
# `None` is guaranteed to be the root node by resolvelib.
|
||||
visit(None)
|
||||
|
||||
# Sanity check: all requirement keys should be in the weights,
|
||||
# and no other keys should be in the weights.
|
||||
difference = set(weights.keys()).difference(requirement_keys)
|
||||
assert not difference, difference
|
||||
|
||||
return weights
|
||||
|
||||
|
||||
def _req_set_item_sorter(
|
||||
item: Tuple[str, InstallRequirement],
|
||||
weights: Dict[Optional[str], int],
|
||||
) -> Tuple[int, str]:
|
||||
"""Key function used to sort install requirements for installation.
|
||||
|
||||
Based on the "weight" mapping calculated in ``get_installation_order()``.
|
||||
The canonical package name is returned as the second member as a tie-
|
||||
breaker to ensure the result is predictable, which is useful in tests.
|
||||
"""
|
||||
name = canonicalize_name(item[0])
|
||||
return weights[name], name
|
Reference in New Issue
Block a user