base.py 5.5 KB

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