|
1 | 1 | import os |
2 | 2 | import subprocess |
| 3 | +import sys |
3 | 4 |
|
4 | 5 | import click |
5 | 6 |
|
6 | 7 |
|
7 | | -@click.command("linter", help="Runs flake8 linter on the 'app' and 'rosemary' directories.") |
| 8 | +@click.command( |
| 9 | + "linter", |
| 10 | + help="Runs flake8 + black --check + isort --check-only on 'app', 'rosemary' and 'core' " |
| 11 | + "(same checks as .github/workflows/CI_lint.yml).", |
| 12 | +) |
8 | 13 | def linter(): |
9 | 14 |
|
10 | | - # Define the directories to be checked with flake8 |
| 15 | + # Mirror the CI Python Lint job exactly: flake8, black --check and |
| 16 | + # isort --check-only, all three pointed at the same three directories. |
| 17 | + # Running them locally via `rosemary linter` used to only cover flake8, |
| 18 | + # so PRs kept bouncing off the black/isort steps after being green |
| 19 | + # locally — fixed by running the full triad here. |
11 | 20 | working_dir = os.getenv("WORKING_DIR", "") |
12 | 21 | directories = [ |
13 | 22 | os.path.join(working_dir, "app"), |
14 | 23 | os.path.join(working_dir, "rosemary"), |
15 | 24 | os.path.join(working_dir, "core"), |
16 | 25 | ] |
17 | 26 |
|
18 | | - # Run flake8 in each directory |
19 | | - for directory in directories: |
20 | | - click.echo(f"Running flake8 on {directory}...") |
21 | | - result = subprocess.run(["flake8", directory]) |
| 27 | + checks = [ |
| 28 | + ("flake8", ["flake8", *directories]), |
| 29 | + ("black --check", ["black", "--check", *directories]), |
| 30 | + ("isort --check-only", ["isort", "--check-only", *directories]), |
| 31 | + ] |
22 | 32 |
|
23 | | - # Check if flake8 encountered problems |
| 33 | + failed = [] |
| 34 | + for label, cmd in checks: |
| 35 | + click.echo(click.style(f"\n==> {label}", fg="cyan")) |
| 36 | + result = subprocess.run(cmd) |
24 | 37 | if result.returncode != 0: |
25 | | - click.echo(click.style(f"flake8 found issues in {directory}.", fg="red")) |
| 38 | + click.echo(click.style(f"{label} found issues.", fg="red")) |
| 39 | + failed.append(label) |
26 | 40 | else: |
27 | | - click.echo(click.style(f"No issues found in {directory}. Congratulations!", fg="green")) |
| 41 | + click.echo(click.style(f"{label}: clean.", fg="green")) |
| 42 | + |
| 43 | + if failed: |
| 44 | + click.echo( |
| 45 | + click.style( |
| 46 | + "\nFailed: " + ", ".join(failed) + ". Run `rosemary linter:fix` to auto-format.", |
| 47 | + fg="red", |
| 48 | + ) |
| 49 | + ) |
| 50 | + sys.exit(1) |
| 51 | + click.echo(click.style("\nAll lint checks passed. Congratulations!", fg="green")) |
28 | 52 |
|
29 | 53 |
|
30 | 54 | @click.command( |
|
0 commit comments