diff --git a/bazarr/init.py b/bazarr/init.py index 6e6af10ea..c2f1628db 100644 --- a/bazarr/init.py +++ b/bazarr/init.py @@ -2,18 +2,20 @@ import os import io -import rarfile import sys import subprocess import subliminal import datetime import time +import rarfile from dogpile.cache.region import register_backend as register_cache_backend from app.config import settings, configure_captcha_func +from app.database import init_db, migrate_db from app.get_args import args from app.logger import configure_logging +from utilities.binaries import get_binary, BinaryNotFound from utilities.path_mappings import path_mappings from utilities.backup import restore_from_backup @@ -193,23 +195,28 @@ with open(os.path.normpath(os.path.join(args.config_dir, 'config', 'config.ini') def init_binaries(): - from utilities.binaries import get_binary - exe = get_binary("unrar") - - rarfile.UNRAR_TOOL = exe - rarfile.ORIG_UNRAR_TOOL = exe try: - rarfile.custom_check([rarfile.UNRAR_TOOL], True) - except Exception: - logging.debug("custom check failed for: %s", exe) - - logging.debug("Using UnRAR from: %s", exe) - unrar = exe - - return unrar + exe = get_binary("unar") + rarfile.UNAR_TOOL = exe + rarfile.UNRAR_TOOL = None + rarfile.tool_setup(unrar=False, unar=True, bsdtar=False, force=True) + except (BinaryNotFound, rarfile.RarCannotExec): + try: + exe = get_binary("unrar") + rarfile.UNRAR_TOOL = exe + rarfile.UNAR_TOOL = None + rarfile.tool_setup(unrar=True, unar=False, bsdtar=False, force=True) + except (BinaryNotFound, rarfile.RarCannotExec): + logging.exception("BAZARR requires a rar archive extraction utilities (unrar, unar) and it can't be found.") + raise BinaryNotFound + else: + logging.debug("Using UnRAR from: %s", exe) + return exe + else: + logging.debug("Using unar from: %s", exe) + return exe -from app.database import init_db, migrate_db # noqa E402 init_db() migrate_db() init_binaries() diff --git a/libs/subliminal/providers/legendastv.py b/libs/subliminal/providers/legendastv.py index 80c433d83..5b8c3ad5b 100644 --- a/libs/subliminal/providers/legendastv.py +++ b/libs/subliminal/providers/legendastv.py @@ -171,9 +171,9 @@ class LegendasTVProvider(Provider): # Provider needs UNRAR installed. If not available raise ConfigurationError try: - rarfile.custom_check(rarfile.UNRAR_TOOL) - except rarfile.RarExecError: - raise ConfigurationError('UNRAR tool not available') + rarfile.tool_setup() + except rarfile.RarCannotExec: + raise ConfigurationError('RAR extraction tool not available') if any((username, password)) and not all((username, password)): raise ConfigurationError('Username and password must be specified') diff --git a/libs/subliminal_patch/providers/legendastv.py b/libs/subliminal_patch/providers/legendastv.py index 318c9a83d..3c3476b53 100644 --- a/libs/subliminal_patch/providers/legendastv.py +++ b/libs/subliminal_patch/providers/legendastv.py @@ -78,8 +78,8 @@ class LegendasTVProvider(_LegendasTVProvider): # Provider needs UNRAR installed. If not available raise ConfigurationError try: rarfile.tool_setup() - except rarfile.RarExecError: - raise ConfigurationError('UNRAR tool not available') + except rarfile.RarCannotExec: + raise ConfigurationError('RAR extraction tool not available') if any((username, password)) and not all((username, password)): raise ConfigurationError('Username and password must be specified') diff --git a/libs/subzero/lib/rar.py b/libs/subzero/lib/rar.py index e9a08bb3b..b82f667ec 100644 --- a/libs/subzero/lib/rar.py +++ b/libs/subzero/lib/rar.py @@ -15,7 +15,20 @@ class RarFile(rarfile.RarFile): :param psw: :return: """ - cmd = [rarfile.UNRAR_TOOL] + list(rarfile.ORIG_OPEN_ARGS) + exe = None + try: + rarfile.tool_setup(unrar=False, unar=True, bsdtar=False, force=True) + except rarfile.RarCannotExec: + try: + rarfile.rarfile.tool_setup(unrar=True, unar=False, bsdtar=False, force=True) + except rarfile.RarCannotExec: + raise rarfile.RarCannotExec + else: + exe = rarfile.UNRAR_TOOL + else: + exe = rarfile.UNAR_TOOL + finally: + cmd = [exe] + list(rarfile.ORIG_OPEN_ARGS) with rarfile.XTempFile(self._rarfile) as rf: log.debug(u"RAR CMD: %s", cmd + [rf, fname])