3 """Run linters on project code. Add --fix to autofix files with linters that support it."""
6 import subprocess as subp
8 from os import path, environ, chdir
10 DRY = "--fix" not in sys.argv or environ.get("CI")
15 You're running linters in non-destructive readonly mode.
16 Some of them support automated fixes (like reformatting code).
17 To apply them, run `lint.py --fix` or `ninja -C build reformat`.
19 print(MSG, file=sys.stderr)
21 source_root = path.dirname(path.realpath(__file__))
22 source_root = environ.get("MESON_SOURCE_ROOT", source_root)
25 # It's best not to use globs that cover everything in the project — if integration
26 # tests are run with a large --repeat value, test working directory can reach
27 # enormous sizes, and linters either get very slow, or start crashing.
32 "--options=.astylerc",
33 "--dry-run" if DRY else "--formatted",
37 ["shfmt", "-d" if DRY else "-w", "-i", "2", "-s", "."],
38 ["black", "--check" if DRY else ".", "."],
40 ["mypy", "--exclude", "build", "."],
41 ["shellcheck", "-x", *glob(".ci/**/*.sh", recursive=True)],
48 print(f"{HEADER} Running linter '{exe}' {HEADER}")
59 or bool(res.returncode)
60 or (exe == "astyle" and "Formatted " in res.stdout)
63 except FileNotFoundError as e:
64 print(f"Warning: linter {exe} is missing", file=sys.stderr)