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.

148 lines
4.8 KiB

4 years ago
  1. """Validation of dependencies of packages
  2. """
  3. from collections import namedtuple
  4. from pip._vendor.packaging.utils import canonicalize_name
  5. from pip._internal.operations.prepare import make_abstract_dist
  6. from pip._internal.utils.misc import get_installed_distributions
  7. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  8. if MYPY_CHECK_RUNNING:
  9. from pip._internal.req.req_install import InstallRequirement # noqa: F401
  10. from typing import ( # noqa: F401
  11. Any, Callable, Dict, Iterator, Optional, Set, Tuple, List
  12. )
  13. # Shorthands
  14. PackageSet = Dict[str, 'PackageDetails']
  15. Missing = Tuple[str, Any]
  16. Conflicting = Tuple[str, str, Any]
  17. MissingDict = Dict[str, List[Missing]]
  18. ConflictingDict = Dict[str, List[Conflicting]]
  19. CheckResult = Tuple[MissingDict, ConflictingDict]
  20. PackageDetails = namedtuple('PackageDetails', ['version', 'requires'])
  21. def create_package_set_from_installed(**kwargs):
  22. # type: (**Any) -> PackageSet
  23. """Converts a list of distributions into a PackageSet.
  24. """
  25. # Default to using all packages installed on the system
  26. if kwargs == {}:
  27. kwargs = {"local_only": False, "skip": ()}
  28. package_set = {}
  29. for dist in get_installed_distributions(**kwargs):
  30. name = canonicalize_name(dist.project_name)
  31. package_set[name] = PackageDetails(dist.version, dist.requires())
  32. return package_set
  33. def check_package_set(package_set, should_ignore=None):
  34. # type: (PackageSet, Optional[Callable[[str], bool]]) -> CheckResult
  35. """Check if a package set is consistent
  36. If should_ignore is passed, it should be a callable that takes a
  37. package name and returns a boolean.
  38. """
  39. if should_ignore is None:
  40. def should_ignore(name):
  41. return False
  42. missing = dict()
  43. conflicting = dict()
  44. for package_name in package_set:
  45. # Info about dependencies of package_name
  46. missing_deps = set() # type: Set[Missing]
  47. conflicting_deps = set() # type: Set[Conflicting]
  48. if should_ignore(package_name):
  49. continue
  50. for req in package_set[package_name].requires:
  51. name = canonicalize_name(req.project_name) # type: str
  52. # Check if it's missing
  53. if name not in package_set:
  54. missed = True
  55. if req.marker is not None:
  56. missed = req.marker.evaluate()
  57. if missed:
  58. missing_deps.add((name, req))
  59. continue
  60. # Check if there's a conflict
  61. version = package_set[name].version # type: str
  62. if not req.specifier.contains(version, prereleases=True):
  63. conflicting_deps.add((name, version, req))
  64. if missing_deps:
  65. missing[package_name] = sorted(missing_deps, key=str)
  66. if conflicting_deps:
  67. conflicting[package_name] = sorted(conflicting_deps, key=str)
  68. return missing, conflicting
  69. def check_install_conflicts(to_install):
  70. # type: (List[InstallRequirement]) -> Tuple[PackageSet, CheckResult]
  71. """For checking if the dependency graph would be consistent after \
  72. installing given requirements
  73. """
  74. # Start from the current state
  75. package_set = create_package_set_from_installed()
  76. # Install packages
  77. would_be_installed = _simulate_installation_of(to_install, package_set)
  78. # Only warn about directly-dependent packages; create a whitelist of them
  79. whitelist = _create_whitelist(would_be_installed, package_set)
  80. return (
  81. package_set,
  82. check_package_set(
  83. package_set, should_ignore=lambda name: name not in whitelist
  84. )
  85. )
  86. # NOTE from @pradyunsg
  87. # This required a minor update in dependency link handling logic over at
  88. # operations.prepare.IsSDist.dist() to get it working
  89. def _simulate_installation_of(to_install, package_set):
  90. # type: (List[InstallRequirement], PackageSet) -> Set[str]
  91. """Computes the version of packages after installing to_install.
  92. """
  93. # Keep track of packages that were installed
  94. installed = set()
  95. # Modify it as installing requirement_set would (assuming no errors)
  96. for inst_req in to_install:
  97. dist = make_abstract_dist(inst_req).dist(finder=None)
  98. name = canonicalize_name(dist.key)
  99. package_set[name] = PackageDetails(dist.version, dist.requires())
  100. installed.add(name)
  101. return installed
  102. def _create_whitelist(would_be_installed, package_set):
  103. # type: (Set[str], PackageSet) -> Set[str]
  104. packages_affected = set(would_be_installed)
  105. for package_name in package_set:
  106. if package_name in packages_affected:
  107. continue
  108. for req in package_set[package_name].requires:
  109. if canonicalize_name(req.name) in packages_affected:
  110. packages_affected.add(package_name)
  111. break
  112. return packages_affected