From b2931b4ab8dc572a40147ac983708a97301765f4 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Fri, 20 Nov 2026 18:19:59 -0700 Subject: [PATCH] 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 --- CHANGELOG.md | 4 ++++ web/build.py | 24 +++++++++++++----------- web/tests/test_build.py | 24 +++++++++++++++++++----- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3f1580..2f73009 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2026-11-20 - The bulk downloads follow content, not file times + +- For contributors: the build now decides whether `all-gpx.zip` and `all-geojson.zip` are up to date by what goes into them, as it does for the rest of the pages, and no longer by the modification times of their files. A file touched with no change leaves them alone, and a changed file with an old time rebuilds them. Nothing a visitor sees changes. + ## 2026-11-20 - List titles in the words people search with - The longest list now opens with the answer: "Longest Hiking Trails in the US: 25 Trails of 1,000+ Miles", and its first line names the longest trail and its length (the Sea-to-Sea Route, 7,775 miles). The line is worked out from the trails, so it follows them. diff --git a/web/build.py b/web/build.py index ecc1a5f..4a686c7 100644 --- a/web/build.py +++ b/web/build.py @@ -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/ (zip, deflate) unless it already holds exactly these files - and is newer than every one of them and than this script.""" + """Write /data/ (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: diff --git a/web/tests/test_build.py b/web/tests/test_build.py index 74444a2..e5de9b5 100644 --- a/web/tests/test_build.py +++ b/web/tests/test_build.py @@ -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()