refactor: one digest loop for files and streams

This commit is contained in:
Abdessamad Derraz committed 2026-11-18 21:42:28 +01:00
1 parent 88581cf95d
commit 2c8a9cfad8
11 files changed
+88 -98

No files matched your search

+3 -2
View File
@@ -29,6 +29,7 @@ from unattended import runner
from unattended import timers as unattended_timers
from unattended import worktree as unattended_worktree
from core import childenv
from core.hashing import file_digest
from core import hostguard
from core import statefile
import bios_integrity as bib
@@ -789,7 +790,7 @@ class AssetSync(Scratch):
(self.root / rel).parent.mkdir(parents=True, exist_ok=True)
(self.root / rel).write_bytes(data)
self.db = {"files": {
sync.sha1_of(self.root / "bios/A/big.bin"): {"path": "bios/A/big.bin", "size": 1},
file_digest(self.root / "bios/A/big.bin"): {"path": "bios/A/big.bin", "size": 1},
"0" * 40: {"path": "bios/C/solo.iso", "size": 1},
}}
@@ -853,7 +854,7 @@ class BiosIntegrity(Scratch):
path.write_bytes(data)
# An old mtime keeps git from re-reading the file as racily clean.
os.utime(path, (self.OLD, self.OLD))
sha1 = bib.sha1_of(path)
sha1 = file_digest(path)
self.db["files"][sha1] = {"path": rel, "sha1": sha1}
run_git(self.repo, "add", ".gitignore", *[r for r in files if "Big" not in r])
run_git(self.repo, "commit", "--quiet", "-m", "chore: start")
+42 -1
View File
@@ -1,12 +1,16 @@
"""compute_hashes reads a file once per process and change."""
"""compute_hashes reads a file once per process and change, and one loop digests files and streams."""
from __future__ import annotations
import tests
import hashlib
import io
import shutil
import tempfile
import unittest
import zipfile
import zlib
from pathlib import Path
from unittest import mock
@@ -41,5 +45,42 @@ class HashesAreReadOncePerContent(unittest.TestCase):
self.assertNotEqual(hashing.compute_hashes(self.file)["sha1"], first["sha1"])
class OneDigestLoopForFilesAndStreams(unittest.TestCase):
"""A ZIP member, a file and an open stream are hashed by the same loop,
so the pack check, the PR review and the recipes agree with the resolver."""
DATA = bytes(range(256)) * 600 # over two 64 KiB reads
def setUp(self):
self.dir = Path(tempfile.mkdtemp(dir=REPO_ROOT / "tmp"))
self.addCleanup(shutil.rmtree, self.dir, True)
self.file = self.dir / "rom.bin"
self.file.write_bytes(self.DATA)
def test_a_stream_gives_the_file_digests(self):
expected = {
"sha1": hashlib.sha1(self.DATA).hexdigest(),
"md5": hashlib.md5(self.DATA).hexdigest(),
"sha256": hashlib.sha256(self.DATA).hexdigest(),
"crc32": format(zlib.crc32(self.DATA) & 0xFFFFFFFF, "08x"),
"adler32": format(zlib.adler32(self.DATA) & 0xFFFFFFFF, "08x"),
}
self.assertEqual(hashing.compute_hashes(self.file, cache=False), expected)
with open(self.file, "rb") as handle:
self.assertEqual(hashing.stream_hashes(handle, frozenset(expected)), expected)
self.assertEqual(hashing.stream_hashes(io.BytesIO(self.DATA), frozenset({"crc32"})),
{"crc32": expected["crc32"]})
def test_a_zip_member_and_md5sum(self):
archive = self.dir / "set.zip"
with zipfile.ZipFile(archive, "w", zipfile.ZIP_DEFLATED) as zf:
zf.writestr("rom.bin", self.DATA)
with zipfile.ZipFile(archive) as zf, zf.open("rom.bin") as member:
sha1 = hashing.stream_hashes(member, frozenset({"sha1"}))["sha1"]
self.assertEqual(sha1, hashing.file_digest(self.file))
self.assertEqual(hashing.md5sum(self.file), hashlib.md5(self.DATA).hexdigest())
self.assertEqual(hashing.md5sum(io.BytesIO(self.DATA)), hashlib.md5(self.DATA).hexdigest())
if __name__ == "__main__":
unittest.main()
+1 -1
View File
@@ -99,7 +99,7 @@ class TestCrossVendorDuplicates(unittest.TestCase):
self._write("Sega/Saturn/cartridge.bin", b"kick")
self._write("Sony/PlayStation/scph5501.bin", b"a longer dump")
self._write("Sega/Saturn/other.bin", b"same")
with mock.patch.object(identity, "_sha1", wraps=identity._sha1) as hashed:
with mock.patch.object(identity, "file_digest", wraps=identity.file_digest) as hashed:
found = self._scan()
read = sorted(Path(call.args[0]).name for call in hashed.call_args_list)
self.assertEqual(read, ["cartridge.bin", "kickstart.rom", "other.bin"])