File size: 2,526 Bytes
becc8f7 |
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 |
"""
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") |