base.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from typing import FrozenSet, Iterable, Optional, Tuple, Union
  2. from pip._vendor.packaging.specifiers import SpecifierSet
  3. from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
  4. from pip._vendor.packaging.version import LegacyVersion, Version
  5. from pip._internal.models.link import Link, links_equivalent
  6. from pip._internal.req.req_install import InstallRequirement
  7. from pip._internal.utils.hashes import Hashes
  8. CandidateLookup = Tuple[Optional["Candidate"], Optional[InstallRequirement]]
  9. CandidateVersion = Union[LegacyVersion, Version]
  10. def format_name(project: str, extras: FrozenSet[str]) -> str:
  11. if not extras:
  12. return project
  13. canonical_extras = sorted(canonicalize_name(e) for e in extras)
  14. return "{}[{}]".format(project, ",".join(canonical_extras))
  15. class Constraint:
  16. def __init__(
  17. self, specifier: SpecifierSet, hashes: Hashes, links: FrozenSet[Link]
  18. ) -> None:
  19. self.specifier = specifier
  20. self.hashes = hashes
  21. self.links = links
  22. @classmethod
  23. def empty(cls) -> "Constraint":
  24. return Constraint(SpecifierSet(), Hashes(), frozenset())
  25. @classmethod
  26. def from_ireq(cls, ireq: InstallRequirement) -> "Constraint":
  27. links = frozenset([ireq.link]) if ireq.link else frozenset()
  28. return Constraint(ireq.specifier, ireq.hashes(trust_internet=False), links)
  29. def __nonzero__(self) -> bool:
  30. return bool(self.specifier) or bool(self.hashes) or bool(self.links)
  31. def __bool__(self) -> bool:
  32. return self.__nonzero__()
  33. def __and__(self, other: InstallRequirement) -> "Constraint":
  34. if not isinstance(other, InstallRequirement):
  35. return NotImplemented
  36. specifier = self.specifier & other.specifier
  37. hashes = self.hashes & other.hashes(trust_internet=False)
  38. links = self.links
  39. if other.link:
  40. links = links.union([other.link])
  41. return Constraint(specifier, hashes, links)
  42. def is_satisfied_by(self, candidate: "Candidate") -> bool:
  43. # Reject if there are any mismatched URL constraints on this package.
  44. if self.links and not all(_match_link(link, candidate) for link in self.links):
  45. return False
  46. # We can safely always allow prereleases here since PackageFinder
  47. # already implements the prerelease logic, and would have filtered out
  48. # prerelease candidates if the user does not expect them.
  49. return self.specifier.contains(candidate.version, prereleases=True)
  50. class Requirement:
  51. @property
  52. def project_name(self) -> NormalizedName:
  53. """The "project name" of a requirement.
  54. This is different from ``name`` if this requirement contains extras,
  55. in which case ``name`` would contain the ``[...]`` part, while this
  56. refers to the name of the project.
  57. """
  58. raise NotImplementedError("Subclass should override")
  59. @property
  60. def name(self) -> str:
  61. """The name identifying this requirement in the resolver.
  62. This is different from ``project_name`` if this requirement contains
  63. extras, where ``project_name`` would not contain the ``[...]`` part.
  64. """
  65. raise NotImplementedError("Subclass should override")
  66. def is_satisfied_by(self, candidate: "Candidate") -> bool:
  67. return False
  68. def get_candidate_lookup(self) -> CandidateLookup:
  69. raise NotImplementedError("Subclass should override")
  70. def format_for_error(self) -> str:
  71. raise NotImplementedError("Subclass should override")
  72. def _match_link(link: Link, candidate: "Candidate") -> bool:
  73. if candidate.source_link:
  74. return links_equivalent(link, candidate.source_link)
  75. return False
  76. class Candidate:
  77. @property
  78. def project_name(self) -> NormalizedName:
  79. """The "project name" of the candidate.
  80. This is different from ``name`` if this candidate contains extras,
  81. in which case ``name`` would contain the ``[...]`` part, while this
  82. refers to the name of the project.
  83. """
  84. raise NotImplementedError("Override in subclass")
  85. @property
  86. def name(self) -> str:
  87. """The name identifying this candidate in the resolver.
  88. This is different from ``project_name`` if this candidate contains
  89. extras, where ``project_name`` would not contain the ``[...]`` part.
  90. """
  91. raise NotImplementedError("Override in subclass")
  92. @property
  93. def version(self) -> CandidateVersion:
  94. raise NotImplementedError("Override in subclass")
  95. @property
  96. def is_installed(self) -> bool:
  97. raise NotImplementedError("Override in subclass")
  98. @property
  99. def is_editable(self) -> bool:
  100. raise NotImplementedError("Override in subclass")
  101. @property
  102. def source_link(self) -> Optional[Link]:
  103. raise NotImplementedError("Override in subclass")
  104. def iter_dependencies(self, with_requires: bool) -> Iterable[Optional[Requirement]]:
  105. raise NotImplementedError("Override in subclass")
  106. def get_install_requirement(self) -> Optional[InstallRequirement]:
  107. raise NotImplementedError("Override in subclass")
  108. def format_for_error(self) -> str:
  109. raise NotImplementedError("Subclass should override")