Skip to content

Commit 3f3ed26

Browse files
committed
scripts: Add helper scripts for ruff baseline excludes
Add simple scripts to convert ruff check and ruff format output to toml exclude sections. These sections can be used to ignore baseline violations for an existing codebase. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent dd96a9b commit 3f3ed26

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

scripts/ruff/gen_format_exclude.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (c) 2024 Basalte bv
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
import sys
7+
8+
# A very simple script to convert ruff format output to toml
9+
# For example:
10+
# ruff format --check | ./scripts/ruff/gen_format_exclude.py >> .ruff-excludes.toml
11+
12+
if __name__ == "__main__":
13+
sys.stdout.write("[format]\n")
14+
sys.stdout.write("exclude = [\n")
15+
for line in sys.stdin:
16+
if line.startswith("Would reformat: "):
17+
sys.stdout.write(f' "./{line[16:-1]}",\n')
18+
sys.stdout.write("]\n")

scripts/ruff/gen_lint_exclude.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (c) 2024 Basalte bv
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
import json
7+
import sys
8+
from pathlib import Path, PurePosixPath
9+
10+
# A very simple script to convert ruff lint output from json to toml
11+
# For example:
12+
# ruff check --output-format=json | ./scripts/ruff/gen_lint_exclude.py >> .ruff-excludes.toml
13+
14+
15+
class RuffRule:
16+
def __init__(self, code: str, url: str) -> None:
17+
self.code = code
18+
self.url = url
19+
20+
def __eq__(self, other: object) -> bool:
21+
if not isinstance(other, type(self)):
22+
return NotImplemented
23+
return self.code.__eq__(other.code)
24+
25+
def __hash__(self) -> int:
26+
return self.code.__hash__()
27+
28+
29+
if __name__ == "__main__":
30+
violations = json.load(sys.stdin)
31+
sys.stdout.write("[lint.per-file-ignores]\n")
32+
33+
rules: dict[str, set[RuffRule]] = {}
34+
for v in violations:
35+
rules.setdefault(v["filename"], set()).add(RuffRule(v["code"], v["url"]))
36+
37+
for f, rs in rules.items():
38+
path = PurePosixPath(f)
39+
sys.stdout.write(f'"./{path.relative_to(Path.cwd())}" = [\n')
40+
for r in sorted(rs, key=lambda x: x.code):
41+
sys.stdout.write(f' "{r.code}",\t# {r.url}\n'.expandtabs())
42+
sys.stdout.write("]\n")

0 commit comments

Comments
 (0)