Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions scripts/build_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,24 @@ def main(version):
if not re.fullmatch(r"\d+\.\d+\.\d+", version):
raise SystemExit("Expected GitVersion MajorMinorPatch.")
source = ROOT / "filamentra"
paths = sorted((p for p in source.rglob("*") if p.is_file()),
key=lambda p: p.relative_to(ROOT).as_posix())
if not paths or any(p.is_symlink() for p in paths):
raise SystemExit("Missing or unsafe Filamentra source.")
# The unit tests import Filamentra before packaging, creating local __pycache__.
# Do not ship machine-specific bytecode or test/tool caches with the source ZIP.
ignored_dirs = {"__pycache__", ".pytest_cache", ".mypy_cache", ".ruff_cache"}
ignored_suffixes = {".pyc", ".pyo"}
paths = []
for path in source.rglob("*"):
if path.is_symlink():
raise SystemExit("Unsafe Filamentra source symlink: " + str(path))
if not path.is_file():
continue
relative = path.relative_to(source)
if (any(part in ignored_dirs for part in relative.parts)
or path.suffix in ignored_suffixes or path.name == ".DS_Store"):
continue
paths.append(path)
paths.sort(key=lambda path: path.relative_to(ROOT).as_posix())
if not paths:
raise SystemExit("Missing Filamentra source.")
out = ROOT / "dist"
out.mkdir(exist_ok=True)
target = out / f"Filamentra-{version}.zip"
Expand Down
63 changes: 63 additions & 0 deletions tests/test_release_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Regression tests for reproducible Filamentra runtime-source distributions."""
import hashlib
import importlib.util
from pathlib import Path
import tempfile
import unittest
import zipfile


ROOT = Path(__file__).resolve().parents[1]
SPEC = importlib.util.spec_from_file_location("filamentra_release_builder",
ROOT / "scripts" / "build_release.py")
builder = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(builder)


class ReleasePackageTests(unittest.TestCase):
def test_generated_bytecode_and_tool_cache_do_not_change_archive(self):
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
source = root / "filamentra"
source.mkdir()
(source / "__init__.py").write_text('"""Package."""\n', encoding="utf-8")
assets = source / "assets"
assets.mkdir()
(assets / "profile.json").write_text('{"color": "blue"}\n', encoding="utf-8")

previous_root = builder.ROOT
try:
builder.ROOT = root
builder.main("1.2.3")
archive = root / "dist" / "Filamentra-1.2.3.zip"
original = archive.read_bytes()

# Python tests and tooling can generate these after checkout.
cache = source / "__pycache__"
cache.mkdir()
(cache / "__init__.cpython-312.pyc").write_bytes(b"binary cache")
(source / "stray.pyc").write_bytes(b"bytecode")
(source / ".DS_Store").write_bytes(b"Finder metadata")
ruff = source / ".ruff_cache"
ruff.mkdir()
(ruff / "state").write_text("tool output", encoding="utf-8")
pytest = source / ".pytest_cache"
pytest.mkdir()
(pytest / "README.md").write_text("test output", encoding="utf-8")
builder.main("1.2.3")

self.assertEqual(archive.read_bytes(), original)
with zipfile.ZipFile(archive) as zipped:
self.assertEqual(sorted(zipped.namelist()), [
"filamentra/__init__.py", "filamentra/assets/profile.json",
"release.json",
])
checksum = archive.with_suffix(".zip.sha256").read_text(encoding="ascii")
self.assertEqual(checksum,
hashlib.sha256(original).hexdigest() + " " + archive.name + "\n")
finally:
builder.ROOT = previous_root


if __name__ == "__main__":
unittest.main()
Loading