resolver.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 pip._internal.utils.deprecation import deprecated
  21. from pip._internal.utils.filetypes import is_archive_file
  22. from .base import Candidate, Requirement
  23. from .factory import Factory
  24. if TYPE_CHECKING:
  25. from pip._vendor.resolvelib.resolvers import Result as RLResult
  26. Result = RLResult[Requirement, Candidate, str]
  27. logger = logging.getLogger(__name__)
  28. class Resolver(BaseResolver):
  29. _allowed_strategies = {"eager", "only-if-needed", "to-satisfy-only"}
  30. def __init__(
  31. self,
  32. preparer: RequirementPreparer,
  33. finder: PackageFinder,
  34. wheel_cache: Optional[WheelCache],
  35. make_install_req: InstallRequirementProvider,
  36. use_user_site: bool,
  37. ignore_dependencies: bool,
  38. ignore_installed: bool,
  39. ignore_requires_python: bool,
  40. force_reinstall: bool,
  41. upgrade_strategy: str,
  42. py_version_info: Optional[Tuple[int, ...]] = None,
  43. ):
  44. super().__init__()
  45. assert upgrade_strategy in self._allowed_strategies
  46. self.factory = Factory(
  47. finder=finder,
  48. preparer=preparer,
  49. make_install_req=make_install_req,
  50. wheel_cache=wheel_cache,
  51. use_user_site=use_user_site,
  52. force_reinstall=force_reinstall,
  53. ignore_installed=ignore_installed,
  54. ignore_requires_python=ignore_requires_python,
  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. looks_like_sdist = (
  123. is_archive_file(candidate.source_link.file_path)
  124. and candidate.source_link.ext != ".zip"
  125. )
  126. if looks_like_sdist:
  127. # is a local sdist -- show a deprecation warning!
  128. reason = (
  129. "Source distribution is being reinstalled despite an "
  130. "installed package having the same name and version as "
  131. "the installed package."
  132. )
  133. replacement = "use --force-reinstall"
  134. deprecated(
  135. reason=reason,
  136. replacement=replacement,
  137. gone_in="21.3",
  138. issue=8711,
  139. )
  140. # is a local sdist or path -- reinstall
  141. ireq.should_reinstall = True
  142. else:
  143. continue
  144. link = candidate.source_link
  145. if link and link.is_yanked:
  146. # The reason can contain non-ASCII characters, Unicode
  147. # is required for Python 2.
  148. msg = (
  149. "The candidate selected for download or install is a "
  150. "yanked version: {name!r} candidate (version {version} "
  151. "at {link})\nReason for being yanked: {reason}"
  152. ).format(
  153. name=candidate.name,
  154. version=candidate.version,
  155. link=link,
  156. reason=link.yanked_reason or "<none given>",
  157. )
  158. logger.warning(msg)
  159. req_set.add_named_requirement(ireq)
  160. reqs = req_set.all_requirements
  161. self.factory.preparer.prepare_linked_requirements_more(reqs)
  162. return req_set
  163. def get_installation_order(
  164. self, req_set: RequirementSet
  165. ) -> List[InstallRequirement]:
  166. """Get order for installation of requirements in RequirementSet.
  167. The returned list contains a requirement before another that depends on
  168. it. This helps ensure that the environment is kept consistent as they
  169. get installed one-by-one.
  170. The current implementation creates a topological ordering of the
  171. dependency graph, while breaking any cycles in the graph at arbitrary
  172. points. We make no guarantees about where the cycle would be broken,
  173. other than they would be broken.
  174. """
  175. assert self._result is not None, "must call resolve() first"
  176. graph = self._result.graph
  177. weights = get_topological_weights(
  178. graph,
  179. expected_node_count=len(self._result.mapping) + 1,
  180. )
  181. sorted_items = sorted(
  182. req_set.requirements.items(),
  183. key=functools.partial(_req_set_item_sorter, weights=weights),
  184. reverse=True,
  185. )
  186. return [ireq for _, ireq in sorted_items]
  187. def get_topological_weights(
  188. graph: "DirectedGraph[Optional[str]]", expected_node_count: int
  189. ) -> Dict[Optional[str], int]:
  190. """Assign weights to each node based on how "deep" they are.
  191. This implementation may change at any point in the future without prior
  192. notice.
  193. We take the length for the longest path to any node from root, ignoring any
  194. paths that contain a single node twice (i.e. cycles). This is done through
  195. a depth-first search through the graph, while keeping track of the path to
  196. the node.
  197. Cycles in the graph result would result in node being revisited while also
  198. being it's own path. In this case, take no action. This helps ensure we
  199. don't get stuck in a cycle.
  200. When assigning weight, the longer path (i.e. larger length) is preferred.
  201. """
  202. path: Set[Optional[str]] = set()
  203. weights: Dict[Optional[str], int] = {}
  204. def visit(node: Optional[str]) -> None:
  205. if node in path:
  206. # We hit a cycle, so we'll break it here.
  207. return
  208. # Time to visit the children!
  209. path.add(node)
  210. for child in graph.iter_children(node):
  211. visit(child)
  212. path.remove(node)
  213. last_known_parent_count = weights.get(node, 0)
  214. weights[node] = max(last_known_parent_count, len(path))
  215. # `None` is guaranteed to be the root node by resolvelib.
  216. visit(None)
  217. # Sanity checks
  218. assert weights[None] == 0
  219. assert len(weights) == expected_node_count
  220. return weights
  221. def _req_set_item_sorter(
  222. item: Tuple[str, InstallRequirement],
  223. weights: Dict[Optional[str], int],
  224. ) -> Tuple[int, str]:
  225. """Key function used to sort install requirements for installation.
  226. Based on the "weight" mapping calculated in ``get_installation_order()``.
  227. The canonical package name is returned as the second member as a tie-
  228. breaker to ensure the result is predictable, which is useful in tests.
  229. """
  230. name = canonicalize_name(item[0])
  231. return weights[name], name