check.py 5.2 KB

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