|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import subprocess |
| 4 | + |
| 5 | + |
| 6 | +ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
| 7 | +TRAY_SCRIPT = os.path.join(ROOT_DIR, "utilities", "tray", "app.py") |
| 8 | +PID_DIR = os.path.join(ROOT_DIR, "user", "Temp") |
| 9 | +PID_PATH = os.path.join(PID_DIR, "tray.pid") |
| 10 | + |
| 11 | + |
| 12 | +def _read_pid(): |
| 13 | + try: |
| 14 | + with open(PID_PATH, "r", encoding="utf-8") as fh: |
| 15 | + raw = fh.read().strip() |
| 16 | + return int(raw) if raw else None |
| 17 | + except Exception: |
| 18 | + return None |
| 19 | + |
| 20 | + |
| 21 | +def _clear_pid(): |
| 22 | + try: |
| 23 | + if os.path.exists(PID_PATH): |
| 24 | + os.remove(PID_PATH) |
| 25 | + except Exception: |
| 26 | + pass |
| 27 | + |
| 28 | + |
| 29 | +def _pid_alive(pid): |
| 30 | + if not isinstance(pid, int) or pid <= 0: |
| 31 | + return False |
| 32 | + if os.name == "nt": |
| 33 | + try: |
| 34 | + proc = subprocess.run( |
| 35 | + ["tasklist", "/FI", f"PID eq {pid}", "/FO", "CSV", "/NH"], |
| 36 | + stdout=subprocess.PIPE, |
| 37 | + stderr=subprocess.PIPE, |
| 38 | + text=True, |
| 39 | + check=False, |
| 40 | + ) |
| 41 | + out = (proc.stdout or "").strip().lower() |
| 42 | + return bool(out) and (not out.startswith("info: no tasks")) and (str(pid) in out) |
| 43 | + except Exception: |
| 44 | + return False |
| 45 | + try: |
| 46 | + os.kill(pid, 0) |
| 47 | + return True |
| 48 | + except OSError: |
| 49 | + return False |
| 50 | + |
| 51 | + |
| 52 | +def _pythonw_executable(): |
| 53 | + current = sys.executable or "python" |
| 54 | + folder = os.path.dirname(current) |
| 55 | + candidate = os.path.join(folder, "pythonw.exe") |
| 56 | + if os.name == "nt" and os.path.exists(candidate): |
| 57 | + return candidate |
| 58 | + return current |
| 59 | + |
| 60 | + |
| 61 | +def _start_tray(): |
| 62 | + if not os.path.exists(TRAY_SCRIPT): |
| 63 | + print("Tray app not found.") |
| 64 | + return |
| 65 | + pid = _read_pid() |
| 66 | + if pid and _pid_alive(pid): |
| 67 | + print(f"Tray already running (pid {pid}).") |
| 68 | + return |
| 69 | + py = _pythonw_executable() |
| 70 | + kwargs = { |
| 71 | + "cwd": ROOT_DIR, |
| 72 | + "stdout": subprocess.DEVNULL, |
| 73 | + "stderr": subprocess.DEVNULL, |
| 74 | + } |
| 75 | + if os.name == "nt": |
| 76 | + flags = 0 |
| 77 | + flags |= getattr(subprocess, "DETACHED_PROCESS", 0) |
| 78 | + flags |= getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0) |
| 79 | + if flags: |
| 80 | + kwargs["creationflags"] = flags |
| 81 | + else: |
| 82 | + kwargs["start_new_session"] = True |
| 83 | + proc = subprocess.Popen([py, TRAY_SCRIPT], **kwargs) |
| 84 | + print(f"Tray start requested (pid {proc.pid}).") |
| 85 | + |
| 86 | + |
| 87 | +def _stop_tray(): |
| 88 | + pid = _read_pid() |
| 89 | + if not pid: |
| 90 | + print("Tray not running.") |
| 91 | + return |
| 92 | + if not _pid_alive(pid): |
| 93 | + _clear_pid() |
| 94 | + print("Tray not running.") |
| 95 | + return |
| 96 | + try: |
| 97 | + if os.name == "nt": |
| 98 | + subprocess.run( |
| 99 | + ["taskkill", "/PID", str(pid), "/T", "/F"], |
| 100 | + stdout=subprocess.DEVNULL, |
| 101 | + stderr=subprocess.DEVNULL, |
| 102 | + check=False, |
| 103 | + ) |
| 104 | + else: |
| 105 | + os.kill(pid, 15) |
| 106 | + except Exception as e: |
| 107 | + print(f"Failed to stop tray: {e}") |
| 108 | + return |
| 109 | + _clear_pid() |
| 110 | + print("Tray stopped.") |
| 111 | + |
| 112 | + |
| 113 | +def _status_tray(): |
| 114 | + pid = _read_pid() |
| 115 | + if pid and _pid_alive(pid): |
| 116 | + print(f"Tray running (pid {pid}).") |
| 117 | + return |
| 118 | + if pid and not _pid_alive(pid): |
| 119 | + _clear_pid() |
| 120 | + print("Tray not running.") |
| 121 | + |
| 122 | + |
| 123 | +def run(args, properties): |
| 124 | + sub = str(args[0]).strip().lower() if args else "status" |
| 125 | + if sub in {"help", "-h", "--help"}: |
| 126 | + print(get_help_message()) |
| 127 | + return |
| 128 | + if sub == "start": |
| 129 | + _start_tray() |
| 130 | + return |
| 131 | + if sub == "stop": |
| 132 | + _stop_tray() |
| 133 | + return |
| 134 | + if sub == "restart": |
| 135 | + _stop_tray() |
| 136 | + _start_tray() |
| 137 | + return |
| 138 | + if sub == "status": |
| 139 | + _status_tray() |
| 140 | + return |
| 141 | + print(get_help_message()) |
| 142 | + |
| 143 | + |
| 144 | +def get_help_message(): |
| 145 | + return """ |
| 146 | +Usage: |
| 147 | + tray start |
| 148 | + tray stop |
| 149 | + tray restart |
| 150 | + tray status |
| 151 | +""" |
0 commit comments