""" Force cleanup of all app data and processes """ import psutil import os import sys import time import tempfile def kill_all_python_processes(): """Force kill ALL Python processes (use with caution)""" killed_processes = [] for proc in psutil.process_iter(['pid', 'name', 'cmdline']): try: if 'python' in proc.info['name'].lower(): print(f"Killing Python process: {proc.info['pid']} - {proc.info['name']}") proc.kill() killed_processes.append(proc.info['pid']) except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): pass if killed_processes: print(f"Killed {len(killed_processes)} Python processes") time.sleep(3) # Give more time for cleanup else: print("No Python processes found") def clear_shared_memory(): """Clear Qt shared memory""" try: from PyQt5.QtCore import QSharedMemory app_names = ["Cluster4NPU", "cluster4npu", "main"] for app_name in app_names: shared_mem = QSharedMemory(app_name) if shared_mem.attach(): shared_mem.detach() print(f"Cleared shared memory for: {app_name}") except Exception as e: print(f"Could not clear shared memory: {e}") def clean_all_temp_files(): """Remove all possible lock and temp files""" possible_files = [ 'app.lock', '.app.lock', 'cluster4npu.lock', '.cluster4npu.lock', 'main.lock', '.main.lock' ] # Check in current directory current_dir_files = [] for filename in possible_files: filepath = os.path.join(os.getcwd(), filename) if os.path.exists(filepath): try: os.remove(filepath) current_dir_files.append(filepath) print(f"Removed: {filepath}") except Exception as e: print(f"Could not remove {filepath}: {e}") # Check in temp directory temp_dir = tempfile.gettempdir() temp_files = [] for filename in possible_files: filepath = os.path.join(temp_dir, filename) if os.path.exists(filepath): try: os.remove(filepath) temp_files.append(filepath) print(f"Removed: {filepath}") except Exception as e: print(f"Could not remove {filepath}: {e}") # Check in user home directory home_dir = os.path.expanduser('~') home_files = [] for filename in possible_files: filepath = os.path.join(home_dir, filename) if os.path.exists(filepath): try: os.remove(filepath) home_files.append(filepath) print(f"Removed: {filepath}") except Exception as e: print(f"Could not remove {filepath}: {e}") total_removed = len(current_dir_files) + len(temp_files) + len(home_files) if total_removed == 0: print("No lock files found") def force_unlock_files(): """Try to unlock any locked files""" try: # On Windows, try to reset file handles import subprocess result = subprocess.run(['tasklist', '/FI', 'IMAGENAME eq python.exe'], capture_output=True, text=True, timeout=10) if result.returncode == 0: lines = result.stdout.strip().split('\n') for line in lines[3:]: # Skip header lines if 'python.exe' in line: parts = line.split() if len(parts) >= 2: pid = parts[1] try: subprocess.run(['taskkill', '/F', '/PID', pid], timeout=5) print(f"Force killed PID: {pid}") except: pass except Exception as e: print(f"Could not force unlock files: {e}") if __name__ == '__main__': print("FORCE CLEANUP - This will kill ALL Python processes!") print("=" * 60) response = input("Are you sure? This will close ALL Python programs (y/N): ") if response.lower() in ['y', 'yes']: print("\n1. Killing all Python processes...") kill_all_python_processes() print("\n2. Clearing shared memory...") clear_shared_memory() print("\n3. Removing lock files...") clean_all_temp_files() print("\n4. Force unlocking files...") force_unlock_files() print("\n" + "=" * 60) print("FORCE CLEANUP COMPLETE!") print("All Python processes killed and lock files removed.") print("You can now start the app with 'python main.py'") else: print("Cleanup cancelled.")