provider.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import collections
  2. import math
  3. from typing import (
  4. TYPE_CHECKING,
  5. Dict,
  6. Iterable,
  7. Iterator,
  8. Mapping,
  9. Sequence,
  10. TypeVar,
  11. Union,
  12. )
  13. from pip._vendor.resolvelib.providers import AbstractProvider
  14. from .base import Candidate, Constraint, Requirement
  15. from .candidates import REQUIRES_PYTHON_IDENTIFIER
  16. from .factory import Factory
  17. if TYPE_CHECKING:
  18. from pip._vendor.resolvelib.providers import Preference
  19. from pip._vendor.resolvelib.resolvers import RequirementInformation
  20. PreferenceInformation = RequirementInformation[Requirement, Candidate]
  21. _ProviderBase = AbstractProvider[Requirement, Candidate, str]
  22. else:
  23. _ProviderBase = AbstractProvider
  24. # Notes on the relationship between the provider, the factory, and the
  25. # candidate and requirement classes.
  26. #
  27. # The provider is a direct implementation of the resolvelib class. Its role
  28. # is to deliver the API that resolvelib expects.
  29. #
  30. # Rather than work with completely abstract "requirement" and "candidate"
  31. # concepts as resolvelib does, pip has concrete classes implementing these two
  32. # ideas. The API of Requirement and Candidate objects are defined in the base
  33. # classes, but essentially map fairly directly to the equivalent provider
  34. # methods. In particular, `find_matches` and `is_satisfied_by` are
  35. # requirement methods, and `get_dependencies` is a candidate method.
  36. #
  37. # The factory is the interface to pip's internal mechanisms. It is stateless,
  38. # and is created by the resolver and held as a property of the provider. It is
  39. # responsible for creating Requirement and Candidate objects, and provides
  40. # services to those objects (access to pip's finder and preparer).
  41. D = TypeVar("D")
  42. V = TypeVar("V")
  43. def _get_with_identifier(
  44. mapping: Mapping[str, V],
  45. identifier: str,
  46. default: D,
  47. ) -> Union[D, V]:
  48. """Get item from a package name lookup mapping with a resolver identifier.
  49. This extra logic is needed when the target mapping is keyed by package
  50. name, which cannot be directly looked up with an identifier (which may
  51. contain requested extras). Additional logic is added to also look up a value
  52. by "cleaning up" the extras from the identifier.
  53. """
  54. if identifier in mapping:
  55. return mapping[identifier]
  56. # HACK: Theoretically we should check whether this identifier is a valid
  57. # "NAME[EXTRAS]" format, and parse out the name part with packaging or
  58. # some regular expression. But since pip's resolver only spits out three
  59. # kinds of identifiers: normalized PEP 503 names, normalized names plus
  60. # extras, and Requires-Python, we can cheat a bit here.
  61. name, open_bracket, _ = identifier.partition("[")
  62. if open_bracket and name in mapping:
  63. return mapping[name]
  64. return default
  65. class PipProvider(_ProviderBase):
  66. """Pip's provider implementation for resolvelib.
  67. :params constraints: A mapping of constraints specified by the user. Keys
  68. are canonicalized project names.
  69. :params ignore_dependencies: Whether the user specified ``--no-deps``.
  70. :params upgrade_strategy: The user-specified upgrade strategy.
  71. :params user_requested: A set of canonicalized package names that the user
  72. supplied for pip to install/upgrade.
  73. """
  74. def __init__(
  75. self,
  76. factory: Factory,
  77. constraints: Dict[str, Constraint],
  78. ignore_dependencies: bool,
  79. upgrade_strategy: str,
  80. user_requested: Dict[str, int],
  81. ) -> None:
  82. self._factory = factory
  83. self._constraints = constraints
  84. self._ignore_dependencies = ignore_dependencies
  85. self._upgrade_strategy = upgrade_strategy
  86. self._user_requested = user_requested
  87. self._known_depths: Dict[str, float] = collections.defaultdict(lambda: math.inf)
  88. def identify(self, requirement_or_candidate: Union[Requirement, Candidate]) -> str:
  89. return requirement_or_candidate.name
  90. def get_preference(
  91. self,
  92. identifier: str,
  93. resolutions: Mapping[str, Candidate],
  94. candidates: Mapping[str, Iterator[Candidate]],
  95. information: Mapping[str, Iterable["PreferenceInformation"]],
  96. backtrack_causes: Sequence["PreferenceInformation"],
  97. ) -> "Preference":
  98. """Produce a sort key for given requirement based on preference.
  99. The lower the return value is, the more preferred this group of
  100. arguments is.
  101. Currently pip considers the following in order:
  102. * Prefer if any of the known requirements is "direct", e.g. points to an
  103. explicit URL.
  104. * If equal, prefer if any requirement is "pinned", i.e. contains
  105. operator ``===`` or ``==``.
  106. * If equal, calculate an approximate "depth" and resolve requirements
  107. closer to the user-specified requirements first. If the depth cannot
  108. by determined (eg: due to no matching parents), it is considered
  109. infinite.
  110. * Order user-specified requirements by the order they are specified.
  111. * If equal, prefers "non-free" requirements, i.e. contains at least one
  112. operator, such as ``>=`` or ``<``.
  113. * If equal, order alphabetically for consistency (helps debuggability).
  114. """
  115. try:
  116. next(iter(information[identifier]))
  117. except StopIteration:
  118. # There is no information for this identifier, so there's no known
  119. # candidates.
  120. has_information = False
  121. else:
  122. has_information = True
  123. if has_information:
  124. lookups = (r.get_candidate_lookup() for r, _ in information[identifier])
  125. candidate, ireqs = zip(*lookups)
  126. else:
  127. candidate, ireqs = None, ()
  128. operators = [
  129. specifier.operator
  130. for specifier_set in (ireq.specifier for ireq in ireqs if ireq)
  131. for specifier in specifier_set
  132. ]
  133. direct = candidate is not None
  134. pinned = any(op[:2] == "==" for op in operators)
  135. unfree = bool(operators)
  136. try:
  137. requested_order: Union[int, float] = self._user_requested[identifier]
  138. except KeyError:
  139. requested_order = math.inf
  140. if has_information:
  141. parent_depths = (
  142. self._known_depths[parent.name] if parent is not None else 0.0
  143. for _, parent in information[identifier]
  144. )
  145. inferred_depth = min(d for d in parent_depths) + 1.0
  146. else:
  147. inferred_depth = math.inf
  148. else:
  149. inferred_depth = 1.0
  150. self._known_depths[identifier] = inferred_depth
  151. requested_order = self._user_requested.get(identifier, math.inf)
  152. # Requires-Python has only one candidate and the check is basically
  153. # free, so we always do it first to avoid needless work if it fails.
  154. requires_python = identifier == REQUIRES_PYTHON_IDENTIFIER
  155. # Prefer the causes of backtracking on the assumption that the problem
  156. # resolving the dependency tree is related to the failures that caused
  157. # the backtracking
  158. backtrack_cause = self.is_backtrack_cause(identifier, backtrack_causes)
  159. return (
  160. not requires_python,
  161. not direct,
  162. not pinned,
  163. not backtrack_cause,
  164. inferred_depth,
  165. requested_order,
  166. not unfree,
  167. identifier,
  168. )
  169. def find_matches(
  170. self,
  171. identifier: str,
  172. requirements: Mapping[str, Iterator[Requirement]],
  173. incompatibilities: Mapping[str, Iterator[Candidate]],
  174. ) -> Iterable[Candidate]:
  175. def _eligible_for_upgrade(identifier: str) -> bool:
  176. """Are upgrades allowed for this project?
  177. This checks the upgrade strategy, and whether the project was one
  178. that the user specified in the command line, in order to decide
  179. whether we should upgrade if there's a newer version available.
  180. (Note that we don't need access to the `--upgrade` flag, because
  181. an upgrade strategy of "to-satisfy-only" means that `--upgrade`
  182. was not specified).
  183. """
  184. if self._upgrade_strategy == "eager":
  185. return True
  186. elif self._upgrade_strategy == "only-if-needed":
  187. user_order = _get_with_identifier(
  188. self._user_requested,
  189. identifier,
  190. default=None,
  191. )
  192. return user_order is not None
  193. return False
  194. constraint = _get_with_identifier(
  195. self._constraints,
  196. identifier,
  197. default=Constraint.empty(),
  198. )
  199. return self._factory.find_candidates(
  200. identifier=identifier,
  201. requirements=requirements,
  202. constraint=constraint,
  203. prefers_installed=(not _eligible_for_upgrade(identifier)),
  204. incompatibilities=incompatibilities,
  205. )
  206. def is_satisfied_by(self, requirement: Requirement, candidate: Candidate) -> bool:
  207. return requirement.is_satisfied_by(candidate)
  208. def get_dependencies(self, candidate: Candidate) -> Sequence[Requirement]:
  209. with_requires = not self._ignore_dependencies
  210. return [r for r in candidate.iter_dependencies(with_requires) if r is not None]
  211. @staticmethod
  212. def is_backtrack_cause(
  213. identifier: str, backtrack_causes: Sequence["PreferenceInformation"]
  214. ) -> bool:
  215. for backtrack_cause in backtrack_causes:
  216. if identifier == backtrack_cause.requirement.name:
  217. return True
  218. if backtrack_cause.parent and identifier == backtrack_cause.parent.name:
  219. return True
  220. return False