cryogenic22's picture
Create utils/storage_init.py
d9e0aef verified
# utils/storage_init.py
import os
import streamlit as st
from pathlib import Path
def initialize_storage_structure():
"""Initialize the storage structure and verify its existence."""
# Check if /data exists (Hugging Face persistent storage)
if os.path.exists('/data'):
base_path = Path('/data')
else:
# Fallback to local data directory in project
base_path = Path(os.getcwd()) / 'data'
try:
# Create main directories
directories = [
base_path / 'database',
base_path / 'files',
base_path / 'vectorstore',
base_path / 'metadata'
]
# Create each directory
for directory in directories:
directory.mkdir(parents=True, exist_ok=True)
# Print debug information
st.write("Storage directories initialized at:", str(base_path))
st.write("Available directories:")
for directory in directories:
st.write(f"- {directory}: {'Exists' if directory.exists() else 'Failed to create'}")
# Try to create a test file to verify write permissions
test_file = base_path / 'database' / '.test_write'
try:
test_file.touch()
test_file.unlink() # Remove the test file
st.success("Storage system initialized successfully!")
except Exception as e:
st.error(f"Write permission test failed: {e}")
return base_path
except Exception as e:
st.error(f"Error initializing storage structure: {e}")
return None
def verify_storage_access(base_path: Path):
"""Verify storage access and permissions."""
try:
# Check directory existence and permissions
checks = {
'exists': base_path.exists(),
'is_dir': base_path.is_dir(),
'readable': os.access(base_path, os.R_OK),
'writable': os.access(base_path, os.W_OK),
'executable': os.access(base_path, os.X_OK)
}
# Print debug information
st.write("Storage Access Verification:")
for check, result in checks.items():
st.write(f"- {check}: {'✅' if result else '❌'}")
# Return overall status
return all(checks.values())
except Exception as e:
st.error(f"Error verifying storage access: {e}")
return False