2018-10-31 19:34:40 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2018-09-12 02:32:09 +00:00
|
|
|
import os
|
2019-09-05 15:30:14 +00:00
|
|
|
import platform
|
2020-05-24 09:45:28 +00:00
|
|
|
import signal
|
2020-01-31 22:04:01 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
from bazarr.app.get_args import args
|
2024-04-20 14:47:53 +00:00
|
|
|
from bazarr.literals import EXIT_PYTHON_UPGRADE_NEEDED, EXIT_NORMAL, FILE_RESTART, FILE_STOP, ENV_RESTARTFILE, ENV_STOPFILE, EXIT_INTERRUPT
|
|
|
|
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2024-03-03 17:00:50 +00:00
|
|
|
def exit_program(status_code):
|
|
|
|
print(f'Bazarr exited with status code {status_code}.')
|
|
|
|
raise SystemExit(status_code)
|
2019-09-05 15:30:14 +00:00
|
|
|
|
2024-04-20 14:47:53 +00:00
|
|
|
|
2019-09-05 15:30:14 +00:00
|
|
|
def check_python_version():
|
|
|
|
python_version = platform.python_version_tuple()
|
2023-12-11 23:03:56 +00:00
|
|
|
minimum_py3_tuple = (3, 8, 0)
|
2020-01-31 22:04:01 +00:00
|
|
|
minimum_py3_str = ".".join(str(i) for i in minimum_py3_tuple)
|
|
|
|
|
2020-04-15 02:20:23 +00:00
|
|
|
if int(python_version[0]) < minimum_py3_tuple[0]:
|
2020-01-31 22:04:01 +00:00
|
|
|
print("Python " + minimum_py3_str + " or greater required. "
|
|
|
|
"Current version is " + platform.python_version() + ". Please upgrade Python.")
|
2024-03-03 17:00:50 +00:00
|
|
|
exit_program(EXIT_PYTHON_UPGRADE_NEEDED)
|
2024-12-14 05:09:51 +00:00
|
|
|
elif int(python_version[0]) == 3 and int(python_version[1]) > 12:
|
|
|
|
print("Python version greater than 3.12.x is unsupported. Current version is " + platform.python_version() +
|
2021-07-07 17:30:12 +00:00
|
|
|
". Keep in mind that even if it works, you're on your own.")
|
2020-04-12 12:50:25 +00:00
|
|
|
elif (int(python_version[0]) == minimum_py3_tuple[0] and int(python_version[1]) < minimum_py3_tuple[1]) or \
|
2020-04-15 02:20:23 +00:00
|
|
|
(int(python_version[0]) != minimum_py3_tuple[0]):
|
2020-04-12 12:50:25 +00:00
|
|
|
print("Python " + minimum_py3_str + " or greater required. "
|
2020-01-31 22:04:01 +00:00
|
|
|
"Current version is " + platform.python_version() + ". Please upgrade Python.")
|
2024-03-03 17:00:50 +00:00
|
|
|
exit_program(EXIT_PYTHON_UPGRADE_NEEDED)
|
2019-09-05 15:30:14 +00:00
|
|
|
|
|
|
|
|
2022-05-08 13:19:38 +00:00
|
|
|
def get_python_path():
|
|
|
|
if sys.platform == "darwin":
|
|
|
|
# Do not run Python from within macOS framework bundle.
|
2022-05-29 02:04:26 +00:00
|
|
|
python_bundle_path = os.path.join(sys.base_exec_prefix, "Resources", "Python.app", "Contents", "MacOS", "Python")
|
2022-05-08 13:19:38 +00:00
|
|
|
if os.path.exists(python_bundle_path):
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
python_path = os.path.join(tempfile.mkdtemp(), "python")
|
|
|
|
os.symlink(python_bundle_path, python_path)
|
|
|
|
|
|
|
|
return python_path
|
|
|
|
|
|
|
|
return sys.executable
|
|
|
|
|
|
|
|
|
2019-09-05 15:30:14 +00:00
|
|
|
check_python_version()
|
|
|
|
|
2018-10-11 01:23:30 +00:00
|
|
|
dir_name = os.path.dirname(__file__)
|
|
|
|
|
2024-04-20 14:47:53 +00:00
|
|
|
|
2024-03-03 17:00:50 +00:00
|
|
|
def start_bazarr():
|
|
|
|
script = [get_python_path(), "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:]
|
2024-04-20 14:47:53 +00:00
|
|
|
ep = subprocess.Popen(script, stdout=None, stderr=None, stdin=subprocess.DEVNULL, env=os.environ)
|
2024-03-03 17:00:50 +00:00
|
|
|
print(f"Bazarr starting child process with PID {ep.pid}...")
|
|
|
|
return ep
|
2024-04-20 14:50:32 +00:00
|
|
|
|
2024-03-03 17:00:50 +00:00
|
|
|
|
|
|
|
def terminate_child():
|
|
|
|
print(f"Terminating child process with PID {child_process.pid}")
|
|
|
|
child_process.terminate()
|
2018-10-31 17:09:46 +00:00
|
|
|
|
2024-03-03 17:00:50 +00:00
|
|
|
|
|
|
|
def get_stop_status_code(input_file):
|
2020-05-18 12:56:16 +00:00
|
|
|
try:
|
2024-04-20 14:50:32 +00:00
|
|
|
with open(input_file, 'r') as file:
|
2024-03-03 17:00:50 +00:00
|
|
|
# read status code from file, if it exists
|
|
|
|
line = file.readline()
|
2024-01-11 04:04:23 +00:00
|
|
|
try:
|
2024-03-03 17:00:50 +00:00
|
|
|
status_code = int(line)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
status_code = EXIT_NORMAL
|
|
|
|
file.close()
|
2024-04-20 14:50:32 +00:00
|
|
|
except Exception:
|
2024-03-03 17:00:50 +00:00
|
|
|
status_code = EXIT_NORMAL
|
|
|
|
return status_code
|
2020-04-15 02:20:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_status():
|
2024-03-03 17:00:50 +00:00
|
|
|
global child_process
|
2024-04-20 14:47:53 +00:00
|
|
|
if os.path.exists(stop_file):
|
|
|
|
status_code = get_stop_status_code(stop_file)
|
2020-04-15 02:20:23 +00:00
|
|
|
try:
|
2024-04-20 14:50:32 +00:00
|
|
|
print("Deleting stop file...")
|
2024-04-20 14:47:53 +00:00
|
|
|
os.remove(stop_file)
|
2024-04-20 14:50:32 +00:00
|
|
|
except Exception:
|
2020-04-15 02:20:23 +00:00
|
|
|
print('Unable to delete stop file.')
|
|
|
|
finally:
|
2024-03-03 17:00:50 +00:00
|
|
|
terminate_child()
|
|
|
|
exit_program(status_code)
|
2020-04-15 02:20:23 +00:00
|
|
|
|
2024-04-20 14:47:53 +00:00
|
|
|
if os.path.exists(restart_file):
|
2020-04-15 02:20:23 +00:00
|
|
|
try:
|
2024-04-20 14:50:32 +00:00
|
|
|
print("Deleting restart file...")
|
2024-04-20 14:47:53 +00:00
|
|
|
os.remove(restart_file)
|
2020-04-15 02:20:23 +00:00
|
|
|
except Exception:
|
|
|
|
print('Unable to delete restart file.')
|
2024-03-03 17:00:50 +00:00
|
|
|
finally:
|
|
|
|
terminate_child()
|
2024-04-20 14:50:32 +00:00
|
|
|
print("Bazarr is restarting...")
|
2024-03-03 17:00:50 +00:00
|
|
|
child_process = start_bazarr()
|
|
|
|
|
|
|
|
|
|
|
|
def interrupt_handler(signum, frame):
|
|
|
|
# catch and ignore keyboard interrupt Ctrl-C
|
|
|
|
# the child process Server object will catch SIGINT and perform an orderly shutdown
|
|
|
|
global interrupted
|
|
|
|
if not interrupted:
|
|
|
|
# ignore user hammering Ctrl-C; we heard you the first time!
|
|
|
|
interrupted = True
|
|
|
|
print('Handling keyboard interrupt...')
|
|
|
|
else:
|
2024-04-20 14:50:32 +00:00
|
|
|
print("Stop doing that! I heard you the first time!")
|
2018-10-04 18:16:49 +00:00
|
|
|
|
|
|
|
|
2018-10-03 10:53:22 +00:00
|
|
|
if __name__ == '__main__':
|
2024-03-03 17:00:50 +00:00
|
|
|
interrupted = False
|
|
|
|
signal.signal(signal.SIGINT, interrupt_handler)
|
2024-04-20 14:47:53 +00:00
|
|
|
restart_file = os.path.join(args.config_dir, FILE_RESTART)
|
|
|
|
stop_file = os.path.join(args.config_dir, FILE_STOP)
|
|
|
|
os.environ[ENV_STOPFILE] = stop_file
|
|
|
|
os.environ[ENV_RESTARTFILE] = restart_file
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2020-04-15 02:20:23 +00:00
|
|
|
# Cleanup leftover files
|
2018-09-12 02:32:09 +00:00
|
|
|
try:
|
2024-04-20 14:47:53 +00:00
|
|
|
os.remove(restart_file)
|
2020-04-15 02:20:23 +00:00
|
|
|
except FileNotFoundError:
|
2018-10-11 01:23:30 +00:00
|
|
|
pass
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2018-10-11 01:23:30 +00:00
|
|
|
try:
|
2024-04-20 14:47:53 +00:00
|
|
|
os.remove(stop_file)
|
2020-04-15 02:20:23 +00:00
|
|
|
except FileNotFoundError:
|
2018-09-12 02:32:09 +00:00
|
|
|
pass
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2020-04-15 02:20:23 +00:00
|
|
|
# Initial start of main bazarr process
|
2024-03-03 17:00:50 +00:00
|
|
|
child_process = start_bazarr()
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2024-03-03 17:00:50 +00:00
|
|
|
# Keep the script running forever until stop is requested through term, special files or keyboard interrupt
|
2020-04-15 02:20:23 +00:00
|
|
|
while True:
|
|
|
|
check_status()
|
|
|
|
try:
|
2024-03-03 17:00:50 +00:00
|
|
|
time.sleep(5)
|
2020-05-24 09:45:28 +00:00
|
|
|
except (KeyboardInterrupt, SystemExit, ChildProcessError):
|
2024-03-03 17:00:50 +00:00
|
|
|
# this code should never be reached, if signal handling is working properly
|
2024-04-20 14:50:32 +00:00
|
|
|
print('Bazarr exited main script file via keyboard interrupt.')
|
2024-03-03 17:00:50 +00:00
|
|
|
exit_program(EXIT_INTERRUPT)
|