提交 bd0f7fe3 编写于 作者: P Paul Moore

Add an ExtrasCandidate class

上级 76de49bc
......@@ -4,10 +4,10 @@ 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
from .base import Candidate
from .base import Candidate, format_name
if MYPY_CHECK_RUNNING:
from typing import Any, Dict, Optional, Sequence
from typing import Any, Dict, Optional, Sequence, Set
from pip._internal.models.link import Link
from pip._internal.operations.prepare import RequirementPreparer
......@@ -17,14 +17,15 @@ if MYPY_CHECK_RUNNING:
from pip._vendor.pkg_resources import Distribution
_CANDIDATE_CACHE = {} # type: Dict[Link, Candidate]
_CANDIDATE_CACHE = {} # type: Dict[Link, LinkCandidate]
def make_candidate(
link, # type: Link
preparer, # type: RequirementPreparer
parent, # type: InstallRequirement
make_install_req # type: InstallRequirementProvider
link, # type: Link
preparer, # type: RequirementPreparer
parent, # type: InstallRequirement
make_install_req, # type: InstallRequirementProvider
extras # type: Set[str]
):
# type: (...) -> Candidate
if link not in _CANDIDATE_CACHE:
......@@ -34,7 +35,10 @@ def make_candidate(
parent=parent,
make_install_req=make_install_req
)
return _CANDIDATE_CACHE[link]
base = _CANDIDATE_CACHE[link]
if extras:
return ExtrasCandidate(base, extras)
return base
def make_install_req_from_link(link, parent):
......@@ -67,7 +71,7 @@ class LinkCandidate(Candidate):
self.link = link
self._preparer = preparer
self._ireq = make_install_req_from_link(link, parent)
self._make_install_req = make_install_req
self._make_install_req = lambda spec: make_install_req(spec, self._ireq)
self._name = None # type: Optional[str]
self._version = None # type: Optional[_BaseVersion]
......@@ -120,11 +124,46 @@ class LinkCandidate(Candidate):
def get_dependencies(self):
# type: () -> Sequence[InstallRequirement]
return [
self._make_install_req(str(r), self._ireq)
for r in self.dist.requires()
]
return [self._make_install_req(str(r)) for r in self.dist.requires()]
def get_install_requirement(self):
# type: () -> InstallRequirement
return self._ireq
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: () -> InstallRequirement
return self.base.get_install_requirement()
......@@ -2,7 +2,7 @@ from pip._vendor.packaging.utils import canonicalize_name
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
from .base import Requirement
from .base import Requirement, format_name
from .candidates import make_candidate
if MYPY_CHECK_RUNNING:
......@@ -28,7 +28,8 @@ def make_requirement(
ireq.link,
preparer,
ireq,
make_install_req
make_install_req,
set()
)
return ExplicitRequirement(candidate)
else:
......@@ -70,17 +71,17 @@ class SpecifierRequirement(Requirement):
):
# type: (...) -> None
assert ireq.link is None, "This is a link, not a specifier"
assert not ireq.req.extras, "Extras not yet supported"
self._ireq = ireq
self._finder = finder
self._preparer = preparer
self._make_install_req = make_install_req
self.extras = ireq.req.extras
@property
def name(self):
# type: () -> str
canonical_name = canonicalize_name(self._ireq.req.name)
return canonical_name
return format_name(canonical_name, self.extras)
def find_matches(self):
# type: () -> Sequence[Candidate]
......@@ -94,7 +95,8 @@ class SpecifierRequirement(Requirement):
ican.link,
self._preparer,
self._ireq,
self._make_install_req
self._make_install_req,
self.extras
)
for ican in found.iter_applicable()
]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册