main
1#!/usr/bin/env -S uv run --script
2# /// script
3# requires-python = ">=3.11"
4# dependencies = [
5# "ruff>=0.8.0",
6# ]
7# ///
8"""Run ruff linter and formatter on Python code.
9
10This script runs both ruff check and ruff format on the specified directory
11or the current directory if none is provided.
12"""
13
14import subprocess
15import sys
16from pathlib import Path
17
18
19def run_command(cmd: list[str]) -> int:
20 """Run a command and return its exit code.
21
22 Args:
23 cmd: Command and arguments to run
24
25 Returns:
26 Exit code from the command
27 """
28 print(f"Running: {' '.join(cmd)}")
29 result = subprocess.run(cmd, check=False)
30 return result.returncode
31
32
33def main() -> int:
34 """Run linter and formatter.
35
36 Returns:
37 Exit code (0 for success, non-zero for errors)
38 """
39 # Get target directory from args or use current directory
40 target = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(".")
41
42 if not target.exists():
43 print(f"Error: {target} does not exist", file=sys.stderr)
44 return 1
45
46 # Run ruff check
47 check_result = run_command(["ruff", "check", "--fix", str(target)])
48
49 # Run ruff format
50 format_result = run_command(["ruff", "format", str(target)])
51
52 # Return non-zero if either failed
53 if check_result != 0 or format_result != 0:
54 return 1
55
56 print("\n✓ Linting and formatting complete!")
57 return 0
58
59
60if __name__ == "__main__":
61 sys.exit(main())