-
Notifications
You must be signed in to change notification settings - Fork 1
/
launch.py
executable file
·88 lines (73 loc) · 2.78 KB
/
launch.py
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
85
86
87
88
#!/usr/bin/env python3
import multiprocessing
import time
import sys
from typing import Any
import webbrowser
from setproctitle import setproctitle
from helpers.the_terminator import Terminator
def startServer(notifications=False):
# Only spend the time importing the Server if we want to start the server. Speeds up web browser opens.
from server import BAPSicleServer
server = multiprocessing.Process(target=BAPSicleServer)
server.start()
sent_start_notif = False
terminator = Terminator()
try:
while not terminator.terminate:
time.sleep(1)
if server and server.is_alive():
if notifications and not sent_start_notif:
notif("Welcome to BAPSicle!")
sent_start_notif = True
pass
else:
printer("Server dead. Exiting.")
if notifications:
notif("BAPSicle Server Stopped!")
sys.exit(0)
if server and server.is_alive():
server.terminate()
server.join(timeout=20) # If we somehow get stuck stopping BAPSicle let it die.
# Catch the handler being killed externally.
except Exception as e:
printer("Received Exception {} with args: {}".format(
type(e).__name__, e.args))
if server and server.is_alive():
server.terminate()
server.join(timeout=20)
def printer(msg: Any):
print("LAUNCHER:{}".format(msg))
def notif(msg: str):
print("NOTIFICATION:{}".format(msg))
if __name__ == "__main__":
# On Windows, calling this function is necessary.
# Causes all kinds of loops if not present.
# IT HAS TO BE RIGHT HERE, AT THE TOP OF __MAIN__
# NOT INSIDE AN IF STATEMENT. RIGHT. HERE.
# If it's not here, multiprocessing just doesn't run in the package.
# Freeze support refers to being packaged with Pyinstaller.
multiprocessing.freeze_support()
setproctitle("BAPSicle - Launcher")
if len(sys.argv) > 1:
# We got an argument! It's probably Platypus's UI.
try:
if (sys.argv[1]) == "Start Server":
notif("BAPSicle is starting, please wait...")
webbrowser.open("http://localhost:13500/")
startServer(notifications=True)
if sys.argv[1] == "Server":
webbrowser.open("http://localhost:13500/")
if sys.argv[1] == "Presenter":
webbrowser.open("http://localhost:13500/presenter/")
except Exception as e:
printer(
"ALERT:BAPSicle failed with exception of type {}:{}".format(
type(e).__name__, e
)
)
sys.exit(1)
sys.exit(0)
else:
startServer()
sys.exit(0)