Skip to content

Commit 864dcb2

Browse files
rmzmrnnthomas-bc
andauthored
Support --exclude option on fprime-util format (nasa#297)
* Add --exclude argument for excluding directories from formatting * Apply black format * Add exclude_file function * Change parsed.excluded to parsed.exclude * Add verbose information in exclude_file function * Apply black format * Allow file path to exclude in formatting * Shorten else if to elif * Apply black format * Update src/fprime/util/cli.py * Update src/fprime/util/commands.py --------- Co-authored-by: Thomas Boyer-Chammard <49786685+thomas-bc@users.noreply.github.com>
1 parent 19afa8c commit 864dcb2

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/fprime/util/cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ def add_special_parsers(
251251
default=[],
252252
help="If specified, --pass-through must be the last argument. Remaining arguments passed to underlying executable",
253253
)
254+
format_parser.add_argument(
255+
"--exclude",
256+
nargs="+",
257+
default=[],
258+
type=Path,
259+
help="Exclude paths from formatting, taking precedence over all input mechanisms",
260+
)
261+
254262
return {
255263
"hash-to-file": run_hash_to_file,
256264
"info": run_info,

src/fprime/util/code_formatter.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ def stage_file(self, filepath: Path) -> None:
7373
else:
7474
self._files_to_format.append(filepath)
7575

76+
def exclude_file(self, filepath: Path) -> None:
77+
"""Request ClangFormatter to exclude the file for formatting.
78+
If the file exists and its extension matches a known C/C++ format,
79+
it will be excluded to clang-format when the execute() function is called.
80+
81+
Args:
82+
filepath (str): file path to be excluded.
83+
"""
84+
if filepath in self._files_to_format:
85+
if self.verbose:
86+
print(f"[INFO] Excluding {filepath} from formatting.")
87+
self._files_to_format.remove(filepath)
88+
elif self.verbose:
89+
print(f"[INFO] {filepath} was not staged for formatting. Skipping.")
90+
7691
def execute(
7792
self, builder: "Build", context: "Path", args: Tuple[Dict[str, str], List[str]]
7893
):

src/fprime/util/commands.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,19 @@ def run_code_format(
201201
for allowed_ext in clang_formatter.allowed_extensions:
202202
for file in dir_path.rglob(f"*{allowed_ext}"):
203203
clang_formatter.stage_file(file)
204+
# Remove staged files that are within excluded paths
205+
for excluded in parsed.exclude:
206+
excluded_path = Path(excluded)
207+
if excluded_path.is_file():
208+
clang_formatter.exclude_file(excluded_path)
209+
elif excluded_path.is_dir():
210+
for allowed_ext in clang_formatter.allowed_extensions:
211+
for file in excluded_path.rglob(f"*{allowed_ext}"):
212+
clang_formatter.exclude_file(file)
213+
else:
214+
print(f"[INFO] {excluded_path} is not a valid path. Skipping.")
215+
continue
216+
204217
return clang_formatter.execute(build, parsed.path, ({}, parsed.pass_through))
205218

206219

0 commit comments

Comments
 (0)