check.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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
  10. from typing import Any, Dict, Iterator, Set, Tuple, List
  11. # Shorthands
  12. PackageSet = Dict[str, 'PackageDetails']
  13. Missing = Tuple[str, Any]
  14. Conflicting = Tuple[str, str, Any]
  15. MissingDict = Dict[str, List[Missing]]
  16. ConflictingDict = Dict[str, List[Conflicting]]
  17. CheckResult = Tuple[MissingDict, ConflictingDict]
  18. PackageDetails = namedtuple('PackageDetails', ['version', 'requires'])
  19. def create_package_set_from_installed(**kwargs):
  20. # type: (**Any) -> PackageSet
  21. """Converts a list of distributions into a PackageSet.
  22. """
  23. # Default to using all packages installed on the system
  24. if kwargs == {}:
  25. kwargs = {"local_only": False, "skip": ()}
  26. retval = {}
  27. for dist in get_installed_distributions(**kwargs):
  28. name = canonicalize_name(dist.project_name)
  29. retval[name] = PackageDetails(dist.version, dist.requires())
  30. return retval
  31. def check_package_set(package_set):
  32. # type: (PackageSet) -> CheckResult
  33. """Check if a package set is consistent
  34. """
  35. missing = dict()
  36. conflicting = dict()
  37. for package_name in package_set:
  38. # Info about dependencies of package_name
  39. missing_deps = set() # type: Set[Missing]
  40. conflicting_deps = set() # type: Set[Conflicting]
  41. for req in package_set[package_name].requires:
  42. name = canonicalize_name(req.project_name) # type: str
  43. # Check if it's missing
  44. if name not in package_set:
  45. missed = True
  46. if req.marker is not None:
  47. missed = req.marker.evaluate()
  48. if missed:
  49. missing_deps.add((name, req))
  50. continue
  51. # Check if there's a conflict
  52. version = package_set[name].version # type: str
  53. if not req.specifier.contains(version, prereleases=True):
  54. conflicting_deps.add((name, version, req))
  55. def str_key(x):
  56. return str(x)
  57. if missing_deps:
  58. missing[package_name] = sorted(missing_deps, key=str_key)
  59. if conflicting_deps:
  60. conflicting[package_name] = sorted(conflicting_deps, key=str_key)
  61. return missing, conflicting
  62. def check_install_conflicts(to_install):
  63. # type: (List[InstallRequirement]) -> Tuple[PackageSet, CheckResult]
  64. """For checking if the dependency graph would be consistent after \
  65. installing given requirements
  66. """
  67. # Start from the current state
  68. state = create_package_set_from_installed()
  69. _simulate_installation_of(to_install, state)
  70. return state, check_package_set(state)
  71. # NOTE from @pradyunsg
  72. # This required a minor update in dependency link handling logic over at
  73. # operations.prepare.IsSDist.dist() to get it working
  74. def _simulate_installation_of(to_install, state):
  75. # type: (List[InstallRequirement], PackageSet) -> None
  76. """Computes the version of packages after installing to_install.
  77. """
  78. # Modify it as installing requirement_set would (assuming no errors)
  79. for inst_req in to_install:
  80. dist = make_abstract_dist(inst_req).dist(finder=None)
  81. name = canonicalize_name(dist.key)
  82. state[name] = PackageDetails(dist.version, dist.requires())