Spaces:
Running
Running
File size: 4,730 Bytes
a63c61f | 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 | """
Quick Error Checker for RAG API
Similar to 'npm run build' for JavaScript, this checks for Python errors.
Usage:
python check_errors.py
"""
import sys
import os
from pathlib import Path
import py_compile
import importlib.util
# Colors for output
GREEN = '\033[92m'
RED = '\033[91m'
YELLOW = '\033[93m'
RESET = '\033[0m'
BOLD = '\033[1m'
def print_header(text):
"""Print section header"""
print(f"\n{BOLD}{'='*60}{RESET}")
print(f"{BOLD}{text}{RESET}")
print(f"{BOLD}{'='*60}{RESET}\n")
def check_syntax(file_path):
"""Check Python syntax (like tsc --noEmit)"""
try:
py_compile.compile(file_path, doraise=True)
return True, None
except py_compile.PyCompileError as e:
return False, str(e)
def check_imports(file_path):
"""Check if file can be imported"""
try:
spec = importlib.util.spec_from_file_location("module", file_path)
if spec and spec.loader:
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return True, None
except Exception as e:
return False, str(e)
def find_python_files(directory):
"""Find all Python files in directory"""
return list(Path(directory).rglob("*.py"))
def main():
"""Main error checking function"""
print(f"{BOLD}π Python Error Checker{RESET}")
print(f"Similar to 'npm run build' for JavaScript\n")
# Get source directory
src_dir = Path(__file__).parent / "src"
if not src_dir.exists():
print(f"{RED}β Source directory not found: {src_dir}{RESET}")
return 1
# Find all Python files
python_files = find_python_files(src_dir)
print(f"Found {len(python_files)} Python files\n")
# Track results
syntax_errors = []
import_errors = []
# ββ Stage 1: Syntax Check ββββββββββββββββββββββββββββββββββββββββββββββ
print_header("Stage 1: Syntax Check (like tsc --noEmit)")
for file_path in python_files:
relative_path = file_path.relative_to(Path.cwd())
success, error = check_syntax(file_path)
if success:
print(f"{GREEN}β{RESET} {relative_path}")
else:
print(f"{RED}β{RESET} {relative_path}")
print(f" {RED}Error: {error}{RESET}")
syntax_errors.append((relative_path, error))
# ββ Stage 2: Import Check ββββββββββββββββββββββββββββββββββββββββββββββ
print_header("Stage 2: Import Check")
# Only check files that passed syntax check
files_to_import = [f for f in python_files if f not in [e[0] for e in syntax_errors]]
# Add src to path for imports
sys.path.insert(0, str(src_dir.parent))
for file_path in files_to_import:
relative_path = file_path.relative_to(Path.cwd())
# Skip __init__.py files
if file_path.name == "__init__.py":
print(f"{YELLOW}β{RESET} {relative_path} (skipped)")
continue
success, error = check_imports(file_path)
if success:
print(f"{GREEN}β{RESET} {relative_path}")
else:
print(f"{RED}β{RESET} {relative_path}")
print(f" {RED}Error: {error[:200]}...{RESET}")
import_errors.append((relative_path, error))
# ββ Summary ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print_header("Summary")
total_files = len(python_files)
syntax_ok = total_files - len(syntax_errors)
import_ok = len(files_to_import) - len(import_errors)
print(f"Total files checked: {total_files}")
print(f"Syntax check: {GREEN}{syntax_ok} passed{RESET}, {RED}{len(syntax_errors)} failed{RESET}")
print(f"Import check: {GREEN}{import_ok} passed{RESET}, {RED}{len(import_errors)} failed{RESET}")
# ββ Exit Code ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if syntax_errors or import_errors:
print(f"\n{RED}{BOLD}β Build Failed{RESET}")
print(f"\nFix the errors above and try again.")
return 1
else:
print(f"\n{GREEN}{BOLD}β
Build Successful{RESET}")
print(f"\nAll files are error-free!")
return 0
if __name__ == "__main__":
sys.exit(main())
|