|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Generated by Claude Code (Sonnet 4) |
| 3 | + |
| 4 | +""" |
| 5 | +Script to test all modules in ansible_base/lib for pure Python compatibility. |
| 6 | +This script ensures that modules in the lib folder can be imported without |
| 7 | +requiring Django apps to be initialized. |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | + |
| 16 | +def find_python_modules(lib_path): |
| 17 | + """Find all Python modules in the lib directory.""" |
| 18 | + modules = [] |
| 19 | + lib_path = Path(lib_path) |
| 20 | + |
| 21 | + for py_file in lib_path.rglob("*.py"): |
| 22 | + if py_file.name == "__init__.py": |
| 23 | + # Convert path to module name (including ansible_base prefix) |
| 24 | + relative_path = py_file.parent.relative_to(lib_path.parent.parent) |
| 25 | + module_name = str(relative_path).replace(os.sep, ".") |
| 26 | + modules.append(module_name) |
| 27 | + else: |
| 28 | + # Convert path to module name (including ansible_base prefix) |
| 29 | + relative_path = py_file.relative_to(lib_path.parent.parent) |
| 30 | + module_name = str(relative_path)[:-3].replace(os.sep, ".") # Remove .py extension |
| 31 | + modules.append(module_name) |
| 32 | + |
| 33 | + return sorted(modules) |
| 34 | + |
| 35 | + |
| 36 | +def test_pure_python_import(module_name, base_path): |
| 37 | + """Test if a module can be imported with pure Python (no Django setup).""" |
| 38 | + test_script = f''' |
| 39 | +import sys |
| 40 | +sys.path.insert(0, "{base_path}") |
| 41 | +
|
| 42 | +try: |
| 43 | + import {module_name} |
| 44 | + print("SUCCESS") |
| 45 | +except Exception as e: |
| 46 | + print(f"ERROR: {{e}}") |
| 47 | +''' |
| 48 | + |
| 49 | + # Run in a clean environment without Django setup |
| 50 | + env = os.environ.copy() |
| 51 | + if 'DJANGO_SETTINGS_MODULE' in env: |
| 52 | + del env['DJANGO_SETTINGS_MODULE'] |
| 53 | + |
| 54 | + try: |
| 55 | + result = subprocess.run(['python', '-c', test_script], capture_output=True, text=True, env=env, timeout=30) |
| 56 | + |
| 57 | + if result.returncode == 0 and "SUCCESS" in result.stdout: |
| 58 | + return True, None |
| 59 | + else: |
| 60 | + error_msg = result.stderr.strip() or result.stdout.strip() |
| 61 | + return False, error_msg |
| 62 | + except subprocess.TimeoutExpired: |
| 63 | + return False, "Import test timed out" |
| 64 | + except Exception as e: |
| 65 | + return False, f"Failed to run test: {e}" |
| 66 | + |
| 67 | + |
| 68 | +def main(): |
| 69 | + """Main function to test all modules in ansible_base/lib.""" |
| 70 | + script_dir = Path(__file__).parent.parent.parent # Go up two levels since we're in tools/scripts/ |
| 71 | + lib_path = script_dir / "ansible_base" / "lib" |
| 72 | + |
| 73 | + if not lib_path.exists(): |
| 74 | + print(f"ERROR: {lib_path} does not exist") |
| 75 | + sys.exit(1) |
| 76 | + |
| 77 | + print("Testing pure Python import compatibility for ansible_base/lib modules...") |
| 78 | + print("=" * 70) |
| 79 | + |
| 80 | + modules = find_python_modules(lib_path) |
| 81 | + |
| 82 | + # Modules that are allowed to fail because they inherently require Django |
| 83 | + allowed_failures = { |
| 84 | + # Abstract models inherently require Django to be initialized |
| 85 | + 'ansible_base.lib.abstract_models', |
| 86 | + 'ansible_base.lib.abstract_models.common', |
| 87 | + 'ansible_base.lib.abstract_models.immutable', |
| 88 | + 'ansible_base.lib.abstract_models.organization', |
| 89 | + 'ansible_base.lib.abstract_models.team', |
| 90 | + 'ansible_base.lib.abstract_models.user', |
| 91 | + # View classes require Django REST framework |
| 92 | + 'ansible_base.lib.utils.views.ansible_base', |
| 93 | + 'ansible_base.lib.utils.views.django_app_api', |
| 94 | + 'ansible_base.lib.utils.views.permissions', |
| 95 | + 'ansible_base.lib.utils.views.urls', |
| 96 | + # Router classes require Django REST framework |
| 97 | + 'ansible_base.lib.routers', |
| 98 | + 'ansible_base.lib.routers.association_resource_router', |
| 99 | + # These modules require Django settings to be configured at import time |
| 100 | + 'ansible_base.lib.backends.prefixed_user_auth', |
| 101 | + 'ansible_base.lib.dynamic_config.dynamic_urls', |
| 102 | + 'ansible_base.lib.serializers.common', |
| 103 | + 'ansible_base.lib.testing.fixtures', |
| 104 | + 'ansible_base.lib.testing.util', |
| 105 | + 'ansible_base.lib.utils.auth', |
| 106 | + } |
| 107 | + |
| 108 | + failed_modules = [] |
| 109 | + passed_modules = [] |
| 110 | + expected_failures = [] |
| 111 | + |
| 112 | + for module_name in modules: |
| 113 | + print(f"Testing {module_name}... ", end="", flush=True) |
| 114 | + success, error = test_pure_python_import(module_name, str(script_dir)) |
| 115 | + |
| 116 | + if success: |
| 117 | + print("✓ PASS") |
| 118 | + passed_modules.append(module_name) |
| 119 | + else: |
| 120 | + if module_name in allowed_failures: |
| 121 | + print("✗ EXPECTED FAIL") |
| 122 | + expected_failures.append((module_name, error)) |
| 123 | + else: |
| 124 | + print("✗ UNEXPECTED FAIL") |
| 125 | + print(f" Error: {error}") |
| 126 | + failed_modules.append((module_name, error)) |
| 127 | + |
| 128 | + print("\n" + "=" * 70) |
| 129 | + print(f"Results: {len(passed_modules)} passed, {len(failed_modules)} unexpected failures, {len(expected_failures)} expected failures") |
| 130 | + |
| 131 | + if failed_modules: |
| 132 | + print("\nUnexpected failures:") |
| 133 | + for module_name, error in failed_modules: |
| 134 | + print(f" - {module_name}: {error}") |
| 135 | + |
| 136 | + print("\nThese modules should be importable with pure Python.") |
| 137 | + print("Consider moving Django-specific imports inline within functions.") |
| 138 | + sys.exit(1) |
| 139 | + else: |
| 140 | + print("\nAll importable modules passed! ✓") |
| 141 | + if expected_failures: |
| 142 | + print(f"({len(expected_failures)} modules have expected failures due to Django dependencies)") |
| 143 | + sys.exit(0) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + main() |
0 commit comments