Spaces:
Building
Building
| """ | |
| Diagnostic script to check runtime permissions in Hugging Face Spaces | |
| Run this in your Space to understand the runtime environment | |
| """ | |
| import os | |
| import sys | |
| import tempfile | |
| import streamlit as st | |
| from pathlib import Path | |
| import subprocess | |
| import pwd | |
| import grp | |
| def check_runtime_permissions(): | |
| """Check various runtime permissions and environment details.""" | |
| st.title("π Runtime Permissions Checker") | |
| # User and process info | |
| st.header("1. User & Process Information") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.write("**Process Info:**") | |
| st.code(f"UID: {os.getuid()}") | |
| st.code(f"GID: {os.getgid()}") | |
| st.code(f"PID: {os.getpid()}") | |
| try: | |
| user_info = pwd.getpwuid(os.getuid()) | |
| st.code(f"Username: {user_info.pw_name}") | |
| st.code(f"Home Dir: {user_info.pw_dir}") | |
| except: | |
| st.error("Could not get user info") | |
| with col2: | |
| st.write("**Groups:**") | |
| try: | |
| groups = [grp.getgrgid(g).gr_name for g in os.getgroups()] | |
| st.code("Groups: " + ", ".join(groups)) | |
| except: | |
| st.code(f"Group IDs: {os.getgroups()}") | |
| # Environment variables | |
| st.header("2. Environment Variables") | |
| with st.expander("Show relevant environment variables"): | |
| env_vars = { | |
| "HOME": os.environ.get("HOME", "Not set"), | |
| "USER": os.environ.get("USER", "Not set"), | |
| "PWD": os.environ.get("PWD", "Not set"), | |
| "TMPDIR": os.environ.get("TMPDIR", "Not set"), | |
| "TEMP": os.environ.get("TEMP", "Not set"), | |
| "TMP": os.environ.get("TMP", "Not set"), | |
| "STREAMLIT_SERVER_PORT": os.environ.get("STREAMLIT_SERVER_PORT", "Not set"), | |
| "STREAMLIT_SERVER_ADDRESS": os.environ.get("STREAMLIT_SERVER_ADDRESS", "Not set"), | |
| } | |
| for key, value in env_vars.items(): | |
| st.code(f"{key}: {value}") | |
| # Directory permissions | |
| st.header("3. Directory Permissions") | |
| directories_to_check = [ | |
| "/tmp", | |
| "/app", | |
| ".", | |
| os.path.expanduser("~"), | |
| "/var/tmp", | |
| tempfile.gettempdir(), | |
| ] | |
| for dir_path in directories_to_check: | |
| if os.path.exists(dir_path): | |
| try: | |
| stat_info = os.stat(dir_path) | |
| permissions = oct(stat_info.st_mode)[-3:] | |
| # Test write access | |
| can_write = os.access(dir_path, os.W_OK) | |
| can_read = os.access(dir_path, os.R_OK) | |
| can_execute = os.access(dir_path, os.X_OK) | |
| st.write(f"**{dir_path}**") | |
| col1, col2, col3 = st.columns(3) | |
| with col1: | |
| st.code(f"Permissions: {permissions}") | |
| with col2: | |
| st.code(f"Read: {'β ' if can_read else 'β'}") | |
| st.code(f"Write: {'β ' if can_write else 'β'}") | |
| with col3: | |
| st.code(f"Execute: {'β ' if can_execute else 'β'}") | |
| # Try to create a test file | |
| if can_write: | |
| try: | |
| test_file = os.path.join(dir_path, "test_write_permission.txt") | |
| with open(test_file, 'w') as f: | |
| f.write("test") | |
| os.remove(test_file) | |
| st.success(f"β Can create/delete files in {dir_path}") | |
| except Exception as e: | |
| st.error(f"β Cannot create files in {dir_path}: {e}") | |
| except Exception as e: | |
| st.error(f"Cannot check {dir_path}: {e}") | |
| else: | |
| st.warning(f"{dir_path} does not exist") | |
| # File upload test | |
| st.header("4. File Upload Test") | |
| uploaded_file = st.file_uploader("Test file upload", type=['txt', 'csv', 'tsv']) | |
| if uploaded_file: | |
| st.write("**File Info:**") | |
| st.code(f"Name: {uploaded_file.name}") | |
| st.code(f"Type: {uploaded_file.type}") | |
| st.code(f"Size: {uploaded_file.size} bytes") | |
| # Test different read methods | |
| st.write("**Read Methods:**") | |
| # Method 1: Direct read | |
| try: | |
| content = uploaded_file.read() | |
| st.success(f"β Direct read: {len(content)} bytes") | |
| uploaded_file.seek(0) | |
| except Exception as e: | |
| st.error(f"β Direct read failed: {e}") | |
| # Method 2: getbuffer | |
| try: | |
| buffer = uploaded_file.getbuffer() | |
| st.success(f"β getbuffer: {len(buffer)} bytes") | |
| except Exception as e: | |
| st.error(f"β getbuffer failed: {e}") | |
| # Method 3: Save to temp | |
| try: | |
| temp_path = os.path.join(tempfile.gettempdir(), f"test_{uploaded_file.name}") | |
| with open(temp_path, 'wb') as f: | |
| f.write(uploaded_file.getbuffer()) | |
| st.success(f"β Saved to: {temp_path}") | |
| # Check if file exists and is readable | |
| if os.path.exists(temp_path): | |
| file_size = os.path.getsize(temp_path) | |
| st.code(f"File size on disk: {file_size} bytes") | |
| os.remove(temp_path) | |
| st.success("β File deleted successfully") | |
| except Exception as e: | |
| st.error(f"β Save to temp failed: {e}") | |
| # System limits | |
| st.header("5. System Limits") | |
| try: | |
| # Check ulimit | |
| result = subprocess.run(['ulimit', '-a'], | |
| shell=True, | |
| capture_output=True, | |
| text=True) | |
| if result.returncode == 0: | |
| st.code(result.stdout) | |
| else: | |
| st.error("Could not get system limits") | |
| except: | |
| st.warning("ulimit command not available") | |
| # Disk space | |
| st.header("6. Disk Space") | |
| try: | |
| result = subprocess.run(['df', '-h', '/tmp', '/app'], | |
| capture_output=True, | |
| text=True) | |
| if result.returncode == 0: | |
| st.code(result.stdout) | |
| except: | |
| st.warning("df command not available") | |
| if __name__ == "__main__": | |
| check_runtime_permissions() |