refactor(worker): defer optional visualization imports

This commit is contained in:
DCCONSTRUCTIONS
2026-08-31 17:15:13 +03:00
parent e2de8188ab
commit 892d0085e3
4 changed files with 858 additions and 302 deletions
+91
View File
@@ -0,0 +1,91 @@
from __future__ import annotations
import os
import subprocess
import sys
import textwrap
from pathlib import Path
REPOSITORY_ROOT = Path(__file__).resolve().parents[1]
SOURCE_ROOT = REPOSITORY_ROOT / "src"
def test_m49_container_import_excludes_optional_visualization_dependencies() -> None:
script = textwrap.dedent(
"""
import hashlib
import importlib.abc
import json
import sys
blocked = frozenset({"PIL", "pyarrow", "rerun"})
class OptionalDependencyBlocker(importlib.abc.MetaPathFinder):
def find_spec(self, fullname, path=None, target=None):
if fullname.partition(".")[0] in blocked:
raise ModuleNotFoundError(
f"blocked optional dependency: {fullname}",
name=fullname,
)
return None
assert blocked.isdisjoint(sys.modules)
sys.meta_path.insert(0, OptionalDependencyBlocker())
import k1link.compute as compute
import k1link.device_plugins.xgrids_k1 as xgrids_k1
import k1link.device_plugins.xgrids_k1.analyze as analyze
import k1link.observatory.m49_worker_container_main as container_main
from k1link.compute import LidarReplayPackV2
from k1link.compute.lidar_replay import LidarReplayPackV2 as DirectReplayPack
from k1link.device_plugins.xgrids_k1.analyze import Kb4ProjectionProfile
from k1link.device_plugins.xgrids_k1.analyze.calibrated_projection import (
Kb4ProjectionProfile as DirectProjectionProfile,
)
expected_public_api = {
compute: (219, "a8616ca1402ee26774fb8c8ca2df12b77877305260bde2d64db75acbdc79b598"),
xgrids_k1: (2, "735cce20efdc7b1abd12006073233051c6702b00e8f15d21c2142d671061a79b"),
analyze: (20, "4a3edcb6b9c735338c0392838be0eac89cad2db1178a5ab7833c1231b6ecc4c2"),
}
for package, (length, digest) in expected_public_api.items():
payload = json.dumps(package.__all__, separators=(",", ":")).encode()
assert len(package.__all__) == length
assert hashlib.sha256(payload).hexdigest() == digest
assert set(package.__all__).issubset(dir(package))
assert LidarReplayPackV2 is DirectReplayPack
assert Kb4ProjectionProfile is DirectProjectionProfile
assert container_main.__name__ == "k1link.observatory.m49_worker_container_main"
assert "k1link.compute.fusion_epoch" not in sys.modules
assert "k1link.device_plugins.xgrids_k1.observation" not in sys.modules
assert (
"k1link.device_plugins.xgrids_k1.analyze.calibrated_overlay"
not in sys.modules
)
assert "k1link.device_plugins.xgrids_k1.analyze.valid_fov" not in sys.modules
assert all(
module.partition(".")[0] not in blocked
for module in sys.modules
)
"""
)
environment = os.environ.copy()
inherited_python_path = environment.get("PYTHONPATH")
python_path = [str(SOURCE_ROOT)]
if inherited_python_path:
python_path.append(inherited_python_path)
environment["PYTHONPATH"] = os.pathsep.join(python_path)
environment["PYTHONNOUSERSITE"] = "1"
completed = subprocess.run(
[sys.executable, "-c", script],
cwd=REPOSITORY_ROOT,
env=environment,
check=False,
capture_output=True,
text=True,
timeout=30,
)
assert completed.returncode == 0, completed.stderr