Spaces:
Paused
Paused
File size: 7,904 Bytes
77aec31 |
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
#!/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())
|