From 93378d1cd50c642ec015afce4920646829207c24 Mon Sep 17 00:00:00 2001 From: Shengcun-Scanner Date: Wed, 18 Nov 2026 03:19:43 -0500 Subject: [PATCH] fix: treat an unusable calibredb as "not in the library" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The argv call raises when the binary is missing or cannot be executed, while the old shell form answered 127 and its empty output simply meant the book was absent — that would have aborted a whole batch. Catch the OS error, warn, and keep the previous reading. Co-Authored-By: Shengcun --- skills/shelfmark/download_books.py | 8 +++++++- tests/skills/test_check_calibre.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/skills/shelfmark/download_books.py b/skills/shelfmark/download_books.py index f9930855..a7df5f27 100755 --- a/skills/shelfmark/download_books.py +++ b/skills/shelfmark/download_books.py @@ -294,7 +294,13 @@ def check_calibre(title: str) -> bool: import subprocess cmd = ["calibredb", "list", "--search", calibre_title_search(title)] - result = subprocess.run(cmd, check=False, capture_output=True, text=True) + try: + result = subprocess.run(cmd, check=False, capture_output=True, text=True) + except OSError as exc: + # calibredb missing, not executable, or unloadable: the old shell call + # answered 127 and its empty output read as "not in the library". + print(f"warning: cannot run calibredb ({exc}); treating the book as absent") + return False output = result.stdout.strip() # calibredb output has header line "id title authors", count non-header lines lines = [l.strip() for l in output.split("\n") if l.strip()] diff --git a/tests/skills/test_check_calibre.py b/tests/skills/test_check_calibre.py index f07282f4..95641742 100644 --- a/tests/skills/test_check_calibre.py +++ b/tests/skills/test_check_calibre.py @@ -70,6 +70,16 @@ def test_title_is_one_argument_and_one_query_term(title): assert _every_quote_escaped(inner), inner +@pytest.mark.parametrize("error", [FileNotFoundError("calibredb"), PermissionError("calibredb")]) +def test_missing_or_unusable_calibredb_reads_as_absent(error, capsys): + """A shell answered 127 and looked like "not in the library"; a missing or + unusable binary must not raise out of the batch.""" + module = _load_module() + with patch.object(subprocess, "run", side_effect=error): + assert module.check_calibre("Dune") is False + assert "cannot run calibredb" in capsys.readouterr().out + + def _every_quote_escaped(inner: str) -> bool: i = 0 while i < len(inner):