check.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. """Validation of dependencies of packages
  2. """
  3. import logging
  4. from typing import Callable, Dict, List, NamedTuple, Optional, Set, Tuple
  5. from pip._vendor.packaging.requirements import Requirement
  6. from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
  7. from pip._internal.distributions import make_distribution_for_install_requirement
  8. from pip._internal.metadata import get_default_environment
  9. from pip._internal.metadata.base import DistributionVersion
  10. from pip._internal.req.req_install import InstallRequirement
  11. logger = logging.getLogger(__name__)
  12. class PackageDetails(NamedTuple):
  13. version: DistributionVersion
  14. dependencies: List[Requirement]
  15. # Shorthands
  16. PackageSet = Dict[NormalizedName, PackageDetails]
  17. Missing = Tuple[NormalizedName, Requirement]
  18. Conflicting = Tuple[NormalizedName, DistributionVersion, Requirement]
  19. MissingDict = Dict[NormalizedName, List[Missing]]
  20. ConflictingDict = Dict[NormalizedName, List[Conflicting]]
  21. CheckResult = Tuple[MissingDict, ConflictingDict]
  22. ConflictDetails = Tuple[PackageSet, CheckResult]
  23. def create_package_set_from_installed() -> Tuple[PackageSet, bool]:
  24. """Converts a list of distributions into a PackageSet."""
  25. package_set = {}
  26. problems = False
  27. env = get_default_environment()
  28. for dist in env.iter_installed_distributions(local_only=False, skip=()):
  29. name = dist.canonical_name
  30. try:
  31. dependencies = list(dist.iter_dependencies())
  32. package_set[name] = PackageDetails(dist.version, dependencies)
  33. except (OSError, ValueError) as e:
  34. # Don't crash on unreadable or broken metadata.
  35. logger.warning("Error parsing requirements for %s: %s", name, e)
  36. problems = True
  37. return package_set, problems
  38. def check_package_set(
  39. package_set: PackageSet, should_ignore: Optional[Callable[[str], bool]] = None
  40. ) -> CheckResult:
  41. """Check if a package set is consistent
  42. If should_ignore is passed, it should be a callable that takes a
  43. package name and returns a boolean.
  44. """
  45. missing = {}
  46. conflicting = {}
  47. for package_name, package_detail in package_set.items():
  48. # Info about dependencies of package_name
  49. missing_deps: Set[Missing] = set()
  50. conflicting_deps: Set[Conflicting] = set()
  51. if should_ignore and should_ignore(package_name):
  52. continue
  53. for req in package_detail.dependencies:
  54. name = canonicalize_name(req.name)
  55. # Check if it's missing
  56. if name not in package_set:
  57. missed = True
  58. if req.marker is not None:
  59. missed = req.marker.evaluate()
  60. if missed:
  61. missing_deps.add((name, req))
  62. continue
  63. # Check if there's a conflict
  64. version = package_set[name].version
  65. if not req.specifier.contains(version, prereleases=True):
  66. conflicting_deps.add((name, version, req))
  67. if missing_deps:
  68. missing[package_name] = sorted(missing_deps, key=str)
  69. if conflicting_deps:
  70. conflicting[package_name] = sorted(conflicting_deps, key=str)
  71. return missing, conflicting
  72. def check_install_conflicts(to_install: List[InstallRequirement]) -> ConflictDetails:
  73. """For checking if the dependency graph would be consistent after \
  74. installing given requirements
  75. """
  76. # Start from the current state
  77. package_set, _ = create_package_set_from_installed()
  78. # Install packages
  79. would_be_installed = _simulate_installation_of(to_install, package_set)
  80. # Only warn about directly-dependent packages; create a whitelist of them
  81. whitelist = _create_whitelist(would_be_installed, package_set)
  82. return (
  83. package_set,
  84. check_package_set(
  85. package_set, should_ignore=lambda name: name not in whitelist
  86. ),
  87. )
  88. def _simulate_installation_of(
  89. to_install: List[InstallRequirement], package_set: PackageSet
  90. ) -> Set[NormalizedName]:
  91. """Computes the version of packages after installing to_install."""
  92. # Keep track of packages that were installed
  93. installed = set()
  94. # Modify it as installing requirement_set would (assuming no errors)
  95. for inst_req in to_install:
  96. abstract_dist = make_distribution_for_install_requirement(inst_req)
  97. dist = abstract_dist.get_metadata_distribution()
  98. name = dist.canonical_name
  99. package_set[name] = PackageDetails(dist.version, list(dist.iter_dependencies()))
  100. installed.add(name)
  101. return installed
  102. def _create_whitelist(
  103. would_be_installed: Set[NormalizedName], package_set: PackageSet
  104. ) -> Set[NormalizedName]:
  105. packages_affected = set(would_be_installed)
  106. for package_name in package_set:
  107. if package_name in packages_affected:
  108. continue
  109. for req in package_set[package_name].dependencies:
  110. if canonicalize_name(req.name) in packages_affected:
  111. packages_affected.add(package_name)
  112. break
  113. return packages_affected