Spaces:
Paused
Paused
| #!/usr/bin/env python3 | |
| """ | |
| Validation Script for Database and Cache Fixes | |
| ============================================ | |
| Tests the fixes for: | |
| 1. SQLite database path issues | |
| 2. Hugging Face cache permissions | |
| """ | |
| import os | |
| import sys | |
| import tempfile | |
| import shutil | |
| from pathlib import Path | |
| def test_database_path(): | |
| """Test database path creation and access""" | |
| print("π Testing database path fixes...") | |
| try: | |
| # Test the new database path | |
| from app.services.database_service import DatabaseManager | |
| # Test with default path (should be /app/data/legal_dashboard.db) | |
| db = DatabaseManager() | |
| print("β Database manager initialized with default path") | |
| # Test if database directory exists | |
| db_dir = os.path.dirname(db.db_path) | |
| if os.path.exists(db_dir): | |
| print(f"β Database directory exists: {db_dir}") | |
| else: | |
| print(f"β Database directory missing: {db_dir}") | |
| return False | |
| # Test database connection | |
| if db.is_connected(): | |
| print("β Database connection successful") | |
| else: | |
| print("β Database connection failed") | |
| return False | |
| db.close() | |
| return True | |
| except Exception as e: | |
| print(f"β Database test failed: {e}") | |
| return False | |
| def test_cache_directory(): | |
| """Test Hugging Face cache directory setup""" | |
| print("\nπ Testing cache directory fixes...") | |
| try: | |
| # Check if cache directory is set | |
| cache_dir = os.environ.get("TRANSFORMERS_CACHE") | |
| if cache_dir: | |
| print(f"β TRANSFORMERS_CACHE set to: {cache_dir}") | |
| else: | |
| print("β TRANSFORMERS_CACHE not set") | |
| return False | |
| # Check if cache directory exists and is writable | |
| if os.path.exists(cache_dir): | |
| print(f"β Cache directory exists: {cache_dir}") | |
| else: | |
| print(f"β Cache directory missing: {cache_dir}") | |
| return False | |
| # Test write permissions | |
| test_file = os.path.join(cache_dir, "test_write.tmp") | |
| try: | |
| with open(test_file, 'w') as f: | |
| f.write("test") | |
| os.remove(test_file) | |
| print("β Cache directory is writable") | |
| except Exception as e: | |
| print(f"β Cache directory not writable: {e}") | |
| return False | |
| return True | |
| except Exception as e: | |
| print(f"β Cache test failed: {e}") | |
| return False | |
| def test_dockerfile_updates(): | |
| """Test Dockerfile changes""" | |
| print("\nπ Testing Dockerfile updates...") | |
| try: | |
| dockerfile_path = "Dockerfile" | |
| if not os.path.exists(dockerfile_path): | |
| print("β Dockerfile not found") | |
| return False | |
| with open(dockerfile_path, 'r') as f: | |
| content = f.read() | |
| # Check for directory creation | |
| if "mkdir -p /app/data /app/cache" in content: | |
| print("β Directory creation command found") | |
| else: | |
| print("β Directory creation command missing") | |
| return False | |
| # Check for permissions | |
| if "chmod -R 777 /app/data /app/cache" in content: | |
| print("β Permission setting command found") | |
| else: | |
| print("β Permission setting command missing") | |
| return False | |
| # Check for environment variables | |
| if "ENV TRANSFORMERS_CACHE=/app/cache" in content: | |
| print("β TRANSFORMERS_CACHE environment variable found") | |
| else: | |
| print("β TRANSFORMERS_CACHE environment variable missing") | |
| return False | |
| if "ENV HF_HOME=/app/cache" in content: | |
| print("β HF_HOME environment variable found") | |
| else: | |
| print("β HF_HOME environment variable missing") | |
| return False | |
| return True | |
| except Exception as e: | |
| print(f"β Dockerfile test failed: {e}") | |
| return False | |
| def test_main_py_updates(): | |
| """Test main.py updates""" | |
| print("\nπ Testing main.py updates...") | |
| try: | |
| main_py_path = "app/main.py" | |
| if not os.path.exists(main_py_path): | |
| print("β main.py not found") | |
| return False | |
| with open(main_py_path, 'r') as f: | |
| content = f.read() | |
| # Check for directory creation | |
| if "os.makedirs(\"/app/cache\", exist_ok=True)" in content: | |
| print("β Cache directory creation found") | |
| else: | |
| print("β Cache directory creation missing") | |
| return False | |
| if "os.makedirs(\"/app/data\", exist_ok=True)" in content: | |
| print("β Data directory creation found") | |
| else: | |
| print("β Data directory creation missing") | |
| return False | |
| # Check for environment variable setting | |
| if "os.environ[\"TRANSFORMERS_CACHE\"] = \"/app/cache\"" in content: | |
| print("β TRANSFORMERS_CACHE environment variable setting found") | |
| else: | |
| print("β TRANSFORMERS_CACHE environment variable setting missing") | |
| return False | |
| return True | |
| except Exception as e: | |
| print(f"β main.py test failed: {e}") | |
| return False | |
| def test_dockerignore_updates(): | |
| """Test .dockerignore updates""" | |
| print("\nπ Testing .dockerignore updates...") | |
| try: | |
| dockerignore_path = ".dockerignore" | |
| if not os.path.exists(dockerignore_path): | |
| print("β .dockerignore not found") | |
| return False | |
| with open(dockerignore_path, 'r') as f: | |
| content = f.read() | |
| # Check for cache exclusions | |
| if "cache/" in content: | |
| print("β Cache directory exclusion found") | |
| else: | |
| print("β Cache directory exclusion missing") | |
| return False | |
| if "/app/cache/" in content: | |
| print("β /app/cache exclusion found") | |
| else: | |
| print("β /app/cache exclusion missing") | |
| return False | |
| return True | |
| except Exception as e: | |
| print(f"β .dockerignore test failed: {e}") | |
| return False | |
| def main(): | |
| """Run all validation tests""" | |
| print("π Legal Dashboard OCR - Fix Validation") | |
| print("=" * 50) | |
| # Change to project directory | |
| project_dir = Path(__file__).parent | |
| os.chdir(project_dir) | |
| # Run tests | |
| tests = [ | |
| test_database_path, | |
| test_cache_directory, | |
| test_dockerfile_updates, | |
| test_main_py_updates, | |
| test_dockerignore_updates | |
| ] | |
| results = [] | |
| for test in tests: | |
| try: | |
| result = test() | |
| results.append(result) | |
| except Exception as e: | |
| print(f"β Test failed with exception: {e}") | |
| results.append(False) | |
| # Summary | |
| print("\n" + "=" * 50) | |
| print("π Validation Results Summary") | |
| print("=" * 50) | |
| passed = sum(results) | |
| total = len(results) | |
| print(f"β Passed: {passed}/{total}") | |
| print(f"β Failed: {total - passed}/{total}") | |
| if all(results): | |
| print("\nπ All fixes validated successfully!") | |
| print("\nβ Runtime errors should be resolved:") | |
| print(" β’ SQLite database path fixed") | |
| print(" β’ Hugging Face cache permissions fixed") | |
| print(" β’ Environment variables properly set") | |
| print(" β’ Docker container ready for deployment") | |
| return 0 | |
| else: | |
| print("\nβ οΈ Some fixes need attention. Please check the errors above.") | |
| return 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |