Skip to content

Commit 01d4662

Browse files
committed
feat: print adaptor and runtime version on start
Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
1 parent 57e7a3e commit 01d4662

2 files changed

Lines changed: 159 additions & 0 deletions

File tree

‎src/openjd/adaptor_runtime/_entrypoint.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import contextlib
6+
import importlib.metadata
67
import logging
78
import os
89
import signal
@@ -46,6 +47,7 @@
4647
ConditionalFormatter,
4748
)
4849
from .adaptors import SemanticVersion
50+
from ._version import version as _RUNTIME_VERSION
4951

5052
if TYPE_CHECKING: # pragma: no cover
5153
from .adaptors.configuration import AdaptorConfiguration
@@ -242,6 +244,37 @@ def _get_version_info(self) -> _VersionInfo:
242244
).integration_data_interface_version,
243245
)
244246

247+
def _get_adaptor_package_version(self) -> str:
248+
"""
249+
Attempts to determine the package version of the adaptor using the runtime.
250+
251+
Uses importlib.metadata to look up the distribution that provides the adaptor's
252+
top-level module. Returns "UNKNOWN" if the version cannot be determined.
253+
"""
254+
adaptor_module_name = self.adaptor_class.__module__
255+
# Walk up the module path trying to find a distribution that owns it
256+
parts = adaptor_module_name.split(".")
257+
for i in range(len(parts), 0, -1):
258+
candidate = ".".join(parts[:i])
259+
try:
260+
dist = importlib.metadata.distribution(candidate)
261+
return dist.version
262+
except importlib.metadata.PackageNotFoundError:
263+
continue
264+
265+
# Fallback: check if the adaptor's package has a _version module already imported
266+
adaptor_package = adaptor_module_name.rsplit(".", 1)[0]
267+
version_module_name = f"{adaptor_package}._version"
268+
version_module = sys.modules.get(version_module_name)
269+
if version_module and hasattr(version_module, "version"):
270+
return version_module.version
271+
272+
_logger.warning(
273+
f"Could not determine package version for adaptor '{self.adaptor_class.__name__}'. "
274+
f"Looked up module: '{adaptor_module_name}'"
275+
)
276+
return "UNKNOWN"
277+
245278
def _get_integration_data(self, parsed_args: Namespace) -> _IntegrationData:
246279
return _IntegrationData(
247280
init_data=parsed_args.init_data if hasattr(parsed_args, "init_data") else {},
@@ -273,6 +306,11 @@ def start(
273306
)
274307
)
275308

309+
# Log package versions at startup
310+
_logger.info(f"openjd-adaptor-runtime version: {_RUNTIME_VERSION}")
311+
adaptor_version = self._get_adaptor_package_version()
312+
_logger.info(f"{self.adaptor_class.__name__} version: {adaptor_version}")
313+
276314
interface_version_info = self._get_version_info()
277315

278316
if parsed_args.command == "is-compatible":

‎test/openjd/adaptor_runtime/unit/test_entrypoint.py‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ def test_version_info(
117117
"MockAdaptor Data Interface Version": "1.5",
118118
}
119119

120+
def test_logs_versions_at_startup(self):
121+
"""Verifies that both the runtime and adaptor versions are logged during start()."""
122+
# GIVEN
123+
entrypoint = EntryPoint(FakeAdaptor)
124+
125+
with (
126+
patch.object(entrypoint, "_get_adaptor_package_version", return_value="1.2.3"),
127+
patch.object(runtime_entrypoint.sys, "argv", ["Adaptor", "run"]),
128+
patch.object(runtime_entrypoint, "_logger") as mock_logger,
129+
):
130+
# WHEN
131+
entrypoint.start()
132+
133+
# THEN
134+
mock_logger.info.assert_any_call(
135+
f"openjd-adaptor-runtime version: {runtime_entrypoint._RUNTIME_VERSION}"
136+
)
137+
mock_logger.info.assert_any_call("FakeAdaptor version: 1.2.3")
138+
120139
@pytest.mark.parametrize("integration_version", ["1.4", "1.5"])
121140
def test_is_compatible(
122141
self,
@@ -904,3 +923,105 @@ def test_raises_on_nonvalid_parsed_data_type(self):
904923

905924
# THEN
906925
assert raised_err.match(f"Expected loaded data to be a dict, but got {type(input)}")
926+
927+
928+
class TestGetAdaptorPackageVersion:
929+
"""
930+
Tests for the EntryPoint._get_adaptor_package_version method
931+
"""
932+
933+
def test_returns_version_from_importlib_metadata(self, mock_adaptor_cls: MagicMock):
934+
"""When importlib.metadata can resolve the distribution, return its version."""
935+
# GIVEN
936+
mock_adaptor_cls.__module__ = "deadline.adaptor_for_maya.MayaAdaptor"
937+
entrypoint = EntryPoint(mock_adaptor_cls)
938+
939+
mock_dist = MagicMock()
940+
mock_dist.version = "1.2.3"
941+
942+
def fake_distribution(name):
943+
if name == "deadline.adaptor_for_maya.MayaAdaptor":
944+
return mock_dist
945+
raise runtime_entrypoint.importlib.metadata.PackageNotFoundError(name)
946+
947+
with patch(
948+
"openjd.adaptor_runtime._entrypoint.importlib.metadata.distribution",
949+
side_effect=fake_distribution,
950+
):
951+
# WHEN
952+
result = entrypoint._get_adaptor_package_version()
953+
954+
# THEN
955+
assert result == "1.2.3"
956+
957+
def test_walks_up_module_path(self, mock_adaptor_cls: MagicMock):
958+
"""When the full module path isn't a distribution, walk up until one is found."""
959+
# GIVEN
960+
mock_adaptor_cls.__module__ = "deadline.adaptor_for_maya.adaptor"
961+
entrypoint = EntryPoint(mock_adaptor_cls)
962+
963+
mock_dist = MagicMock()
964+
mock_dist.version = "2.0.0"
965+
966+
def fake_distribution(name):
967+
if name == "deadline.adaptor_for_maya":
968+
return mock_dist
969+
raise runtime_entrypoint.importlib.metadata.PackageNotFoundError(name)
970+
971+
with patch(
972+
"openjd.adaptor_runtime._entrypoint.importlib.metadata.distribution",
973+
side_effect=fake_distribution,
974+
):
975+
# WHEN
976+
result = entrypoint._get_adaptor_package_version()
977+
978+
# THEN
979+
assert result == "2.0.0"
980+
981+
def test_falls_back_to_version_module(self, mock_adaptor_cls: MagicMock):
982+
"""When importlib.metadata can't find the distribution, fall back to _version module."""
983+
# GIVEN
984+
mock_adaptor_cls.__module__ = "deadline.adaptor_for_maya.adaptor"
985+
entrypoint = EntryPoint(mock_adaptor_cls)
986+
987+
mock_version_module = MagicMock()
988+
mock_version_module.version = "3.5.1"
989+
990+
with (
991+
patch(
992+
"openjd.adaptor_runtime._entrypoint.importlib.metadata.distribution",
993+
side_effect=runtime_entrypoint.importlib.metadata.PackageNotFoundError("x"),
994+
),
995+
patch.dict(
996+
runtime_entrypoint.sys.modules,
997+
{"deadline.adaptor_for_maya._version": mock_version_module},
998+
),
999+
):
1000+
# WHEN
1001+
result = entrypoint._get_adaptor_package_version()
1002+
1003+
# THEN
1004+
assert result == "3.5.1"
1005+
1006+
def test_returns_unknown_when_version_not_found(self, mock_adaptor_cls: MagicMock):
1007+
"""When no version can be determined, return 'UNKNOWN' and log a warning."""
1008+
# GIVEN
1009+
mock_adaptor_cls.__module__ = "deadline.adaptor_for_maya.adaptor"
1010+
mock_adaptor_cls.__name__ = "MayaAdaptor"
1011+
entrypoint = EntryPoint(mock_adaptor_cls)
1012+
1013+
with (
1014+
patch(
1015+
"openjd.adaptor_runtime._entrypoint.importlib.metadata.distribution",
1016+
side_effect=runtime_entrypoint.importlib.metadata.PackageNotFoundError("x"),
1017+
),
1018+
patch.dict(runtime_entrypoint.sys.modules, {}, clear=False),
1019+
):
1020+
# Ensure the _version module is NOT in sys.modules
1021+
runtime_entrypoint.sys.modules.pop("deadline.adaptor_for_maya._version", None)
1022+
1023+
# WHEN
1024+
result = entrypoint._get_adaptor_package_version()
1025+
1026+
# THEN
1027+
assert result == "UNKNOWN"

0 commit comments

Comments
 (0)