fix: treat an unusable calibredb as "not in the library"

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 <business@rs-systems-web.com>
This commit is contained in:
Shengcun-Scanner committed 2026-11-18 03:19:43 -05:00
1 parent ce4a81b5b3
commit 93378d1cd5
2 files changed
+17 -1

No files matched your search

+7 -1
View File
@@ -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()]
+10
View File
@@ -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):