-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pw_presubmit: Split out formatting summary logic
As a step towards building out a Bazel-friendly CLI for the pw format tool, this change begins to migrate pieces of the CLI into the isolated format package. Bug: b/326309165 Change-Id: If35379baa751b41d24b950941921e2aa0be05e87 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/252294 Commit-Queue: Auto-Submit <auto-submit@pigweed-service-accounts.iam.gserviceaccount.com> Reviewed-by: Wyatt Hepler <hepler@google.com> Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com> Docs-Not-Needed: Armando Montanez <amontanez@google.com> Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com> Pigweed-Auto-Submit: Armando Montanez <amontanez@google.com>
- Loading branch information
1 parent
d8a16b2
commit 310f262
Showing
5 changed files
with
129 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2024 The Pigweed Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. |
87 changes: 87 additions & 0 deletions
87
pw_presubmit/py/pw_presubmit/format/private/cli_support.py
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,87 @@ | ||
# Copyright 2024 The Pigweed Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
"""Supporting helpers for format CLI tooling.""" | ||
|
||
import logging | ||
from pathlib import Path | ||
import sys | ||
from typing import ( | ||
Sequence, | ||
TextIO, | ||
) | ||
|
||
from pw_cli import color | ||
from pw_cli.diff import colorize_diff | ||
from pw_presubmit.format.core import FormattedDiff | ||
|
||
_LOG: logging.Logger = logging.getLogger(__name__) | ||
|
||
|
||
def findings_to_formatted_diffs( | ||
diffs: dict[Path, str] | ||
) -> Sequence[FormattedDiff]: | ||
"""Converts legacy formatter findings to structured findings.""" | ||
return [ | ||
FormattedDiff( | ||
ok=True, | ||
diff=finding, | ||
error_message=None, | ||
file_path=path, | ||
) | ||
for path, finding in diffs.items() | ||
] | ||
|
||
|
||
def summarize_findings( | ||
findings: Sequence[FormattedDiff], | ||
log_fix_command: bool, | ||
log_oneliner_summary: bool, | ||
file: TextIO = sys.stdout, | ||
) -> None: | ||
"""Prints a summary of a format check's findings.""" | ||
if not findings: | ||
return | ||
|
||
if log_oneliner_summary: | ||
_LOG.warning( | ||
'Found %d files with formatting issues:', | ||
len(findings), | ||
) | ||
|
||
paths_to_fix = [] | ||
for formatting_finding in findings: | ||
if not formatting_finding.diff: | ||
continue | ||
|
||
paths_to_fix.append(formatting_finding.file_path) | ||
diff = ( | ||
colorize_diff(formatting_finding.diff) | ||
if color.is_enabled(file) | ||
else formatting_finding.diff | ||
) | ||
file.write(diff) | ||
|
||
if log_fix_command: | ||
# TODO: https://pwbug.dev/326309165 - Add a Bazel-specific command. | ||
format_command = "pw format --fix" | ||
|
||
def path_relative_to_cwd(path: Path): | ||
try: | ||
return Path(path).resolve().relative_to(Path.cwd().resolve()) | ||
except ValueError: | ||
return Path(path).resolve() | ||
|
||
paths = " ".join([str(path_relative_to_cwd(p)) for p in paths_to_fix]) | ||
message = f' {format_command} {paths}' | ||
_LOG.warning('To fix formatting, run:\n\n%s\n', message) |
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