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())