Decide whether the two bulk zips are up to date by the content of what goes into them, not by file modification times, with a test

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-20 18:19:59 -07:00
1 parent 4c72fd705e
commit b2931b4ab8
3 files changed
+36 -16

No files matched your search

+13 -11
View File
@@ -10,6 +10,7 @@ sys.dont_write_bytecode = True
import argparse
import concurrent.futures
import filecmp
import hashlib
import json
import multiprocessing
@@ -572,21 +573,14 @@ ARCHIVE_TIME = (1980, 1, 1, 0, 0, 0) # the earliest time a zip can hold
def build_archive(name, recs, out_dir, stats):
"""Write /data/<name> (zip, deflate) unless it already holds exactly these files
and is newer than every one of them and than this script."""
"""Write /data/<name> (zip, deflate) unless the archive there is already the one these files
make. What goes in is decided by content (the files' bytes, this build's code and the README),
never by file times: the store holds the archive made from exactly that, and the one in the
output is up to date when it is the same bytes."""
path = os.path.join(out_dir, "data", name)
members = archive_members(name, recs, out_dir, ROOT)
members = [(arc, src) for arc, src in members if os.path.isfile(src)]
expected = ["README.txt"] + [arc for arc, _ in members]
newest = max([mtime(__file__)] + [mtime(src) for _, src in members])
if os.path.isfile(path) and mtime(path) >= newest:
try:
with zipfile.ZipFile(path) as zf:
if zf.namelist() == expected:
stats.skipped += 1
return
except (OSError, zipfile.BadZipFile):
pass
# The store keeps an archive by what goes into it, so one whose files have not changed (in
# content: it does not matter when they were written) is copied, not compressed again.
digest = hashlib.sha256(code_digest().encode() + archive_readme(name).encode())
@@ -595,6 +589,14 @@ def build_archive(name, recs, out_dir, stats):
with open(src, "rb") as f:
digest.update(hashlib.sha256(f.read()).digest())
kept = os.path.join(store_dir(), "archives", f"{name}.{digest.hexdigest()}")
if os.path.isfile(path) and os.path.isfile(kept) and filecmp.cmp(path, kept, shallow=False):
try:
with zipfile.ZipFile(path) as zf:
if zf.namelist() == expected:
stats.skipped += 1
return
except (OSError, zipfile.BadZipFile):
pass
tmp = path + ".tmp" # written beside, then moved over: never a half-written archive
try:
try:
+19 -5
View File
@@ -227,7 +227,7 @@ class BuildTreeTest(unittest.TestCase):
build.main(["--out", self.out])
self.assertIn("trails processed 0", out.getvalue())
def test_archives_rebuilt_only_when_an_input_is_newer(self):
def test_archives_rebuilt_only_when_an_input_changes_in_content(self):
self.build()
gpx, geojson = self.path("data", "all-gpx.zip"), self.path("data", "all-geojson.zip")
with zipfile.ZipFile(gpx) as zf:
@@ -253,12 +253,26 @@ class BuildTreeTest(unittest.TestCase):
stamps = (os.path.getmtime(gpx), os.path.getmtime(geojson))
self.build()
self.assertEqual((os.path.getmtime(gpx), os.path.getmtime(geojson)), stamps)
# a newer input (here a release.txt) rebuilds both archives
# a release.txt touched to a later time, with the same content, is not a change
later = max(stamps) + 10
os.utime(os.path.join(self.root, "avon", "release.txt"), (later, later))
release = os.path.join(self.root, "avon", "release.txt")
os.utime(release, (later, later))
self.build()
self.assertNotEqual(os.path.getmtime(gpx), stamps[0])
self.assertNotEqual(os.path.getmtime(geojson), stamps[1])
self.assertEqual((os.path.getmtime(gpx), os.path.getmtime(geojson)), stamps)
# nor is an archive older than every input (put back to an old time)
os.utime(gpx, (1000, 1000))
os.utime(geojson, (1000, 1000))
self.build()
self.assertEqual((os.path.getmtime(gpx), os.path.getmtime(geojson)), (1000, 1000))
# but a release.txt with other content rebuilds both archives, with the new content in them
with open(release, "a", encoding="utf-8") as f:
f.write("\nChanged.\n")
os.utime(release, (1, 1)) # and with an old time: the content decides
self.build()
self.assertNotEqual(os.path.getmtime(gpx), 1000)
self.assertNotEqual(os.path.getmtime(geojson), 1000)
with zipfile.ZipFile(gpx) as zf:
self.assertIn("Changed.", zf.read("avon/release.txt").decode("utf-8"))
def test_archive_members_are_regular_files_with_the_usual_mode(self):
self.build()