2018-11-01 03:34:40 +08:00
|
|
|
# coding=utf-8
|
|
|
|
|
2018-09-12 10:32:09 +08:00
|
|
|
import os
|
2019-09-05 23:30:14 +08:00
|
|
|
import platform
|
2020-05-24 17:45:28 +08:00
|
|
|
import signal
|
2020-02-01 06:04:01 +08:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
2020-04-15 10:20:23 +08:00
|
|
|
import atexit
|
2018-10-03 18:53:22 +08:00
|
|
|
|
2022-05-01 20:00:20 +08:00
|
|
|
from bazarr.app.get_args import args
|
2018-10-03 18:53:22 +08:00
|
|
|
|
2019-09-05 23:30:14 +08:00
|
|
|
|
|
|
|
def check_python_version():
|
|
|
|
python_version = platform.python_version_tuple()
|
2023-12-12 07:03:56 +08:00
|
|
|
minimum_py3_tuple = (3, 8, 0)
|
2020-02-01 06:04:01 +08:00
|
|
|
minimum_py3_str = ".".join(str(i) for i in minimum_py3_tuple)
|
|
|
|
|
2020-04-15 10:20:23 +08:00
|
|
|
if int(python_version[0]) < minimum_py3_tuple[0]:
|
2020-02-01 06:04:01 +08:00
|
|
|
print("Python " + minimum_py3_str + " or greater required. "
|
|
|
|
"Current version is " + platform.python_version() + ". Please upgrade Python.")
|
|
|
|
sys.exit(1)
|
2023-06-23 12:03:23 +08:00
|
|
|
elif int(python_version[0]) == 3 and int(python_version[1]) > 11:
|
|
|
|
print("Python version greater than 3.11.x is unsupported. Current version is " + platform.python_version() +
|
2021-07-08 01:30:12 +08:00
|
|
|
". Keep in mind that even if it works, you're on your own.")
|
2020-04-12 20:50:25 +08:00
|
|
|
elif (int(python_version[0]) == minimum_py3_tuple[0] and int(python_version[1]) < minimum_py3_tuple[1]) or \
|
2020-04-15 10:20:23 +08:00
|
|
|
(int(python_version[0]) != minimum_py3_tuple[0]):
|
2020-04-12 20:50:25 +08:00
|
|
|
print("Python " + minimum_py3_str + " or greater required. "
|
2020-02-01 06:04:01 +08:00
|
|
|
"Current version is " + platform.python_version() + ". Please upgrade Python.")
|
|
|
|
sys.exit(1)
|
2019-09-05 23:30:14 +08:00
|
|
|
|
|
|
|
|
2022-05-08 21:19:38 +08:00
|
|
|
def get_python_path():
|
|
|
|
if sys.platform == "darwin":
|
|
|
|
# Do not run Python from within macOS framework bundle.
|
2022-05-29 10:04:26 +08:00
|
|
|
python_bundle_path = os.path.join(sys.base_exec_prefix, "Resources", "Python.app", "Contents", "MacOS", "Python")
|
2022-05-08 21:19:38 +08: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 23:30:14 +08:00
|
|
|
check_python_version()
|
|
|
|
|
2018-10-11 09:23:30 +08:00
|
|
|
dir_name = os.path.dirname(__file__)
|
|
|
|
|
2018-11-01 01:09:46 +08:00
|
|
|
|
2020-05-18 20:56:16 +08:00
|
|
|
def end_child_process(ep):
|
|
|
|
try:
|
2024-01-11 12:04:23 +08:00
|
|
|
if os.name != 'nt':
|
|
|
|
try:
|
|
|
|
ep.send_signal(signal.SIGINT)
|
|
|
|
except ProcessLookupError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
import win32api
|
|
|
|
import win32con
|
|
|
|
try:
|
|
|
|
win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT, ep.pid)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2020-05-18 20:56:16 +08:00
|
|
|
except:
|
2020-05-24 17:45:28 +08:00
|
|
|
ep.terminate()
|
|
|
|
|
2020-05-18 20:56:16 +08:00
|
|
|
|
2020-04-15 10:20:23 +08:00
|
|
|
def start_bazarr():
|
2022-05-08 21:19:38 +08:00
|
|
|
script = [get_python_path(), "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:]
|
2020-04-15 10:20:23 +08:00
|
|
|
ep = subprocess.Popen(script, stdout=None, stderr=None, stdin=subprocess.DEVNULL)
|
2020-05-18 20:56:16 +08:00
|
|
|
atexit.register(end_child_process, ep=ep)
|
2024-01-11 12:04:23 +08:00
|
|
|
signal.signal(signal.SIGTERM, lambda signal_no, frame: end_child_process(ep))
|
2020-04-15 10:20:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
def check_status():
|
|
|
|
if os.path.exists(stopfile):
|
|
|
|
try:
|
|
|
|
os.remove(stopfile)
|
|
|
|
except Exception:
|
|
|
|
print('Unable to delete stop file.')
|
|
|
|
finally:
|
|
|
|
print('Bazarr exited.')
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if os.path.exists(restartfile):
|
|
|
|
try:
|
|
|
|
os.remove(restartfile)
|
|
|
|
except Exception:
|
|
|
|
print('Unable to delete restart file.')
|
|
|
|
else:
|
|
|
|
print("Bazarr is restarting...")
|
|
|
|
start_bazarr()
|
2018-10-05 02:16:49 +08:00
|
|
|
|
|
|
|
|
2018-10-03 18:53:22 +08:00
|
|
|
if __name__ == '__main__':
|
2020-04-15 10:20:23 +08:00
|
|
|
restartfile = os.path.join(args.config_dir, 'bazarr.restart')
|
|
|
|
stopfile = os.path.join(args.config_dir, 'bazarr.stop')
|
2020-02-01 06:04:01 +08:00
|
|
|
|
2020-04-15 10:20:23 +08:00
|
|
|
# Cleanup leftover files
|
2018-09-12 10:32:09 +08:00
|
|
|
try:
|
2018-10-03 18:53:22 +08:00
|
|
|
os.remove(restartfile)
|
2020-04-15 10:20:23 +08:00
|
|
|
except FileNotFoundError:
|
2018-10-11 09:23:30 +08:00
|
|
|
pass
|
2020-02-01 06:04:01 +08:00
|
|
|
|
2018-10-11 09:23:30 +08:00
|
|
|
try:
|
2018-10-03 18:53:22 +08:00
|
|
|
os.remove(stopfile)
|
2020-04-15 10:20:23 +08:00
|
|
|
except FileNotFoundError:
|
2018-09-12 10:32:09 +08:00
|
|
|
pass
|
2020-02-01 06:04:01 +08:00
|
|
|
|
2020-04-15 10:20:23 +08:00
|
|
|
# Initial start of main bazarr process
|
|
|
|
print("Bazarr starting...")
|
|
|
|
start_bazarr()
|
2020-02-01 06:04:01 +08:00
|
|
|
|
2020-01-12 22:27:14 +08:00
|
|
|
# Keep the script running forever until stop is requested through term or keyboard interrupt
|
2020-04-15 10:20:23 +08:00
|
|
|
while True:
|
|
|
|
check_status()
|
|
|
|
try:
|
2020-04-28 11:03:24 +08:00
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
time.sleep(5)
|
|
|
|
else:
|
|
|
|
os.wait()
|
2020-05-19 07:28:21 +08:00
|
|
|
time.sleep(1)
|
2020-05-24 17:45:28 +08:00
|
|
|
except (KeyboardInterrupt, SystemExit, ChildProcessError):
|
2020-05-18 20:56:16 +08:00
|
|
|
print('Bazarr exited.')
|
|
|
|
sys.exit(0)
|