provider.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. from typing import TYPE_CHECKING, Dict, Iterable, Iterator, Mapping, Sequence, Union
  2. from pip._vendor.resolvelib.providers import AbstractProvider
  3. from .base import Candidate, Constraint, Requirement
  4. from .factory import Factory
  5. if TYPE_CHECKING:
  6. from pip._vendor.resolvelib.providers import Preference
  7. from pip._vendor.resolvelib.resolvers import RequirementInformation
  8. PreferenceInformation = RequirementInformation[Requirement, Candidate]
  9. _ProviderBase = AbstractProvider[Requirement, Candidate, str]
  10. else:
  11. _ProviderBase = AbstractProvider
  12. # Notes on the relationship between the provider, the factory, and the
  13. # candidate and requirement classes.
  14. #
  15. # The provider is a direct implementation of the resolvelib class. Its role
  16. # is to deliver the API that resolvelib expects.
  17. #
  18. # Rather than work with completely abstract "requirement" and "candidate"
  19. # concepts as resolvelib does, pip has concrete classes implementing these two
  20. # ideas. The API of Requirement and Candidate objects are defined in the base
  21. # classes, but essentially map fairly directly to the equivalent provider
  22. # methods. In particular, `find_matches` and `is_satisfied_by` are
  23. # requirement methods, and `get_dependencies` is a candidate method.
  24. #
  25. # The factory is the interface to pip's internal mechanisms. It is stateless,
  26. # and is created by the resolver and held as a property of the provider. It is
  27. # responsible for creating Requirement and Candidate objects, and provides
  28. # services to those objects (access to pip's finder and preparer).
  29. class PipProvider(_ProviderBase):
  30. """Pip's provider implementation for resolvelib.
  31. :params constraints: A mapping of constraints specified by the user. Keys
  32. are canonicalized project names.
  33. :params ignore_dependencies: Whether the user specified ``--no-deps``.
  34. :params upgrade_strategy: The user-specified upgrade strategy.
  35. :params user_requested: A set of canonicalized package names that the user
  36. supplied for pip to install/upgrade.
  37. """
  38. def __init__(
  39. self,
  40. factory, # type: Factory
  41. constraints, # type: Dict[str, Constraint]
  42. ignore_dependencies, # type: bool
  43. upgrade_strategy, # type: str
  44. user_requested, # type: Dict[str, int]
  45. ):
  46. # type: (...) -> None
  47. self._factory = factory
  48. self._constraints = constraints
  49. self._ignore_dependencies = ignore_dependencies
  50. self._upgrade_strategy = upgrade_strategy
  51. self._user_requested = user_requested
  52. def identify(self, requirement_or_candidate):
  53. # type: (Union[Requirement, Candidate]) -> str
  54. return requirement_or_candidate.name
  55. def get_preference(
  56. self,
  57. identifier: str,
  58. resolutions: Mapping[str, Candidate],
  59. candidates: Mapping[str, Iterator[Candidate]],
  60. information: Mapping[str, Iterator["PreferenceInformation"]],
  61. ) -> "Preference":
  62. """Produce a sort key for given requirement based on preference.
  63. The lower the return value is, the more preferred this group of
  64. arguments is.
  65. Currently pip considers the followings in order:
  66. * Prefer if any of the known requirements points to an explicit URL.
  67. * If equal, prefer if any requirements contain ``===`` and ``==``.
  68. * If equal, prefer if requirements include version constraints, e.g.
  69. ``>=`` and ``<``.
  70. * If equal, prefer user-specified (non-transitive) requirements, and
  71. order user-specified requirements by the order they are specified.
  72. * If equal, order alphabetically for consistency (helps debuggability).
  73. """
  74. def _get_restrictive_rating(requirements):
  75. # type: (Iterable[Requirement]) -> int
  76. """Rate how restrictive a set of requirements are.
  77. ``Requirement.get_candidate_lookup()`` returns a 2-tuple for
  78. lookup. The first element is ``Optional[Candidate]`` and the
  79. second ``Optional[InstallRequirement]``.
  80. * If the requirement is an explicit one, the explicitly-required
  81. candidate is returned as the first element.
  82. * If the requirement is based on a PEP 508 specifier, the backing
  83. ``InstallRequirement`` is returned as the second element.
  84. We use the first element to check whether there is an explicit
  85. requirement, and the second for equality operator.
  86. """
  87. lookups = (r.get_candidate_lookup() for r in requirements)
  88. cands, ireqs = zip(*lookups)
  89. if any(cand is not None for cand in cands):
  90. return 0
  91. spec_sets = (ireq.specifier for ireq in ireqs if ireq)
  92. operators = [
  93. specifier.operator for spec_set in spec_sets for specifier in spec_set
  94. ]
  95. if any(op in ("==", "===") for op in operators):
  96. return 1
  97. if operators:
  98. return 2
  99. # A "bare" requirement without any version requirements.
  100. return 3
  101. rating = _get_restrictive_rating(r for r, _ in information[identifier])
  102. order = self._user_requested.get(identifier, float("inf"))
  103. # HACK: Setuptools have a very long and solid backward compatibility
  104. # track record, and extremely few projects would request a narrow,
  105. # non-recent version range of it since that would break a lot things.
  106. # (Most projects specify it only to request for an installer feature,
  107. # which does not work, but that's another topic.) Intentionally
  108. # delaying Setuptools helps reduce branches the resolver has to check.
  109. # This serves as a temporary fix for issues like "apache-airlfow[all]"
  110. # while we work on "proper" branch pruning techniques.
  111. delay_this = identifier == "setuptools"
  112. return (delay_this, rating, order, identifier)
  113. def find_matches(
  114. self,
  115. identifier: str,
  116. requirements: Mapping[str, Iterator[Requirement]],
  117. incompatibilities: Mapping[str, Iterator[Candidate]],
  118. ) -> Iterable[Candidate]:
  119. def _eligible_for_upgrade(name):
  120. # type: (str) -> bool
  121. """Are upgrades allowed for this project?
  122. This checks the upgrade strategy, and whether the project was one
  123. that the user specified in the command line, in order to decide
  124. whether we should upgrade if there's a newer version available.
  125. (Note that we don't need access to the `--upgrade` flag, because
  126. an upgrade strategy of "to-satisfy-only" means that `--upgrade`
  127. was not specified).
  128. """
  129. if self._upgrade_strategy == "eager":
  130. return True
  131. elif self._upgrade_strategy == "only-if-needed":
  132. return name in self._user_requested
  133. return False
  134. return self._factory.find_candidates(
  135. identifier=identifier,
  136. requirements=requirements,
  137. constraint=self._constraints.get(identifier, Constraint.empty()),
  138. prefers_installed=(not _eligible_for_upgrade(identifier)),
  139. incompatibilities=incompatibilities,
  140. )
  141. def is_satisfied_by(self, requirement, candidate):
  142. # type: (Requirement, Candidate) -> bool
  143. return requirement.is_satisfied_by(candidate)
  144. def get_dependencies(self, candidate):
  145. # type: (Candidate) -> Sequence[Requirement]
  146. with_requires = not self._ignore_dependencies
  147. return [r for r in candidate.iter_dependencies(with_requires) if r is not None]