base.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 __bool__(self) -> bool:
  30. return bool(self.specifier) or bool(self.hashes) or bool(self.links)
  31. def __and__(self, other: InstallRequirement) -> "Constraint":
  32. if not isinstance(other, InstallRequirement):
  33. return NotImplemented
  34. specifier = self.specifier & other.specifier
  35. hashes = self.hashes & other.hashes(trust_internet=False)
  36. links = self.links
  37. if other.link:
  38. links = links.union([other.link])
  39. return Constraint(specifier, hashes, links)
  40. def is_satisfied_by(self, candidate: "Candidate") -> bool:
  41. # Reject if there are any mismatched URL constraints on this package.
  42. if self.links and not all(_match_link(link, candidate) for link in self.links):
  43. return False
  44. # We can safely always allow prereleases here since PackageFinder
  45. # already implements the prerelease logic, and would have filtered out
  46. # prerelease candidates if the user does not expect them.
  47. return self.specifier.contains(candidate.version, prereleases=True)
  48. class Requirement:
  49. @property
  50. def project_name(self) -> NormalizedName:
  51. """The "project name" of a requirement.
  52. This is different from ``name`` if this requirement contains extras,
  53. in which case ``name`` would contain the ``[...]`` part, while this
  54. refers to the name of the project.
  55. """
  56. raise NotImplementedError("Subclass should override")
  57. @property
  58. def name(self) -> str:
  59. """The name identifying this requirement in the resolver.
  60. This is different from ``project_name`` if this requirement contains
  61. extras, where ``project_name`` would not contain the ``[...]`` part.
  62. """
  63. raise NotImplementedError("Subclass should override")
  64. def is_satisfied_by(self, candidate: "Candidate") -> bool:
  65. return False
  66. def get_candidate_lookup(self) -> CandidateLookup:
  67. raise NotImplementedError("Subclass should override")
  68. def format_for_error(self) -> str:
  69. raise NotImplementedError("Subclass should override")
  70. def _match_link(link: Link, candidate: "Candidate") -> bool:
  71. if candidate.source_link:
  72. return links_equivalent(link, candidate.source_link)
  73. return False
  74. class Candidate:
  75. @property
  76. def project_name(self) -> NormalizedName:
  77. """The "project name" of the candidate.
  78. This is different from ``name`` if this candidate contains extras,
  79. in which case ``name`` would contain the ``[...]`` part, while this
  80. refers to the name of the project.
  81. """
  82. raise NotImplementedError("Override in subclass")
  83. @property
  84. def name(self) -> str:
  85. """The name identifying this candidate in the resolver.
  86. This is different from ``project_name`` if this candidate contains
  87. extras, where ``project_name`` would not contain the ``[...]`` part.
  88. """
  89. raise NotImplementedError("Override in subclass")
  90. @property
  91. def version(self) -> CandidateVersion:
  92. raise NotImplementedError("Override in subclass")
  93. @property
  94. def is_installed(self) -> bool:
  95. raise NotImplementedError("Override in subclass")
  96. @property
  97. def is_editable(self) -> bool:
  98. raise NotImplementedError("Override in subclass")
  99. @property
  100. def source_link(self) -> Optional[Link]:
  101. raise NotImplementedError("Override in subclass")
  102. def iter_dependencies(self, with_requires: bool) -> Iterable[Optional[Requirement]]:
  103. raise NotImplementedError("Override in subclass")
  104. def get_install_requirement(self) -> Optional[InstallRequirement]:
  105. raise NotImplementedError("Override in subclass")
  106. def format_for_error(self) -> str:
  107. raise NotImplementedError("Subclass should override")