Skip to content

Commit

Permalink
apply_fixes: Generalize option for searching at specific paths
Browse files Browse the repository at this point in the history
Users don't have to provide a search path pointing exactly to the root of
'bazel-bin'. This is merely the default for the automatic deduction logic.
When users manually provide the search path, they can freely provide any
sub tree of 'bazel-bin'
  • Loading branch information
martis42 committed Feb 11, 2024
1 parent 244dd07 commit 919b972
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
19 changes: 11 additions & 8 deletions src/apply_fixes/apply_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ def get_workspace(main_args: Namespace) -> Path | None:
return Path(workspace_root)


def get_bazel_bin_dir(main_args: Namespace, workspace_root: Path) -> Path:
if main_args.bazel_bin:
return Path(main_args.bazel_bin)
def get_reports_search_dir(main_args: Namespace, workspace_root: Path) -> Path:
"""
Unless a dedicated search directory is provided, try to deduce the 'bazel-bin' dir.
"""
if main_args.search_path:
return Path(main_args.search_path)

if main_args.use_bazel_info:
process = execute_and_capture(
Expand All @@ -61,12 +64,12 @@ def get_bazel_bin_dir(main_args: Namespace, workspace_root: Path) -> Path:
return bazel_bin_link.resolve()


def gather_reports(bazel_bin: Path) -> list[Path]:
def gather_reports(search_path: Path) -> list[Path]:
"""
We explicitly use os.walk() as it has better performance than Path.glob() in large and deeply nested file trees.
"""
reports = []
for root, _, files in walk(bazel_bin):
for root, _, files in walk(search_path):
for file in files:
if file.endswith("_dwyu_report.json"):
reports.append(Path(root) / file) # noqa: PERF401
Expand Down Expand Up @@ -223,10 +226,10 @@ def main(args: Namespace) -> int:
return 1
logging.debug(f"Workspace: '{workspace}'")

bin_dir = get_bazel_bin_dir(main_args=args, workspace_root=workspace)
logging.debug(f"Bazel-bin directory: '{bin_dir}'")
reports_search_dir = get_reports_search_dir(main_args=args, workspace_root=workspace)
logging.debug(f"Reports search directory: '{reports_search_dir}'")

reports = gather_reports(bin_dir)
reports = gather_reports(reports_search_dir)
if not reports:
logging.fatal(
"""
Expand Down
7 changes: 4 additions & 3 deletions src/apply_fixes/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ def cli() -> Namespace:
naming scheme or do not point to the Bazel output directory containing the DWYU reports.""",
)
parser.add_argument(
"--bazel-bin",
"--search-path",
metavar="PATH",
help="""
Path to the bazel-bin directory inside which the DWYU reports are located.
Path to the directory below which the DWYU reports are located.
Using this option is recommended if neither the convenience symlinks nor the 'bazel info' command are suited to
deduce the Bazel output directory containing the DWYU report files.""",
deduce the Bazel output directory containing the DWYU report files. Or if you want to search only in a sub tree
of te Bazel output directories.""",
)
parser.add_argument(
"--fix-unused-deps",
Expand Down
2 changes: 1 addition & 1 deletion test/apply_fixes/tool_cli/test_use_custom_output_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def windows_compatible(self) -> bool:
def execute_test_logic(self) -> Result:
with TemporaryDirectory() as output_base:
self._create_reports(startup_args=[f"--output_base={output_base}"])
self._run_automatic_fix(extra_args=["--fix-unused", f"--bazel-bin={output_base}"])
self._run_automatic_fix(extra_args=["--fix-unused", f"--search-path={output_base}"])

target_deps = self._get_target_attribute(target=self.test_target, attribute="deps")
if (expected := set()) != target_deps: # type: ignore[var-annotated]
Expand Down

0 comments on commit 919b972

Please sign in to comment.