refactor: one torrentzip comment test for capture

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 06:02:01 +01:00
1 parent 30cd881ef3
commit eebe6a66c3
2 files changed
+8 -5

No files matched your search

+6 -1
View File
@@ -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)
+2 -4
View File
@@ -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