inspect.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import logging
  2. from optparse import Values
  3. from typing import Any, Dict, List
  4. from pip._vendor.packaging.markers import default_environment
  5. from pip._vendor.rich import print_json
  6. from pip import __version__
  7. from pip._internal.cli import cmdoptions
  8. from pip._internal.cli.req_command import Command
  9. from pip._internal.cli.status_codes import SUCCESS
  10. from pip._internal.metadata import BaseDistribution, get_environment
  11. from pip._internal.utils.compat import stdlib_pkgs
  12. from pip._internal.utils.urls import path_to_url
  13. logger = logging.getLogger(__name__)
  14. class InspectCommand(Command):
  15. """
  16. Inspect the content of a Python environment and produce a report in JSON format.
  17. """
  18. ignore_require_venv = True
  19. usage = """
  20. %prog [options]"""
  21. def add_options(self) -> None:
  22. self.cmd_opts.add_option(
  23. "--local",
  24. action="store_true",
  25. default=False,
  26. help=(
  27. "If in a virtualenv that has global access, do not list "
  28. "globally-installed packages."
  29. ),
  30. )
  31. self.cmd_opts.add_option(
  32. "--user",
  33. dest="user",
  34. action="store_true",
  35. default=False,
  36. help="Only output packages installed in user-site.",
  37. )
  38. self.cmd_opts.add_option(cmdoptions.list_path())
  39. self.parser.insert_option_group(0, self.cmd_opts)
  40. def run(self, options: Values, args: List[str]) -> int:
  41. logger.warning(
  42. "pip inspect is currently an experimental command. "
  43. "The output format may change in a future release without prior warning."
  44. )
  45. cmdoptions.check_list_path_option(options)
  46. dists = get_environment(options.path).iter_installed_distributions(
  47. local_only=options.local,
  48. user_only=options.user,
  49. skip=set(stdlib_pkgs),
  50. )
  51. output = {
  52. "version": "0",
  53. "pip_version": __version__,
  54. "installed": [self._dist_to_dict(dist) for dist in dists],
  55. "environment": default_environment(),
  56. # TODO tags? scheme?
  57. }
  58. print_json(data=output)
  59. return SUCCESS
  60. def _dist_to_dict(self, dist: BaseDistribution) -> Dict[str, Any]:
  61. res: Dict[str, Any] = {
  62. "metadata": dist.metadata_dict,
  63. "metadata_location": dist.info_location,
  64. }
  65. # direct_url. Note that we don't have download_info (as in the installation
  66. # report) since it is not recorded in installed metadata.
  67. direct_url = dist.direct_url
  68. if direct_url is not None:
  69. res["direct_url"] = direct_url.to_dict()
  70. else:
  71. # Emulate direct_url for legacy editable installs.
  72. editable_project_location = dist.editable_project_location
  73. if editable_project_location is not None:
  74. res["direct_url"] = {
  75. "url": path_to_url(editable_project_location),
  76. "dir_info": {
  77. "editable": True,
  78. },
  79. }
  80. # installer
  81. installer = dist.installer
  82. if dist.installer:
  83. res["installer"] = installer
  84. # requested
  85. if dist.installed_with_dist_info:
  86. res["requested"] = dist.requested
  87. return res