diff --git a/scripts/core/torrentzip.py b/scripts/core/torrentzip.py index 949468f40..331d8595b 100644 --- a/scripts/core/torrentzip.py +++ b/scripts/core/torrentzip.py @@ -180,7 +180,12 @@ def rebuild_torrentzip(source: str | Path) -> bytes: return build_torrentzip(members) +def has_torrentzip_comment(comment: bytes) -> bool: + """Report whether an archive comment is the TorrentZip signature.""" + return comment.startswith(_COMMENT_PREFIX.encode("ascii")) + + def is_torrentzip(source: str | Path) -> bool: """Report whether an archive already carries the TorrentZip signature.""" with zipfile.ZipFile(source) as zf: - return zf.comment.decode("ascii", "replace").startswith(_COMMENT_PREFIX) + return has_torrentzip_comment(zf.comment) diff --git a/scripts/romrecipes/capture.py b/scripts/romrecipes/capture.py index 7ea253246..fc8a40d5a 100644 --- a/scripts/romrecipes/capture.py +++ b/scripts/romrecipes/capture.py @@ -11,14 +11,12 @@ from pathlib import Path from typing import BinaryIO from core.hashing import parse_hash_list -from core.torrentzip import write_torrentzip, zlib_ng_version +from core.torrentzip import has_torrentzip_comment, write_torrentzip, zlib_ng_version from romrecipes.ledger import database_archives from romrecipes.members import member_sha1, member_opener from romrecipes.pins import container_entries from romrecipes.romsets import collect_recipes, load_dat_recipes -_TORRENTZIP_COMMENT = b"TORRENTZIPPED-" - # --- capture ---------------------------------------------------------------- @@ -44,7 +42,7 @@ class HashSink: def _file_infos(path: Path) -> tuple[list[zipfile.ZipInfo], list[str], bool]: with zipfile.ZipFile(path) as archive: infos = archive.infolist() - torrentzip = archive.comment.startswith(_TORRENTZIP_COMMENT) + torrentzip = has_torrentzip_comment(archive.comment) files = [info for info in infos if not info.is_dir()] directories = [info.filename for info in infos if info.is_dir()] return files, directories, torrentzip