44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Simple Python syntax validator - verifies Python files parse correctly."""
|
|
|
|
import ast
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
def validate_python_file(filepath: str) -> tuple[bool, str]:
|
|
"""Validate a Python file's syntax.
|
|
|
|
Returns:
|
|
(True, "OK") if valid syntax
|
|
(False, error_message) if invalid
|
|
"""
|
|
try:
|
|
path = Path(filepath)
|
|
source = path.read_text()
|
|
ast.parse(source, filename=str(path))
|
|
return True, "OK"
|
|
except FileNotFoundError:
|
|
return False, f"File not found: {filepath}"
|
|
except SyntaxError as e:
|
|
return False, f"SyntaxError at line {e.lineno}: {e.msg}"
|
|
except Exception as e:
|
|
return False, f"Error: {e}"
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Usage: python_syntax_validator.py <file1.py> [file2.py] ...")
|
|
sys.exit(1)
|
|
|
|
all_valid = True
|
|
for filepath in sys.argv[1:]:
|
|
valid, msg = validate_python_file(filepath)
|
|
if valid:
|
|
print(f"VALID: {filepath}")
|
|
else:
|
|
print(f"INVALID: {filepath} - {msg}")
|
|
all_valid = False
|
|
|
|
sys.exit(0 if all_valid else 1)
|
|
|
|
if __name__ == "__main__":
|
|
main() |