|
15 | 15 | # specific language governing permissions and limitations |
16 | 16 | # under the License. |
17 | 17 |
|
18 | | -"""Run ruff linter on Python files outside py/ directory.""" |
| 18 | +"""Run ruff check on Python files across the project. |
| 19 | +
|
| 20 | +Usage: |
| 21 | + bazel run //py:ruff-check -- [ruff check args] |
| 22 | +""" |
19 | 23 |
|
20 | 24 | import os |
21 | 25 | import subprocess |
22 | 26 | import sys |
23 | 27 |
|
24 | 28 | from python.runfiles import Runfiles |
25 | 29 |
|
26 | | -LINT_DIRS = ["scripts", "common", "dotnet", "java", "javascript", "rb"] |
| 30 | +ALL_DIRS = ["py", "scripts", "common", "dotnet", "java", "javascript", "rb"] |
27 | 31 | EXCLUDES = ["**/node_modules/**", "**/.bundle/**"] |
28 | 32 |
|
29 | 33 |
|
| 34 | +def run_check(ruff, exclude_args, dirs, extra_args): |
| 35 | + """Run ruff check (linting).""" |
| 36 | + cmd = [ruff, "check", "--config=py/pyproject.toml"] |
| 37 | + return subprocess.run(cmd + exclude_args + dirs + extra_args).returncode |
| 38 | + |
| 39 | + |
30 | 40 | if __name__ == "__main__": |
31 | 41 | r = Runfiles.Create() |
32 | 42 | ruff = r.Rlocation("rules_multitool++multitool+multitool/tools/ruff/ruff") |
33 | 43 |
|
34 | 44 | os.chdir(os.environ["BUILD_WORKSPACE_DIRECTORY"]) |
35 | 45 |
|
36 | | - # Check if --check flag is passed (for CI - verify without fixing) |
37 | | - check_only = "--check" in sys.argv |
38 | | - extra_args = [arg for arg in sys.argv[1:] if arg != "--check"] |
39 | | - |
40 | 46 | exclude_args = [] |
41 | 47 | for pattern in EXCLUDES: |
42 | 48 | exclude_args.extend(["--exclude", pattern]) |
43 | 49 |
|
44 | | - check_cmd = [ruff, "check", "--config=py/pyproject.toml"] |
45 | | - if not check_only: |
46 | | - check_cmd.extend(["--fix", "--show-fixes"]) |
47 | | - check_result = subprocess.run(check_cmd + exclude_args + LINT_DIRS + extra_args) |
48 | | - |
49 | | - format_cmd = [ruff, "format", "--config=py/pyproject.toml"] |
50 | | - if check_only: |
51 | | - format_cmd.append("--check") |
52 | | - format_result = subprocess.run(format_cmd + exclude_args + LINT_DIRS) |
53 | | - |
54 | | - sys.exit(check_result.returncode or format_result.returncode) |
| 50 | + sys.exit(run_check(ruff, exclude_args, ALL_DIRS, sys.argv[1:])) |
0 commit comments