mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 03:39:52 +00:00
feat(release): build checksum manifests and license inventories.
SHA-256 artifact manifests with evidence validation and deterministic dependency license listings for release packaging. Signed-off-by: Ranveer Soni <git@ranveersoni.me>
This commit is contained in:
1 parent
95ddc28518
commit
5d4d15d596
2 files changed
+121
No files matched your search
@@ -0,0 +1,74 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""Reproducible artifact manifests, checksums and simple SBOM stubs."""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
from dataclasses import asdict, dataclass
|
||||
from pathlib import Path
|
||||
from typing import Iterable
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ArtifactRecord:
|
||||
path: str
|
||||
sha256: str
|
||||
size: int
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Manifest:
|
||||
revision: str
|
||||
artifacts: tuple[ArtifactRecord, ...]
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"revision": self.revision,
|
||||
"artifacts": [asdict(a) for a in self.artifacts],
|
||||
}
|
||||
|
||||
|
||||
def sha256_bytes(data: bytes) -> str:
|
||||
return hashlib.sha256(data).hexdigest()
|
||||
|
||||
|
||||
def sha256_file(path: Path) -> ArtifactRecord:
|
||||
data = path.read_bytes()
|
||||
return ArtifactRecord(path=str(path), sha256=sha256_bytes(data), size=len(data))
|
||||
|
||||
|
||||
def build_manifest(paths: Iterable[Path], *, revision: str) -> Manifest:
|
||||
records = tuple(sha256_file(Path(p)) for p in paths)
|
||||
return Manifest(revision=revision, artifacts=records)
|
||||
|
||||
|
||||
def dumps_manifest(manifest: Manifest) -> str:
|
||||
return json.dumps(manifest.to_dict(), indent=2, sort_keys=True) + "\n"
|
||||
|
||||
|
||||
def license_inventory(entries: Iterable[tuple[str, str]]) -> str:
|
||||
"""Build a deterministic dependency license inventory from (name, license) pairs."""
|
||||
rows = sorted((name, lic) for name, lic in entries)
|
||||
lines = ["# Dependency license inventory", ""]
|
||||
for name, lic in rows:
|
||||
lines.append(f"- `{name}`: {lic}")
|
||||
lines.append("")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def validate_evidence(
|
||||
*,
|
||||
manifest_sha_by_path: dict[str, str],
|
||||
observed_sha_by_path: dict[str, str],
|
||||
) -> tuple[bool, tuple[str, ...]]:
|
||||
problems: list[str] = []
|
||||
for path, expected in sorted(manifest_sha_by_path.items()):
|
||||
got = observed_sha_by_path.get(path)
|
||||
if got is None:
|
||||
problems.append(f"missing artifact: {path}")
|
||||
elif got != expected:
|
||||
problems.append(f"checksum mismatch: {path}")
|
||||
for path in sorted(observed_sha_by_path):
|
||||
if path not in manifest_sha_by_path:
|
||||
problems.append(f"unexpected artifact: {path}")
|
||||
return not problems, tuple(problems)
|
||||
@@ -0,0 +1,47 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from release.manifest import (
|
||||
build_manifest,
|
||||
dumps_manifest,
|
||||
license_inventory,
|
||||
sha256_bytes,
|
||||
validate_evidence,
|
||||
)
|
||||
|
||||
|
||||
class Manifest(unittest.TestCase):
|
||||
def test_checksums_and_validation(self):
|
||||
self.assertEqual(sha256_bytes(b"abc"),
|
||||
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
p = Path(tmp) / "a.bin"
|
||||
p.write_bytes(b"hello")
|
||||
manifest = build_manifest([p], revision="rev1")
|
||||
payload = json.loads(dumps_manifest(manifest))
|
||||
self.assertEqual(payload["revision"], "rev1")
|
||||
expected = {payload["artifacts"][0]["path"]: payload["artifacts"][0]["sha256"]}
|
||||
ok, problems = validate_evidence(
|
||||
manifest_sha_by_path=expected,
|
||||
observed_sha_by_path=expected,
|
||||
)
|
||||
self.assertTrue(ok)
|
||||
self.assertEqual(problems, ())
|
||||
bad, problems = validate_evidence(
|
||||
manifest_sha_by_path=expected,
|
||||
observed_sha_by_path={payload["artifacts"][0]["path"]: "0" * 64},
|
||||
)
|
||||
self.assertFalse(bad)
|
||||
self.assertTrue(problems)
|
||||
|
||||
def test_license_inventory_sorted(self):
|
||||
text = license_inventory([("zeta", "MIT"), ("alpha", "Apache-2.0")])
|
||||
self.assertLess(text.index("alpha"), text.index("zeta"))
|
||||
self.assertIn("Apache-2.0", text)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user