Build the shared test site into a directory that is made new, with a test, and give the zip archives fixed member times so equal files make equal bytes

This commit is contained in:
swaits committed 2026-11-20 16:33:34 -07:00
1 parent c645159ae7
commit 5d86a42ad1
3 files changed
+58 -4

No files matched your search

+13 -3
View File
@@ -277,7 +277,7 @@ def copy_sources(tid, files, tdir, stats):
# own directory) decide them, so a change to any of them makes a new key. Kept by content, not by # own directory) decide them, so a change to any of them makes a new key. Kept by content, not by
# modification time, so a store restored from a CI cache or made in another checkout is as good as one # modification time, so a store restored from a CI cache or made in another checkout is as good as one
# made here. # made here.
STORE_VERSION = "1" STORE_VERSION = "2" # 2: archives have fixed member times
_code_digest = None _code_digest = None
@@ -569,6 +569,9 @@ def archive_readme(name):
f"{resolution}") f"{resolution}")
ARCHIVE_TIME = (1980, 1, 1, 0, 0, 0) # the earliest time a zip can hold
def build_archive(name, recs, out_dir, stats): def build_archive(name, recs, out_dir, stats):
"""Write /data/<name> (zip, deflate) unless it already holds exactly these files """Write /data/<name> (zip, deflate) unless it already holds exactly these files
and is newer than every one of them and than this script.""" and is newer than every one of them and than this script."""
@@ -606,12 +609,19 @@ def build_archive(name, recs, out_dir, stats):
stats.skipped += 1 stats.skipped += 1
return return
with zipfile.ZipFile(tmp, "w", compression=zipfile.ZIP_DEFLATED) as zf: with zipfile.ZipFile(tmp, "w", compression=zipfile.ZIP_DEFLATED) as zf:
info = zipfile.ZipInfo("README.txt", date_time=time.localtime(mtime(__file__))[:6]) # Every member gets the same fixed time and mode, so the same files always make the
# same bytes, whenever and wherever the archive is made (and so the store can hand
# one made elsewhere to a build here).
info = zipfile.ZipInfo("README.txt", date_time=ARCHIVE_TIME)
info.compress_type = zipfile.ZIP_DEFLATED info.compress_type = zipfile.ZIP_DEFLATED
info.external_attr = 0o644 << 16 info.external_attr = 0o644 << 16
zf.writestr(info, archive_readme(name)) zf.writestr(info, archive_readme(name))
for arc, src in members: for arc, src in members:
zf.write(src, arc) info = zipfile.ZipInfo(arc, date_time=ARCHIVE_TIME)
info.compress_type = zipfile.ZIP_DEFLATED
info.external_attr = 0o644 << 16
with open(src, "rb") as source, zf.open(info, "w") as member:
shutil.copyfileobj(source, member, 1 << 20)
try: try:
os.makedirs(os.path.dirname(kept), exist_ok=True) os.makedirs(os.path.dirname(kept), exist_ok=True)
for old in os.listdir(os.path.dirname(kept)): for old in os.listdir(os.path.dirname(kept)):
+4 -1
View File
@@ -32,7 +32,10 @@ _built = bool(PREBUILT)
def build(out, *extra, env=None): def build(out, *extra, env=None):
"""Run the site build into out (a directory that need not exist). Raises when it fails.""" """Run the site build into out, a directory that is made new: whatever was there is removed first,
so nothing in it can be a page an earlier build (of another branch, say) wrote and this one
does not. Raises when the build fails."""
shutil.rmtree(out, ignore_errors=True)
done = subprocess.run([sys.executable, os.path.join(WEB, "build.py"), "--out", out, *extra], done = subprocess.run([sys.executable, os.path.join(WEB, "build.py"), "--out", out, *extra],
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True, env=env) stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True, env=env)
if done.returncode: if done.returncode:
+41
View File
@@ -0,0 +1,41 @@
"""The shared test build is always a fresh one: nothing in the directory tests read can be left over
from an earlier build, such as a page that another branch wrote and this one does not."""
import os
import subprocess
import sys
import tempfile
import unittest
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import builtsite # noqa: E402
class FreshBuildTest(unittest.TestCase):
def test_a_page_left_by_an_earlier_build_is_not_there_when_the_next_build_starts(self):
with tempfile.TemporaryDirectory() as tmp:
out = os.path.join(tmp, "site")
os.makedirs(os.path.join(out, "regions", "gone"))
for rel in (os.path.join("regions", "gone", "index.html"), "old.json"):
with open(os.path.join(out, rel), "w") as f:
f.write("left over")
seen = []
def fake_build(cmd, **kwargs):
seen.append(sorted(os.listdir(out)) if os.path.isdir(out) else None)
os.makedirs(out, exist_ok=True)
return subprocess.CompletedProcess(cmd, 0, "", "")
with mock.patch.object(builtsite.subprocess, "run", fake_build):
builtsite.build(out)
self.assertIn(seen[0], (None, []))
self.assertEqual(os.listdir(out), [])
def test_the_shared_site_is_in_a_directory_named_for_this_process(self):
if not builtsite.PREBUILT:
self.assertTrue(builtsite.OUT.endswith(f"test-site-{os.getpid()}"))
if __name__ == "__main__":
unittest.main()