base.py 4.9 KB

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