|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Quick validation script for ShieldX ML Service |
| 4 | +Checks Python syntax and basic imports without running full tests |
| 5 | +""" |
| 6 | + |
| 7 | +import sys |
| 8 | +import os |
| 9 | +from pathlib import Path |
| 10 | +import py_compile |
| 11 | +import importlib.util |
| 12 | + |
| 13 | +class Colors: |
| 14 | + GREEN = '\033[0;32m' |
| 15 | + RED = '\033[0;31m' |
| 16 | + YELLOW = '\033[1;33m' |
| 17 | + BLUE = '\033[0;34m' |
| 18 | + BOLD = '\033[1m' |
| 19 | + NC = '\033[0m' |
| 20 | + |
| 21 | +def print_header(text: str): |
| 22 | + print(f"\n{Colors.BOLD}{Colors.BLUE}{'='*60}{Colors.NC}") |
| 23 | + print(f"{Colors.BOLD}{Colors.BLUE}{text:^60}{Colors.NC}") |
| 24 | + print(f"{Colors.BOLD}{Colors.BLUE}{'='*60}{Colors.NC}\n") |
| 25 | + |
| 26 | +def check_syntax(file_path: Path) -> bool: |
| 27 | + """Check Python file syntax""" |
| 28 | + try: |
| 29 | + py_compile.compile(str(file_path), doraise=True) |
| 30 | + return True |
| 31 | + except py_compile.PyCompileError as e: |
| 32 | + print(f"{Colors.RED}✗ Syntax Error:{Colors.NC} {file_path}") |
| 33 | + print(f" {e}") |
| 34 | + return False |
| 35 | + |
| 36 | +def check_imports(file_path: Path) -> bool: |
| 37 | + """Check if file can be imported (basic check)""" |
| 38 | + try: |
| 39 | + with open(file_path) as f: |
| 40 | + content = f.read() |
| 41 | + |
| 42 | + # Check for common syntax issues |
| 43 | + if 'import' in content: |
| 44 | + # Basic validation |
| 45 | + lines = content.split('\n') |
| 46 | + for i, line in enumerate(lines, 1): |
| 47 | + stripped = line.strip() |
| 48 | + if stripped.startswith('import ') or stripped.startswith('from '): |
| 49 | + # Check for basic import syntax |
| 50 | + if stripped.endswith('\\'): |
| 51 | + continue # Multi-line import |
| 52 | + if 'import' in stripped and not any(x in stripped for x in ['(', ',', 'as']): |
| 53 | + # Simple import |
| 54 | + parts = stripped.split() |
| 55 | + if len(parts) < 2: |
| 56 | + print(f"{Colors.YELLOW}⚠ Warning:{Colors.NC} Line {i}: {stripped}") |
| 57 | + |
| 58 | + return True |
| 59 | + except Exception as e: |
| 60 | + print(f"{Colors.RED}✗ Import Check Failed:{Colors.NC} {file_path}") |
| 61 | + print(f" {e}") |
| 62 | + return False |
| 63 | + |
| 64 | +def validate_directory(directory: str, pattern: str = "**/*.py") -> tuple: |
| 65 | + """Validate all Python files in directory""" |
| 66 | + path = Path(directory) |
| 67 | + if not path.exists(): |
| 68 | + print(f"{Colors.YELLOW}Directory not found: {directory}{Colors.NC}") |
| 69 | + return 0, 0 |
| 70 | + |
| 71 | + print(f"\n{Colors.BOLD}Validating: {directory}{Colors.NC}") |
| 72 | + print("-" * 60) |
| 73 | + |
| 74 | + files = list(path.glob(pattern)) |
| 75 | + if not files: |
| 76 | + print(f"{Colors.YELLOW}No Python files found{Colors.NC}") |
| 77 | + return 0, 0 |
| 78 | + |
| 79 | + passed = 0 |
| 80 | + failed = 0 |
| 81 | + |
| 82 | + for file in sorted(files): |
| 83 | + # Skip __pycache__ and venv |
| 84 | + if '__pycache__' in str(file) or 'venv' in str(file): |
| 85 | + continue |
| 86 | + |
| 87 | + # Check syntax |
| 88 | + if check_syntax(file): |
| 89 | + # Check imports |
| 90 | + if check_imports(file): |
| 91 | + print(f"{Colors.GREEN}✓{Colors.NC} {file.relative_to(path.parent)}") |
| 92 | + passed += 1 |
| 93 | + else: |
| 94 | + failed += 1 |
| 95 | + else: |
| 96 | + failed += 1 |
| 97 | + |
| 98 | + return passed, failed |
| 99 | + |
| 100 | +def main(): |
| 101 | + print_header("ShieldX ML Service - Quick Validation") |
| 102 | + |
| 103 | + os.chdir('/home/vananh/shieldx/services/shieldx-ml') |
| 104 | + print(f"Working directory: {os.getcwd()}\n") |
| 105 | + |
| 106 | + total_passed = 0 |
| 107 | + total_failed = 0 |
| 108 | + |
| 109 | + # Validate main ML service code |
| 110 | + directories = [ |
| 111 | + 'ml-service', |
| 112 | + 'tests', |
| 113 | + 'ml-service/tests' |
| 114 | + ] |
| 115 | + |
| 116 | + for directory in directories: |
| 117 | + if os.path.exists(directory): |
| 118 | + passed, failed = validate_directory(directory) |
| 119 | + total_passed += passed |
| 120 | + total_failed += failed |
| 121 | + |
| 122 | + # Summary |
| 123 | + print_header("Validation Summary") |
| 124 | + print(f"Total Files: {total_passed + total_failed}") |
| 125 | + print(f"{Colors.GREEN}Passed: {total_passed}{Colors.NC}") |
| 126 | + print(f"{Colors.RED}Failed: {total_failed}{Colors.NC}") |
| 127 | + |
| 128 | + if total_failed == 0: |
| 129 | + print(f"\n{Colors.GREEN}{Colors.BOLD}✓ ALL FILES VALID!{Colors.NC}") |
| 130 | + # Guidance for running full tests |
| 131 | + print(f"\n{Colors.YELLOW}Note: Full test execution requires installing test dependencies.{Colors.NC}") |
| 132 | + print(f"{Colors.YELLOW}Run: pip3 install -r requirements-test.txt{Colors.NC}") |
| 133 | + return 0 |
| 134 | + else: |
| 135 | + print(f"\n{Colors.RED}{Colors.BOLD}✗ VALIDATION FAILED{Colors.NC}") |
| 136 | + return 1 |
| 137 | + |
| 138 | +if __name__ == '__main__': |
| 139 | + sys.exit(main()) |
0 commit comments