2021-04-04 21:34:46 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-10-25 01:23:24 +00:00
|
|
|
import multiprocessing
|
|
|
|
import time
|
2020-11-01 00:38:46 +00:00
|
|
|
import sys
|
2021-04-17 21:51:43 +00:00
|
|
|
from typing import Any
|
2020-11-01 00:38:46 +00:00
|
|
|
import webbrowser
|
2021-04-04 21:34:46 +00:00
|
|
|
from setproctitle import setproctitle
|
2020-10-25 01:23:24 +00:00
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
from helpers.the_terminator import Terminator
|
2020-10-25 01:23:24 +00:00
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
|
|
|
|
def startServer(notifications=False):
|
2021-04-19 14:45:20 +00:00
|
|
|
# Only spend the time importing the Server if we want to start the server. Speeds up web browser opens.
|
|
|
|
from server import BAPSicleServer
|
|
|
|
|
2020-11-01 00:31:58 +00:00
|
|
|
server = multiprocessing.Process(target=BAPSicleServer)
|
|
|
|
server.start()
|
|
|
|
|
2021-04-17 20:28:57 +00:00
|
|
|
sent_start_notif = False
|
2021-04-17 21:51:43 +00:00
|
|
|
|
|
|
|
terminator = Terminator()
|
2021-04-04 21:34:46 +00:00
|
|
|
try:
|
2021-04-17 21:51:43 +00:00
|
|
|
while not terminator.terminate:
|
|
|
|
time.sleep(1)
|
2021-04-04 21:34:46 +00:00
|
|
|
if server and server.is_alive():
|
2021-04-17 20:28:57 +00:00
|
|
|
if notifications and not sent_start_notif:
|
|
|
|
print("NOTIFICATION:Welcome to BAPSicle!")
|
|
|
|
sent_start_notif = True
|
2021-04-04 21:34:46 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
print("Server dead. Exiting.")
|
2021-04-17 20:28:57 +00:00
|
|
|
if notifications:
|
|
|
|
print("NOTIFICATION:BAPSicle Server Stopped!")
|
2021-04-04 21:34:46 +00:00
|
|
|
sys.exit(0)
|
2021-04-17 21:51:43 +00:00
|
|
|
|
|
|
|
if server and server.is_alive():
|
|
|
|
server.terminate()
|
|
|
|
server.join()
|
|
|
|
|
|
|
|
# Catch the handler being killed externally.
|
2021-04-04 21:34:46 +00:00
|
|
|
except Exception as e:
|
2021-04-17 21:51:43 +00:00
|
|
|
printer("Received Exception {} with args: {}".format(type(e).__name__, e.args))
|
|
|
|
if server and server.is_alive():
|
|
|
|
server.terminate()
|
|
|
|
server.join()
|
|
|
|
|
|
|
|
|
|
|
|
def printer(msg: Any):
|
|
|
|
print("LAUNCHER:{}".format(msg))
|
2020-11-01 00:31:58 +00:00
|
|
|
|
2020-11-01 02:37:40 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
if __name__ == "__main__":
|
2020-11-01 02:37:40 +00:00
|
|
|
# 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()
|
2021-04-17 20:28:57 +00:00
|
|
|
setproctitle("BAPSicle Launcher")
|
2020-11-01 00:38:46 +00:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
# We got an argument! It's probably Platypus's UI.
|
|
|
|
try:
|
|
|
|
if (sys.argv[1]) == "Start Server":
|
2021-04-19 14:45:20 +00:00
|
|
|
print("NOTIFICATION:BAPSicle is starting, please wait...")
|
2020-11-01 00:38:46 +00:00
|
|
|
webbrowser.open("http://localhost:13500/")
|
2021-04-17 20:28:57 +00:00
|
|
|
startServer(notifications=True)
|
|
|
|
if sys.argv[1] == "Server":
|
|
|
|
webbrowser.open("http://localhost:13500/")
|
|
|
|
if sys.argv[1] == "Presenter":
|
|
|
|
webbrowser.open("http://localhost:13500/presenter/")
|
2020-11-01 00:38:46 +00:00
|
|
|
except Exception as e:
|
2021-09-11 15:49:08 +00:00
|
|
|
print(
|
|
|
|
"ALERT:BAPSicle failed with exception of type {}:{}".format(
|
|
|
|
type(e).__name__, e
|
|
|
|
)
|
|
|
|
)
|
2021-04-04 21:34:46 +00:00
|
|
|
sys.exit(1)
|
2020-11-01 00:38:46 +00:00
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
startServer()
|