resolver.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. # The following comment should be removed at some point in the future.
  11. # mypy: strict-optional=False
  12. import logging
  13. import sys
  14. from collections import defaultdict
  15. from itertools import chain
  16. from typing import DefaultDict, Iterable, List, Optional, Set, Tuple
  17. from pip._vendor.packaging import specifiers
  18. from pip._vendor.packaging.requirements import Requirement
  19. from pip._internal.cache import WheelCache
  20. from pip._internal.exceptions import (
  21. BestVersionAlreadyInstalled,
  22. DistributionNotFound,
  23. HashError,
  24. HashErrors,
  25. InstallationError,
  26. NoneMetadataError,
  27. UnsupportedPythonVersion,
  28. )
  29. from pip._internal.index.package_finder import PackageFinder
  30. from pip._internal.metadata import BaseDistribution
  31. from pip._internal.models.link import Link
  32. from pip._internal.models.wheel import Wheel
  33. from pip._internal.operations.prepare import RequirementPreparer
  34. from pip._internal.req.req_install import (
  35. InstallRequirement,
  36. check_invalid_constraint_type,
  37. )
  38. from pip._internal.req.req_set import RequirementSet
  39. from pip._internal.resolution.base import BaseResolver, InstallRequirementProvider
  40. from pip._internal.utils import compatibility_tags
  41. from pip._internal.utils.compatibility_tags import get_supported
  42. from pip._internal.utils.direct_url_helpers import direct_url_from_link
  43. from pip._internal.utils.logging import indent_log
  44. from pip._internal.utils.misc import normalize_version_info
  45. from pip._internal.utils.packaging import check_requires_python
  46. logger = logging.getLogger(__name__)
  47. DiscoveredDependencies = DefaultDict[str, List[InstallRequirement]]
  48. def _check_dist_requires_python(
  49. dist: BaseDistribution,
  50. version_info: Tuple[int, int, int],
  51. ignore_requires_python: bool = False,
  52. ) -> None:
  53. """
  54. Check whether the given Python version is compatible with a distribution's
  55. "Requires-Python" value.
  56. :param version_info: A 3-tuple of ints representing the Python
  57. major-minor-micro version to check.
  58. :param ignore_requires_python: Whether to ignore the "Requires-Python"
  59. value if the given Python version isn't compatible.
  60. :raises UnsupportedPythonVersion: When the given Python version isn't
  61. compatible.
  62. """
  63. # This idiosyncratically converts the SpecifierSet to str and let
  64. # check_requires_python then parse it again into SpecifierSet. But this
  65. # is the legacy resolver so I'm just not going to bother refactoring.
  66. try:
  67. requires_python = str(dist.requires_python)
  68. except FileNotFoundError as e:
  69. raise NoneMetadataError(dist, str(e))
  70. try:
  71. is_compatible = check_requires_python(
  72. requires_python,
  73. version_info=version_info,
  74. )
  75. except specifiers.InvalidSpecifier as exc:
  76. logger.warning(
  77. "Package %r has an invalid Requires-Python: %s", dist.raw_name, exc
  78. )
  79. return
  80. if is_compatible:
  81. return
  82. version = ".".join(map(str, version_info))
  83. if ignore_requires_python:
  84. logger.debug(
  85. "Ignoring failed Requires-Python check for package %r: %s not in %r",
  86. dist.raw_name,
  87. version,
  88. requires_python,
  89. )
  90. return
  91. raise UnsupportedPythonVersion(
  92. "Package {!r} requires a different Python: {} not in {!r}".format(
  93. dist.raw_name, version, requires_python
  94. )
  95. )
  96. class Resolver(BaseResolver):
  97. """Resolves which packages need to be installed/uninstalled to perform \
  98. the requested operation without breaking the requirements of any package.
  99. """
  100. _allowed_strategies = {"eager", "only-if-needed", "to-satisfy-only"}
  101. def __init__(
  102. self,
  103. preparer: RequirementPreparer,
  104. finder: PackageFinder,
  105. wheel_cache: Optional[WheelCache],
  106. make_install_req: InstallRequirementProvider,
  107. use_user_site: bool,
  108. ignore_dependencies: bool,
  109. ignore_installed: bool,
  110. ignore_requires_python: bool,
  111. force_reinstall: bool,
  112. upgrade_strategy: str,
  113. py_version_info: Optional[Tuple[int, ...]] = None,
  114. ) -> None:
  115. super().__init__()
  116. assert upgrade_strategy in self._allowed_strategies
  117. if py_version_info is None:
  118. py_version_info = sys.version_info[:3]
  119. else:
  120. py_version_info = normalize_version_info(py_version_info)
  121. self._py_version_info = py_version_info
  122. self.preparer = preparer
  123. self.finder = finder
  124. self.wheel_cache = wheel_cache
  125. self.upgrade_strategy = upgrade_strategy
  126. self.force_reinstall = force_reinstall
  127. self.ignore_dependencies = ignore_dependencies
  128. self.ignore_installed = ignore_installed
  129. self.ignore_requires_python = ignore_requires_python
  130. self.use_user_site = use_user_site
  131. self._make_install_req = make_install_req
  132. self._discovered_dependencies: DiscoveredDependencies = defaultdict(list)
  133. def resolve(
  134. self, root_reqs: List[InstallRequirement], check_supported_wheels: bool
  135. ) -> RequirementSet:
  136. """Resolve what operations need to be done
  137. As a side-effect of this method, the packages (and their dependencies)
  138. are downloaded, unpacked and prepared for installation. This
  139. preparation is done by ``pip.operations.prepare``.
  140. Once PyPI has static dependency metadata available, it would be
  141. possible to move the preparation to become a step separated from
  142. dependency resolution.
  143. """
  144. requirement_set = RequirementSet(check_supported_wheels=check_supported_wheels)
  145. for req in root_reqs:
  146. if req.constraint:
  147. check_invalid_constraint_type(req)
  148. self._add_requirement_to_set(requirement_set, req)
  149. # Actually prepare the files, and collect any exceptions. Most hash
  150. # exceptions cannot be checked ahead of time, because
  151. # _populate_link() needs to be called before we can make decisions
  152. # based on link type.
  153. discovered_reqs: List[InstallRequirement] = []
  154. hash_errors = HashErrors()
  155. for req in chain(requirement_set.all_requirements, discovered_reqs):
  156. try:
  157. discovered_reqs.extend(self._resolve_one(requirement_set, req))
  158. except HashError as exc:
  159. exc.req = req
  160. hash_errors.append(exc)
  161. if hash_errors:
  162. raise hash_errors
  163. return requirement_set
  164. def _add_requirement_to_set(
  165. self,
  166. requirement_set: RequirementSet,
  167. install_req: InstallRequirement,
  168. parent_req_name: Optional[str] = None,
  169. extras_requested: Optional[Iterable[str]] = None,
  170. ) -> Tuple[List[InstallRequirement], Optional[InstallRequirement]]:
  171. """Add install_req as a requirement to install.
  172. :param parent_req_name: The name of the requirement that needed this
  173. added. The name is used because when multiple unnamed requirements
  174. resolve to the same name, we could otherwise end up with dependency
  175. links that point outside the Requirements set. parent_req must
  176. already be added. Note that None implies that this is a user
  177. supplied requirement, vs an inferred one.
  178. :param extras_requested: an iterable of extras used to evaluate the
  179. environment markers.
  180. :return: Additional requirements to scan. That is either [] if
  181. the requirement is not applicable, or [install_req] if the
  182. requirement is applicable and has just been added.
  183. """
  184. # If the markers do not match, ignore this requirement.
  185. if not install_req.match_markers(extras_requested):
  186. logger.info(
  187. "Ignoring %s: markers '%s' don't match your environment",
  188. install_req.name,
  189. install_req.markers,
  190. )
  191. return [], None
  192. # If the wheel is not supported, raise an error.
  193. # Should check this after filtering out based on environment markers to
  194. # allow specifying different wheels based on the environment/OS, in a
  195. # single requirements file.
  196. if install_req.link and install_req.link.is_wheel:
  197. wheel = Wheel(install_req.link.filename)
  198. tags = compatibility_tags.get_supported()
  199. if requirement_set.check_supported_wheels and not wheel.supported(tags):
  200. raise InstallationError(
  201. "{} is not a supported wheel on this platform.".format(
  202. wheel.filename
  203. )
  204. )
  205. # This next bit is really a sanity check.
  206. assert (
  207. not install_req.user_supplied or parent_req_name is None
  208. ), "a user supplied req shouldn't have a parent"
  209. # Unnamed requirements are scanned again and the requirement won't be
  210. # added as a dependency until after scanning.
  211. if not install_req.name:
  212. requirement_set.add_unnamed_requirement(install_req)
  213. return [install_req], None
  214. try:
  215. existing_req: Optional[
  216. InstallRequirement
  217. ] = requirement_set.get_requirement(install_req.name)
  218. except KeyError:
  219. existing_req = None
  220. has_conflicting_requirement = (
  221. parent_req_name is None
  222. and existing_req
  223. and not existing_req.constraint
  224. and existing_req.extras == install_req.extras
  225. and existing_req.req
  226. and install_req.req
  227. and existing_req.req.specifier != install_req.req.specifier
  228. )
  229. if has_conflicting_requirement:
  230. raise InstallationError(
  231. "Double requirement given: {} (already in {}, name={!r})".format(
  232. install_req, existing_req, install_req.name
  233. )
  234. )
  235. # When no existing requirement exists, add the requirement as a
  236. # dependency and it will be scanned again after.
  237. if not existing_req:
  238. requirement_set.add_named_requirement(install_req)
  239. # We'd want to rescan this requirement later
  240. return [install_req], install_req
  241. # Assume there's no need to scan, and that we've already
  242. # encountered this for scanning.
  243. if install_req.constraint or not existing_req.constraint:
  244. return [], existing_req
  245. does_not_satisfy_constraint = install_req.link and not (
  246. existing_req.link and install_req.link.path == existing_req.link.path
  247. )
  248. if does_not_satisfy_constraint:
  249. raise InstallationError(
  250. "Could not satisfy constraints for '{}': "
  251. "installation from path or url cannot be "
  252. "constrained to a version".format(install_req.name)
  253. )
  254. # If we're now installing a constraint, mark the existing
  255. # object for real installation.
  256. existing_req.constraint = False
  257. # If we're now installing a user supplied requirement,
  258. # mark the existing object as such.
  259. if install_req.user_supplied:
  260. existing_req.user_supplied = True
  261. existing_req.extras = tuple(
  262. sorted(set(existing_req.extras) | set(install_req.extras))
  263. )
  264. logger.debug(
  265. "Setting %s extras to: %s",
  266. existing_req,
  267. existing_req.extras,
  268. )
  269. # Return the existing requirement for addition to the parent and
  270. # scanning again.
  271. return [existing_req], existing_req
  272. def _is_upgrade_allowed(self, req: InstallRequirement) -> bool:
  273. if self.upgrade_strategy == "to-satisfy-only":
  274. return False
  275. elif self.upgrade_strategy == "eager":
  276. return True
  277. else:
  278. assert self.upgrade_strategy == "only-if-needed"
  279. return req.user_supplied or req.constraint
  280. def _set_req_to_reinstall(self, req: InstallRequirement) -> None:
  281. """
  282. Set a requirement to be installed.
  283. """
  284. # Don't uninstall the conflict if doing a user install and the
  285. # conflict is not a user install.
  286. if not self.use_user_site or req.satisfied_by.in_usersite:
  287. req.should_reinstall = True
  288. req.satisfied_by = None
  289. def _check_skip_installed(
  290. self, req_to_install: InstallRequirement
  291. ) -> Optional[str]:
  292. """Check if req_to_install should be skipped.
  293. This will check if the req is installed, and whether we should upgrade
  294. or reinstall it, taking into account all the relevant user options.
  295. After calling this req_to_install will only have satisfied_by set to
  296. None if the req_to_install is to be upgraded/reinstalled etc. Any
  297. other value will be a dist recording the current thing installed that
  298. satisfies the requirement.
  299. Note that for vcs urls and the like we can't assess skipping in this
  300. routine - we simply identify that we need to pull the thing down,
  301. then later on it is pulled down and introspected to assess upgrade/
  302. reinstalls etc.
  303. :return: A text reason for why it was skipped, or None.
  304. """
  305. if self.ignore_installed:
  306. return None
  307. req_to_install.check_if_exists(self.use_user_site)
  308. if not req_to_install.satisfied_by:
  309. return None
  310. if self.force_reinstall:
  311. self._set_req_to_reinstall(req_to_install)
  312. return None
  313. if not self._is_upgrade_allowed(req_to_install):
  314. if self.upgrade_strategy == "only-if-needed":
  315. return "already satisfied, skipping upgrade"
  316. return "already satisfied"
  317. # Check for the possibility of an upgrade. For link-based
  318. # requirements we have to pull the tree down and inspect to assess
  319. # the version #, so it's handled way down.
  320. if not req_to_install.link:
  321. try:
  322. self.finder.find_requirement(req_to_install, upgrade=True)
  323. except BestVersionAlreadyInstalled:
  324. # Then the best version is installed.
  325. return "already up-to-date"
  326. except DistributionNotFound:
  327. # No distribution found, so we squash the error. It will
  328. # be raised later when we re-try later to do the install.
  329. # Why don't we just raise here?
  330. pass
  331. self._set_req_to_reinstall(req_to_install)
  332. return None
  333. def _find_requirement_link(self, req: InstallRequirement) -> Optional[Link]:
  334. upgrade = self._is_upgrade_allowed(req)
  335. best_candidate = self.finder.find_requirement(req, upgrade)
  336. if not best_candidate:
  337. return None
  338. # Log a warning per PEP 592 if necessary before returning.
  339. link = best_candidate.link
  340. if link.is_yanked:
  341. reason = link.yanked_reason or "<none given>"
  342. msg = (
  343. # Mark this as a unicode string to prevent
  344. # "UnicodeEncodeError: 'ascii' codec can't encode character"
  345. # in Python 2 when the reason contains non-ascii characters.
  346. "The candidate selected for download or install is a "
  347. "yanked version: {candidate}\n"
  348. "Reason for being yanked: {reason}"
  349. ).format(candidate=best_candidate, reason=reason)
  350. logger.warning(msg)
  351. return link
  352. def _populate_link(self, req: InstallRequirement) -> None:
  353. """Ensure that if a link can be found for this, that it is found.
  354. Note that req.link may still be None - if the requirement is already
  355. installed and not needed to be upgraded based on the return value of
  356. _is_upgrade_allowed().
  357. If preparer.require_hashes is True, don't use the wheel cache, because
  358. cached wheels, always built locally, have different hashes than the
  359. files downloaded from the index server and thus throw false hash
  360. mismatches. Furthermore, cached wheels at present have undeterministic
  361. contents due to file modification times.
  362. """
  363. if req.link is None:
  364. req.link = self._find_requirement_link(req)
  365. if self.wheel_cache is None or self.preparer.require_hashes:
  366. return
  367. cache_entry = self.wheel_cache.get_cache_entry(
  368. link=req.link,
  369. package_name=req.name,
  370. supported_tags=get_supported(),
  371. )
  372. if cache_entry is not None:
  373. logger.debug("Using cached wheel link: %s", cache_entry.link)
  374. if req.link is req.original_link and cache_entry.persistent:
  375. req.cached_wheel_source_link = req.link
  376. if cache_entry.origin is not None:
  377. req.download_info = cache_entry.origin
  378. else:
  379. # Legacy cache entry that does not have origin.json.
  380. # download_info may miss the archive_info.hashes field.
  381. req.download_info = direct_url_from_link(
  382. req.link, link_is_in_wheel_cache=cache_entry.persistent
  383. )
  384. req.link = cache_entry.link
  385. def _get_dist_for(self, req: InstallRequirement) -> BaseDistribution:
  386. """Takes a InstallRequirement and returns a single AbstractDist \
  387. representing a prepared variant of the same.
  388. """
  389. if req.editable:
  390. return self.preparer.prepare_editable_requirement(req)
  391. # satisfied_by is only evaluated by calling _check_skip_installed,
  392. # so it must be None here.
  393. assert req.satisfied_by is None
  394. skip_reason = self._check_skip_installed(req)
  395. if req.satisfied_by:
  396. return self.preparer.prepare_installed_requirement(req, skip_reason)
  397. # We eagerly populate the link, since that's our "legacy" behavior.
  398. self._populate_link(req)
  399. dist = self.preparer.prepare_linked_requirement(req)
  400. # NOTE
  401. # The following portion is for determining if a certain package is
  402. # going to be re-installed/upgraded or not and reporting to the user.
  403. # This should probably get cleaned up in a future refactor.
  404. # req.req is only avail after unpack for URL
  405. # pkgs repeat check_if_exists to uninstall-on-upgrade
  406. # (#14)
  407. if not self.ignore_installed:
  408. req.check_if_exists(self.use_user_site)
  409. if req.satisfied_by:
  410. should_modify = (
  411. self.upgrade_strategy != "to-satisfy-only"
  412. or self.force_reinstall
  413. or self.ignore_installed
  414. or req.link.scheme == "file"
  415. )
  416. if should_modify:
  417. self._set_req_to_reinstall(req)
  418. else:
  419. logger.info(
  420. "Requirement already satisfied (use --upgrade to upgrade): %s",
  421. req,
  422. )
  423. return dist
  424. def _resolve_one(
  425. self,
  426. requirement_set: RequirementSet,
  427. req_to_install: InstallRequirement,
  428. ) -> List[InstallRequirement]:
  429. """Prepare a single requirements file.
  430. :return: A list of additional InstallRequirements to also install.
  431. """
  432. # Tell user what we are doing for this requirement:
  433. # obtain (editable), skipping, processing (local url), collecting
  434. # (remote url or package name)
  435. if req_to_install.constraint or req_to_install.prepared:
  436. return []
  437. req_to_install.prepared = True
  438. # Parse and return dependencies
  439. dist = self._get_dist_for(req_to_install)
  440. # This will raise UnsupportedPythonVersion if the given Python
  441. # version isn't compatible with the distribution's Requires-Python.
  442. _check_dist_requires_python(
  443. dist,
  444. version_info=self._py_version_info,
  445. ignore_requires_python=self.ignore_requires_python,
  446. )
  447. more_reqs: List[InstallRequirement] = []
  448. def add_req(subreq: Requirement, extras_requested: Iterable[str]) -> None:
  449. # This idiosyncratically converts the Requirement to str and let
  450. # make_install_req then parse it again into Requirement. But this is
  451. # the legacy resolver so I'm just not going to bother refactoring.
  452. sub_install_req = self._make_install_req(str(subreq), req_to_install)
  453. parent_req_name = req_to_install.name
  454. to_scan_again, add_to_parent = self._add_requirement_to_set(
  455. requirement_set,
  456. sub_install_req,
  457. parent_req_name=parent_req_name,
  458. extras_requested=extras_requested,
  459. )
  460. if parent_req_name and add_to_parent:
  461. self._discovered_dependencies[parent_req_name].append(add_to_parent)
  462. more_reqs.extend(to_scan_again)
  463. with indent_log():
  464. # We add req_to_install before its dependencies, so that we
  465. # can refer to it when adding dependencies.
  466. if not requirement_set.has_requirement(req_to_install.name):
  467. # 'unnamed' requirements will get added here
  468. # 'unnamed' requirements can only come from being directly
  469. # provided by the user.
  470. assert req_to_install.user_supplied
  471. self._add_requirement_to_set(
  472. requirement_set, req_to_install, parent_req_name=None
  473. )
  474. if not self.ignore_dependencies:
  475. if req_to_install.extras:
  476. logger.debug(
  477. "Installing extra requirements: %r",
  478. ",".join(req_to_install.extras),
  479. )
  480. missing_requested = sorted(
  481. set(req_to_install.extras) - set(dist.iter_provided_extras())
  482. )
  483. for missing in missing_requested:
  484. logger.warning(
  485. "%s %s does not provide the extra '%s'",
  486. dist.raw_name,
  487. dist.version,
  488. missing,
  489. )
  490. available_requested = sorted(
  491. set(dist.iter_provided_extras()) & set(req_to_install.extras)
  492. )
  493. for subreq in dist.iter_dependencies(available_requested):
  494. add_req(subreq, extras_requested=available_requested)
  495. return more_reqs
  496. def get_installation_order(
  497. self, req_set: RequirementSet
  498. ) -> List[InstallRequirement]:
  499. """Create the installation order.
  500. The installation order is topological - requirements are installed
  501. before the requiring thing. We break cycles at an arbitrary point,
  502. and make no other guarantees.
  503. """
  504. # The current implementation, which we may change at any point
  505. # installs the user specified things in the order given, except when
  506. # dependencies must come earlier to achieve topological order.
  507. order = []
  508. ordered_reqs: Set[InstallRequirement] = set()
  509. def schedule(req: InstallRequirement) -> None:
  510. if req.satisfied_by or req in ordered_reqs:
  511. return
  512. if req.constraint:
  513. return
  514. ordered_reqs.add(req)
  515. for dep in self._discovered_dependencies[req.name]:
  516. schedule(dep)
  517. order.append(req)
  518. for install_req in req_set.requirements.values():
  519. schedule(install_req)
  520. return order