HiddenEye-Legacy/venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/candidates.py

601 lines
20 KiB
Python
Raw Normal View History

2020-08-17 21:33:09 +08:00
import logging
import sys
2020-08-23 02:46:15 +08:00
from pip._vendor.contextlib2 import suppress
2020-08-17 21:33:09 +08:00
from pip._vendor.packaging.specifiers import InvalidSpecifier, SpecifierSet
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import Version
2020-08-23 02:46:15 +08:00
from pip._internal.exceptions import HashError, MetadataInconsistent
from pip._internal.network.lazy_wheel import (
HTTPRangeRequestUnsupported,
dist_from_wheel_url,
)
2020-08-17 21:33:09 +08:00
from pip._internal.req.constructors import (
install_req_from_editable,
install_req_from_line,
)
from pip._internal.req.req_install import InstallRequirement
2020-08-23 02:46:15 +08:00
from pip._internal.utils.logging import indent_log
from pip._internal.utils.misc import dist_is_editable, normalize_version_info
2020-08-17 21:33:09 +08:00
from pip._internal.utils.packaging import get_requires_python
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
from .base import Candidate, format_name
if MYPY_CHECK_RUNNING:
2020-08-23 02:46:15 +08:00
from typing import Any, FrozenSet, Iterable, Optional, Tuple, Union
2020-08-17 21:33:09 +08:00
from pip._vendor.packaging.version import _BaseVersion
from pip._vendor.pkg_resources import Distribution
from pip._internal.distributions import AbstractDistribution
from pip._internal.models.link import Link
from .base import Requirement
from .factory import Factory
BaseCandidate = Union[
"AlreadyInstalledCandidate",
"EditableCandidate",
"LinkCandidate",
]
logger = logging.getLogger(__name__)
2020-08-23 02:46:15 +08:00
def make_install_req_from_link(link, template):
2020-08-17 21:33:09 +08:00
# type: (Link, InstallRequirement) -> InstallRequirement
2020-08-23 02:46:15 +08:00
assert not template.editable, "template is editable"
if template.req:
line = str(template.req)
else:
line = link.url
ireq = install_req_from_line(
line,
user_supplied=template.user_supplied,
comes_from=template.comes_from,
use_pep517=template.use_pep517,
isolated=template.isolated,
constraint=template.constraint,
2020-08-17 21:33:09 +08:00
options=dict(
2020-08-23 02:46:15 +08:00
install_options=template.install_options,
global_options=template.global_options,
hashes=template.hash_options
2020-08-17 21:33:09 +08:00
),
)
2020-08-23 02:46:15 +08:00
ireq.original_link = template.original_link
ireq.link = link
return ireq
2020-08-17 21:33:09 +08:00
2020-08-23 02:46:15 +08:00
def make_install_req_from_editable(link, template):
2020-08-17 21:33:09 +08:00
# type: (Link, InstallRequirement) -> InstallRequirement
2020-08-23 02:46:15 +08:00
assert template.editable, "template not editable"
2020-08-17 21:33:09 +08:00
return install_req_from_editable(
link.url,
2020-08-23 02:46:15 +08:00
user_supplied=template.user_supplied,
comes_from=template.comes_from,
use_pep517=template.use_pep517,
isolated=template.isolated,
constraint=template.constraint,
2020-08-17 21:33:09 +08:00
options=dict(
2020-08-23 02:46:15 +08:00
install_options=template.install_options,
global_options=template.global_options,
hashes=template.hash_options
2020-08-17 21:33:09 +08:00
),
)
2020-08-23 02:46:15 +08:00
def make_install_req_from_dist(dist, template):
2020-08-17 21:33:09 +08:00
# type: (Distribution, InstallRequirement) -> InstallRequirement
2020-08-23 02:46:15 +08:00
project_name = canonicalize_name(dist.project_name)
if template.req:
line = str(template.req)
elif template.link:
line = "{} @ {}".format(project_name, template.link.url)
else:
line = "{}=={}".format(project_name, dist.parsed_version)
2020-08-17 21:33:09 +08:00
ireq = install_req_from_line(
2020-08-23 02:46:15 +08:00
line,
user_supplied=template.user_supplied,
comes_from=template.comes_from,
use_pep517=template.use_pep517,
isolated=template.isolated,
constraint=template.constraint,
2020-08-17 21:33:09 +08:00
options=dict(
2020-08-23 02:46:15 +08:00
install_options=template.install_options,
global_options=template.global_options,
hashes=template.hash_options
2020-08-17 21:33:09 +08:00
),
)
ireq.satisfied_by = dist
return ireq
class _InstallRequirementBackedCandidate(Candidate):
2020-08-23 02:46:15 +08:00
"""A candidate backed by an ``InstallRequirement``.
This represents a package request with the target not being already
in the environment, and needs to be fetched and installed. The backing
``InstallRequirement`` is responsible for most of the leg work; this
class exposes appropriate information to the resolver.
:param link: The link passed to the ``InstallRequirement``. The backing
``InstallRequirement`` will use this link to fetch the distribution.
:param source_link: The link this candidate "originates" from. This is
different from ``link`` when the link is found in the wheel cache.
``link`` would point to the wheel cache, while this points to the
found remote link (e.g. from pypi.org).
"""
is_installed = False
2020-08-17 21:33:09 +08:00
def __init__(
self,
link, # type: Link
2020-08-23 02:46:15 +08:00
source_link, # type: Link
2020-08-17 21:33:09 +08:00
ireq, # type: InstallRequirement
factory, # type: Factory
name=None, # type: Optional[str]
version=None, # type: Optional[_BaseVersion]
):
# type: (...) -> None
2020-08-23 02:46:15 +08:00
self._link = link
self._source_link = source_link
2020-08-17 21:33:09 +08:00
self._factory = factory
self._ireq = ireq
self._name = name
self._version = version
self._dist = None # type: Optional[Distribution]
2020-08-23 02:46:15 +08:00
self._prepared = False
2020-08-17 21:33:09 +08:00
def __repr__(self):
# type: () -> str
return "{class_name}({link!r})".format(
class_name=self.__class__.__name__,
2020-08-23 02:46:15 +08:00
link=str(self._link),
2020-08-17 21:33:09 +08:00
)
2020-08-23 02:46:15 +08:00
def __hash__(self):
# type: () -> int
return hash((self.__class__, self._link))
2020-08-17 21:33:09 +08:00
def __eq__(self, other):
# type: (Any) -> bool
if isinstance(other, self.__class__):
2020-08-23 02:46:15 +08:00
return self._link == other._link
2020-08-17 21:33:09 +08:00
return False
# Needed for Python 2, which does not implement this by default
def __ne__(self, other):
# type: (Any) -> bool
return not self.__eq__(other)
2020-08-23 02:46:15 +08:00
@property
def source_link(self):
# type: () -> Optional[Link]
return self._source_link
2020-08-17 21:33:09 +08:00
@property
def name(self):
# type: () -> str
"""The normalised name of the project the candidate refers to"""
if self._name is None:
self._name = canonicalize_name(self.dist.project_name)
return self._name
@property
def version(self):
# type: () -> _BaseVersion
if self._version is None:
self._version = self.dist.parsed_version
return self._version
2020-08-23 02:46:15 +08:00
def format_for_error(self):
# type: () -> str
return "{} {} (from {})".format(
self.name,
self.version,
self._link.file_path if self._link.is_file else self._link
)
2020-08-17 21:33:09 +08:00
def _prepare_abstract_distribution(self):
# type: () -> AbstractDistribution
raise NotImplementedError("Override in subclass")
2020-08-23 02:46:15 +08:00
def _check_metadata_consistency(self):
# type: () -> None
"""Check for consistency of project name and version of dist."""
# TODO: (Longer term) Rather than abort, reject this candidate
# and backtrack. This would need resolvelib support.
dist = self._dist # type: Distribution
name = canonicalize_name(dist.project_name)
if self._name is not None and self._name != name:
raise MetadataInconsistent(self._ireq, "name", dist.project_name)
version = dist.parsed_version
if self._version is not None and self._version != version:
raise MetadataInconsistent(self._ireq, "version", dist.version)
2020-08-17 21:33:09 +08:00
def _prepare(self):
# type: () -> None
2020-08-23 02:46:15 +08:00
if self._prepared:
2020-08-17 21:33:09 +08:00
return
2020-08-23 02:46:15 +08:00
try:
abstract_dist = self._prepare_abstract_distribution()
except HashError as e:
e.req = self._ireq
raise
2020-08-17 21:33:09 +08:00
self._dist = abstract_dist.get_pkg_resources_distribution()
assert self._dist is not None, "Distribution already installed"
2020-08-23 02:46:15 +08:00
self._check_metadata_consistency()
self._prepared = True
2020-08-17 21:33:09 +08:00
2020-08-23 02:46:15 +08:00
def _fetch_metadata(self):
# type: () -> None
"""Fetch metadata, using lazy wheel if possible."""
preparer = self._factory.preparer
use_lazy_wheel = self._factory.use_lazy_wheel
remote_wheel = self._link.is_wheel and not self._link.is_file
if use_lazy_wheel and remote_wheel and not preparer.require_hashes:
assert self._name is not None
logger.info('Collecting %s', self._ireq.req or self._ireq)
# If HTTPRangeRequestUnsupported is raised, fallback silently.
with indent_log(), suppress(HTTPRangeRequestUnsupported):
logger.info(
'Obtaining dependency information from %s %s',
self._name, self._version,
)
url = self._link.url.split('#', 1)[0]
session = preparer.downloader._session
self._dist = dist_from_wheel_url(self._name, url, session)
self._check_metadata_consistency()
if self._dist is None:
self._prepare()
2020-08-17 21:33:09 +08:00
@property
def dist(self):
# type: () -> Distribution
2020-08-23 02:46:15 +08:00
if self._dist is None:
self._fetch_metadata()
2020-08-17 21:33:09 +08:00
return self._dist
def _get_requires_python_specifier(self):
# type: () -> Optional[SpecifierSet]
requires_python = get_requires_python(self.dist)
if requires_python is None:
return None
try:
spec = SpecifierSet(requires_python)
except InvalidSpecifier as e:
logger.warning(
"Package %r has an invalid Requires-Python: %s", self.name, e,
)
return None
return spec
2020-08-23 02:46:15 +08:00
def iter_dependencies(self, with_requires):
# type: (bool) -> Iterable[Optional[Requirement]]
if not with_requires:
return
for r in self.dist.requires():
yield self._factory.make_requirement_from_spec(str(r), self._ireq)
2020-08-17 21:33:09 +08:00
python_dep = self._factory.make_requires_python_requirement(
self._get_requires_python_specifier(),
)
if python_dep:
2020-08-23 02:46:15 +08:00
yield python_dep
2020-08-17 21:33:09 +08:00
def get_install_requirement(self):
# type: () -> Optional[InstallRequirement]
self._prepare()
return self._ireq
class LinkCandidate(_InstallRequirementBackedCandidate):
2020-08-23 02:46:15 +08:00
is_editable = False
2020-08-17 21:33:09 +08:00
def __init__(
self,
link, # type: Link
2020-08-23 02:46:15 +08:00
template, # type: InstallRequirement
2020-08-17 21:33:09 +08:00
factory, # type: Factory
name=None, # type: Optional[str]
version=None, # type: Optional[_BaseVersion]
):
# type: (...) -> None
2020-08-23 02:46:15 +08:00
source_link = link
cache_entry = factory.get_wheel_cache_entry(link, name)
if cache_entry is not None:
logger.debug("Using cached wheel link: %s", cache_entry.link)
link = cache_entry.link
ireq = make_install_req_from_link(link, template)
if (cache_entry is not None and
cache_entry.persistent and
template.link is template.original_link):
ireq.original_link_is_in_wheel_cache = True
2020-08-17 21:33:09 +08:00
super(LinkCandidate, self).__init__(
link=link,
2020-08-23 02:46:15 +08:00
source_link=source_link,
ireq=ireq,
2020-08-17 21:33:09 +08:00
factory=factory,
name=name,
version=version,
)
def _prepare_abstract_distribution(self):
# type: () -> AbstractDistribution
2020-08-23 02:46:15 +08:00
return self._factory.preparer.prepare_linked_requirement(
self._ireq, parallel_builds=True,
)
2020-08-17 21:33:09 +08:00
class EditableCandidate(_InstallRequirementBackedCandidate):
2020-08-23 02:46:15 +08:00
is_editable = True
2020-08-17 21:33:09 +08:00
def __init__(
self,
link, # type: Link
2020-08-23 02:46:15 +08:00
template, # type: InstallRequirement
2020-08-17 21:33:09 +08:00
factory, # type: Factory
name=None, # type: Optional[str]
version=None, # type: Optional[_BaseVersion]
):
# type: (...) -> None
super(EditableCandidate, self).__init__(
link=link,
2020-08-23 02:46:15 +08:00
source_link=link,
ireq=make_install_req_from_editable(link, template),
2020-08-17 21:33:09 +08:00
factory=factory,
name=name,
version=version,
)
def _prepare_abstract_distribution(self):
# type: () -> AbstractDistribution
return self._factory.preparer.prepare_editable_requirement(self._ireq)
class AlreadyInstalledCandidate(Candidate):
2020-08-23 02:46:15 +08:00
is_installed = True
source_link = None
2020-08-17 21:33:09 +08:00
def __init__(
self,
dist, # type: Distribution
2020-08-23 02:46:15 +08:00
template, # type: InstallRequirement
2020-08-17 21:33:09 +08:00
factory, # type: Factory
):
# type: (...) -> None
self.dist = dist
2020-08-23 02:46:15 +08:00
self._ireq = make_install_req_from_dist(dist, template)
2020-08-17 21:33:09 +08:00
self._factory = factory
# This is just logging some messages, so we can do it eagerly.
# The returned dist would be exactly the same as self.dist because we
# set satisfied_by in make_install_req_from_dist.
# TODO: Supply reason based on force_reinstall and upgrade_strategy.
skip_reason = "already satisfied"
factory.preparer.prepare_installed_requirement(self._ireq, skip_reason)
def __repr__(self):
# type: () -> str
return "{class_name}({distribution!r})".format(
class_name=self.__class__.__name__,
distribution=self.dist,
)
2020-08-23 02:46:15 +08:00
def __hash__(self):
# type: () -> int
return hash((self.__class__, self.name, self.version))
2020-08-17 21:33:09 +08:00
def __eq__(self, other):
# type: (Any) -> bool
if isinstance(other, self.__class__):
return self.name == other.name and self.version == other.version
return False
# Needed for Python 2, which does not implement this by default
def __ne__(self, other):
# type: (Any) -> bool
return not self.__eq__(other)
@property
def name(self):
# type: () -> str
return canonicalize_name(self.dist.project_name)
@property
def version(self):
# type: () -> _BaseVersion
return self.dist.parsed_version
2020-08-23 02:46:15 +08:00
@property
def is_editable(self):
# type: () -> bool
return dist_is_editable(self.dist)
def format_for_error(self):
# type: () -> str
return "{} {} (Installed)".format(self.name, self.version)
def iter_dependencies(self, with_requires):
# type: (bool) -> Iterable[Optional[Requirement]]
if not with_requires:
return
for r in self.dist.requires():
yield self._factory.make_requirement_from_spec(str(r), self._ireq)
2020-08-17 21:33:09 +08:00
def get_install_requirement(self):
# type: () -> Optional[InstallRequirement]
return None
class ExtrasCandidate(Candidate):
"""A candidate that has 'extras', indicating additional dependencies.
Requirements can be for a project with dependencies, something like
foo[extra]. The extras don't affect the project/version being installed
directly, but indicate that we need additional dependencies. We model that
by having an artificial ExtrasCandidate that wraps the "base" candidate.
The ExtrasCandidate differs from the base in the following ways:
1. It has a unique name, of the form foo[extra]. This causes the resolver
to treat it as a separate node in the dependency graph.
2. When we're getting the candidate's dependencies,
a) We specify that we want the extra dependencies as well.
2020-08-23 02:46:15 +08:00
b) We add a dependency on the base candidate.
See below for why this is needed.
2020-08-17 21:33:09 +08:00
3. We return None for the underlying InstallRequirement, as the base
candidate will provide it, and we don't want to end up with duplicates.
The dependency on the base candidate is needed so that the resolver can't
decide that it should recommend foo[extra1] version 1.0 and foo[extra2]
version 2.0. Having those candidates depend on foo=1.0 and foo=2.0
respectively forces the resolver to recognise that this is a conflict.
"""
def __init__(
self,
base, # type: BaseCandidate
2020-08-23 02:46:15 +08:00
extras, # type: FrozenSet[str]
2020-08-17 21:33:09 +08:00
):
# type: (...) -> None
self.base = base
self.extras = extras
def __repr__(self):
# type: () -> str
return "{class_name}(base={base!r}, extras={extras!r})".format(
class_name=self.__class__.__name__,
base=self.base,
extras=self.extras,
)
2020-08-23 02:46:15 +08:00
def __hash__(self):
# type: () -> int
return hash((self.base, self.extras))
2020-08-17 21:33:09 +08:00
def __eq__(self, other):
# type: (Any) -> bool
if isinstance(other, self.__class__):
return self.base == other.base and self.extras == other.extras
return False
# Needed for Python 2, which does not implement this by default
def __ne__(self, other):
# type: (Any) -> bool
return not self.__eq__(other)
@property
def name(self):
# type: () -> str
"""The normalised name of the project the candidate refers to"""
return format_name(self.base.name, self.extras)
@property
def version(self):
# type: () -> _BaseVersion
return self.base.version
2020-08-23 02:46:15 +08:00
def format_for_error(self):
# type: () -> str
return "{} [{}]".format(
self.base.format_for_error(),
", ".join(sorted(self.extras))
)
@property
def is_installed(self):
# type: () -> bool
return self.base.is_installed
@property
def is_editable(self):
# type: () -> bool
return self.base.is_editable
@property
def source_link(self):
# type: () -> Optional[Link]
return self.base.source_link
def iter_dependencies(self, with_requires):
# type: (bool) -> Iterable[Optional[Requirement]]
2020-08-17 21:33:09 +08:00
factory = self.base._factory
2020-08-23 02:46:15 +08:00
# Add a dependency on the exact base
# (See note 2b in the class docstring)
yield factory.make_requirement_from_candidate(self.base)
if not with_requires:
return
2020-08-17 21:33:09 +08:00
# The user may have specified extras that the candidate doesn't
# support. We ignore any unsupported extras here.
valid_extras = self.extras.intersection(self.base.dist.extras)
invalid_extras = self.extras.difference(self.base.dist.extras)
2020-08-23 02:46:15 +08:00
for extra in sorted(invalid_extras):
2020-08-17 21:33:09 +08:00
logger.warning(
2020-08-23 02:46:15 +08:00
"%s %s does not provide the extra '%s'",
self.base.name,
self.version,
extra
2020-08-17 21:33:09 +08:00
)
2020-08-23 02:46:15 +08:00
for r in self.base.dist.requires(valid_extras):
requirement = factory.make_requirement_from_spec(
str(r), self.base._ireq, valid_extras,
)
if requirement:
yield requirement
2020-08-17 21:33:09 +08:00
def get_install_requirement(self):
# type: () -> Optional[InstallRequirement]
# We don't return anything here, because we always
# depend on the base candidate, and we'll get the
# install requirement from that.
return None
class RequiresPythonCandidate(Candidate):
2020-08-23 02:46:15 +08:00
is_installed = False
source_link = None
2020-08-17 21:33:09 +08:00
def __init__(self, py_version_info):
# type: (Optional[Tuple[int, ...]]) -> None
if py_version_info is not None:
version_info = normalize_version_info(py_version_info)
else:
version_info = sys.version_info[:3]
self._version = Version(".".join(str(c) for c in version_info))
# We don't need to implement __eq__() and __ne__() since there is always
# only one RequiresPythonCandidate in a resolution, i.e. the host Python.
# The built-in object.__eq__() and object.__ne__() do exactly what we want.
@property
def name(self):
# type: () -> str
# Avoid conflicting with the PyPI package "Python".
2020-08-23 02:46:15 +08:00
return "<Python from Requires-Python>"
2020-08-17 21:33:09 +08:00
@property
def version(self):
# type: () -> _BaseVersion
return self._version
2020-08-23 02:46:15 +08:00
def format_for_error(self):
# type: () -> str
return "Python {}".format(self.version)
def iter_dependencies(self, with_requires):
# type: (bool) -> Iterable[Optional[Requirement]]
return ()
2020-08-17 21:33:09 +08:00
def get_install_requirement(self):
# type: () -> Optional[InstallRequirement]
return None