Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /ParisNeo_lollms /backend /routers /admin /system_management.py
| # backend/routers/admin/system_management.py | |
| import sys | |
| import os | |
| import asyncio | |
| import statistics | |
| import subprocess | |
| from collections import defaultdict | |
| from datetime import datetime, timedelta, timezone | |
| from pathlib import Path | |
| from typing import List, Optional, Dict, Any | |
| import psutil | |
| from fastapi import APIRouter, Depends, Request, HTTPException | |
| from pydantic import BaseModel | |
| from sqlalchemy import func, desc, update | |
| from lollms_client import LollmsDataManager | |
| from packaging.requirements import Requirement | |
| from importlib.metadata import version as get_installed_version, PackageNotFoundError | |
| from packaging.version import parse as parse_version | |
| from sqlalchemy.orm import Session, joinedload, defer | |
| from backend.db import get_db | |
| from backend.db.models.user import User as DBUser | |
| from backend.db.models.service import App as DBApp | |
| from backend.db.models.connections import WebSocketConnection | |
| from backend.db.models.db_task import DBTask | |
| from backend.models import UserAuthDetails, SystemUsageStats, GPUInfo, DiskInfo, TaskInfo | |
| from backend.models.admin import GlobalGenerationStats, UserActivityStat, ForceGlobalConfigPayload, RequirementInfo, InstallReqPayload | |
| from backend.config import PROJECT_ROOT, APP_DATA_DIR, SERVER_CONFIG, APP_VERSION, USERS_DIR_NAME, TEMP_UPLOADS_DIR_NAME | |
| from backend.session import get_current_admin_user, get_user_data_root, user_sessions | |
| from backend.ws_manager import manager | |
| from backend.task_manager import task_manager, Task | |
| from backend.utils import get_local_ip_addresses | |
| from backend.tasks.system_tasks import _create_backup_task, _analyze_logs_task, _prune_old_tasks_task | |
| from backend.settings import settings | |
| from ascii_colors import trace_exception, ASCIIColors | |
| system_management_router = APIRouter() | |
| class AdminBroadcastRequest(BaseModel): | |
| message: str | |
| class BackupRequest(BaseModel): | |
| password: str | |
| class ConnectedUser(BaseModel): | |
| id: int | |
| username: str | |
| icon: Optional[str] = None | |
| class Config: | |
| from_attributes = True | |
| class AppPortInfo(BaseModel): | |
| port: int | |
| app_name: str | |
| app_id: str | |
| class ServerInfo(BaseModel): | |
| lollms_version: str | |
| python_version: str | |
| host: str | |
| port: int | |
| https_enabled: bool | |
| url: str | |
| active_ports: List[AppPortInfo] | |
| cors_origins: List[str] | |
| local_ips: Optional[List[str]] = None | |
| class GPUProcessInfo(BaseModel): | |
| pid: int | |
| name: str | |
| memory_used: float | |
| class ExpandedGPUInfo(GPUInfo): | |
| gpu_utilization: int = 0 | |
| processes: List[GPUProcessInfo] = [] | |
| class ExpandedSystemUsageStats(SystemUsageStats): | |
| gpus: List[ExpandedGPUInfo] | |
| class KillProcessRequest(BaseModel): | |
| pid: int | |
| class ModelUsageStat(BaseModel): | |
| model_name: str | |
| count: int | |
| class LogEntry(BaseModel): | |
| task_id: str | |
| task_name: str | |
| timestamp: str | |
| level: str | |
| message: str | |
| async def force_all_users_config(payload: ForceGlobalConfigPayload, db: Session = Depends(get_db)): | |
| """ | |
| Instantly updates all users in the database to use specific models for various services. | |
| Also clears active server-side sessions to enforce the change immediately on the next request. | |
| """ | |
| try: | |
| update_data = payload.model_dump(exclude_unset=True) | |
| if not update_data: | |
| raise HTTPException(status_code=400, detail="No configuration provided to force.") | |
| # Batch update all users | |
| stmt = update(DBUser).values(**update_data) | |
| result = db.execute(stmt) | |
| db.commit() | |
| # Clear all in-memory user sessions to force client re-initialization | |
| user_sessions.clear() | |
| # Notify all connected clients that their configuration has changed | |
| await manager.broadcast({"type": "settings_updated", "data": {"reason": "global_force_update"}}) | |
| return {"message": f"Successfully forced configuration on {result.rowcount} users."} | |
| except Exception as e: | |
| db.rollback() | |
| trace_exception(e) | |
| raise HTTPException(status_code=500, detail=f"Failed to apply global configuration: {str(e)}") | |
| def _purge_unused_temp_files_task(task: Task): | |
| task.log("Starting purge of unused temporary files older than 24 hours.") | |
| deleted_count, total_scanned = 0, 0 | |
| retention_period = timedelta(hours=24) | |
| now = datetime.now(timezone.utc) | |
| users_root_path = APP_DATA_DIR / USERS_DIR_NAME | |
| if not users_root_path.exists(): | |
| task.set_progress(100) | |
| return {"message": "Users directory not found."} | |
| all_user_dirs = [d for d in users_root_path.iterdir() if d.is_dir()] | |
| if not all_user_dirs: | |
| task.set_progress(100) | |
| return {"message": "No user directories found."} | |
| for i, user_dir in enumerate(all_user_dirs): | |
| if task.cancellation_event.is_set(): | |
| task.log("Purge task cancelled.", level="WARNING") | |
| break | |
| temp_uploads_path = user_dir / TEMP_UPLOADS_DIR_NAME | |
| if temp_uploads_path.exists(): | |
| for file_path in temp_uploads_path.iterdir(): | |
| if file_path.is_file(): | |
| total_scanned += 1 | |
| try: | |
| file_mtime = datetime.fromtimestamp(file_path.stat().st_mtime, timezone.utc) | |
| if (now - file_mtime) > retention_period: | |
| file_path.unlink() | |
| deleted_count += 1 | |
| task.log(f"Deleted old file: {file_path.name}") | |
| except Exception as e: | |
| task.log(f"Error with file {file_path.name}: {e}", level="ERROR") | |
| task.set_progress(5 + int(90 * (i + 1) / len(all_user_dirs))) | |
| task.set_progress(100) | |
| return {"message": f"Purge complete. Scanned {total_scanned} files, deleted {deleted_count} files."} | |
| async def get_system_status(): | |
| ram = psutil.virtual_memory() | |
| disks_info = [] | |
| try: | |
| partitions = psutil.disk_partitions(all=False) | |
| def find_mount(path, parts): | |
| abs_path = Path(path).resolve() | |
| return next((p.mountpoint for p in sorted(parts, key=lambda p: len(p.mountpoint), reverse=True) if str(abs_path).startswith(p.mountpoint)), None) | |
| app_mount = find_mount(PROJECT_ROOT, partitions) | |
| data_mount = find_mount(APP_DATA_DIR, partitions) | |
| for part in partitions: | |
| try: | |
| usage = psutil.disk_usage(part.mountpoint) | |
| disks_info.append(DiskInfo( | |
| mount_point=part.mountpoint, total_gb=usage.total / (1024**3), | |
| used_gb=usage.used / (1024**3), available_gb=usage.free / (1024**3), | |
| usage_percent=usage.percent, is_app_disk=(part.mountpoint == app_mount), | |
| is_data_disk=(part.mountpoint == data_mount) | |
| )) | |
| except OSError: continue | |
| except Exception: pass | |
| gpus_info = [] | |
| try: | |
| from pynvml import ( | |
| nvmlInit, nvmlShutdown, nvmlDeviceGetCount, nvmlDeviceGetHandleByIndex, | |
| nvmlDeviceGetMemoryInfo, nvmlDeviceGetName, nvmlDeviceGetComputeRunningProcesses, | |
| nvmlDeviceGetGraphicsRunningProcesses, nvmlDeviceGetUtilizationRates | |
| ) | |
| nvmlInit() | |
| count = nvmlDeviceGetCount() | |
| for i in range(count): | |
| handle = nvmlDeviceGetHandleByIndex(i) | |
| name = nvmlDeviceGetName(handle) | |
| gpu_name = name.decode("utf-8") if isinstance(name, (bytes, bytearray)) else str(name) | |
| mem = nvmlDeviceGetMemoryInfo(handle) | |
| total_gb = mem.total / (1024 ** 3) | |
| used_gb = mem.used / (1024 ** 3) | |
| usage_pct = (mem.used / mem.total) * 100 if mem.total > 0 else 0.0 | |
| gpu_util = 0 | |
| try: | |
| util = nvmlDeviceGetUtilizationRates(handle) | |
| gpu_util = util.gpu | |
| except Exception: | |
| pass | |
| procs = [] | |
| try: | |
| compute_procs = nvmlDeviceGetComputeRunningProcesses(handle) | |
| graphics_procs = nvmlDeviceGetGraphicsRunningProcesses(handle) | |
| all_nvml_procs = compute_procs + graphics_procs | |
| for p in all_nvml_procs: | |
| try: | |
| sys_proc = psutil.Process(p.pid) | |
| proc_name = sys_proc.name() | |
| except psutil.NoSuchProcess: | |
| proc_name = "Unknown/Terminated" | |
| mem_used = p.usedGpuMemory / (1024**2) if p.usedGpuMemory else 0 | |
| procs.append(GPUProcessInfo(pid=p.pid, name=proc_name, memory_used=mem_used)) | |
| except Exception: | |
| pass | |
| gpus_info.append(ExpandedGPUInfo( | |
| id=i, | |
| name=gpu_name, | |
| vram_total_gb=total_gb, | |
| vram_used_gb=used_gb, | |
| vram_usage_percent=usage_pct, | |
| gpu_utilization=gpu_util, | |
| processes=procs | |
| )) | |
| nvmlShutdown() | |
| except (ImportError, Exception): pass | |
| return ExpandedSystemUsageStats( | |
| cpu_ram_total_gb=ram.total / (1024**3), cpu_ram_used_gb=ram.used / (1024**3), | |
| cpu_ram_available_gb=ram.available / (1024**3), cpu_ram_usage_percent=ram.percent, | |
| disks=disks_info, gpus=gpus_info, | |
| ) | |
| async def analyze_logs(current_user: UserAuthDetails = Depends(get_current_admin_user)): | |
| db_task = task_manager.submit_task( | |
| name="Analyze System Logs", | |
| target=_analyze_logs_task, | |
| args=(current_user.username,), | |
| description="Analyzing recent task logs and system events for issues.", | |
| owner_username=current_user.username | |
| ) | |
| return db_task | |
| async def get_system_logs(limit: int = 200, db: Session = Depends(get_db), current_user: UserAuthDetails = Depends(get_current_admin_user)): | |
| recent_tasks = db.query(DBTask)\ | |
| .options(defer(DBTask.result), defer(DBTask.error))\ | |
| .order_by(desc(DBTask.updated_at))\ | |
| .limit(30)\ | |
| .all() | |
| all_logs = [] | |
| count = 0 | |
| for t in recent_tasks: | |
| if t.logs: | |
| for l in reversed(t.logs): | |
| all_logs.append(LogEntry( | |
| task_id=t.id, | |
| task_name=t.name, | |
| timestamp=l.get('timestamp') or datetime.now().isoformat(), | |
| level=l.get('level', 'INFO'), | |
| message=l.get('message', '') | |
| )) | |
| count += 1 | |
| if count >= limit: | |
| break | |
| if count >= limit: | |
| break | |
| all_logs.sort(key=lambda x: x.timestamp, reverse=True) | |
| return all_logs[:limit] | |
| async def kill_process(payload: KillProcessRequest, current_user: UserAuthDetails = Depends(get_current_admin_user)): | |
| try: | |
| proc = psutil.Process(payload.pid) | |
| proc.kill() | |
| return {"message": f"Process {payload.pid} killed."} | |
| except psutil.NoSuchProcess: | |
| raise HTTPException(status_code=404, detail="Process not found.") | |
| except psutil.AccessDenied: | |
| raise HTTPException(status_code=403, detail="Permission denied to kill process.") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def get_model_usage_stats(db: Session = Depends(get_db)): | |
| results = db.query(DBUser.lollms_model_name, func.count(DBUser.id)).group_by(DBUser.lollms_model_name).all() | |
| stats = [] | |
| for model_name, count in results: | |
| stats.append(ModelUsageStat(model_name=model_name or "Not Set", count=count)) | |
| return sorted(stats, key=lambda x: x.count, reverse=True) | |
| async def get_server_info(request: Request, db: Session = Depends(get_db)): | |
| running_apps = db.query(DBApp).filter(DBApp.status == 'running', DBApp.port != None).all() | |
| active_ports = [AppPortInfo(port=app.port, app_name=app.name, app_id=app.id) for app in running_apps] | |
| allowed_origins = next((m.options.get('allow_origins', []) for m in request.app.user_middleware if "CORSMiddleware" in str(m)), []) | |
| host = settings.get("host", SERVER_CONFIG.get("host", "localhost")) | |
| port = settings.get("port", SERVER_CONFIG.get("port", 9642)) | |
| https = settings.get("https_enabled", SERVER_CONFIG.get("https_enabled", False)) | |
| protocol = "https" if https else "http" | |
| local_ips = None | |
| if host == "0.0.0.0": | |
| local_ips = get_local_ip_addresses() | |
| display_host = local_ips[0] if local_ips else "localhost" | |
| else: | |
| display_host = host | |
| url = f"{protocol}://{display_host}:{port}" | |
| return ServerInfo( | |
| lollms_version=APP_VERSION, python_version=sys.version, | |
| host=host, port=port, | |
| https_enabled=https, | |
| url=url, | |
| active_ports=active_ports, cors_origins=allowed_origins, | |
| local_ips=local_ips | |
| ) | |
| async def broadcast_message_to_all_users(payload: AdminBroadcastRequest, current_user: UserAuthDetails = Depends(get_current_admin_user)): | |
| await asyncio.sleep(0.1) | |
| await manager.broadcast({"type": "admin_broadcast", "data": {"message": payload.message, "sender": current_user.username}}) | |
| return {"message": "Broadcast sent."} | |
| async def get_websocket_connections(db: Session = Depends(get_db)): | |
| user_ids = [uid for uid, in db.query(WebSocketConnection.user_id).distinct().all()] | |
| return db.query(DBUser).filter(DBUser.id.in_(user_ids)).all() if user_ids else [] | |
| async def get_my_websocket_status(current_user: UserAuthDetails = Depends(get_current_admin_user)): | |
| return { | |
| "is_connected": current_user.id in manager.active_connections, | |
| "is_registered_as_admin": current_user.id in manager.admin_user_ids | |
| } | |
| async def purge_temp_files(current_admin: UserAuthDetails = Depends(get_current_admin_user)): | |
| db_task = task_manager.submit_task( | |
| name="Purge unused temporary files", | |
| target=_purge_unused_temp_files_task, | |
| description="Scans user temp folders and deletes files older than 24 hours.", | |
| owner_username=current_admin.username | |
| ) | |
| return db_task | |
| def get_global_generation_stats(db: Session = Depends(get_db)): | |
| all_users = db.query(DBUser).all() | |
| thirty_days_ago = datetime.now(timezone.utc) - timedelta(days=30) | |
| daily_totals = defaultdict(int) | |
| for user in all_users: | |
| discussions_db_path = get_user_data_root(user.username) / "discussions.db" | |
| if discussions_db_path.exists(): | |
| try: | |
| dm = LollmsDataManager(db_path=f"sqlite:///{discussions_db_path.resolve()}") | |
| with dm.get_session() as session: | |
| results = session.query( | |
| func.date(dm.MessageModel.created_at), | |
| func.count(dm.MessageModel.id) | |
| ).filter( | |
| dm.MessageModel.sender_type == 'assistant', | |
| dm.MessageModel.created_at >= thirty_days_ago | |
| ).group_by(func.date(dm.MessageModel.created_at)).all() | |
| for date_str, count in results: | |
| daily_totals[date_str] += count | |
| except Exception as e: | |
| trace_exception(e) | |
| print(f"Warning: Could not process discussions DB for user {user.username}: {e}") | |
| generations_per_day_list = [ | |
| UserActivityStat(date=datetime.strptime(date_str, '%Y-%m-%d').date(), count=count) | |
| for date_str, count in daily_totals.items() | |
| ] | |
| generations_per_day_list.sort(key=lambda x: x.date) | |
| weekday_data = defaultdict(list) | |
| for stat in generations_per_day_list: | |
| weekday_data[stat.date.weekday()].append(stat.count) | |
| weekday_names = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] | |
| mean_per_weekday = {} | |
| variance_per_weekday = {} | |
| for i, day_name in enumerate(weekday_names): | |
| counts = weekday_data.get(i, []) | |
| if len(counts) > 0: | |
| mean_per_weekday[day_name] = statistics.mean(counts) | |
| variance_per_weekday[day_name] = statistics.variance(counts) if len(counts) > 1 else 0.0 | |
| else: | |
| mean_per_weekday[day_name] = 0.0 | |
| variance_per_weekday[day_name] = 0.0 | |
| return GlobalGenerationStats( | |
| generations_per_day=generations_per_day_list, | |
| mean_per_weekday=mean_per_weekday, | |
| variance_per_weekday=variance_per_weekday | |
| ) | |
| async def create_backup( | |
| request: BackupRequest, | |
| current_admin: UserAuthDetails = Depends(get_current_admin_user) | |
| ): | |
| db_task = task_manager.submit_task( | |
| name="Create Application Backup", | |
| target=_create_backup_task, | |
| args=(request.password,), | |
| description="Creating a secure password-protected zip archive of the entire application.", | |
| owner_username=current_admin.username | |
| ) | |
| return db_task | |
| async def trigger_manual_task_pruning(current_admin: UserAuthDetails = Depends(get_current_admin_user)): | |
| """Manually triggers the background task to delete finished tasks older than configured retention.""" | |
| db_task = task_manager.submit_task( | |
| name="Manual Task Pruning", | |
| target=_prune_old_tasks_task, | |
| description="Cleaning up old finished background tasks.", | |
| owner_username=current_admin.username | |
| ) | |
| return db_task | |
| async def get_system_requirements(current_user: UserAuthDetails = Depends(get_current_admin_user)): | |
| requirements_path = PROJECT_ROOT / "requirements.txt" | |
| if not requirements_path.exists(): | |
| raise HTTPException(status_code=404, detail="requirements.txt not found") | |
| results = [] | |
| with open(requirements_path, 'r') as f: | |
| for line in f: | |
| line = line.strip() | |
| if not line or line.startswith('#'): | |
| continue | |
| try: | |
| req = Requirement(line) | |
| name = req.name | |
| # Extract required version from specs if == is present | |
| required_version = None | |
| for spec in req.specifier: | |
| if spec.operator == '==': | |
| required_version = spec.version | |
| break | |
| installed_version = None | |
| try: | |
| installed_version = get_installed_version(name) | |
| except PackageNotFoundError: | |
| pass | |
| status = 'missing' | |
| if installed_version: | |
| if not required_version: | |
| status = 'ok' # No specific version required | |
| else: | |
| inst_v = parse_version(installed_version) | |
| req_v = parse_version(required_version) | |
| if inst_v == req_v: | |
| status = 'ok' | |
| elif inst_v > req_v: | |
| status = 'newer' | |
| else: | |
| status = 'older' | |
| results.append(RequirementInfo( | |
| name=name, | |
| required_version=required_version or "Any", | |
| installed_version=installed_version or "Not Installed", | |
| status=status | |
| )) | |
| except Exception as e: | |
| print(f"Error parsing requirement line '{line}': {e}") | |
| continue | |
| return results | |
| async def install_requirement(payload: InstallReqPayload, current_user: UserAuthDetails = Depends(get_current_admin_user)): | |
| def _install_task(task: Task, pkg_name: str, pkg_version: str): | |
| task.log(f"Installing {pkg_name}...") | |
| cmd = [sys.executable, "-m", "pip", "install"] | |
| if pkg_version: | |
| cmd.append(f"{pkg_name}=={pkg_version}") | |
| else: | |
| cmd.append("--upgrade") | |
| cmd.append(pkg_name) | |
| process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) | |
| task.process = process | |
| for line in process.stdout: | |
| task.log(line.strip()) | |
| process.wait() | |
| if process.returncode == 0: | |
| task.log(f"Successfully installed {pkg_name}") | |
| return {"message": "Installation successful"} | |
| else: | |
| raise Exception(f"Installation failed with code {process.returncode}") | |
| target_version = payload.version | |
| description = f"Installing {payload.name}" | |
| if target_version: | |
| description += f" version {target_version}" | |
| else: | |
| description += " (Latest)" | |
| return task_manager.submit_task( | |
| name=f"Install Requirement: {payload.name}", | |
| target=_install_task, | |
| args=(payload.name, target_version), | |
| description=description, | |
| owner_username=current_user.username | |
| ) | |
| # --- Update and Reboot Endpoints (Service Compatible) --- | |
| async def reboot_server(current_admin: UserAuthDetails = Depends(get_current_admin_user)): | |
| """ | |
| Signals the wrapper script (run.sh or run_windows.bat) to restart the application. | |
| This works by creating a 'restart' flag if needed, or simply exiting the process. | |
| The wrapper script loop will detect the exit and restart the process. | |
| """ | |
| try: | |
| ASCIIColors.yellow("Initiating Server Reboot (Service/Loop Mode)...") | |
| # We don't strictly need a file for a simple restart if the loop is infinite, | |
| # but the flag is good practice to distinguish intentional restarts from crashes. | |
| # However, to keep it compatible with simple loops, just exiting 0 is often enough. | |
| def delayed_exit(): | |
| import time | |
| time.sleep(1) | |
| ASCIIColors.red("Exiting process for reboot.") | |
| # Exit with code 0 to indicate clean shutdown to wrapper | |
| os._exit(0) | |
| import threading | |
| threading.Thread(target=delayed_exit, daemon=True).start() | |
| return {"message": "Server is rebooting..."} | |
| except Exception as e: | |
| trace_exception(e) | |
| raise HTTPException(status_code=500, detail=f"Failed to initiate reboot: {str(e)}") | |
| async def update_server(current_admin: UserAuthDetails = Depends(get_current_admin_user)): | |
| """ | |
| Signals the wrapper script to perform an update before restarting. | |
| Creates an 'update_request.flag' file in the root directory. | |
| The run.sh/run_windows.bat script checks for this file on loop iteration. | |
| """ | |
| try: | |
| ASCIIColors.yellow("Initiating Server Update (Service/Loop Mode)...") | |
| # Create flag file | |
| flag_path = PROJECT_ROOT / "update_request.flag" | |
| with open(flag_path, 'w') as f: | |
| f.write("update_requested") | |
| def delayed_exit(): | |
| import time | |
| time.sleep(1) | |
| ASCIIColors.red("Exiting process for update sequence.") | |
| os._exit(0) # Exit to let wrapper handle the update | |
| import threading | |
| threading.Thread(target=delayed_exit, daemon=True).start() | |
| return {"message": "Server is restarting to apply updates."} | |
| except Exception as e: | |
| trace_exception(e) | |
| raise HTTPException(status_code=500, detail=f"Failed to initiate update: {str(e)}") | |
| async def fix_all_requirements(current_user: UserAuthDetails = Depends(get_current_admin_user)): | |
| """ | |
| Installs the correct version for all requirements that are not 'ok'. | |
| Returns a task that can be monitored for progress. | |
| """ | |
| def _fix_all_task(task: Task): | |
| task.log("Starting to fix all requirements...") | |
| # Get requirements directly from the requirements.txt file | |
| requirements_path = PROJECT_ROOT / "requirements.txt" | |
| if not requirements_path.exists(): | |
| task.log("requirements.txt not found", level="ERROR") | |
| raise Exception("requirements.txt not found") | |
| # Parse requirements | |
| requirements_to_fix = [] | |
| with open(requirements_path, 'r') as f: | |
| for line in f: | |
| line = line.strip() | |
| if not line or line.startswith('#'): | |
| continue | |
| if line.startswith("-e") or "://" in line: | |
| continue | |
| try: | |
| req = Requirement(line) | |
| name = req.name | |
| # Extract required version from specs if == is present | |
| required_version = None | |
| for spec in req.specifier: | |
| if spec.operator == '==': | |
| required_version = spec.version | |
| break | |
| installed_version = None | |
| try: | |
| installed_version = get_installed_version(name) | |
| except PackageNotFoundError: | |
| pass | |
| status = 'missing' | |
| if installed_version: | |
| if not required_version: | |
| status = 'ok' # No specific version required | |
| else: | |
| inst_v = parse_version(installed_version) | |
| req_v = parse_version(required_version) | |
| if inst_v == req_v: | |
| status = 'ok' | |
| elif inst_v > req_v: | |
| status = 'newer' | |
| else: | |
| status = 'older' | |
| if status != 'ok': | |
| requirements_to_fix.append({ | |
| "name": name, | |
| "required_version": required_version | |
| }) | |
| except Exception as e: | |
| task.log(f"Error parsing requirement line '{line}': {e}", level="ERROR") | |
| continue | |
| if not requirements_to_fix: | |
| task.log("All requirements are already satisfied.") | |
| task.set_progress(100) | |
| return {"message": "All requirements are already satisfied."} | |
| total_reqs = len(requirements_to_fix) | |
| fixed_count = 0 | |
| for i, req in enumerate(requirements_to_fix): | |
| if task.cancellation_event.is_set(): | |
| task.log("Fix all requirements task cancelled.", level="WARNING") | |
| break | |
| task.log(f"Fixing {req['name']} (required: {req['required_version'] or 'latest'})...") | |
| try: | |
| cmd = [sys.executable, "-m", "pip", "install"] | |
| if req['required_version']: | |
| cmd.append(f"{req['name']}=={req['required_version']}") | |
| else: | |
| cmd.append("--upgrade") | |
| cmd.append(req['name']) | |
| process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) | |
| task.process = process | |
| for line in process.stdout: | |
| task.log(line.strip()) | |
| process.wait() | |
| if process.returncode == 0: | |
| task.log(f"Successfully installed {req['name']}") | |
| fixed_count += 1 | |
| else: | |
| task.log(f"Installation failed for {req['name']} with code {process.returncode}", level="ERROR") | |
| except Exception as e: | |
| task.log(f"Failed to install {req['name']}: {str(e)}", level="ERROR") | |
| task.set_progress(int(90 * (i + 1) / total_reqs)) | |
| task.set_progress(100) | |
| return {"message": f"Fixed {fixed_count} out of {total_reqs} requirements."} | |
| db_task = task_manager.submit_task( | |
| name="Fix All Requirements", | |
| target=_fix_all_task, | |
| description="Installing correct versions for all requirements that are not satisfied.", | |
| owner_username=current_user.username | |
| ) | |
| return db_task | |
Xet Storage Details
- Size:
- 29.7 kB
- Xet hash:
- aaed777291bbb5ebb76772e5c377a8c52952ddb2eeb20c1fc5b73d39af85228f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.