From 4db97b5bb6b0fd936219835f8bb780e3ae017881 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Thu, 7 Nov 2024 21:09:05 +0100 Subject: [PATCH] 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 --- scripts/ruff/gen_format_exclude.py | 18 +++++++++++++ scripts/ruff/gen_lint_exclude.py | 42 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100755 scripts/ruff/gen_format_exclude.py create mode 100755 scripts/ruff/gen_lint_exclude.py diff --git a/scripts/ruff/gen_format_exclude.py b/scripts/ruff/gen_format_exclude.py new file mode 100755 index 00000000000..6f3b093e70f --- /dev/null +++ b/scripts/ruff/gen_format_exclude.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +# Copyright (c) 2024 Basalte bv +# SPDX-License-Identifier: Apache-2.0 + +import sys + +# A very simple script to convert ruff format output to toml +# For example: +# ruff format --check | ./scripts/ruff/gen_format_exclude.py >> .ruff-excludes.toml + +if __name__ == "__main__": + sys.stdout.write("[format]\n") + sys.stdout.write("exclude = [\n") + for line in sys.stdin: + if line.startswith("Would reformat: "): + sys.stdout.write(f' "./{line[16:-1]}",\n') + sys.stdout.write("]\n") diff --git a/scripts/ruff/gen_lint_exclude.py b/scripts/ruff/gen_lint_exclude.py new file mode 100755 index 00000000000..af627cf82ce --- /dev/null +++ b/scripts/ruff/gen_lint_exclude.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# Copyright (c) 2024 Basalte bv +# SPDX-License-Identifier: Apache-2.0 + +import json +import sys +from pathlib import Path, PurePosixPath + +# A very simple script to convert ruff lint output from json to toml +# For example: +# ruff check --output-format=json | ./scripts/ruff/gen_lint_exclude.py >> .ruff-excludes.toml + + +class RuffRule: + def __init__(self, code: str, url: str) -> None: + self.code = code + self.url = url + + def __eq__(self, other: object) -> bool: + if not isinstance(other, type(self)): + return NotImplemented + return self.code.__eq__(other.code) + + def __hash__(self) -> int: + return self.code.__hash__() + + +if __name__ == "__main__": + violations = json.load(sys.stdin) + sys.stdout.write("[lint.per-file-ignores]\n") + + rules: dict[str, set[RuffRule]] = {} + for v in violations: + rules.setdefault(v["filename"], set()).add(RuffRule(v["code"], v["url"])) + + for f, rs in rules.items(): + path = PurePosixPath(f) + sys.stdout.write(f'"./{path.relative_to(Path.cwd())}" = [\n') + for r in sorted(rs, key=lambda x: x.code): + sys.stdout.write(f' "{r.code}",\t# {r.url}\n'.expandtabs()) + sys.stdout.write("]\n")