Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Make OS update archives reproducible across build times (#694)

Dee427899680f51
diff --git a/os/tests/test_package.py b/os/tests/test_package.py
index fa1446415..adc718482 100644
--- a/os/tests/test_package.py
+++ b/os/tests/test_package.py
@@ -4,6 +4,7 @@ from pathlib import Path
import tarfile
import tempfile
import unittest
+from unittest.mock import patch
spec = importlib.util.spec_from_file_location('os_package', Path(__file__).parents[1] / 'tools/build-package.py')
@@ -12,6 +13,19 @@ spec.loader.exec_module(package)
class PackageIdentity(unittest.TestCase):
+ def test_same_source_is_byte_identical_across_build_times_and_output_names(self):
+ with tempfile.TemporaryDirectory() as temp:
+ root = Path(temp) / 'stage'
+ root.mkdir()
+ (root / 'contents').write_bytes(b'unchanged source\n')
+ artifacts = []
+ for name, clock in [('first.pkg.tar.gz', 1234567), ('second.pkg.tar.gz', 9876543)]:
+ output = Path(temp) / name
+ with patch('time.time', return_value=clock):
+ package.archive_package(root, output, 'pkgname = fixture\npkgver = 1-1\n', 123456)
+ artifacts.append(output.read_bytes())
+ self.assertEqual(artifacts[0], artifacts[1])
+
def test_macos_wrong_architecture_dirty_or_different_source_cannot_be_packaged(self):
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
diff --git a/os/tools/build-package.py b/os/tools/build-package.py
index 390df63c3..55947e902 100644
--- a/os/tools/build-package.py
+++ b/os/tools/build-package.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""Build the same small Harness package for an ISO or an installed test machine."""
import argparse
+import gzip
import hashlib
import json
from pathlib import Path
@@ -77,17 +78,22 @@ def archive_package(root, output, pkginfo, timestamp):
(root / '.PKGINFO').write_text(pkginfo)
partial = output.with_name(output.name + '.partial')
try:
- with tarfile.open(partial, 'w:gz', compresslevel=6, dereference=False) as archive:
- for path in sorted(root.rglob('*')):
- info = archive.gettarinfo(str(path), str(path.relative_to(root)))
- info.uid = info.gid = 0
- info.uname = info.gname = 'root'
- info.mtime = timestamp
- if info.isfile():
- with path.open('rb') as handle:
- archive.addfile(info, handle)
- else:
- archive.addfile(info)
+ # Both container and members use the source time. Gzip's defaults put
+ # the build clock and temporary output filename into otherwise equal
+ # packages, preventing checksum reuse across native test environments.
+ with partial.open('wb') as raw, gzip.GzipFile(
+ filename='', mode='wb', fileobj=raw, compresslevel=6, mtime=timestamp) as compressed:
+ with tarfile.open(fileobj=compressed, mode='w', dereference=False) as archive:
+ for path in sorted(root.rglob('*')):
+ info = archive.gettarinfo(str(path), str(path.relative_to(root)))
+ info.uid = info.gid = 0
+ info.uname = info.gname = 'root'
+ info.mtime = timestamp
+ if info.isfile():
+ with path.open('rb') as handle:
+ archive.addfile(info, handle)
+ else:
+ archive.addfile(info)
partial.replace(output)
finally:
partial.unlink(missing_ok=True)