BAPSicle/launch.py

89 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2020-10-25 01:23:24 +00:00
import multiprocessing
import time
import sys
from typing import Any
import webbrowser
from setproctitle import setproctitle
2020-10-25 01:23:24 +00:00
from helpers.the_terminator import Terminator
2020-10-25 01:23:24 +00:00
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
2020-11-01 00:31:58 +00:00
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:
2022-03-12 15:59:36 +00:00
notif("Welcome to BAPSicle!")
sent_start_notif = True
pass
else:
2022-03-12 15:59:36 +00:00
printer("Server dead. Exiting.")
if notifications:
2022-03-12 15:59:36 +00:00
notif("BAPSicle Server Stopped!")
sys.exit(0)
if server and server.is_alive():
server.terminate()
2022-03-11 23:55:53 +00:00
server.join(timeout=20) # If we somehow get stuck stopping BAPSicle let it die.
# Catch the handler being killed externally.
except Exception as e:
2021-09-11 16:18:35 +00:00
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))
2020-11-01 00:31:58 +00:00
2022-03-12 17:08:29 +00:00
2022-03-12 15:59:36 +00:00
def notif(msg: str):
print("NOTIFICATION:{}".format(msg))
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()
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":
2022-03-12 15:59:36 +00:00
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:
2022-03-12 15:59:36 +00:00
printer(
2021-09-11 15:49:08 +00:00
"ALERT:BAPSicle failed with exception of type {}:{}".format(
type(e).__name__, e
)
)
sys.exit(1)
sys.exit(0)
else:
startServer()
sys.exit(0)