Skip to content

Commit 40187ef

Browse files
krishanbhasin-pxKrishanBhasin
authored andcommitted
Add the outline for structured output
1 parent f5ff4fa commit 40187ef

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/pip/_internal/cli/cmdoptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,14 @@ def _handle_config_settings(
887887
"pip only finds stable versions.",
888888
)
889889

890+
json: Callable[..., Option] = partial(
891+
Option,
892+
"--json",
893+
action="store_true",
894+
default=False,
895+
help="Output data in a machine-readable JSON format.",
896+
)
897+
890898
disable_pip_version_check: Callable[..., Option] = partial(
891899
Option,
892900
"--disable-pip-version-check",

src/pip/_internal/commands/index.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import json
12
import logging
23
from optparse import Values
34
from typing import Any, Iterable, List, Optional
45

6+
from pip._internal.metadata import get_default_environment
57
from pip._vendor.packaging.version import Version
68

79
from pip._internal.cli import cmdoptions
@@ -34,6 +36,7 @@ def add_options(self) -> None:
3436

3537
self.cmd_opts.add_option(cmdoptions.ignore_requires_python())
3638
self.cmd_opts.add_option(cmdoptions.pre())
39+
self.cmd_opts.add_option(cmdoptions.json())
3740
self.cmd_opts.add_option(cmdoptions.no_binary())
3841
self.cmd_opts.add_option(cmdoptions.only_binary())
3942

@@ -134,6 +137,22 @@ def get_available_package_versions(self, options: Values, args: List[Any]) -> No
134137
formatted_versions = [str(ver) for ver in sorted(versions, reverse=True)]
135138
latest = formatted_versions[0]
136139

140+
if options.json:
141+
env = get_default_environment()
142+
dist = env.get_distribution(query)
143+
structured_output = {
144+
"name": query,
145+
"versions": formatted_versions,
146+
"latest": latest,
147+
}
148+
149+
if dist is not None:
150+
structured_output["installed_version"] = dist.version
151+
152+
153+
write_output(json.dumps(structured_output))
154+
return
155+
137156
write_output(f"{query} ({latest})")
138157
write_output("Available versions: {}".format(", ".join(formatted_versions)))
139158
print_dist_installation_info(query, latest)

tests/functional/test_index.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import pytest
23

34
from pip._internal.cli.status_codes import ERROR, SUCCESS
@@ -6,6 +7,31 @@
67
from tests.lib import PipTestEnvironment
78

89

10+
@pytest.mark.network
11+
def test_json_structured_output(script: PipTestEnvironment) -> None:
12+
"""
13+
Test that --json flag returns structured output
14+
"""
15+
output = script.pip("index", "versions", "pip", "--json", allow_stderr_warning=True)
16+
structured_output = json.loads(output.stdout)
17+
18+
assert "name" in structured_output
19+
assert "versions" in structured_output
20+
assert "latest" in structured_output
21+
assert (
22+
"20.2.3, 20.2.2, 20.2.1, 20.2, 20.1.1, 20.1, 20.0.2"
23+
", 20.0.1, 19.3.1, 19.3, 19.2.3, 19.2.2, 19.2.1, 19.2, 19.1.1"
24+
", 19.1, 19.0.3, 19.0.2, 19.0.1, 19.0, 18.1, 18.0, 10.0.1, 10.0.0, "
25+
"9.0.3, 9.0.2, 9.0.1, 9.0.0, 8.1.2, 8.1.1, "
26+
"8.1.0, 8.0.3, 8.0.2, 8.0.1, 8.0.0, 7.1.2, 7.1.1, 7.1.0, 7.0.3, "
27+
"7.0.2, 7.0.1, 7.0.0, 6.1.1, 6.1.0, 6.0.8, 6.0.7, 6.0.6, 6.0.5, "
28+
"6.0.4, 6.0.3, 6.0.2, 6.0.1, 6.0, 1.5.6, 1.5.5, 1.5.4, 1.5.3, "
29+
"1.5.2, 1.5.1, 1.5, 1.4.1, 1.4, 1.3.1, 1.3, 1.2.1, 1.2, 1.1, 1.0.2,"
30+
" 1.0.1, 1.0, 0.8.3, 0.8.2, 0.8.1, 0.8, 0.7.2, 0.7.1, 0.7, 0.6.3, "
31+
"0.6.2, 0.6.1, 0.6, 0.5.1, 0.5, 0.4, 0.3.1, "
32+
"0.3, 0.2.1, 0.2" in structured_output.get("versions")
33+
)
34+
935
@pytest.mark.network
1036
def test_list_all_versions_basic_search(script: PipTestEnvironment) -> None:
1137
"""

0 commit comments

Comments
 (0)