Download tools/test_image_source_masters.py from maxwellinked/time-lapse-artifacts: direct link, hf CLI and curl.
- Browser
- Download file 15.3 kB
-
https://huggingface.co/datasets/maxwellinked/time-lapse-artifacts/resolve/refs%2Fpr%2F113/tools/test_image_source_masters.py
- Command line
-
hf download hf://datasets/maxwellinked/time-lapse-artifacts@refs/pr/113/tools/test_image_source_masters.py
-
curl -L -o test_image_source_masters.py https://huggingface.co/datasets/maxwellinked/time-lapse-artifacts/resolve/refs%2Fpr%2F113/tools/test_image_source_masters.py
15.3 kB
| #!/usr/bin/env python3 | |
| """Behavior tests for the separate, source-only DNG contract; no real RAW is copied.""" | |
| import copy | |
| import csv | |
| import hashlib | |
| import importlib.util | |
| import io | |
| import json | |
| from pathlib import Path | |
| import subprocess | |
| import sys | |
| import tempfile | |
| import unittest | |
| LAYER = "Image_Source_Masters/v1" | |
| FIELDS = ( | |
| "schema_version", "asset_id", "file_name", "original_filename", "sha256", | |
| "hub_xet_hash", "size_bytes", "source_format", "representation_class", | |
| "source_role_status", "work_id", "study_set_id", "relationship_status", | |
| "relationship_receipt_path", "relationship_receipt_sha256", | |
| "owner_finish_date", "owner_finish_time", "tools", "media", "support", | |
| "dimensions", "dimension_unit", "raw_reader_receipt_path", | |
| "raw_reader_receipt_sha256", "raw_width_px", "raw_height_px", | |
| "raw_array_channels", "raw_dtype", "reader_orientation_flip", "limits", | |
| ) | |
| SOURCES = [ | |
| ("664e8776f87a4d3ca47c1ba7ac298843099070331929f5536b0bdff432e5d4b9", | |
| 93431484, "IMG_3326.DNG", "reference_9x12", "2026-10-27", "01:00", "9x12", "6"), | |
| ("7625f42a74ecf9c06211828489ba3eaecad00651fb7f7893df59ce41bf94e612", | |
| 105075416, "2026-10-29.2234.Pencil.Graphite.FountainPen.Paper.11x14.DNG", | |
| "comparison_11x14", "2026-10-29", "22:34", "11x14", "0"), | |
| ] | |
| RECEIPTS = ("ingest-authority.json", "relationships.json", "raw-unpack.json", | |
| "source-review.json", "media-identities.json") | |
| LIMITS = "Static reference candidate; no endpoint, calibrated color, timing, final-state or camera-original claim." | |
| def digest(data): | |
| return hashlib.sha256(data).hexdigest() | |
| def write_json(path, value): | |
| path.write_text(json.dumps(value, indent=2) + "\n", encoding="utf-8", newline="\n") | |
| def read_json(path): | |
| return json.loads(path.read_text(encoding="utf-8")) | |
| def seal(root): | |
| layer = root / LAYER | |
| package = read_json(layer / "MANIFEST.json") | |
| package["file_sha256"] = { | |
| p.relative_to(root).as_posix(): digest(p.read_bytes()) | |
| for p in sorted(layer.rglob("*")) | |
| if p.is_file() and p.name not in {"MANIFEST.json", "SHA256SUMS"} | |
| and "objects" not in p.relative_to(layer).parts | |
| } | |
| write_json(layer / "MANIFEST.json", package) | |
| entries = dict(package["file_sha256"]) | |
| entries[LAYER + "/MANIFEST.json"] = digest((layer / "MANIFEST.json").read_bytes()) | |
| for sha, size, name, study, date, time, dimensions, flip in SOURCES: | |
| entries[LAYER + "/objects/" + sha + ".DNG"] = sha | |
| (layer / "SHA256SUMS").write_text( | |
| "".join(f"{sha} {path}\n" for path, sha in sorted(entries.items())), | |
| encoding="utf-8", newline="\n") | |
| def fixture(root, bound=True): | |
| layer = root / LAYER | |
| (layer / "Receipts").mkdir(parents=True) | |
| (layer / "objects").mkdir() | |
| (layer / "README.md").write_text("Separate source-only collection.\n", encoding="utf-8") | |
| (layer / "SCHEMA.md").write_text("Versioned DNG container contract.\n", encoding="utf-8") | |
| objects = [ | |
| dict(asset_id="imsrc:sha256:" + s[0], file_name=LAYER + "/objects/" + s[0] + ".DNG", | |
| sha256=s[0], size_bytes=s[1], original_filename=s[2], study_set_id=s[3], | |
| owner_finish_date=s[4], owner_finish_time=s[5], dimensions=s[6], dimension_unit="in", | |
| tools="Pencil;FountainPen", media="Graphite;Ink", support="Paper") | |
| for s in SOURCES] | |
| write_json(layer / "Receipts/ingest-authority.json", { | |
| "schema_version": "image-source-master-ingest-authority/v1", | |
| "authorization": "Ingest with the understanding, this investigation remains open to me", | |
| "scope": "two_unchanged_supplied_DNG_containers", "objects": objects, | |
| "work_registration_authorized": False, "primary_selection_authorized": False, | |
| "research_conclusions_authorized": False}) | |
| write_json(layer / "Receipts/relationships.json", { | |
| "schema_version": "image-source-master-relationships/v1", | |
| "scope": "study_set_static_reference_candidate_only", | |
| "objects": [dict(asset_id=o["asset_id"], sha256=o["sha256"], | |
| study_set_id=o["study_set_id"], relationship_status="candidate_same_drawing") | |
| for o in objects], | |
| "work_identity_established": False, "endpoint_established": False, | |
| "final_state_established": False, "calibrated_registration_established": False}) | |
| write_json(layer / "Receipts/raw-unpack.json", { | |
| "schema_version": "image-source-master-raw-unpack/v1", | |
| "reader": {"rawpy": "0.27.1", "libraw": "0.22.1", "numpy": "2.4.6"}, | |
| "operation": "raw_image_visible_unpack_no_postprocess", | |
| "results": [dict(sha256=s[0], size_bytes=s[1], | |
| status="passed_recorded_visible_raw_unpack", | |
| decoded_array_shape=[6048, 8064, 4], decoded_array_dtype="uint16", | |
| reader_orientation_flip=int(s[7])) | |
| for s in SOURCES], | |
| "source_modified": False, "rgb_render_performed": False, | |
| "color_fidelity_verified": False, "camera_original_lineage_verified": False}) | |
| write_json(layer / "Receipts/source-review.json", { | |
| "schema_version": "image-source-master-source-review/v1", | |
| "objects": [dict(sha256=o["sha256"], | |
| state="reviewed_publication_approved" if bound else "pending", | |
| embedded_metadata_reviewed=bound, complete_frame_reviewed=bound, | |
| reviewed_representation="unchanged_supplied_DNG_container") | |
| for o in objects]}) | |
| write_json(layer / "Receipts/media-identities.json", { | |
| "schema_version": "image-source-master-media-identities/v1", | |
| "state": "observed_pinned_hub_identity" if bound else "unbound_local_preparation", | |
| "repository": "maxwellinked/time-lapse-artifacts", | |
| "revision": "a" * 40 if bound else None, | |
| "observation_method": "pinned_hub_lfs_sha_size_xet_observation", | |
| "objects": [dict(file_name=o["file_name"], sha256=o["sha256"], | |
| size_bytes=o["size_bytes"], hub_xet_hash=str(i + 1) * 64) | |
| for i, o in enumerate(objects)] if bound else []}) | |
| relation_sha = digest((layer / "Receipts/relationships.json").read_bytes()) | |
| raw_sha = digest((layer / "Receipts/raw-unpack.json").read_bytes()) | |
| values = [] | |
| for i, s in enumerate(SOURCES): | |
| sha, size, name, study, date, time, dimensions, flip = s | |
| row = dict(zip(FIELDS, [ | |
| "image-source-master-row/v1", "imsrc:sha256:" + sha, | |
| LAYER + "/objects/" + sha + ".DNG", name, sha, | |
| str(i + 1) * 64 if bound else "", str(size), "DNG", | |
| "unchanged_supplied_DNG_container", "not_independently_verified_as_camera_original", | |
| "", study, "candidate_same_drawing", LAYER + "/Receipts/relationships.json", | |
| relation_sha, date, time, "Pencil;FountainPen", "Graphite;Ink", "Paper", | |
| dimensions, "in", LAYER + "/Receipts/raw-unpack.json", raw_sha, | |
| "8064", "6048", "4", "uint16", flip, LIMITS])) | |
| values.append(row) | |
| if bound: | |
| (root / row["file_name"]).write_text( | |
| f"version https://git-lfs.github.com/spec/v1\noid sha256:{sha}\nsize {size}\n", | |
| encoding="ascii", newline="\n") | |
| write_rows(root, values) | |
| write_json(layer / "MANIFEST.json", { | |
| "schema_version": "image-source-masters-package/v1", | |
| "canonical_repository": "maxwellinked/time-lapse-artifacts", | |
| "base_revision": "3a9c13f603f7255a86ddf2e47d403934dbe09f78", | |
| "asset_ids": [o["asset_id"] for o in objects], | |
| "object_count": 2, "new_work_registrations": 0, "new_primary_selections": 0, | |
| "native_configuration_changes": False, "research_conclusions": False, | |
| "file_sha256": {}}) | |
| seal(root) | |
| return values | |
| def write_rows(root, values): | |
| out = io.StringIO(newline="") | |
| writer = csv.DictWriter(out, fieldnames=FIELDS, lineterminator="\n") | |
| writer.writeheader() | |
| writer.writerows(values) | |
| (root / LAYER / "metadata.csv").write_text(out.getvalue(), encoding="utf-8", newline="\n") | |
| class SourceMasterContractTests(unittest.TestCase): | |
| def setUp(self): | |
| test_work = Path(__file__).resolve().parents[1] / "work" | |
| test_work.mkdir(exist_ok=True) | |
| self.temp = tempfile.TemporaryDirectory(dir=test_work) | |
| self.root = Path(self.temp.name) | |
| self.rows = fixture(self.root) | |
| script = Path(__file__).with_name("validate_image_source_masters.py") | |
| self.assertTrue(script.is_file(), "DNG contract validator is not implemented") | |
| spec = importlib.util.spec_from_file_location("source_master_contract", script) | |
| self.module = importlib.util.module_from_spec(spec) | |
| spec.loader.exec_module(self.module) | |
| def tearDown(self): | |
| if hasattr(self, "temp"): | |
| self.temp.cleanup() | |
| def check(self, preflight=False): | |
| return self.module.validate(self.root, preflight=preflight) | |
| def edit_receipt(self, name, change): | |
| path = self.root / LAYER / "Receipts" / name | |
| value = read_json(path) | |
| change(value) | |
| write_json(path, value) | |
| if name in {"raw-unpack.json", "relationships.json"}: | |
| field = "raw_reader_receipt_sha256" if name == "raw-unpack.json" else "relationship_receipt_sha256" | |
| for row in self.rows: | |
| row[field] = digest(path.read_bytes()) | |
| write_rows(self.root, self.rows) | |
| seal(self.root) | |
| def test_valid_bound_contract_distinguishes_pointer_check_from_payload_fixity(self): | |
| result = self.check() | |
| self.assertEqual(result["status"], "release_metadata_contract_passed") | |
| self.assertEqual(result["objects"], 2) | |
| self.assertFalse(result["payload_bytes_verified"]) | |
| self.assertFalse(result["public_availability_verified"]) | |
| def test_unbound_release_fails_and_explicit_preflight_never_claims_publication(self): | |
| other = self.root / "unbound" | |
| fixture(other, bound=False) | |
| with self.assertRaisesRegex(ValueError, "remote|Xet|identity"): | |
| self.module.validate(other) | |
| result = self.module.validate(other, preflight=True) | |
| self.assertEqual(result["status"], "local_preflight_only") | |
| self.assertFalse(result["publication_verified"]) | |
| self.assertFalse(result["payload_bytes_verified"]) | |
| def test_rejects_mismatched_even_well_formed_xet_identity(self): | |
| self.rows[0]["hub_xet_hash"] = "f" * 64 | |
| write_rows(self.root, self.rows) | |
| seal(self.root) | |
| with self.assertRaisesRegex(ValueError, "identity|Xet"): | |
| self.check() | |
| def test_rejects_remote_receipt_without_immutable_revision(self): | |
| self.edit_receipt("media-identities.json", lambda v: v.update(revision="main")) | |
| with self.assertRaisesRegex(ValueError, "revision"): | |
| self.check() | |
| def test_rejects_substituted_or_duplicate_source_members(self): | |
| self.rows[1] = copy.deepcopy(self.rows[0]) | |
| write_rows(self.root, self.rows) | |
| seal(self.root) | |
| with self.assertRaisesRegex(ValueError, "duplicate|scope|identity"): | |
| self.check() | |
| def test_rejects_nonblank_work_identity_and_primary_roles(self): | |
| for field, value in [("work_id", "tlaw-1234567890abcdef"), | |
| ("representation_class", "high_res_primary")]: | |
| with self.subTest(field=field): | |
| old = self.rows[0][field] | |
| self.rows[0][field] = value | |
| write_rows(self.root, self.rows) | |
| seal(self.root) | |
| with self.assertRaisesRegex(ValueError, "work|representation"): | |
| self.check() | |
| self.rows[0][field] = old | |
| write_rows(self.root, self.rows) | |
| def test_rejects_changed_owner_finish_label_or_dimensions(self): | |
| self.rows[0]["dimensions"] = "11x14" | |
| write_rows(self.root, self.rows) | |
| seal(self.root) | |
| with self.assertRaisesRegex(ValueError, "owner|scope"): | |
| self.check() | |
| def test_rejects_invented_capture_time_column(self): | |
| path = self.root / LAYER / "metadata.csv" | |
| path.write_text(path.read_text().replace("owner_finish_time", "capture_time"), encoding="utf-8") | |
| seal(self.root) | |
| with self.assertRaisesRegex(ValueError, "fields|header"): | |
| self.check() | |
| def test_rejects_reader_receipt_source_or_geometry_mismatch(self): | |
| self.edit_receipt("raw-unpack.json", | |
| lambda v: v["results"][0].update(decoded_array_shape=[8064, 6048, 4])) | |
| with self.assertRaisesRegex(ValueError, "RAW|reader|shape"): | |
| self.check() | |
| def test_rejects_pending_source_representation_review_in_release(self): | |
| self.edit_receipt("source-review.json", lambda v: v["objects"][0].update(state="pending")) | |
| with self.assertRaisesRegex(ValueError, "review"): | |
| self.check() | |
| def test_rejects_missing_checksum_member(self): | |
| path = self.root / LAYER / "SHA256SUMS" | |
| path.write_text("\n".join(path.read_text().splitlines()[1:]) + "\n", encoding="utf-8") | |
| with self.assertRaisesRegex(ValueError, "checksum|coverage"): | |
| self.check() | |
| def test_rejects_modified_receipt_without_resealing(self): | |
| path = self.root / LAYER / "Receipts/relationships.json" | |
| path.write_text(path.read_text() + " ", encoding="utf-8") | |
| with self.assertRaisesRegex(ValueError, "checksum|hash"): | |
| self.check() | |
| def test_rejects_absolute_paths_in_public_evidence(self): | |
| self.edit_receipt("ingest-authority.json", lambda v: v.update(private_source_path="C:\\Users\\private\\file")) | |
| with self.assertRaisesRegex(ValueError, "private|absolute"): | |
| self.check() | |
| def test_rejects_wrong_lfs_pointer_oid(self): | |
| (self.root / self.rows[0]["file_name"]).write_text( | |
| "version https://git-lfs.github.com/spec/v1\noid sha256:" + "f" * 64 + | |
| "\nsize 93431484\n", encoding="ascii") | |
| with self.assertRaisesRegex(ValueError, "object|pointer|bytes"): | |
| self.check() | |
| def test_rejects_traversal_paths(self): | |
| self.rows[0]["file_name"] = "../outside.DNG" | |
| write_rows(self.root, self.rows) | |
| seal(self.root) | |
| with self.assertRaisesRegex(ValueError, "path|identity"): | |
| self.check() | |
| def test_command_line_fails_closed_without_explicit_preflight(self): | |
| other = self.root / "unbound" | |
| fixture(other, bound=False) | |
| script = Path(__file__).with_name("validate_image_source_masters.py") | |
| result = subprocess.run([sys.executable, str(script), "--root", str(other)], | |
| capture_output=True, text=True) | |
| self.assertNotEqual(result.returncode, 0) | |
| result = subprocess.run([sys.executable, str(script), "--root", str(other), "--preflight"], | |
| capture_output=True, text=True) | |
| self.assertEqual(result.returncode, 0, result.stderr + result.stdout) | |
| self.assertEqual(json.loads(result.stdout)["status"], "local_preflight_only") | |
| if __name__ == "__main__": | |
| unittest.main() | |