-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlauncher.py
More file actions
84 lines (63 loc) · 2.38 KB
/
Copy pathlauncher.py
File metadata and controls
84 lines (63 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
ATC Bot launcher — starts main.py with output to console and bot.log.
Closing the console window cleanly kills the bot process.
Compile with: pyinstaller --onefile --console --name "ATC Bot" launcher.py
"""
import os
import signal
import shutil
import subprocess
import sys
from datetime import datetime
def _find_python() -> str | None:
"""Find the Python interpreter, excluding this exe if running compiled."""
this = os.path.abspath(sys.executable).lower()
for candidate in (sys.executable, "python", "python3"):
found = shutil.which(candidate)
if found and os.path.abspath(found).lower() != this:
return found
return None
proc = None
def _shutdown(signum=None, frame=None):
"""Kill the bot subprocess and exit."""
if proc and proc.poll() is None:
proc.terminate()
try:
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
proc.kill()
sys.exit(0)
def main():
global proc
# Handle Ctrl+C and console close events
signal.signal(signal.SIGINT, _shutdown)
signal.signal(signal.SIGTERM, _shutdown)
if sys.platform == "win32":
signal.signal(signal.SIGBREAK, _shutdown)
here = os.path.dirname(os.path.abspath(sys.executable if getattr(sys, "frozen", False) else __file__))
script = os.path.join(here, "main.py")
log_path = os.path.join(here, "bot.log")
python = _find_python()
with open(log_path, "a", encoding="utf-8") as log:
log.write(f"\n{'='*60}\n")
log.write(f"ATC Bot started at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
log.write(f"{'='*60}\n")
if not python:
msg = "ERROR: Python interpreter not found in PATH.\n"
log.write(msg)
print(msg)
return
log.write(f"Python: {python}\n")
log.write(f"Script: {script}\n\n")
log.flush()
print(f"ATC Bot running — output in bot.log")
print(f"Press Ctrl+C or close this window to stop.\n")
kwargs = {"cwd": here, "stdout": log, "stderr": subprocess.STDOUT}
proc = subprocess.Popen([python, "-u", script], **kwargs)
try:
proc.wait()
except KeyboardInterrupt:
_shutdown()
log.write(f"\nBot exited with code {proc.returncode} at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
if __name__ == "__main__":
main()