generated from google-gemini/aistudio-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_syntax_checker.py
More file actions
executable file
·89 lines (78 loc) · 2.91 KB
/
_syntax_checker.py
File metadata and controls
executable file
·89 lines (78 loc) · 2.91 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
import os
import ast
import json
import subprocess
import sys
# Directories to skip
EXCLUDE_DIRS = {'node_modules', '.git', '.ai_backups', '.snapshots', '__pycache__', 'dist', 'build'}
def check_python(filepath):
try:
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
source = f.read()
compile(source, filepath, 'exec')
return None
except SyntaxError as e:
return f"Python Syntax Error: {e}"
except Exception as e:
return f"Error reading/parsing: {e}"
def check_json(filepath):
try:
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
json.load(f)
return None
except json.JSONDecodeError as e:
return f"JSON Syntax Error: {e}"
except Exception as e:
return f"Error reading: {e}"
def check_bash(filepath):
try:
# Check for syntax errors only using bash -n
result = subprocess.run(['bash', '-n', filepath], capture_output=True, text=True)
if result.returncode != 0:
return f"Bash Syntax Error: {result.stderr.strip()}"
return None
except Exception as e:
return f"Error checking bash: {e}"
def check_js(filepath):
try:
# Node check syntax using node -c
# Note: This requires Node.js to be installed.
result = subprocess.run(['node', '-c', filepath], capture_output=True, text=True)
if result.returncode != 0:
return f"JS Syntax Error: {result.stderr.strip()}"
return None
except Exception as e:
# If node is missing, we might want to just skip or log a warning
return f"Error checking JS (Node.js might be missing): {e}"
def scan_dir(start_dir):
issues = []
for root, dirs, files in os.walk(start_dir):
# Skip excluded directories in-place
dirs[:] = [d for d in dirs if d not in EXCLUDE_DIRS and not d.startswith('.')]
for file in files:
filepath = os.path.join(root, file)
ext = os.path.splitext(file)[1].lower()
error = None
if ext == '.py':
error = check_python(filepath)
elif ext == '.json':
error = check_json(filepath)
elif ext == '.sh':
error = check_bash(filepath)
elif ext in ('.js', '.mjs', '.cjs'):
error = check_js(filepath)
if error:
issues.append((filepath, error))
print(f"[FAIL] {filepath}: {error}")
# else:
# print(f"[OK] {filepath}")
return issues
if __name__ == "__main__":
target = '.' if len(sys.argv) < 2 else sys.argv[1]
print(f"Scanning {os.path.abspath(target)} for syntax errors...")
found_issues = scan_dir(target)
print(f"\nScan complete. Found {len(found_issues)} files with errors.")
if found_issues:
sys.exit(1)
else:
sys.exit(0)