From cd6c527d89e4ea660677909586a54e2c9ddcd151 Mon Sep 17 00:00:00 2001 From: josdion Date: Tue, 14 Apr 2020 09:40:37 +0300 Subject: [PATCH 1/4] FIx #928 - don't raise an exception in case of unsupported archive --- libs/subliminal_patch/providers/subssabbz.py | 3 ++- libs/subliminal_patch/providers/subsunacs.py | 3 ++- libs/subliminal_patch/providers/yavkanet.py | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/libs/subliminal_patch/providers/subssabbz.py b/libs/subliminal_patch/providers/subssabbz.py index f2bb05450..de93e47b3 100644 --- a/libs/subliminal_patch/providers/subssabbz.py +++ b/libs/subliminal_patch/providers/subssabbz.py @@ -188,4 +188,5 @@ class SubsSabBzProvider(Provider): elif is_zipfile(archive_stream): return self.process_archive_subtitle_files( ZipFile(archive_stream), language, video, link ) else: - raise ValueError('Not a valid archive') + logger.error('Ignore unsupported archive %r', request.headers) + return [] diff --git a/libs/subliminal_patch/providers/subsunacs.py b/libs/subliminal_patch/providers/subsunacs.py index 87c97c486..e3237b80b 100644 --- a/libs/subliminal_patch/providers/subsunacs.py +++ b/libs/subliminal_patch/providers/subsunacs.py @@ -196,4 +196,5 @@ class SubsUnacsProvider(Provider): elif is_zipfile(archive_stream): return self.process_archive_subtitle_files( ZipFile(archive_stream), language, video, link ) else: - raise ValueError('Not a valid archive') + logger.error('Ignore unsupported archive %r', request.headers) + return [] diff --git a/libs/subliminal_patch/providers/yavkanet.py b/libs/subliminal_patch/providers/yavkanet.py index d695245ee..1ea0d2ca0 100644 --- a/libs/subliminal_patch/providers/yavkanet.py +++ b/libs/subliminal_patch/providers/yavkanet.py @@ -175,5 +175,5 @@ class YavkaNetProvider(Provider): elif is_zipfile(archive_stream): return self.process_archive_subtitle_files( ZipFile(archive_stream), language, video, link ) else: - raise ValueError('Not a valid archive') - + logger.error('Ignore unsupported archive %r', request.headers) + return [] From 2c6a4583d001528002b02e71bbf01586c3671007 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Tue, 14 Apr 2020 22:20:23 -0400 Subject: [PATCH 2/4] Simplify daemon mechanism. --- bazarr.py | 164 +++++++++++------------------------------------ views/system.tpl | 19 ++---- 2 files changed, 46 insertions(+), 137 deletions(-) diff --git a/bazarr.py b/bazarr.py index 9466fbd02..30aa26755 100644 --- a/bazarr.py +++ b/bazarr.py @@ -2,28 +2,25 @@ import os import platform -import signal import subprocess import sys import time +import atexit from bazarr.get_args import args -from libs.six import PY3 def check_python_version(): python_version = platform.python_version_tuple() - minimum_py2_tuple = (2, 7, 13) minimum_py3_tuple = (3, 7, 0) - minimum_py2_str = ".".join(str(i) for i in minimum_py2_tuple) minimum_py3_str = ".".join(str(i) for i in minimum_py3_tuple) - if int(python_version[0]) < 3: + if int(python_version[0]) < minimum_py3_tuple[0]: print("Python " + minimum_py3_str + " or greater required. " "Current version is " + platform.python_version() + ". Please upgrade Python.") sys.exit(1) elif (int(python_version[0]) == minimum_py3_tuple[0] and int(python_version[1]) < minimum_py3_tuple[1]) or \ - (int(python_version[0]) != minimum_py3_tuple[0] and int(python_version[0]) != minimum_py2_tuple[0]): + (int(python_version[0]) != minimum_py3_tuple[0]): print("Python " + minimum_py3_str + " or greater required. " "Current version is " + platform.python_version() + ". Please upgrade Python.") sys.exit(1) @@ -34,138 +31,55 @@ check_python_version() 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) - - @staticmethod - 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: - 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 - - def stop(self): - """ - Flags this instance as should stop and terminates as smoothly as possible children processes. - """ - self.__should_stop = True - DaemonStatus.__send_signal(self.__processes, signal.SIGINT, list()) - - def force_stop(self): - self.__should_stop = True - DaemonStatus.__send_signal(self.__processes, signal.SIGTERM) - - def should_stop(self): - return self.__should_stop - - -def start_bazarr(process_registry=ProcessRegistry()): +def start_bazarr(): script = [sys.executable, "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:] + ep = subprocess.Popen(script, stdout=None, stderr=None, stdin=subprocess.DEVNULL) + atexit.register(lambda: ep.kill()) - print("Bazarr starting...") - if PY3: - ep = subprocess.Popen(script, stdout=None, stderr=None, stdin=subprocess.DEVNULL) - else: - ep = subprocess.Popen(script, stdout=None, stderr=None, stdin=None) - process_registry.register(ep) - try: - ep.wait() - process_registry.unregister(ep) - except KeyboardInterrupt: - pass + +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() 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')) + restartfile = os.path.join(args.config_dir, 'bazarr.restart') + stopfile = os.path.join(args.config_dir, 'bazarr.stop') + # Cleanup leftover files try: os.remove(restartfile) - except Exception: + except FileNotFoundError: pass try: os.remove(stopfile) - except Exception: + except FileNotFoundError: pass - - def daemon(bazarr_runner=lambda: start_bazarr()): - if os.path.exists(stopfile): - try: - os.remove(stopfile) - except Exception: - print('Unable to delete stop file.') - else: - print('Bazarr exited.') - sys.exit(0) - - if os.path.exists(restartfile): - try: - os.remove(restartfile) - except Exception: - print('Unable to delete restart file.') - else: - bazarr_runner() - - - bazarr_runner = lambda: start_bazarr() - - should_stop = lambda: False - - if PY3: - daemonStatus = DaemonStatus() - - def force_shutdown(): - # force the killing of children processes - daemonStatus.force_stop() - # if a new SIGTERM signal is caught the standard behaviour should be followed - signal.signal(signal.SIGTERM, signal.SIG_DFL) - # emulate a Ctrl C command on itself (bypasses the signal thing but, then, emulates the "Ctrl+C break") - os.kill(os.getpid(), signal.SIGINT) - - def shutdown(): - # indicates that everything should stop - daemonStatus.stop() - # if a new sigterm signal is caught it should force the shutdown of children processes - signal.signal(signal.SIGTERM, lambda signal_no, frame: force_shutdown()) - - signal.signal(signal.SIGTERM, lambda signal_no, frame: shutdown()) - - should_stop = lambda: daemonStatus.should_stop() - bazarr_runner = lambda: start_bazarr(daemonStatus) - - bazarr_runner() + # Initial start of main bazarr process + print("Bazarr starting...") + start_bazarr() # Keep the script running forever until stop is requested through term or keyboard interrupt - while not should_stop(): - daemon(bazarr_runner) - time.sleep(1) + while True: + check_status() + try: + time.sleep(1) + except (KeyboardInterrupt, SystemExit): + pass diff --git a/views/system.tpl b/views/system.tpl index e3a04ca81..dd73ba382 100644 --- a/views/system.tpl +++ b/views/system.tpl @@ -403,15 +403,12 @@ }); $('#shutdown').on('click', function(){ - $.ajax({ - url: "{{base_url}}shutdown", - async: false + document.open(); + document.write('Bazarr has shutdown.'); + document.close(); + $.ajax({ + url: "{{base_url}}shutdown" }) - .always(function(){ - document.open(); - document.write('Bazarr has shutdown.'); - document.close(); - }); }); $('#logout').on('click', function(){ @@ -422,11 +419,9 @@ $('#loader_text').text("Bazarr is restarting, please wait..."); $.ajax({ url: "{{base_url}}restart", - async: true, - error: (function () { - setTimeout(function () { setInterval(ping, 2000); }, 8000); - }) + async: true }); + setTimeout(function () { setInterval(ping, 2000); }, 8000); }); % from config import settings From b434ff2666b1887dbeba85a0d6461195582f103d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Wed, 15 Apr 2020 12:54:07 -0400 Subject: [PATCH 3/4] Simplify daemon mechanism. --- bazarr.py | 2 +- bazarr/main.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bazarr.py b/bazarr.py index 30aa26755..d4929937a 100644 --- a/bazarr.py +++ b/bazarr.py @@ -80,6 +80,6 @@ if __name__ == '__main__': while True: check_status() try: - time.sleep(1) + time.sleep(5) except (KeyboardInterrupt, SystemExit): pass diff --git a/bazarr/main.py b/bazarr/main.py index b12c2c543..13f7ef189 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -222,7 +222,7 @@ def doShutdown(): else: stop_file.write(six.text_type('')) stop_file.close() - sys.exit(0) + os._exit(0) @route(base_url + 'restart') @@ -243,7 +243,7 @@ def restart(): logging.info('Bazarr is being restarted...') restart_file.write(six.text_type('')) restart_file.close() - sys.exit(0) + os._exit(0) @route(base_url + 'wizard') From 59c9ed1fcf68767d1c0effd33b10b9d80ea912c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Fri, 17 Apr 2020 09:24:47 -0400 Subject: [PATCH 4/4] Fix for PGS subtitles not ignored correctly. --- bazarr/list_subtitles.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bazarr/list_subtitles.py b/bazarr/list_subtitles.py index c1a8794e5..ff9fd3d97 100644 --- a/bazarr/list_subtitles.py +++ b/bazarr/list_subtitles.py @@ -40,7 +40,7 @@ def store_subtitles(original_path, reversed_path): subtitle_languages = embedded_subs_reader.list_languages(reversed_path) for subtitle_language, subtitle_forced, subtitle_codec in subtitle_languages: try: - if settings.general.getboolean("ignore_pgs_subs") and subtitle_codec == "hdmv_pgs_subtitle": + if settings.general.getboolean("ignore_pgs_subs") and subtitle_codec == "PGS": logging.debug("BAZARR skipping pgs sub for language: " + str(alpha2_from_alpha3(subtitle_language))) continue @@ -116,7 +116,7 @@ def store_subtitles_movie(original_path, reversed_path): subtitle_languages = embedded_subs_reader.list_languages(reversed_path) for subtitle_language, subtitle_forced, subtitle_codec in subtitle_languages: try: - if settings.general.getboolean("ignore_pgs_subs") and subtitle_codec == "hdmv_pgs_subtitle": + if settings.general.getboolean("ignore_pgs_subs") and subtitle_codec == "PGS": logging.debug("BAZARR skipping pgs sub for language: " + str(alpha2_from_alpha3(subtitle_language))) continue