From fc004f4f0c9d4bbb0e9817f6983f82bce76dd2ac Mon Sep 17 00:00:00 2001 From: Thore Schillmann Date: Thu, 7 Jul 2022 11:41:51 +0000 Subject: [PATCH 1/2] moved `get_calibre_binarypath()` to `helper.py` --- cps/admin.py | 5 ++--- cps/config_sql.py | 11 +---------- cps/constants.py | 2 +- cps/helper.py | 15 +++++++++++++-- cps/tasks/convert.py | 6 +++--- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/cps/admin.py b/cps/admin.py index 149d4aa4..e487b5ca 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -43,7 +43,7 @@ from . import constants, logger, helper, services, cli_param from . import db, calibre_db, ub, web_server, config, updater_thread, gdriveutils, \ kobo_sync_status, schedule from .helper import check_valid_domain, send_test_mail, reset_password, generate_password_hash, check_email, \ - valid_email, check_username + valid_email, check_username, get_calibre_binarypath from .gdriveutils import is_gdrive_ready, gdrive_support from .render_template import render_title_template, get_sidebar_config from .services.worker import WorkerThread @@ -1727,8 +1727,7 @@ def _configuration_update_helper(): calibre_status = helper.check_calibre(config.config_binariesdir) if calibre_status: return _configuration_result(calibre_status) - # ToDo: Remove this and 'self.config_converterpath' and replace with 'config.get_calibre_binarypath("ebook-convert")' everywhere - to_save["config_converterpath"] = config.get_calibre_binarypath("ebook-convert") + to_save["config_converterpath"] = get_calibre_binarypath("ebook-convert") _config_string(to_save, "config_converterpath") reboot_required |= _config_int(to_save, "config_login_type") diff --git a/cps/config_sql.py b/cps/config_sql.py index 7867b6f3..9b8c29fa 100644 --- a/cps/config_sql.py +++ b/cps/config_sql.py @@ -269,15 +269,6 @@ class _ConfigSQL(object): def get_scheduled_task_settings(self): return {k:v for k, v in self.__dict__.items() if k.startswith('schedule_')} - def get_calibre_binarypath(self, binary): - binariesdir = self.config_binariesdir - if binariesdir: - if binary in constants.SUPPORTED_CALIBRE_BINARIES: - return os.path.join(binariesdir, binary) - else: - raise ValueError("'{}' is not a supported Calibre binary".format(binary)) - return "" - def set_from_dictionary(self, dictionary, field, convertor=None, default=None, encode=None): """Possibly updates a field of this object. The new value, if present, is grabbed from the given dictionary, and optionally passed through a convertor. @@ -428,7 +419,7 @@ def autodetect_calibre_binaries(): else: calibre_path = ["/opt/calibre/"] for element in calibre_path: - supported_binary_paths = [os.path.join(element, binary) for binary in constants.SUPPORTED_CALIBRE_BINARIES] + supported_binary_paths = [os.path.join(element, binary) for binary in constants.SUPPORTED_CALIBRE_BINARIES.values()] if all(os.path.isfile(binary_path) and os.access(binary_path, os.X_OK) for binary_path in supported_binary_paths): values = [process_wait([binary_path, "--version"], pattern='\(calibre (.*)\)') for binary_path in supported_binary_paths] if all(values): diff --git a/cps/constants.py b/cps/constants.py index 849f9562..b613d0aa 100644 --- a/cps/constants.py +++ b/cps/constants.py @@ -154,7 +154,7 @@ EXTENSIONS_UPLOAD = {'txt', 'pdf', 'epub', 'kepub', 'mobi', 'azw', 'azw3', 'cbr' _extension = "" if sys.platform == "win32": _extension = ".exe" -SUPPORTED_CALIBRE_BINARIES = [binary + _extension for binary in ["ebook-convert", "calibredb"]] +SUPPORTED_CALIBRE_BINARIES = {binary:binary + _extension for binary in ["ebook-convert", "calibredb"]} def has_flag(value, bit_flag): diff --git a/cps/helper.py b/cps/helper.py index 31b15eb9..a16245fa 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -951,7 +951,7 @@ def check_calibre(calibre_location): return _('Please specify a directory, not a file') try: - supported_binary_paths = [os.path.join(calibre_location, binary) for binary in SUPPORTED_CALIBRE_BINARIES] + supported_binary_paths = [os.path.join(calibre_location, binary) for binary in SUPPORTED_CALIBRE_BINARIES.values()] binaries_available=[os.path.isfile(binary_path) and os.access(binary_path, os.X_OK) for binary_path in supported_binary_paths] if all(binaries_available): values = [process_wait([binary_path, "--version"], pattern='\(calibre (.*)\)') for binary_path in supported_binary_paths] @@ -961,7 +961,7 @@ def check_calibre(calibre_location): else: return _('Calibre binaries not viable') else: - missing_binaries=[path for path, available in zip(SUPPORTED_CALIBRE_BINARIES, binaries_available) if not available] + missing_binaries=[path for path, available in zip(SUPPORTED_CALIBRE_BINARIES.values(), binaries_available) if not available] return _('Missing calibre binaries: %(missing)s', missing=", ".join(missing_binaries)) except (OSError, UnicodeDecodeError) as err: @@ -1032,6 +1032,17 @@ def get_download_link(book_id, book_format, client): abort(404) +def get_calibre_binarypath(binary): + binariesdir = config.config_binariesdir + if binariesdir: + try: + return os.path.join(binariesdir, SUPPORTED_CALIBRE_BINARIES[binary]) + except KeyError as ex: + log.error("Binary not supported by Calibre-Web: %s", SUPPORTED_CALIBRE_BINARIES[binary]) + pass + return "" + + def clear_cover_thumbnail_cache(book_id): if config.schedule_generate_book_covers: WorkerThread.add(None, TaskClearCoverThumbnailCache(book_id), hidden=True) diff --git a/cps/tasks/convert.py b/cps/tasks/convert.py index 1c9975e1..25cf562d 100644 --- a/cps/tasks/convert.py +++ b/cps/tasks/convert.py @@ -37,7 +37,7 @@ from cps.ub import init_db_thread from cps.tasks.mail import TaskEmail from cps import gdriveutils - +from cps.constants import SUPPORTED_CALIBRE_BINARIES log = logger.create() @@ -238,7 +238,7 @@ class TaskConvert(CalibreTask): tmp_dir = os.path.join(gettempdir(), 'calibre_web') if not os.path.isdir(tmp_dir): os.mkdir(tmp_dir) - calibredb_binarypath = config.get_calibre_binarypath("calibredb") + calibredb_binarypath = os.path.join(config.config_binariesdir, SUPPORTED_CALIBRE_BINARIES["calibredb"]) opf_command = [calibredb_binarypath, 'show_metadata', '--as-opf', str(book_id), '--with-library', config.config_calibre_dir] p = process_open(opf_command, quotes) path_tmp_opf = os.path.join(tmp_dir, "metadata_" + str(current_milli_time()) + ".opf") @@ -258,7 +258,7 @@ class TaskConvert(CalibreTask): quotes_index += 1 p = process_open(command, quotes, newlines=False) - except (ValueError, OSError) as e: + except OSError as e: return 1, N_(u"Ebook-converter failed: %(error)s", error=e) while p.poll() is None: From 4913673e8fe4f394b48926252402b66c95a7a1fd Mon Sep 17 00:00:00 2001 From: Thore Schillmann Date: Thu, 7 Jul 2022 11:47:10 +0000 Subject: [PATCH 2/2] added `subprocess.wait()` when getting metadata --- cps/tasks/convert.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cps/tasks/convert.py b/cps/tasks/convert.py index 25cf562d..79d7ddbd 100644 --- a/cps/tasks/convert.py +++ b/cps/tasks/convert.py @@ -241,6 +241,7 @@ class TaskConvert(CalibreTask): calibredb_binarypath = os.path.join(config.config_binariesdir, SUPPORTED_CALIBRE_BINARIES["calibredb"]) opf_command = [calibredb_binarypath, 'show_metadata', '--as-opf', str(book_id), '--with-library', config.config_calibre_dir] p = process_open(opf_command, quotes) + p.wait() path_tmp_opf = os.path.join(tmp_dir, "metadata_" + str(current_milli_time()) + ".opf") with open(path_tmp_opf, 'w') as fd: copyfileobj(p.stdout, fd)