|
|
|
|
|
""" |
|
|
Test script to verify upload folder permissions and file operations. |
|
|
""" |
|
|
import os |
|
|
import tempfile |
|
|
from pathlib import Path |
|
|
|
|
|
print("Upload Folder Permission Test") |
|
|
|
|
|
|
|
|
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_folders = [ |
|
|
'uploads', |
|
|
'./uploads', |
|
|
'/tmp/uploads', |
|
|
'/tmp/cognichat_uploads' |
|
|
] |
|
|
|
|
|
print("\nTesting Upload Folder Options") |
|
|
|
|
|
for folder in test_folders: |
|
|
print(f"\nTesting: {folder}") |
|
|
|
|
|
try: |
|
|
|
|
|
os.makedirs(folder, exist_ok=True) |
|
|
print(f"Directory created/exists") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
with open(test_file, 'r') as f: |
|
|
content = f.read() |
|
|
print(f"Read permission verified") |
|
|
|
|
|
|
|
|
os.remove(test_file) |
|
|
print(f" File deletion works") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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}'") |
|
|
|
|
|
|
|
|
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") |