resolver.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import functools
  2. import logging
  3. import os
  4. from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, cast
  5. from pip._vendor.packaging.utils import canonicalize_name
  6. from pip._vendor.resolvelib import BaseReporter, ResolutionImpossible
  7. from pip._vendor.resolvelib import Resolver as RLResolver
  8. from pip._vendor.resolvelib.structs import DirectedGraph
  9. from pip._internal.cache import WheelCache
  10. from pip._internal.index.package_finder import PackageFinder
  11. from pip._internal.operations.prepare import RequirementPreparer
  12. from pip._internal.req.req_install import InstallRequirement
  13. from pip._internal.req.req_set import RequirementSet
  14. from pip._internal.resolution.base import BaseResolver, InstallRequirementProvider
  15. from pip._internal.resolution.resolvelib.provider import PipProvider
  16. from pip._internal.resolution.resolvelib.reporter import (
  17. PipDebuggingReporter,
  18. PipReporter,
  19. )
  20. from .base import Candidate, Requirement
  21. from .factory import Factory
  22. if TYPE_CHECKING:
  23. from pip._vendor.resolvelib.resolvers import Result as RLResult
  24. Result = RLResult[Requirement, Candidate, str]
  25. logger = logging.getLogger(__name__)
  26. class Resolver(BaseResolver):
  27. _allowed_strategies = {"eager", "only-if-needed", "to-satisfy-only"}
  28. def __init__(
  29. self,
  30. preparer: RequirementPreparer,
  31. finder: PackageFinder,
  32. wheel_cache: Optional[WheelCache],
  33. make_install_req: InstallRequirementProvider,
  34. use_user_site: bool,
  35. ignore_dependencies: bool,
  36. ignore_installed: bool,
  37. ignore_requires_python: bool,
  38. force_reinstall: bool,
  39. upgrade_strategy: str,
  40. suppress_build_failures: bool,
  41. py_version_info: Optional[Tuple[int, ...]] = None,
  42. ):
  43. super().__init__()
  44. assert upgrade_strategy in self._allowed_strategies
  45. self.factory = Factory(
  46. finder=finder,
  47. preparer=preparer,
  48. make_install_req=make_install_req,
  49. wheel_cache=wheel_cache,
  50. use_user_site=use_user_site,
  51. force_reinstall=force_reinstall,
  52. ignore_installed=ignore_installed,
  53. ignore_requires_python=ignore_requires_python,
  54. suppress_build_failures=suppress_build_failures,
  55. py_version_info=py_version_info,
  56. )
  57. self.ignore_dependencies = ignore_dependencies
  58. self.upgrade_strategy = upgrade_strategy
  59. self._result: Optional[Result] = None
  60. def resolve(
  61. self, root_reqs: List[InstallRequirement], check_supported_wheels: bool
  62. ) -> RequirementSet:
  63. collected = self.factory.collect_root_requirements(root_reqs)
  64. provider = PipProvider(
  65. factory=self.factory,
  66. constraints=collected.constraints,
  67. ignore_dependencies=self.ignore_dependencies,
  68. upgrade_strategy=self.upgrade_strategy,
  69. user_requested=collected.user_requested,
  70. )
  71. if "PIP_RESOLVER_DEBUG" in os.environ:
  72. reporter: BaseReporter = PipDebuggingReporter()
  73. else:
  74. reporter = PipReporter()
  75. resolver: RLResolver[Requirement, Candidate, str] = RLResolver(
  76. provider,
  77. reporter,
  78. )
  79. try:
  80. try_to_avoid_resolution_too_deep = 2000000
  81. result = self._result = resolver.resolve(
  82. collected.requirements, max_rounds=try_to_avoid_resolution_too_deep
  83. )
  84. except ResolutionImpossible as e:
  85. error = self.factory.get_installation_error(
  86. cast("ResolutionImpossible[Requirement, Candidate]", e),
  87. collected.constraints,
  88. )
  89. raise error from e
  90. req_set = RequirementSet(check_supported_wheels=check_supported_wheels)
  91. for candidate in result.mapping.values():
  92. ireq = candidate.get_install_requirement()
  93. if ireq is None:
  94. continue
  95. # Check if there is already an installation under the same name,
  96. # and set a flag for later stages to uninstall it, if needed.
  97. installed_dist = self.factory.get_dist_to_uninstall(candidate)
  98. if installed_dist is None:
  99. # There is no existing installation -- nothing to uninstall.
  100. ireq.should_reinstall = False
  101. elif self.factory.force_reinstall:
  102. # The --force-reinstall flag is set -- reinstall.
  103. ireq.should_reinstall = True
  104. elif installed_dist.version != candidate.version:
  105. # The installation is different in version -- reinstall.
  106. ireq.should_reinstall = True
  107. elif candidate.is_editable or installed_dist.editable:
  108. # The incoming distribution is editable, or different in
  109. # editable-ness to installation -- reinstall.
  110. ireq.should_reinstall = True
  111. elif candidate.source_link and candidate.source_link.is_file:
  112. # The incoming distribution is under file://
  113. if candidate.source_link.is_wheel:
  114. # is a local wheel -- do nothing.
  115. logger.info(
  116. "%s is already installed with the same version as the "
  117. "provided wheel. Use --force-reinstall to force an "
  118. "installation of the wheel.",
  119. ireq.name,
  120. )
  121. continue
  122. # is a local sdist or path -- reinstall
  123. ireq.should_reinstall = True
  124. else:
  125. continue
  126. link = candidate.source_link
  127. if link and link.is_yanked:
  128. # The reason can contain non-ASCII characters, Unicode
  129. # is required for Python 2.
  130. msg = (
  131. "The candidate selected for download or install is a "
  132. "yanked version: {name!r} candidate (version {version} "
  133. "at {link})\nReason for being yanked: {reason}"
  134. ).format(
  135. name=candidate.name,
  136. version=candidate.version,
  137. link=link,
  138. reason=link.yanked_reason or "<none given>",
  139. )
  140. logger.warning(msg)
  141. req_set.add_named_requirement(ireq)
  142. reqs = req_set.all_requirements
  143. self.factory.preparer.prepare_linked_requirements_more(reqs)
  144. return req_set
  145. def get_installation_order(
  146. self, req_set: RequirementSet
  147. ) -> List[InstallRequirement]:
  148. """Get order for installation of requirements in RequirementSet.
  149. The returned list contains a requirement before another that depends on
  150. it. This helps ensure that the environment is kept consistent as they
  151. get installed one-by-one.
  152. The current implementation creates a topological ordering of the
  153. dependency graph, giving more weight to packages with less
  154. or no dependencies, while breaking any cycles in the graph at
  155. arbitrary points. We make no guarantees about where the cycle
  156. would be broken, other than it *would* be broken.
  157. """
  158. assert self._result is not None, "must call resolve() first"
  159. if not req_set.requirements:
  160. # Nothing is left to install, so we do not need an order.
  161. return []
  162. graph = self._result.graph
  163. weights = get_topological_weights(graph, set(req_set.requirements.keys()))
  164. sorted_items = sorted(
  165. req_set.requirements.items(),
  166. key=functools.partial(_req_set_item_sorter, weights=weights),
  167. reverse=True,
  168. )
  169. return [ireq for _, ireq in sorted_items]
  170. def get_topological_weights(
  171. graph: "DirectedGraph[Optional[str]]", requirement_keys: Set[str]
  172. ) -> Dict[Optional[str], int]:
  173. """Assign weights to each node based on how "deep" they are.
  174. This implementation may change at any point in the future without prior
  175. notice.
  176. We first simplify the dependency graph by pruning any leaves and giving them
  177. the highest weight: a package without any dependencies should be installed
  178. first. This is done again and again in the same way, giving ever less weight
  179. to the newly found leaves. The loop stops when no leaves are left: all
  180. remaining packages have at least one dependency left in the graph.
  181. Then we continue with the remaining graph, by taking the length for the
  182. longest path to any node from root, ignoring any paths that contain a single
  183. node twice (i.e. cycles). This is done through a depth-first search through
  184. the graph, while keeping track of the path to the node.
  185. Cycles in the graph result would result in node being revisited while also
  186. being on its own path. In this case, take no action. This helps ensure we
  187. don't get stuck in a cycle.
  188. When assigning weight, the longer path (i.e. larger length) is preferred.
  189. We are only interested in the weights of packages that are in the
  190. requirement_keys.
  191. """
  192. path: Set[Optional[str]] = set()
  193. weights: Dict[Optional[str], int] = {}
  194. def visit(node: Optional[str]) -> None:
  195. if node in path:
  196. # We hit a cycle, so we'll break it here.
  197. return
  198. # Time to visit the children!
  199. path.add(node)
  200. for child in graph.iter_children(node):
  201. visit(child)
  202. path.remove(node)
  203. if node not in requirement_keys:
  204. return
  205. last_known_parent_count = weights.get(node, 0)
  206. weights[node] = max(last_known_parent_count, len(path))
  207. # Simplify the graph, pruning leaves that have no dependencies.
  208. # This is needed for large graphs (say over 200 packages) because the
  209. # `visit` function is exponentially slower then, taking minutes.
  210. # See https://github.com/pypa/pip/issues/10557
  211. # We will loop until we explicitly break the loop.
  212. while True:
  213. leaves = set()
  214. for key in graph:
  215. if key is None:
  216. continue
  217. for _child in graph.iter_children(key):
  218. # This means we have at least one child
  219. break
  220. else:
  221. # No child.
  222. leaves.add(key)
  223. if not leaves:
  224. # We are done simplifying.
  225. break
  226. # Calculate the weight for the leaves.
  227. weight = len(graph) - 1
  228. for leaf in leaves:
  229. if leaf not in requirement_keys:
  230. continue
  231. weights[leaf] = weight
  232. # Remove the leaves from the graph, making it simpler.
  233. for leaf in leaves:
  234. graph.remove(leaf)
  235. # Visit the remaining graph.
  236. # `None` is guaranteed to be the root node by resolvelib.
  237. visit(None)
  238. # Sanity check: all requirement keys should be in the weights,
  239. # and no other keys should be in the weights.
  240. difference = set(weights.keys()).difference(requirement_keys)
  241. assert not difference, difference
  242. return weights
  243. def _req_set_item_sorter(
  244. item: Tuple[str, InstallRequirement],
  245. weights: Dict[Optional[str], int],
  246. ) -> Tuple[int, str]:
  247. """Key function used to sort install requirements for installation.
  248. Based on the "weight" mapping calculated in ``get_installation_order()``.
  249. The canonical package name is returned as the second member as a tie-
  250. breaker to ensure the result is predictable, which is useful in tests.
  251. """
  252. name = canonicalize_name(item[0])
  253. return weights[name], name