mirror of
https://github.com/morpheus65535/bazarr
synced 2025-01-18 13:01:10 +00:00
code cleanup: bazarr
This commit is contained in:
parent
a1a452e452
commit
0083ae3137
2 changed files with 55 additions and 60 deletions
114
bazarr.py
114
bazarr.py
|
@ -1,38 +1,32 @@
|
||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
|
|
||||||
from __future__ import absolute_import
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import bazarr.libs
|
|
||||||
from six import PY3
|
|
||||||
import subprocess as sp
|
|
||||||
import time
|
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
import platform
|
import platform
|
||||||
import re
|
|
||||||
import signal
|
import signal
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
from bazarr.get_args import args
|
from bazarr.get_args import args
|
||||||
|
from libs.six import PY3
|
||||||
|
|
||||||
|
|
||||||
def check_python_version():
|
def check_python_version():
|
||||||
python_version = platform.python_version_tuple()
|
python_version = platform.python_version_tuple()
|
||||||
minimum_python_version_tuple = (2, 7, 13)
|
minimum_py2_tuple = (2, 7, 13)
|
||||||
minimum_python3_version_tuple = (3, 6, 0)
|
minimum_py3_tuple = (3, 6, 0)
|
||||||
minimum_python_version = ".".join(str(i) for i in minimum_python_version_tuple)
|
minimum_py2_str = ".".join(str(i) for i in minimum_py2_tuple)
|
||||||
minimum_python3_version = ".".join(str(i) for i in minimum_python3_version_tuple)
|
minimum_py3_str = ".".join(str(i) for i in minimum_py3_tuple)
|
||||||
|
|
||||||
if int(python_version[0]) == minimum_python3_version_tuple[0]:
|
if (int(python_version[0]) == minimum_py3_tuple[0] and int(python_version[1]) < minimum_py3_tuple[1]) or \
|
||||||
if int(python_version[1]) >= minimum_python3_version_tuple[1]:
|
(int(python_version[0]) != minimum_py3_tuple[0] and int(python_version[0]) != minimum_py2_tuple[0]):
|
||||||
pass
|
print("Python " + minimum_py3_str + " or greater required. "
|
||||||
else:
|
"Current version is " + platform.python_version() + ". Please upgrade Python.")
|
||||||
print("Python " + minimum_python3_version + " or greater required. Current version is " + platform.python_version() + ". Please upgrade Python.")
|
sys.exit(1)
|
||||||
os._exit(0)
|
elif int(python_version[0]) == minimum_py2_tuple[0] and int(python_version[1]) < minimum_py2_tuple[1]:
|
||||||
|
print("Python " + minimum_py2_str + " or greater required. "
|
||||||
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]:
|
"Current version is " + platform.python_version() + ". Please upgrade Python.")
|
||||||
print("Python " + minimum_python_version + " or greater required. Current version is " + platform.python_version() + ". Please upgrade Python.")
|
sys.exit(1)
|
||||||
os._exit(0)
|
|
||||||
|
|
||||||
|
|
||||||
check_python_version()
|
check_python_version()
|
||||||
|
@ -61,11 +55,11 @@ class DaemonStatus(ProcessRegistry):
|
||||||
def unregister(self, process):
|
def unregister(self, process):
|
||||||
self.__processes.remove(process)
|
self.__processes.remove(process)
|
||||||
|
|
||||||
'''
|
|
||||||
Waits all the provided processes for the specified amount of time in seconds.
|
|
||||||
'''
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __wait_for_processes(processes, timeout):
|
def __wait_for_processes(processes, timeout):
|
||||||
|
"""
|
||||||
|
Waits all the provided processes for the specified amount of time in seconds.
|
||||||
|
"""
|
||||||
reference_ts = time.time()
|
reference_ts = time.time()
|
||||||
elapsed = 0
|
elapsed = 0
|
||||||
remaining_processes = list(processes)
|
remaining_processes = list(processes)
|
||||||
|
@ -80,22 +74,21 @@ class DaemonStatus(ProcessRegistry):
|
||||||
try:
|
try:
|
||||||
ep.wait(remaining_time)
|
ep.wait(remaining_time)
|
||||||
remaining_processes.remove(ep)
|
remaining_processes.remove(ep)
|
||||||
except sp.TimeoutExpired:
|
except subprocess.TimeoutExpired:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
'''
|
# In python 2 there is no such thing as some mechanism to wait with a timeout
|
||||||
In python 2 there is no such thing as some mechanism to wait with a timeout.
|
|
||||||
'''
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
elapsed = time.time() - reference_ts
|
elapsed = time.time() - reference_ts
|
||||||
remaining_time = timeout - elapsed
|
remaining_time = timeout - elapsed
|
||||||
return remaining_processes
|
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
|
@staticmethod
|
||||||
def __send_signal(processes, signal_no, live_processes=None):
|
def __send_signal(processes, signal_no, live_processes=None):
|
||||||
|
"""
|
||||||
|
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.
|
||||||
|
"""
|
||||||
for ep in processes:
|
for ep in processes:
|
||||||
if ep.poll() is None:
|
if ep.poll() is None:
|
||||||
if live_processes is not None:
|
if live_processes is not None:
|
||||||
|
@ -103,13 +96,14 @@ class DaemonStatus(ProcessRegistry):
|
||||||
try:
|
try:
|
||||||
ep.send_signal(signal_no)
|
ep.send_signal(signal_no)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Failed sending signal %s to process %s because of an unexpected error: %s' % (signal_no, ep.pid, e))
|
print('Failed sending signal %s to process %s because of an unexpected error: %s' % (
|
||||||
|
signal_no, ep.pid, e))
|
||||||
return live_processes
|
return live_processes
|
||||||
|
|
||||||
'''
|
|
||||||
Flags this instance as should stop and terminates as smoothly as possible children processes.
|
|
||||||
'''
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
|
"""
|
||||||
|
Flags this instance as should stop and terminates as smoothly as possible children processes.
|
||||||
|
"""
|
||||||
self.__should_stop = True
|
self.__should_stop = True
|
||||||
live_processes = DaemonStatus.__send_signal(self.__processes, signal.SIGINT, list())
|
live_processes = DaemonStatus.__send_signal(self.__processes, signal.SIGINT, list())
|
||||||
live_processes = DaemonStatus.__wait_for_processes(live_processes, 120)
|
live_processes = DaemonStatus.__wait_for_processes(live_processes, 120)
|
||||||
|
@ -117,12 +111,12 @@ class DaemonStatus(ProcessRegistry):
|
||||||
|
|
||||||
def should_stop(self):
|
def should_stop(self):
|
||||||
return self.__should_stop
|
return self.__should_stop
|
||||||
|
|
||||||
|
|
||||||
def start_bazarr(process_registry=ProcessRegistry()):
|
def start_bazarr(process_registry=ProcessRegistry()):
|
||||||
script = [sys.executable, "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:]
|
script = [sys.executable, "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:]
|
||||||
|
|
||||||
ep = sp.Popen(script, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE)
|
ep = subprocess.Popen(script, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
|
||||||
process_registry.register(ep)
|
process_registry.register(ep)
|
||||||
print("Bazarr starting...")
|
print("Bazarr starting...")
|
||||||
try:
|
try:
|
||||||
|
@ -144,44 +138,44 @@ def start_bazarr(process_registry=ProcessRegistry()):
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
restartfile = os.path.normcase(os.path.join(args.config_dir, 'bazarr.restart'))
|
restartfile = os.path.normcase(os.path.join(args.config_dir, 'bazarr.restart'))
|
||||||
stopfile = os.path.normcase(os.path.join(args.config_dir, 'bazarr.stop'))
|
stopfile = os.path.normcase(os.path.join(args.config_dir, 'bazarr.stop'))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.remove(restartfile)
|
os.remove(restartfile)
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.remove(stopfile)
|
os.remove(stopfile)
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def daemon(bazarr_runner = lambda: start_bazarr()):
|
def daemon(bazarr_runner=lambda: start_bazarr()):
|
||||||
if os.path.exists(stopfile):
|
if os.path.exists(stopfile):
|
||||||
try:
|
try:
|
||||||
os.remove(stopfile)
|
os.remove(stopfile)
|
||||||
except:
|
except Exception:
|
||||||
print('Unable to delete stop file.')
|
print('Unable to delete stop file.')
|
||||||
else:
|
else:
|
||||||
print('Bazarr exited.')
|
print('Bazarr exited.')
|
||||||
os._exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if os.path.exists(restartfile):
|
if os.path.exists(restartfile):
|
||||||
try:
|
try:
|
||||||
os.remove(restartfile)
|
os.remove(restartfile)
|
||||||
except:
|
except Exception:
|
||||||
print('Unable to delete restart file.')
|
print('Unable to delete restart file.')
|
||||||
else:
|
else:
|
||||||
bazarr_runner()
|
bazarr_runner()
|
||||||
|
|
||||||
|
|
||||||
bazarr_runner = lambda: start_bazarr()
|
bazarr_runner = lambda: start_bazarr()
|
||||||
|
|
||||||
should_stop = lambda: False
|
should_stop = lambda: False
|
||||||
|
|
||||||
if PY3:
|
if PY3:
|
||||||
daemonStatus = DaemonStatus()
|
daemonStatus = DaemonStatus()
|
||||||
|
|
||||||
def shutdown():
|
def shutdown():
|
||||||
# indicates that everything should stop
|
# indicates that everything should stop
|
||||||
daemonStatus.stop()
|
daemonStatus.stop()
|
||||||
|
@ -189,12 +183,12 @@ if __name__ == '__main__':
|
||||||
os.kill(os.getpid(), signal.SIGINT)
|
os.kill(os.getpid(), signal.SIGINT)
|
||||||
|
|
||||||
signal.signal(signal.SIGTERM, lambda signal_no, frame: shutdown())
|
signal.signal(signal.SIGTERM, lambda signal_no, frame: shutdown())
|
||||||
|
|
||||||
should_stop = lambda: daemonStatus.should_stop()
|
should_stop = lambda: daemonStatus.should_stop()
|
||||||
bazarr_runner = lambda: start_bazarr(daemonStatus)
|
bazarr_runner = lambda: start_bazarr(daemonStatus)
|
||||||
|
|
||||||
bazarr_runner()
|
bazarr_runner()
|
||||||
|
|
||||||
# Keep the script running forever until stop is requested through term or keyboard interrupt
|
# Keep the script running forever until stop is requested through term or keyboard interrupt
|
||||||
while not should_stop():
|
while not should_stop():
|
||||||
daemon(bazarr_runner)
|
daemon(bazarr_runner)
|
||||||
|
|
1
libs/__init__.py
Normal file
1
libs/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
# coding=utf-8
|
Loading…
Reference in a new issue