Cogni-Chat-document-reader-v2 / test_upload_permissions.py
riteshraut
fix/new update
becc8f7
raw
history blame
2.53 kB
"""
Test script to verify upload folder permissions and file operations.
"""
import os
import tempfile
from pathlib import Path
print("Upload Folder Permission Test")
# Detect environment
is_hf_spaces = bool(os.getenv("SPACE_ID") or os.getenv("SPACES_ZERO_GPU"))
print(f"Environment: {'Hugging Face Spaces' if is_hf_spaces else 'Local Development'}")
# Test different upload folder configurations
test_folders = [
'uploads', # Relative path (will fail in HF Spaces)
'./uploads', # Current directory relative path
'/tmp/uploads', # Recommended for HF Spaces
'/tmp/cognichat_uploads' # Alternative temp location
]
print("\nTesting Upload Folder Options")
for folder in test_folders:
print(f"\nTesting: {folder}")
try:
# Try to create the directory
os.makedirs(folder, exist_ok=True)
print(f"Directory created/exists")
# Test write permissions
test_file = os.path.join(folder, 'test_write.txt')
with open(test_file, 'w') as f:
f.write('test content')
print(f"Write permission verified")
# Test read permissions
with open(test_file, 'r') as f:
content = f.read()
print(f"Read permission verified")
# Clean up test file
os.remove(test_file)
print(f" File deletion works")
# Get absolute path
abs_path = os.path.abspath(folder)
print(f"Full path: {abs_path}")
print(f"Writable: {os.access(folder, os.W_OK)}")
except PermissionError as e:
print(f"Permission denied: {e}")
except Exception as e:
print(f"Error: {e}")
# Recommended configuration
print(f"\nRecommended Configuration")
if is_hf_spaces:
recommended_folder = '/tmp/uploads'
print(f"For Hugging Face Spaces: {recommended_folder}")
else:
recommended_folder = 'uploads'
print(f"For local development: {recommended_folder}")
print(f"\nUse this in your Flask app:")
print(f"app.config['UPLOAD_FOLDER'] = '{recommended_folder}'")
# Test the current working directory permissions
print(f"\nCurrent Directory Info")
cwd = os.getcwd()
print(f"Current working directory: {cwd}")
print(f"CWD is writable: {os.access(cwd, os.W_OK)}")
print(f"\nPath Environment Variables")
path_vars = ['HOME', 'TMPDIR', 'TEMP', 'TMP', 'SPACE_ID', 'SPACES_ZERO_GPU']
for var in path_vars:
value = os.getenv(var)
if value:
print(f"{var}: {value}")
print(f"\nTest Complete")