-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode_quality.py
More file actions
142 lines (110 loc) Β· 3.96 KB
/
code_quality.py
File metadata and controls
142 lines (110 loc) Β· 3.96 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
"""
Code quality utilities for formatting and linting.
Usage:
python code_quality.py <path> [--fix] [--type TYPE]
"""
import sys
from pathlib import Path
try:
import codomyrmex
except ImportError:
project_root = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(project_root / "src"))
import argparse
import subprocess
def run_tool(cmd: list, cwd: str = ".") -> tuple:
"""Run a tool and return (success, output)."""
try:
result = subprocess.run(
cmd, capture_output=True, text=True, cwd=cwd, timeout=120
)
return result.returncode == 0, result.stdout + result.stderr
except FileNotFoundError:
return False, f"Tool not found: {cmd[0]}"
except Exception as e:
return False, str(e)
def check_black(path: str, fix: bool = False) -> dict:
"""Run Black formatter."""
cmd = ["black", "--check" if not fix else "", path]
cmd = [c for c in cmd if c]
success, output = run_tool(cmd)
return {"tool": "black", "success": success, "output": output}
def check_ruff(path: str, fix: bool = False) -> dict:
"""Run Ruff linter."""
cmd = ["ruff", "check", path]
if fix:
cmd.append("--fix")
success, output = run_tool(cmd)
return {"tool": "ruff", "success": success, "output": output}
def check_isort(path: str, fix: bool = False) -> dict:
"""Run isort import sorter."""
cmd = ["isort", "--check-only" if not fix else "", path]
cmd = [c for c in cmd if c]
success, output = run_tool(cmd)
return {"tool": "isort", "success": success, "output": output}
TOOLS = {
"black": check_black,
"ruff": check_ruff,
"isort": check_isort,
}
def main():
# Auto-injected: Load configuration
from pathlib import Path
import yaml
config_path = (
Path(__file__).resolve().parent.parent.parent
/ "config"
/ "coding"
/ "config.yaml"
)
if config_path.exists():
with open(config_path) as f:
yaml.safe_load(f) or {}
print("Loaded config from config/coding/config.yaml")
parser = argparse.ArgumentParser(description="Code quality checks")
parser.add_argument("path", nargs="?", default=".", help="File or directory")
parser.add_argument("--fix", "-f", action="store_true", help="Auto-fix issues")
parser.add_argument(
"--type", "-t", choices=[*list(TOOLS.keys()), "all"], default="all"
)
args = parser.parse_args()
if args.path == "." and not Path("pyproject.toml").exists():
print("π§ Code Quality Tools\n")
print("Usage:")
print(" python code_quality.py src/")
print(" python code_quality.py src/ --fix")
print(" python code_quality.py script.py --type black")
print("\nTools: black (formatting), ruff (linting), isort (imports)")
return 0
target = Path(args.path)
if not target.exists():
print(f"β Path not found: {args.path}")
return 1
mode = "fixing" if args.fix else "checking"
print(f"π§ Code Quality ({mode}): {target}\n")
tools_to_run = TOOLS if args.type == "all" else {args.type: TOOLS[args.type]}
all_passed = True
for name, check_fn in tools_to_run.items():
result = check_fn(str(target), args.fix)
if result["success"]:
print(f" β
{name}")
else:
print(f" β {name}")
all_passed = False
# Show first few lines of output
lines = result["output"].strip().split("\n")
for line in lines[:5]:
print(f" {line}")
if len(lines) > 5:
print(f" ... ({len(lines) - 5} more lines)")
print()
if all_passed:
print("β
All checks passed")
else:
print("β Some checks failed")
if not args.fix:
print(" Run with --fix to auto-fix issues")
return 0 if all_passed else 1
if __name__ == "__main__":
sys.exit(main())