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
|
2020-04-15 02:20:23 +00:00
|
|
|
import atexit
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
from bazarr.get_args import args
|
2018-10-03 10:53:22 +00:00
|
|
|
|
2019-09-05 15:30:14 +00:00
|
|
|
|
|
|
|
def check_python_version():
|
|
|
|
python_version = platform.python_version_tuple()
|
2020-04-11 12:05:12 +00:00
|
|
|
minimum_py3_tuple = (3, 7, 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.")
|
|
|
|
sys.exit(1)
|
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.")
|
|
|
|
sys.exit(1)
|
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__)
|
|
|
|
|
2018-10-31 17:09:46 +00:00
|
|
|
|
2020-05-18 12:56:16 +00:00
|
|
|
def end_child_process(ep):
|
|
|
|
try:
|
|
|
|
ep.kill()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2020-05-24 09:45:28 +00:00
|
|
|
def terminate_child_process(ep):
|
|
|
|
try:
|
|
|
|
ep.terminate()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2020-05-18 12:56:16 +00:00
|
|
|
|
2020-04-15 02:20:23 +00:00
|
|
|
def start_bazarr():
|
2018-10-31 19:34:40 +00:00
|
|
|
script = [sys.executable, "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:]
|
2020-04-15 02:20:23 +00:00
|
|
|
ep = subprocess.Popen(script, stdout=None, stderr=None, stdin=subprocess.DEVNULL)
|
2020-05-18 12:56:16 +00:00
|
|
|
atexit.register(end_child_process, ep=ep)
|
2020-05-24 09:45:28 +00:00
|
|
|
signal.signal(signal.SIGTERM, lambda signal_no, frame: terminate_child_process(ep))
|
2020-04-15 02:20:23 +00: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-04 18:16:49 +00:00
|
|
|
|
|
|
|
|
2018-10-03 10:53:22 +00:00
|
|
|
if __name__ == '__main__':
|
2020-04-15 02:20:23 +00:00
|
|
|
restartfile = os.path.join(args.config_dir, 'bazarr.restart')
|
|
|
|
stopfile = os.path.join(args.config_dir, 'bazarr.stop')
|
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:
|
2018-10-03 10:53:22 +00:00
|
|
|
os.remove(restartfile)
|
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:
|
2018-10-03 10:53:22 +00:00
|
|
|
os.remove(stopfile)
|
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
|
|
|
|
print("Bazarr starting...")
|
|
|
|
start_bazarr()
|
2020-01-31 22:04:01 +00:00
|
|
|
|
2020-01-12 14:27:14 +00:00
|
|
|
# Keep the script running forever until stop is requested through term or keyboard interrupt
|
2020-04-15 02:20:23 +00:00
|
|
|
while True:
|
|
|
|
check_status()
|
|
|
|
try:
|
2020-04-28 03:03:24 +00:00
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
time.sleep(5)
|
|
|
|
else:
|
|
|
|
os.wait()
|
2020-05-18 23:28:21 +00:00
|
|
|
time.sleep(1)
|
2020-05-24 09:45:28 +00:00
|
|
|
except (KeyboardInterrupt, SystemExit, ChildProcessError):
|
2020-05-18 12:56:16 +00:00
|
|
|
print('Bazarr exited.')
|
|
|
|
sys.exit(0)
|