You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

resolve.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. """Dependency Resolution
  2. The dependency resolution in pip is performed as follows:
  3. for top-level requirements:
  4. a. only one spec allowed per project, regardless of conflicts or not.
  5. otherwise a "double requirement" exception is raised
  6. b. they override sub-dependency requirements.
  7. for sub-dependencies
  8. a. "first found, wins" (where the order is breadth first)
  9. """
  10. import logging
  11. from collections import defaultdict
  12. from itertools import chain
  13. from pip._internal.exceptions import (
  14. BestVersionAlreadyInstalled, DistributionNotFound, HashError, HashErrors,
  15. UnsupportedPythonVersion,
  16. )
  17. from pip._internal.req.constructors import install_req_from_req_string
  18. from pip._internal.utils.logging import indent_log
  19. from pip._internal.utils.misc import dist_in_usersite, ensure_dir
  20. from pip._internal.utils.packaging import check_dist_requires_python
  21. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  22. if MYPY_CHECK_RUNNING:
  23. from typing import Optional, DefaultDict, List, Set # noqa: F401
  24. from pip._internal.download import PipSession # noqa: F401
  25. from pip._internal.req.req_install import InstallRequirement # noqa: F401
  26. from pip._internal.index import PackageFinder # noqa: F401
  27. from pip._internal.req.req_set import RequirementSet # noqa: F401
  28. from pip._internal.operations.prepare import ( # noqa: F401
  29. DistAbstraction, RequirementPreparer
  30. )
  31. from pip._internal.cache import WheelCache # noqa: F401
  32. logger = logging.getLogger(__name__)
  33. class Resolver(object):
  34. """Resolves which packages need to be installed/uninstalled to perform \
  35. the requested operation without breaking the requirements of any package.
  36. """
  37. _allowed_strategies = {"eager", "only-if-needed", "to-satisfy-only"}
  38. def __init__(
  39. self,
  40. preparer, # type: RequirementPreparer
  41. session, # type: PipSession
  42. finder, # type: PackageFinder
  43. wheel_cache, # type: Optional[WheelCache]
  44. use_user_site, # type: bool
  45. ignore_dependencies, # type: bool
  46. ignore_installed, # type: bool
  47. ignore_requires_python, # type: bool
  48. force_reinstall, # type: bool
  49. isolated, # type: bool
  50. upgrade_strategy, # type: str
  51. use_pep517=None # type: Optional[bool]
  52. ):
  53. # type: (...) -> None
  54. super(Resolver, self).__init__()
  55. assert upgrade_strategy in self._allowed_strategies
  56. self.preparer = preparer
  57. self.finder = finder
  58. self.session = session
  59. # NOTE: This would eventually be replaced with a cache that can give
  60. # information about both sdist and wheels transparently.
  61. self.wheel_cache = wheel_cache
  62. # This is set in resolve
  63. self.require_hashes = None # type: Optional[bool]
  64. self.upgrade_strategy = upgrade_strategy
  65. self.force_reinstall = force_reinstall
  66. self.isolated = isolated
  67. self.ignore_dependencies = ignore_dependencies
  68. self.ignore_installed = ignore_installed
  69. self.ignore_requires_python = ignore_requires_python
  70. self.use_user_site = use_user_site
  71. self.use_pep517 = use_pep517
  72. self._discovered_dependencies = \
  73. defaultdict(list) # type: DefaultDict[str, List]
  74. def resolve(self, requirement_set):
  75. # type: (RequirementSet) -> None
  76. """Resolve what operations need to be done
  77. As a side-effect of this method, the packages (and their dependencies)
  78. are downloaded, unpacked and prepared for installation. This
  79. preparation is done by ``pip.operations.prepare``.
  80. Once PyPI has static dependency metadata available, it would be
  81. possible to move the preparation to become a step separated from
  82. dependency resolution.
  83. """
  84. # make the wheelhouse
  85. if self.preparer.wheel_download_dir:
  86. ensure_dir(self.preparer.wheel_download_dir)
  87. # If any top-level requirement has a hash specified, enter
  88. # hash-checking mode, which requires hashes from all.
  89. root_reqs = (
  90. requirement_set.unnamed_requirements +
  91. list(requirement_set.requirements.values())
  92. )
  93. self.require_hashes = (
  94. requirement_set.require_hashes or
  95. any(req.has_hash_options for req in root_reqs)
  96. )
  97. # Display where finder is looking for packages
  98. locations = self.finder.get_formatted_locations()
  99. if locations:
  100. logger.info(locations)
  101. # Actually prepare the files, and collect any exceptions. Most hash
  102. # exceptions cannot be checked ahead of time, because
  103. # req.populate_link() needs to be called before we can make decisions
  104. # based on link type.
  105. discovered_reqs = [] # type: List[InstallRequirement]
  106. hash_errors = HashErrors()
  107. for req in chain(root_reqs, discovered_reqs):
  108. try:
  109. discovered_reqs.extend(
  110. self._resolve_one(requirement_set, req)
  111. )
  112. except HashError as exc:
  113. exc.req = req
  114. hash_errors.append(exc)
  115. if hash_errors:
  116. raise hash_errors
  117. def _is_upgrade_allowed(self, req):
  118. # type: (InstallRequirement) -> bool
  119. if self.upgrade_strategy == "to-satisfy-only":
  120. return False
  121. elif self.upgrade_strategy == "eager":
  122. return True
  123. else:
  124. assert self.upgrade_strategy == "only-if-needed"
  125. return req.is_direct
  126. def _set_req_to_reinstall(self, req):
  127. # type: (InstallRequirement) -> None
  128. """
  129. Set a requirement to be installed.
  130. """
  131. # Don't uninstall the conflict if doing a user install and the
  132. # conflict is not a user install.
  133. if not self.use_user_site or dist_in_usersite(req.satisfied_by):
  134. req.conflicts_with = req.satisfied_by
  135. req.satisfied_by = None
  136. # XXX: Stop passing requirement_set for options
  137. def _check_skip_installed(self, req_to_install):
  138. # type: (InstallRequirement) -> Optional[str]
  139. """Check if req_to_install should be skipped.
  140. This will check if the req is installed, and whether we should upgrade
  141. or reinstall it, taking into account all the relevant user options.
  142. After calling this req_to_install will only have satisfied_by set to
  143. None if the req_to_install is to be upgraded/reinstalled etc. Any
  144. other value will be a dist recording the current thing installed that
  145. satisfies the requirement.
  146. Note that for vcs urls and the like we can't assess skipping in this
  147. routine - we simply identify that we need to pull the thing down,
  148. then later on it is pulled down and introspected to assess upgrade/
  149. reinstalls etc.
  150. :return: A text reason for why it was skipped, or None.
  151. """
  152. if self.ignore_installed:
  153. return None
  154. req_to_install.check_if_exists(self.use_user_site)
  155. if not req_to_install.satisfied_by:
  156. return None
  157. if self.force_reinstall:
  158. self._set_req_to_reinstall(req_to_install)
  159. return None
  160. if not self._is_upgrade_allowed(req_to_install):
  161. if self.upgrade_strategy == "only-if-needed":
  162. return 'already satisfied, skipping upgrade'
  163. return 'already satisfied'
  164. # Check for the possibility of an upgrade. For link-based
  165. # requirements we have to pull the tree down and inspect to assess
  166. # the version #, so it's handled way down.
  167. if not req_to_install.link:
  168. try:
  169. self.finder.find_requirement(req_to_install, upgrade=True)
  170. except BestVersionAlreadyInstalled:
  171. # Then the best version is installed.
  172. return 'already up-to-date'
  173. except DistributionNotFound:
  174. # No distribution found, so we squash the error. It will
  175. # be raised later when we re-try later to do the install.
  176. # Why don't we just raise here?
  177. pass
  178. self._set_req_to_reinstall(req_to_install)
  179. return None
  180. def _get_abstract_dist_for(self, req):
  181. # type: (InstallRequirement) -> DistAbstraction
  182. """Takes a InstallRequirement and returns a single AbstractDist \
  183. representing a prepared variant of the same.
  184. """
  185. assert self.require_hashes is not None, (
  186. "require_hashes should have been set in Resolver.resolve()"
  187. )
  188. if req.editable:
  189. return self.preparer.prepare_editable_requirement(
  190. req, self.require_hashes, self.use_user_site, self.finder,
  191. )
  192. # satisfied_by is only evaluated by calling _check_skip_installed,
  193. # so it must be None here.
  194. assert req.satisfied_by is None
  195. skip_reason = self._check_skip_installed(req)
  196. if req.satisfied_by:
  197. return self.preparer.prepare_installed_requirement(
  198. req, self.require_hashes, skip_reason
  199. )
  200. upgrade_allowed = self._is_upgrade_allowed(req)
  201. abstract_dist = self.preparer.prepare_linked_requirement(
  202. req, self.session, self.finder, upgrade_allowed,
  203. self.require_hashes
  204. )
  205. # NOTE
  206. # The following portion is for determining if a certain package is
  207. # going to be re-installed/upgraded or not and reporting to the user.
  208. # This should probably get cleaned up in a future refactor.
  209. # req.req is only avail after unpack for URL
  210. # pkgs repeat check_if_exists to uninstall-on-upgrade
  211. # (#14)
  212. if not self.ignore_installed:
  213. req.check_if_exists(self.use_user_site)
  214. if req.satisfied_by:
  215. should_modify = (
  216. self.upgrade_strategy != "to-satisfy-only" or
  217. self.force_reinstall or
  218. self.ignore_installed or
  219. req.link.scheme == 'file'
  220. )
  221. if should_modify:
  222. self._set_req_to_reinstall(req)
  223. else:
  224. logger.info(
  225. 'Requirement already satisfied (use --upgrade to upgrade):'
  226. ' %s', req,
  227. )
  228. return abstract_dist
  229. def _resolve_one(
  230. self,
  231. requirement_set, # type: RequirementSet
  232. req_to_install # type: InstallRequirement
  233. ):
  234. # type: (...) -> List[InstallRequirement]
  235. """Prepare a single requirements file.
  236. :return: A list of additional InstallRequirements to also install.
  237. """
  238. # Tell user what we are doing for this requirement:
  239. # obtain (editable), skipping, processing (local url), collecting
  240. # (remote url or package name)
  241. if req_to_install.constraint or req_to_install.prepared:
  242. return []
  243. req_to_install.prepared = True
  244. # register tmp src for cleanup in case something goes wrong
  245. requirement_set.reqs_to_cleanup.append(req_to_install)
  246. abstract_dist = self._get_abstract_dist_for(req_to_install)
  247. # Parse and return dependencies
  248. dist = abstract_dist.dist()
  249. try:
  250. check_dist_requires_python(dist)
  251. except UnsupportedPythonVersion as err:
  252. if self.ignore_requires_python:
  253. logger.warning(err.args[0])
  254. else:
  255. raise
  256. more_reqs = [] # type: List[InstallRequirement]
  257. def add_req(subreq, extras_requested):
  258. sub_install_req = install_req_from_req_string(
  259. str(subreq),
  260. req_to_install,
  261. isolated=self.isolated,
  262. wheel_cache=self.wheel_cache,
  263. use_pep517=self.use_pep517
  264. )
  265. parent_req_name = req_to_install.name
  266. to_scan_again, add_to_parent = requirement_set.add_requirement(
  267. sub_install_req,
  268. parent_req_name=parent_req_name,
  269. extras_requested=extras_requested,
  270. )
  271. if parent_req_name and add_to_parent:
  272. self._discovered_dependencies[parent_req_name].append(
  273. add_to_parent
  274. )
  275. more_reqs.extend(to_scan_again)
  276. with indent_log():
  277. # We add req_to_install before its dependencies, so that we
  278. # can refer to it when adding dependencies.
  279. if not requirement_set.has_requirement(req_to_install.name):
  280. # 'unnamed' requirements will get added here
  281. req_to_install.is_direct = True
  282. requirement_set.add_requirement(
  283. req_to_install, parent_req_name=None,
  284. )
  285. if not self.ignore_dependencies:
  286. if req_to_install.extras:
  287. logger.debug(
  288. "Installing extra requirements: %r",
  289. ','.join(req_to_install.extras),
  290. )
  291. missing_requested = sorted(
  292. set(req_to_install.extras) - set(dist.extras)
  293. )
  294. for missing in missing_requested:
  295. logger.warning(
  296. '%s does not provide the extra \'%s\'',
  297. dist, missing
  298. )
  299. available_requested = sorted(
  300. set(dist.extras) & set(req_to_install.extras)
  301. )
  302. for subreq in dist.requires(available_requested):
  303. add_req(subreq, extras_requested=available_requested)
  304. if not req_to_install.editable and not req_to_install.satisfied_by:
  305. # XXX: --no-install leads this to report 'Successfully
  306. # downloaded' for only non-editable reqs, even though we took
  307. # action on them.
  308. requirement_set.successfully_downloaded.append(req_to_install)
  309. return more_reqs
  310. def get_installation_order(self, req_set):
  311. # type: (RequirementSet) -> List[InstallRequirement]
  312. """Create the installation order.
  313. The installation order is topological - requirements are installed
  314. before the requiring thing. We break cycles at an arbitrary point,
  315. and make no other guarantees.
  316. """
  317. # The current implementation, which we may change at any point
  318. # installs the user specified things in the order given, except when
  319. # dependencies must come earlier to achieve topological order.
  320. order = []
  321. ordered_reqs = set() # type: Set[InstallRequirement]
  322. def schedule(req):
  323. if req.satisfied_by or req in ordered_reqs:
  324. return
  325. if req.constraint:
  326. return
  327. ordered_reqs.add(req)
  328. for dep in self._discovered_dependencies[req.name]:
  329. schedule(dep)
  330. order.append(req)
  331. for install_req in req_set.requirements.values():
  332. schedule(install_req)
  333. return order