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