forked from programmable-web-project-unioulu/PWP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_quality.py
executable file
·92 lines (71 loc) · 2.86 KB
/
code_quality.py
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
#!/usr/bin/env python3
"""
Script to run code quality analysis tools on the project.
"""
import os
import subprocess
import sys
from datetime import datetime
# Colors for console output
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
RESET = "\033[0m"
BOLD = "\033[1m"
def print_header(text):
"""Display a formatted header."""
print(f"\n{BOLD}{YELLOW}{'=' * 80}{RESET}")
print(f"{BOLD}{YELLOW}= {text}{RESET}")
print(f"{BOLD}{YELLOW}{'=' * 80}{RESET}\n")
def run_command(command, title):
"""Execute a command and display the result."""
print_header(title)
result = subprocess.run(command, shell=True, capture_output=True, text=True)
print(result.stdout)
if result.stderr:
print(f"{RED}{result.stderr}{RESET}")
return result.returncode
def main():
"""Main function that executes all analysis tools."""
# Create a directory for reports if it doesn't exist
reports_dir = "reports"
if not os.path.exists(reports_dir):
os.makedirs(reports_dir)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
report_file = f"{reports_dir}/code_quality_report_{timestamp}.txt"
# Redirect output to a file
original_stdout = sys.stdout
with open(report_file, "w") as f:
sys.stdout = f
print(f"Code Quality Report - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 80)
# Run Black (code formatter)
run_command("black --check .", "BLACK - Code formatting check")
# Run isort (import sorter)
run_command("isort --check-only --profile black .", "ISORT - Import order check")
# Run Flake8 (linter)
run_command("flake8 .", "FLAKE8 - Code style check")
# Run Pylint (static analysis)
run_command(
"pylint app.py models.py routes validators schemas", "PYLINT - Static code analysis"
)
# Run Bandit (security analysis)
run_command("bandit -r .", "BANDIT - Code security analysis")
# Run MyPy (type checking)
run_command("mypy app.py models.py routes validators schemas", "MYPY - Type checking")
# Run tests with coverage
run_command("pytest --cov=. tests/", "PYTEST - Running tests with coverage")
# Restore standard output
sys.stdout = original_stdout
print(f"{GREEN}Code quality analysis report generated: {report_file}{RESET}")
print(f"{YELLOW}To automatically apply formatting corrections, run:{RESET}")
print(f"{BOLD}python code_quality.py --fix{RESET}")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "--fix":
# Automatic correction mode
print_header("AUTOMATIC CODE CORRECTION")
run_command("black .", "BLACK - Code formatting")
run_command("isort --profile black .", "ISORT - Import sorting")
print(f"{GREEN}Automatic corrections applied.{RESET}")
else:
main()