From 4f9ada5a6f37b91d272d8156a487d38543009c8c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 12 Oct 2026 05:31:46 +0200 Subject: [PATCH] ship_cc: refuse a cultcache-py that is not the tested version _libraries reads [project].version from the pyproject.toml beside the cultcache-py source before importing it and raises ValueError naming the version found and the packages path when it differs from TESTED_CULTCACHE_PY (0.3.0) or the file is missing. Builds: yes. test_refuses_a_file_with_no_record fails as before (cultcache-py unpack error on a 0-byte file). Co-Authored-By: Claude Opus 5.5 --- tools/blender/aetheria_ships/ship_cc.py | 19 ++++++++++++ tools/blender/tests/test_ship_cc.py | 40 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/tools/blender/aetheria_ships/ship_cc.py b/tools/blender/aetheria_ships/ship_cc.py index df76cdb6f..5ef61445d 100644 --- a/tools/blender/aetheria_ships/ship_cc.py +++ b/tools/blender/aetheria_ships/ship_cc.py @@ -9,11 +9,16 @@ from __future__ import annotations import sys import hashlib +import tomllib from dataclasses import replace from datetime import datetime, timezone from pathlib import Path from typing import Any, NamedTuple +# The cultcache-py this module's raw envelope writes were tested against. cultcache_py has no __version__ and is loaded +# from source, so its version is [project].version in the pyproject.toml beside that source. +TESTED_CULTCACHE_PY = "0.3.0" + SCHEMA = "aetheria.ship_authoring" HULL_SCHEMA = "aetheria.hulldata" @@ -62,7 +67,21 @@ def encode_hardpoint(**fields: Any) -> list[Any]: return [fields[name] for name in HARDPOINT_MEMBERS] +def _cultcache_py_version(cultlib_packages: str) -> str: + pyproject = Path(cultlib_packages) / "cultcache-py" / "pyproject.toml" + try: + with pyproject.open("rb") as handle: + return str(tomllib.load(handle)["project"]["version"]) + except (OSError, tomllib.TOMLDecodeError, KeyError): + raise ValueError(f"cultcache-py's pyproject.toml is missing or unreadable under {cultlib_packages}; this add-on " + f"is tested against cultcache-py {TESTED_CULTCACHE_PY}") from None + + def _libraries(cultlib_packages: str): + found = _cultcache_py_version(cultlib_packages) + if found != TESTED_CULTCACHE_PY: + raise ValueError(f"cultcache-py {found} under {cultlib_packages} is not the tested {TESTED_CULTCACHE_PY}; the " + f"add-on writes raw envelopes and refuses a CultLib whose write path may have moved") source = Path(cultlib_packages) / "cultcache-py" / "src" if source.exists() and str(source) not in sys.path: sys.path.insert(0, str(source)) diff --git a/tools/blender/tests/test_ship_cc.py b/tools/blender/tests/test_ship_cc.py index fb9b9994e..5762df25c 100644 --- a/tools/blender/tests/test_ship_cc.py +++ b/tools/blender/tests/test_ship_cc.py @@ -568,6 +568,46 @@ class RefusalsNameNoInputTests(ShipFileCase): self.assertNotIn(forbidden, str(caught.exception)) +class UntestedCultCacheTests(ShipFileCase): + """_libraries refuses a CultLib whose cultcache-py is not the version the raw envelope writes were tested against.""" + + def packages(self, version=None): + root = Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, root, True) + (root / "cultcache-py").mkdir() + if version is not None: + (root / "cultcache-py" / "pyproject.toml").write_text( + f'[project]\nname = "cultcache-py"\nversion = "{version}"\n', encoding="utf-8") + return str(root) + + def test_a_different_version_is_refused_naming_the_version_and_the_packages_path(self): + stub = self.packages("9.9.9") + before = self.bytes() + for call in (lambda: ship_cc.read(self.path, stub), lambda: ship_cc.read_layout(self.path, stub)): + with self.assertRaises(ValueError) as caught: + call() + self.assertIn("9.9.9", str(caught.exception)) + self.assertIn(stub, str(caught.exception)) + self.assertEqual(before, self.bytes()) + + def test_a_save_through_an_untested_cultcache_writes_nothing(self): + _, _, revision = ship_cc.read_layout(self.path, PACKAGES) + before = self.bytes() + with self.assertRaises(ValueError): + ship_cc.replace_layout(self.path, self.packages("9.9.9"), "mod.skiff", revision, [2, 2, [True] * 4], []) + self.assertEqual(before, self.bytes()) + + def test_a_missing_pyproject_is_refused(self): + stub = self.packages() + with self.assertRaises(ValueError) as caught: + ship_cc.read(self.path, stub) + self.assertIn(stub, str(caught.exception)) + + def test_the_tested_version_is_the_one_the_real_packages_directory_carries(self): + self.assertEqual(ship_cc.TESTED_CULTCACHE_PY, ship_cc._cultcache_py_version(PACKAGES)) + self.assertEqual("mod.skiff", ship_cc.read(self.path, PACKAGES).ship.body[0]) + + class ShipIdTests(unittest.TestCase): def test_admits_the_ids_the_c_sharp_rule_admits(self): for ship_id in ("probe.a", "a", "0x", "a_b-c", "com10", "com0", "lpt0", "console", "aux2", "a.nul", "x.com1"):