|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +pyeetd - based on https://github.com/biscuitehh/yeetd |
| 4 | +
|
| 5 | +how to use: |
| 6 | +python Scripts/pyeetd/main.py & PYEETD_PID=$! |
| 7 | +... |
| 8 | +kill $PYEETD_PID |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | +import signal |
| 13 | +import time |
| 14 | +import subprocess |
| 15 | +import re |
| 16 | +from dataclasses import dataclass |
| 17 | +from enum import Enum |
| 18 | + |
| 19 | +OS_PROCESSES = { |
| 20 | + "Spotlight", |
| 21 | + "ReportCrash", |
| 22 | + "ecosystemanalyticsd" |
| 23 | + "com.apple.ecosystemd", |
| 24 | + "com.apple.metadata.mds", |
| 25 | +} |
| 26 | + |
| 27 | +SIMULATOR_PROCESSES = { |
| 28 | + "AegirPoster", |
| 29 | + "InfographPoster", |
| 30 | + "CollectionsPoster", |
| 31 | + "ExtragalacticPoster", |
| 32 | + "KaleidoscopePoster", |
| 33 | + "EmojiPosterExtension", |
| 34 | + "AmbientPhotoFramePosterProvider", |
| 35 | + "PhotosPosterProvider", |
| 36 | + "AvatarPosterExtension", |
| 37 | + "GradientPosterExtension", |
| 38 | + "MonogramPosterExtension" |
| 39 | +} |
| 40 | + |
| 41 | +SIMULATOR_PATH_SEARCH_KEY = "simruntime/Contents/Resources/RuntimeRoot" |
| 42 | + |
| 43 | +# How long to sleep between checks in seconds |
| 44 | +SLEEP_DELAY = 5 |
| 45 | + |
| 46 | +# How often to print process info (in seconds) |
| 47 | +PRINT_PROCESSES_INTERVAL = 60 |
| 48 | + |
| 49 | +@dataclass |
| 50 | +class ProcessInfo: |
| 51 | + pid: int |
| 52 | + cpu_percent: float |
| 53 | + memory_percent: float |
| 54 | + name: str |
| 55 | + is_simulator: bool |
| 56 | + |
| 57 | + @property |
| 58 | + def environment(self) -> str: |
| 59 | + return "Simulator" if self.is_simulator else "OS" |
| 60 | + |
| 61 | + @property |
| 62 | + def output_string(self) -> str: |
| 63 | + return f"{self.pid}\t{self.cpu_percent}%\t{self.memory_percent}%\t{self.name}\t{self.environment}" |
| 64 | + |
| 65 | +class ProcessSort(Enum): |
| 66 | + CPU = "cpu" |
| 67 | + MEMORY = "memory" |
| 68 | + |
| 69 | +def get_processes(sort_by=ProcessSort.CPU): |
| 70 | + """Get all processes using ps command - equivalent to Swift's proc_listallpids""" |
| 71 | + sorty_by = "-ero" if sort_by == ProcessSort.CPU else "-emo" |
| 72 | + result = subprocess.run(['ps', sorty_by, 'pid,pcpu,pmem,comm'], |
| 73 | + capture_output=True, text=True, check=True) |
| 74 | + processes = [] |
| 75 | + |
| 76 | + for line in result.stdout.splitlines()[1:]: # Skip header |
| 77 | + parts = line.strip().split(None, 3) |
| 78 | + if len(parts) >= 3: |
| 79 | + pid = int(parts[0]) |
| 80 | + cpu_percent = float(parts[1]) |
| 81 | + memory_percent = float(parts[2]) |
| 82 | + name = parts[3] |
| 83 | + is_simulator = SIMULATOR_PATH_SEARCH_KEY in name |
| 84 | + processes.append(ProcessInfo(pid, cpu_percent, memory_percent, name, is_simulator)) |
| 85 | + |
| 86 | + return processes |
| 87 | + |
| 88 | +def print_processes(processes, limit=-1): |
| 89 | + output = [] |
| 90 | + output.append("================================") |
| 91 | + output.append("⚡️ Processes sorted by CPU usage:") |
| 92 | + output.append("PID\tCPU%\tMemory%\tName\tEnvironment") |
| 93 | + limit = len(processes) if limit == -1 else limit |
| 94 | + for p in processes[:limit]: |
| 95 | + output.append(p.output_string) |
| 96 | + |
| 97 | + output.append("--------------------------------") |
| 98 | + output.append("🧠 Processes sorted by memory usage:") |
| 99 | + output.append("PID\tCPU%\tMemory%\tName\tEnvironment") |
| 100 | + processes_sorted_by_memory = sorted(processes, key=lambda x: x.memory_percent, reverse=True) |
| 101 | + for p in processes_sorted_by_memory[:limit]: |
| 102 | + output.append(p.output_string) |
| 103 | + |
| 104 | + output.append("================================") |
| 105 | + print("\n".join(output)) |
| 106 | + |
| 107 | +def find_unwanted(processes): |
| 108 | + yeeting = [] |
| 109 | + for p in processes: |
| 110 | + process_target_list = SIMULATOR_PROCESSES if p.is_simulator else OS_PROCESSES |
| 111 | + for k in process_target_list: |
| 112 | + if k in p.name: |
| 113 | + yeeting.append(p) |
| 114 | + return yeeting |
| 115 | + |
| 116 | +def yeet(processes): |
| 117 | + output = [] |
| 118 | + for p in processes: |
| 119 | + output.append(f"🤠 pyeetd: Stopping - {p.output_string}") |
| 120 | + os.killpg(p.pid, signal.SIGKILL) |
| 121 | + return output |
| 122 | + |
| 123 | +def main(): |
| 124 | + print_cycles = PRINT_PROCESSES_INTERVAL // SLEEP_DELAY |
| 125 | + i = 0 |
| 126 | + while True: |
| 127 | + output = [] |
| 128 | + processes = get_processes(ProcessSort.CPU) |
| 129 | + processes_to_yeet = find_unwanted(processes) |
| 130 | + output.extend(yeet(processes_to_yeet)) |
| 131 | + output.append(f"🤠 {time.strftime('%Y-%m-%d %H:%M:%S')} - pyeetd {len(processes_to_yeet)} processes.") |
| 132 | + print("\n".join(output)) |
| 133 | + if i % print_cycles == 0: |
| 134 | + print_processes(processes, 10) |
| 135 | + i += 1 |
| 136 | + time.sleep(SLEEP_DELAY) |
| 137 | + |
| 138 | +if __name__ == '__main__': |
| 139 | + main() |
0 commit comments