1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/src/pip/_internal/resolution/resolvelib/candidates.py

176 lines
5.6 KiB
Python
Raw Normal View History

2020-03-18 15:54:15 +01:00
from pip._vendor.packaging.utils import canonicalize_name
2020-03-12 16:18:47 +01:00
from pip._internal.req.constructors import install_req_from_line
from pip._internal.req.req_install import InstallRequirement
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
2020-03-25 13:25:23 +01:00
from .base import Candidate, format_name
2020-03-12 16:18:47 +01:00
if MYPY_CHECK_RUNNING:
2020-03-25 13:25:23 +01:00
from typing import Any, Dict, Optional, Sequence, Set
2020-03-12 16:18:47 +01:00
from pip._internal.models.link import Link
from pip._internal.operations.prepare import RequirementPreparer
from pip._internal.resolution.base import InstallRequirementProvider
from pip._vendor.packaging.version import _BaseVersion
from pip._vendor.pkg_resources import Distribution
2020-03-25 13:25:23 +01:00
_CANDIDATE_CACHE = {} # type: Dict[Link, LinkCandidate]
2020-03-12 16:18:47 +01:00
def make_candidate(
2020-03-25 13:25:23 +01:00
link, # type: Link
preparer, # type: RequirementPreparer
parent, # type: InstallRequirement
make_install_req, # type: InstallRequirementProvider
extras # type: Set[str]
2020-03-12 16:18:47 +01:00
):
# type: (...) -> Candidate
if link not in _CANDIDATE_CACHE:
_CANDIDATE_CACHE[link] = LinkCandidate(
link,
preparer,
parent=parent,
make_install_req=make_install_req
)
2020-03-25 13:25:23 +01:00
base = _CANDIDATE_CACHE[link]
if extras:
return ExtrasCandidate(base, extras)
return base
2020-03-12 16:18:47 +01:00
def make_install_req_from_link(link, parent):
# type: (Link, InstallRequirement) -> InstallRequirement
# TODO: Do we need to support editables?
return install_req_from_line(
link.url,
comes_from=parent.comes_from,
use_pep517=parent.use_pep517,
isolated=parent.isolated,
wheel_cache=parent._wheel_cache,
constraint=parent.constraint,
options=dict(
install_options=parent.install_options,
global_options=parent.global_options,
hashes=parent.hash_options
),
)
class LinkCandidate(Candidate):
def __init__(
self,
link, # type: Link
preparer, # type: RequirementPreparer
parent, # type: InstallRequirement
make_install_req, # type: InstallRequirementProvider
):
# type: (...) -> None
self.link = link
self._preparer = preparer
self._ireq = make_install_req_from_link(link, parent)
self._make_install_req = lambda spec: make_install_req(
spec,
self._ireq
)
2020-03-12 16:18:47 +01:00
self._name = None # type: Optional[str]
self._version = None # type: Optional[_BaseVersion]
self._dist = None # type: Optional[Distribution]
2020-03-19 11:53:15 +01:00
def __eq__(self, other):
# type: (Any) -> bool
if isinstance(other, self.__class__):
return self.link == other.link
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-03-12 16:18:47 +01:00
@property
def name(self):
# type: () -> str
2020-03-19 11:53:15 +01:00
"""The normalised name of the project the candidate refers to"""
2020-03-12 16:18:47 +01:00
if self._name is None:
2020-03-18 15:54:15 +01:00
self._name = canonicalize_name(self.dist.project_name)
2020-03-12 16:18:47 +01:00
return self._name
@property
def version(self):
# type: () -> _BaseVersion
if self._version is None:
self._version = self.dist.parsed_version
return self._version
@property
def dist(self):
# type: () -> Distribution
if self._dist is None:
abstract_dist = self._preparer.prepare_linked_requirement(
self._ireq
)
self._dist = abstract_dist.get_pkg_resources_distribution()
# TODO: Only InstalledDistribution can return None here :-(
assert self._dist is not None
# These should be "proper" errors, not just asserts, as they
# can result from user errors like a requirement "foo @ URL"
# when the project at URL has a name of "bar" in its metadata.
2020-03-19 11:53:15 +01:00
assert (self._name is None or
self._name == canonicalize_name(self._dist.project_name))
2020-03-12 16:18:47 +01:00
assert (self._version is None or
self._version == self.dist.parsed_version)
return self._dist
def get_dependencies(self):
# type: () -> Sequence[InstallRequirement]
2020-03-25 13:25:23 +01:00
return [self._make_install_req(str(r)) for r in self.dist.requires()]
def get_install_requirement(self):
# type: () -> Optional[InstallRequirement]
return self._ireq
2020-03-25 13:25:23 +01:00
class ExtrasCandidate(LinkCandidate):
def __init__(
self,
base, # type: LinkCandidate
extras, # type: Set[str]
):
# type: (...) -> None
self.base = base
self.extras = extras
self.link = base.link
@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
def get_dependencies(self):
# type: () -> Sequence[InstallRequirement]
deps = [
self.base._make_install_req(str(r))
for r in self.base.dist.requires(self.extras)
]
# Add a dependency on the exact base
spec = "{}=={}".format(self.base.name, self.base.version)
deps.append(self.base._make_install_req(spec))
return deps
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