-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoding-guidelines.py
More file actions
executable file
·140 lines (104 loc) · 4.24 KB
/
coding-guidelines.py
File metadata and controls
executable file
·140 lines (104 loc) · 4.24 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
#!/usr/bin/env python3
import ast
import logging
import os
import re
import sys
logging.basicConfig(level=logging.INFO, format="%(message)s", stream=sys.stdout)
logger = logging.getLogger(__name__)
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "."))
def load_gitignore_patterns():
"""
Load patterns from .gitignore file if it exists.
"""
gitignore_path = os.path.join(PROJECT_ROOT, ".gitignore")
patterns = set()
if os.path.exists(gitignore_path):
with open(gitignore_path, encoding="utf-8") as f:
for line in f:
line = line.strip()
if line and not line.startswith("#"):
if line.endswith("/"):
patterns.add(line[:-1])
elif "*" not in line and "?" not in line and "/" not in line:
patterns.add(line)
return patterns
EXCLUDED_PATTERNS = load_gitignore_patterns()
RED = "\033[91m"
YELLOW = "\033[93m"
GREEN = "\033[92m"
RESET = "\033[0m"
def find_python_files(base_dir):
"""
Find all Python files recursively, excluding directories and files from .gitignore.
"""
for root, dirs, files in os.walk(base_dir):
dirs[:] = [d for d in dirs if d not in EXCLUDED_PATTERNS]
for file in files:
if file in EXCLUDED_PATTERNS:
continue
if file.endswith(".py"):
filepath = os.path.join(root, file)
rel_path = os.path.relpath(filepath, base_dir)
if any(part in EXCLUDED_PATTERNS for part in rel_path.split(os.sep)):
continue
yield rel_path
def extract_routes_with_underscores(filepath):
"""
Extract routes with invalid underscores from a Python file.
"""
routes_with_underscores = []
try:
with open(filepath, encoding="utf-8") as f:
content = f.read()
tree = ast.parse(content, filename=filepath)
except (SyntaxError, UnicodeDecodeError, FileNotFoundError) as e:
logger.info(f"{YELLOW}Warning: Could not parse {filepath}: {str(e)}{RESET}")
return []
for node in ast.walk(tree):
if isinstance(node, ast.Call) and hasattr(node.func, "attr"):
# Check for FastAPI and HTTP route decorators
if node.func.attr in {"get", "post", "put", "delete", "patch", "route"}:
for arg in node.args:
if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
if has_invalid_underscore_in_route(arg.value):
routes_with_underscores.append((arg.value, filepath))
return routes_with_underscores
def has_invalid_underscore_in_route(path: str) -> bool:
"""
Check if a route path contains invalid underscores (excluding path variables).
"""
stripped_path = re.sub(r"{[^}]*}", "", path)
return "_" in stripped_path
def check_framework_routes(base_path, framework_identifier, check_message):
"""
Check routes for a specific framework.
"""
violations = []
for py_file in find_python_files(base_path):
try:
with open(py_file, encoding="utf-8") as f:
content = f.read()
if framework_identifier in content:
violations.extend(extract_routes_with_underscores(py_file))
except (UnicodeDecodeError, FileNotFoundError) as e:
logger.info(f"{YELLOW}Warning: Could not read {py_file}: {str(e)}{RESET}")
if violations:
logger.info(f"{check_message} violations (underscores in paths):{RESET}")
for route, file in violations:
logger.info(f"{RED}{file}:{RESET} {YELLOW}{route}{RESET}")
return True
logger.info(f"{GREEN}No {check_message} violations found.{RESET}")
return False
def main():
"""
Main function to check for coding guidelines violations."
"""
base_path = PROJECT_ROOT
has_fastapi_violations = check_framework_routes(base_path, "fastapi", "FastAPI route")
has_http_route_violations = check_framework_routes(base_path, "http.route", "HTTP route decorator")
if has_fastapi_violations or has_http_route_violations:
return 1
return 0
if __name__ == "__main__":
sys.exit(main())