inspect.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. cmdoptions.check_list_path_option(options)
  42. dists = get_environment(options.path).iter_installed_distributions(
  43. local_only=options.local,
  44. user_only=options.user,
  45. skip=set(stdlib_pkgs),
  46. )
  47. output = {
  48. "version": "1",
  49. "pip_version": __version__,
  50. "installed": [self._dist_to_dict(dist) for dist in dists],
  51. "environment": default_environment(),
  52. # TODO tags? scheme?
  53. }
  54. print_json(data=output)
  55. return SUCCESS
  56. def _dist_to_dict(self, dist: BaseDistribution) -> Dict[str, Any]:
  57. res: Dict[str, Any] = {
  58. "metadata": dist.metadata_dict,
  59. "metadata_location": dist.info_location,
  60. }
  61. # direct_url. Note that we don't have download_info (as in the installation
  62. # report) since it is not recorded in installed metadata.
  63. direct_url = dist.direct_url
  64. if direct_url is not None:
  65. res["direct_url"] = direct_url.to_dict()
  66. else:
  67. # Emulate direct_url for legacy editable installs.
  68. editable_project_location = dist.editable_project_location
  69. if editable_project_location is not None:
  70. res["direct_url"] = {
  71. "url": path_to_url(editable_project_location),
  72. "dir_info": {
  73. "editable": True,
  74. },
  75. }
  76. # installer
  77. installer = dist.installer
  78. if dist.installer:
  79. res["installer"] = installer
  80. # requested
  81. if dist.installed_with_dist_info:
  82. res["requested"] = dist.requested
  83. return res