bazarr/bazarr.py

202 lines
6.5 KiB
Python
Raw Normal View History

# coding=utf-8
2019-09-13 19:12:26 +00:00
from __future__ import absolute_import
from __future__ import print_function
2019-09-28 16:38:26 +00:00
import bazarr.libs
2019-09-23 20:21:24 +00:00
from six import PY3
import subprocess as sp
import time
import os
import sys
2019-09-05 15:30:14 +00:00
import platform
import re
import signal
from bazarr.get_args import args
2019-09-05 15:30:14 +00:00
def check_python_version():
python_version = platform.python_version_tuple()
minimum_python_version_tuple = (2, 7, 13)
2019-09-13 19:12:26 +00:00
minimum_python3_version_tuple = (3, 6, 0)
2019-09-05 15:30:14 +00:00
minimum_python_version = ".".join(str(i) for i in minimum_python_version_tuple)
2019-09-13 19:12:26 +00:00
minimum_python3_version = ".".join(str(i) for i in minimum_python3_version_tuple)
2019-09-05 15:30:14 +00:00
2019-09-23 02:27:23 +00:00
if int(python_version[0]) == minimum_python3_version_tuple[0]:
if int(python_version[1]) >= minimum_python3_version_tuple[1]:
pass
else:
print("Python " + minimum_python3_version + " or greater required. Current version is " + platform.python_version() + ". Please upgrade Python.")
os._exit(0)
2019-09-05 15:30:14 +00:00
elif int(python_version[1]) < minimum_python_version_tuple[1] or int(re.search(r'\d+', python_version[2]).group()) < minimum_python_version_tuple[2]:
2019-09-13 19:12:26 +00:00
print("Python " + minimum_python_version + " or greater required. Current version is " + platform.python_version() + ". Please upgrade Python.")
2019-09-05 15:30:14 +00:00
os._exit(0)
check_python_version()
2018-10-11 01:23:30 +00:00
dir_name = os.path.dirname(__file__)
class ProcessRegistry:
def register(self, process):
pass
def unregister(self, process):
pass
class DaemonStatus(ProcessRegistry):
def __init__(self):
self.__should_stop = False
self.__processes = set()
def register(self, process):
self.__processes.add(process)
def unregister(self, process):
self.__processes.remove(process)
'''
Waits all the provided processes for the specified amount of time in seconds.
'''
@staticmethod
def __wait_for_processes(processes, timeout):
reference_ts = time.time()
elapsed = 0
remaining_processes = list(processes)
while elapsed < timeout and len(remaining_processes) > 0:
remaining_time = timeout - elapsed
for ep in list(remaining_processes):
if ep.poll() is not None:
remaining_processes.remove(ep)
else:
if remaining_time > 0:
if PY3:
try:
ep.wait(remaining_time)
remaining_processes.remove(ep)
except sp.TimeoutExpired:
pass
else:
'''
In python 2 there is no such thing as some mechanism to wait with a timeout.
'''
time.sleep(1)
elapsed = time.time() - reference_ts
remaining_time = timeout - elapsed
return remaining_processes
'''
Sends to every single of the specified processes the given signal and (if live_processes is not None) append to it processes which are still alive.
'''
@staticmethod
def __send_signal(processes, signal_no, live_processes=None):
for ep in processes:
if ep.poll() is None:
if live_processes is not None:
live_processes.append(ep)
try:
ep.send_signal(signal_no)
except Exception as e:
print('Failed sending signal %s to process %s because of an unexpected error: %s' % (signal_no, ep.pid, e))
return live_processes
'''
Flags this instance as should stop and terminates as smoothly as possible children processes.
'''
def stop(self):
self.__should_stop = True
live_processes = DaemonStatus.__send_signal(self.__processes, signal.SIGINT, list())
live_processes = DaemonStatus.__wait_for_processes(live_processes, 120)
DaemonStatus.__send_signal(live_processes, signal.SIGTERM)
def should_stop(self):
return self.__should_stop
def start_bazarr(process_registry=ProcessRegistry()):
script = [sys.executable, "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:]
2019-06-11 18:45:48 +00:00
ep = sp.Popen(script, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE)
process_registry.register(ep)
2019-09-13 19:12:26 +00:00
print("Bazarr starting...")
try:
2019-12-07 03:40:40 +00:00
while True:
line = ep.stdout.readline()
if line == '' or not line:
# Process ended so let's unregister it
process_registry.unregister(ep)
2019-12-07 03:40:40 +00:00
break
2019-09-23 20:21:24 +00:00
if PY3:
sys.stdout.buffer.write(line)
else:
sys.stdout.write(line)
sys.stdout.flush()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
restartfile = os.path.normcase(os.path.join(args.config_dir, 'bazarr.restart'))
stopfile = os.path.normcase(os.path.join(args.config_dir, 'bazarr.stop'))
2019-06-11 18:45:48 +00:00
try:
os.remove(restartfile)
2018-10-11 01:23:30 +00:00
except:
pass
2019-06-11 18:45:48 +00:00
2018-10-11 01:23:30 +00:00
try:
os.remove(stopfile)
except:
pass
2019-06-11 18:45:48 +00:00
def daemon(bazarr_runner = lambda: start_bazarr()):
if os.path.exists(stopfile):
try:
os.remove(stopfile)
except:
2019-09-13 19:12:26 +00:00
print('Unable to delete stop file.')
else:
2019-09-13 19:12:26 +00:00
print('Bazarr exited.')
os._exit(0)
2019-06-11 18:45:48 +00:00
if os.path.exists(restartfile):
try:
os.remove(restartfile)
except:
2019-09-13 19:12:26 +00:00
print('Unable to delete restart file.')
else:
bazarr_runner()
bazarr_runner = lambda: start_bazarr()
2019-06-11 18:45:48 +00:00
should_stop = lambda: False
2019-06-11 18:45:48 +00:00
if PY3:
daemonStatus = DaemonStatus()
def shutdown():
# indicates that everything should stop
daemonStatus.stop()
# emulate a Ctrl C command on itself (bypasses the signal thing but, then, emulates the "Ctrl+C break")
os.kill(os.getpid(), signal.SIGINT)
signal.signal(signal.SIGTERM, lambda signal_no, frame: shutdown())
should_stop = lambda: daemonStatus.should_stop()
bazarr_runner = lambda: start_bazarr(daemonStatus)
bazarr_runner()
2019-06-11 18:45:48 +00:00
# Keep the script running forever until stop is requested through term or keyboard interrupt
while not should_stop():
daemon(bazarr_runner)
2019-05-31 23:00:32 +00:00
time.sleep(1)