resolver.py 11 KB

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