Skip to content

Commit 7fdff17

Browse files
committed
install report: add suport for stdout output
1 parent d32a62b commit 7fdff17

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/pip/_internal/commands/install.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import shutil
66
import site
7+
import sys
78
from optparse import SUPPRESS_HELP, Values
89
from typing import Iterable, List, Optional
910

@@ -261,7 +262,8 @@ def add_options(self) -> None:
261262
"Generate a JSON file describing what pip did to install "
262263
"the provided requirements. "
263264
"Can be used in combination with --dry-run and --ignore-installed "
264-
"to 'resolve' the requirements."
265+
"to 'resolve' the requirements. "
266+
"When - is used as file name it writes to stdout."
265267
),
266268
)
267269

@@ -370,8 +372,11 @@ def run(self, options: Values, args: List[str]) -> int:
370372

371373
if options.json_report_file:
372374
report = InstallationReport(requirement_set.requirements_to_install)
373-
with open(options.json_report_file, "w", encoding="utf-8") as f:
374-
json.dump(report.to_dict(), f)
375+
if options.json_report_file == "-":
376+
json.dump(report.to_dict(), sys.stdout, indent=2, ensure_ascii=True)
377+
else:
378+
with open(options.json_report_file, "w", encoding="utf-8") as f:
379+
json.dump(report.to_dict(), f, indent=2, ensure_ascii=False)
375380

376381
if options.dry_run:
377382
would_install_items = sorted(

tests/functional/test_install_report.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,24 @@ def test_install_report_vcs_editable(
175175
"/src/pip-test-package"
176176
)
177177
assert pip_test_package_report["download_info"]["dir_info"]["editable"] is True
178+
179+
180+
@pytest.mark.usefixtures("with_wheel")
181+
def test_install_report_to_stdout(
182+
script: PipTestEnvironment, shared_data: TestData
183+
) -> None:
184+
result = script.pip(
185+
"install",
186+
"simplewheel",
187+
"--quiet",
188+
"--dry-run",
189+
"--no-index",
190+
"--find-links",
191+
str(shared_data.root / "packages/"),
192+
"--report",
193+
"-",
194+
)
195+
assert not result.stderr
196+
report = json.loads(result.stdout)
197+
assert "install" in report
198+
assert len(report["install"]) == 1

0 commit comments

Comments
 (0)