check.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import logging
  2. from optparse import Values
  3. from pip._internal.cli.base_command import Command
  4. from pip._internal.cli.status_codes import ERROR, SUCCESS
  5. from pip._internal.metadata import get_default_environment
  6. from pip._internal.operations.check import (
  7. check_package_set,
  8. check_unsupported,
  9. create_package_set_from_installed,
  10. )
  11. from pip._internal.utils.compatibility_tags import get_supported
  12. from pip._internal.utils.misc import write_output
  13. logger = logging.getLogger(__name__)
  14. class CheckCommand(Command):
  15. """Verify installed packages have compatible dependencies."""
  16. ignore_require_venv = True
  17. usage = """
  18. %prog [options]"""
  19. def run(self, options: Values, args: list[str]) -> int:
  20. package_set, parsing_probs = create_package_set_from_installed()
  21. missing, conflicting = check_package_set(package_set)
  22. unsupported = list(
  23. check_unsupported(
  24. get_default_environment().iter_installed_distributions(),
  25. get_supported(),
  26. )
  27. )
  28. for project_name in missing:
  29. version = package_set[project_name].version
  30. for dependency in missing[project_name]:
  31. write_output(
  32. "%s %s requires %s, which is not installed.",
  33. project_name,
  34. version,
  35. dependency[0],
  36. )
  37. for project_name in conflicting:
  38. version = package_set[project_name].version
  39. for dep_name, dep_version, req in conflicting[project_name]:
  40. write_output(
  41. "%s %s has requirement %s, but you have %s %s.",
  42. project_name,
  43. version,
  44. req,
  45. dep_name,
  46. dep_version,
  47. )
  48. for package in unsupported:
  49. write_output(
  50. "%s %s is not supported on this platform",
  51. package.raw_name,
  52. package.version,
  53. )
  54. if missing or conflicting or parsing_probs or unsupported:
  55. return ERROR
  56. else:
  57. write_output("No broken requirements found.")
  58. return SUCCESS