-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apply_fixes can parse report files from a DWYU execution log
For large workspaces discovering the DWYU report files by crawling the bazel-out directory can be quite slow due to an enormous amount of files and directories being present. To work around this, we enable the apply_fixes script to parse a log file containing the command line output of executing the DWYU aspect. This execution log is then parsed and the DWYU report paths deduced.
- Loading branch information
Showing
17 changed files
with
276 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
import sys | ||
from os import walk | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING | ||
|
||
from src.apply_fixes.utils import args_string_to_list, execute_and_capture | ||
|
||
if TYPE_CHECKING: | ||
import argparse | ||
|
||
|
||
def gather_reports(main_args: argparse.Namespace, search_path: Path) -> list[Path]: | ||
if main_args.dwyu_log_file: | ||
return [search_path / log.split("/bin/", 1)[1] for log in parse_dwyu_execution_log(main_args.dwyu_log_file)] | ||
|
||
reports = [] | ||
# We explicitly use os.walk() as it has better performance than Path.glob() in large and deeply nested file trees. | ||
for root, _, files in walk(search_path): | ||
for file in files: | ||
if file.endswith("_dwyu_report.json"): | ||
reports.append(Path(root) / file) # noqa: PERF401 | ||
return reports | ||
|
||
|
||
def parse_dwyu_execution_log(log_file: Path) -> list[str]: | ||
dwyu_report_anchor = "DWYU Report: " | ||
with log_file.open() as log: | ||
return [ | ||
line.strip().split(dwyu_report_anchor)[1] for line in log.readlines() if line.startswith(dwyu_report_anchor) | ||
] | ||
|
||
|
||
def get_reports_search_dir(main_args: argparse.Namespace, workspace_root: Path) -> Path: | ||
""" | ||
Unless an alternative method is selected, follow the convenience symlinks at the workspace root to discover the | ||
DWYU report files. | ||
""" | ||
if main_args.search_path: | ||
return Path(main_args.search_path) | ||
|
||
if main_args.use_bazel_info: | ||
process = execute_and_capture( | ||
cmd=[ | ||
"bazel", | ||
*args_string_to_list(main_args.bazel_startup_args), | ||
"info", | ||
*args_string_to_list(main_args.bazel_args), | ||
"bazel-bin", | ||
], | ||
cwd=workspace_root, | ||
) | ||
return Path(process.stdout.strip()) | ||
|
||
bazel_bin_link = workspace_root / "bazel-bin" | ||
if not bazel_bin_link.is_dir(): | ||
logging.fatal(f"ERROR: convenience symlink '{bazel_bin_link}' does not exist.") | ||
sys.exit(1) | ||
return bazel_bin_link.resolve() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import unittest | ||
from pathlib import Path | ||
|
||
from src.apply_fixes.get_dwyu_reports import parse_dwyu_execution_log | ||
|
||
|
||
class TestParseDwyuExecutionLog(unittest.TestCase): | ||
def test_parse_dwyu_execution_log(self) -> None: | ||
test_log = Path("test_log.txt") | ||
with test_log.open(mode="wt") as fp: | ||
fp.write( | ||
""" | ||
Some unrelated stuff | ||
DWYU Report: bazel-out/opt/bin/some/target_dwyu_report.json | ||
ERROR: Unrelated error | ||
DWYU Report: bazel-out/opt/bin/root_target_dwyu_report.json | ||
""".strip() | ||
) | ||
|
||
logs = parse_dwyu_execution_log(test_log) | ||
self.assertEqual( | ||
logs, ["bazel-out/opt/bin/some/target_dwyu_report.json", "bazel-out/opt/bin/root_target_dwyu_report.json"] | ||
) | ||
|
||
def test_parse_dwyu_execution_log_empty(self) -> None: | ||
test_log = Path("test_log.txt") | ||
with test_log.open(mode="wt") as fp: | ||
fp.write("") | ||
|
||
logs = parse_dwyu_execution_log(test_log) | ||
self.assertEqual(logs, []) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import unittest | ||
|
||
from src.apply_fixes.utils import args_string_to_list | ||
|
||
|
||
class TestArgsStringToList(unittest.TestCase): | ||
def test_no_args(self) -> None: | ||
self.assertEqual(args_string_to_list(None), []) | ||
self.assertEqual(args_string_to_list(""), []) | ||
|
||
def test_single_arg(self) -> None: | ||
self.assertEqual(args_string_to_list("foo"), ["foo"]) | ||
|
||
def test_multiple_args(self) -> None: | ||
self.assertEqual(args_string_to_list("--foo --bar=42 baz 1337"), ["--foo", "--bar=42", "baz", "1337"]) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.