Create utils/storage_init.py
Browse files- utils/storage_init.py +72 -0
utils/storage_init.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils/storage_init.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
def initialize_storage_structure():
|
| 8 |
+
"""Initialize the storage structure and verify its existence."""
|
| 9 |
+
# Check if /data exists (Hugging Face persistent storage)
|
| 10 |
+
if os.path.exists('/data'):
|
| 11 |
+
base_path = Path('/data')
|
| 12 |
+
else:
|
| 13 |
+
# Fallback to local data directory in project
|
| 14 |
+
base_path = Path(os.getcwd()) / 'data'
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
# Create main directories
|
| 18 |
+
directories = [
|
| 19 |
+
base_path / 'database',
|
| 20 |
+
base_path / 'files',
|
| 21 |
+
base_path / 'vectorstore',
|
| 22 |
+
base_path / 'metadata'
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
# Create each directory
|
| 26 |
+
for directory in directories:
|
| 27 |
+
directory.mkdir(parents=True, exist_ok=True)
|
| 28 |
+
|
| 29 |
+
# Print debug information
|
| 30 |
+
st.write("Storage directories initialized at:", str(base_path))
|
| 31 |
+
st.write("Available directories:")
|
| 32 |
+
for directory in directories:
|
| 33 |
+
st.write(f"- {directory}: {'Exists' if directory.exists() else 'Failed to create'}")
|
| 34 |
+
|
| 35 |
+
# Try to create a test file to verify write permissions
|
| 36 |
+
test_file = base_path / 'database' / '.test_write'
|
| 37 |
+
try:
|
| 38 |
+
test_file.touch()
|
| 39 |
+
test_file.unlink() # Remove the test file
|
| 40 |
+
st.success("Storage system initialized successfully!")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
st.error(f"Write permission test failed: {e}")
|
| 43 |
+
|
| 44 |
+
return base_path
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
st.error(f"Error initializing storage structure: {e}")
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
def verify_storage_access(base_path: Path):
|
| 51 |
+
"""Verify storage access and permissions."""
|
| 52 |
+
try:
|
| 53 |
+
# Check directory existence and permissions
|
| 54 |
+
checks = {
|
| 55 |
+
'exists': base_path.exists(),
|
| 56 |
+
'is_dir': base_path.is_dir(),
|
| 57 |
+
'readable': os.access(base_path, os.R_OK),
|
| 58 |
+
'writable': os.access(base_path, os.W_OK),
|
| 59 |
+
'executable': os.access(base_path, os.X_OK)
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
# Print debug information
|
| 63 |
+
st.write("Storage Access Verification:")
|
| 64 |
+
for check, result in checks.items():
|
| 65 |
+
st.write(f"- {check}: {'✅' if result else '❌'}")
|
| 66 |
+
|
| 67 |
+
# Return overall status
|
| 68 |
+
return all(checks.values())
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
st.error(f"Error verifying storage access: {e}")
|
| 72 |
+
return False
|