Pieter De Gendt | 4db97b5 | 2024-11-07 21:09:05 +0100 | [diff] [blame] | 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") |