-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlint.py
More file actions
93 lines (77 loc) · 3.07 KB
/
Copy pathlint.py
File metadata and controls
93 lines (77 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
"""
Simple script to run ruff linting, formatting, mypy type checking, and djLint HTML formatting.
Usage:
python lint.py <command> [files...]
Commands:
check - Check for linting issues
fix - Fix auto-fixable linting issues
format - Format code with ruff
mypy - Run mypy type checking
html - Lint HTML templates with djLint
html-fix - Format HTML templates with djLint
all - Run all checks and formatting
If files are provided, only those files are acted on.
If no files are provided, runs on the entire project.
"""
from typing import List, Optional, Dict
import subprocess
import sys
import os
def run_command(cmd: List[str], env: Optional[Dict[str, str]] = None) -> int:
print(f"Running: {' '.join(cmd)}")
result = subprocess.run(cmd, env=env)
return result.returncode
def main() -> None:
if len(sys.argv) < 2:
print(__doc__)
sys.exit(1)
command = sys.argv[1]
files = sys.argv[2:]
py_files = [f for f in files if f.endswith(".py")]
html_files = [f for f in files if f.endswith((".html", ".htm"))]
if command == "check":
exit_code = run_command(["ruff", "check"] + py_files)
elif command == "fix":
exit_code = run_command(["ruff", "check", "--fix"] + py_files)
elif command == "format":
exit_code = run_command(["ruff", "format"] + py_files)
elif command == "mypy":
python_exe = sys.executable
targets = py_files if py_files else ["src/borgitory"]
exit_code = run_command([python_exe, "-m", "mypy"] + targets)
elif command == "html":
targets = html_files if html_files else ["src/borgitory/templates"]
exit_code = run_command(["djlint"] + targets)
elif command == "html-fix":
targets = html_files if html_files else ["src/borgitory/templates"]
exit_code = run_command(["djlint"] + targets + ["--reformat"])
elif command == "all":
exit_code = 0
if not files or py_files:
print("Running ruff check...")
exit_code = run_command(["ruff", "check", "--fix"] + py_files)
if exit_code == 0 and (not files or py_files):
print("Running ruff format...")
exit_code = run_command(["ruff", "format"] + py_files)
if exit_code == 0 and (not files or py_files):
print("Running mypy type checking...")
env = os.environ.copy()
env["PYTHONPATH"] = "src"
python_exe = sys.executable
targets = py_files if py_files else ["src/borgitory"]
exit_code = run_command(
[python_exe, "-m", "mypy"] + targets,
env=env,
)
if exit_code == 0 and (not files or html_files):
print("Running djLint HTML formatting...")
targets = html_files if html_files else ["src/borgitory/templates"]
exit_code = run_command(["djlint"] + targets + ["--reformat"])
else:
print(f"Unknown command: {command}")
print(__doc__)
sys.exit(1)
sys.exit(exit_code)
if __name__ == "__main__":
main()