Skip to content

Commit e153f3b

Browse files
committed
Add CADET meta information to Runner classes
1 parent a1e8148 commit e153f3b

File tree

2 files changed

+126
-7
lines changed

2 files changed

+126
-7
lines changed

Diff for: cadet/cadet_dll.py

+26-6
Original file line numberDiff line numberDiff line change
@@ -1625,25 +1625,25 @@ def __init__(self, dll_path: os.PathLike | str) -> None:
16251625
dll_path : os.PathLike or str
16261626
Path to the CADET DLL.
16271627
"""
1628-
self.cadet_path = Path(dll_path)
1629-
self._lib = ctypes.cdll.LoadLibrary(self.cadet_path.as_posix())
1628+
self._cadet_path = Path(dll_path)
1629+
self._lib = ctypes.cdll.LoadLibrary(self._cadet_path.as_posix())
16301630

16311631
# Query meta information
16321632
cdtGetLibraryVersion = self._lib.cdtGetLibraryVersion
16331633
cdtGetLibraryVersion.restype = ctypes.c_char_p
1634-
self.cadet_version = cdtGetLibraryVersion().decode('utf-8')
1634+
self._cadet_version = cdtGetLibraryVersion().decode('utf-8')
16351635

16361636
cdtGetLibraryCommitHash = self._lib.cdtGetLibraryCommitHash
16371637
cdtGetLibraryCommitHash.restype = ctypes.c_char_p
1638-
self.cadet_commit_hash = cdtGetLibraryCommitHash().decode('utf-8')
1638+
self._cadet_commit_hash = cdtGetLibraryCommitHash().decode('utf-8')
16391639

16401640
cdtGetLibraryBranchRefspec = self._lib.cdtGetLibraryBranchRefspec
16411641
cdtGetLibraryBranchRefspec.restype = ctypes.c_char_p
1642-
self.cadet_branch = cdtGetLibraryBranchRefspec().decode('utf-8')
1642+
self._cadet_branch = cdtGetLibraryBranchRefspec().decode('utf-8')
16431643

16441644
cdtGetLibraryBuildType = self._lib.cdtGetLibraryBuildType
16451645
cdtGetLibraryBuildType.restype = ctypes.c_char_p
1646-
self.cadet_build_type = cdtGetLibraryBuildType().decode('utf-8')
1646+
self._cadet_build_type = cdtGetLibraryBuildType().decode('utf-8')
16471647

16481648
# Define the log handler callback type
16491649
self.LOG_HANDLER_CLBK = ctypes.CFUNCTYPE(
@@ -2097,3 +2097,23 @@ def load_meta(self, sim: "Cadet") -> None:
20972097
sim.root.meta.cadet_version = self.cadet_version
20982098
sim.root.meta.file_format = self.res.file_format()
20992099
sim.root.meta.time_sim = self.res.time_sim()
2100+
2101+
@property
2102+
def cadet_version(self) -> str:
2103+
return self._cadet_version
2104+
2105+
@property
2106+
def cadet_branch(self) -> str:
2107+
return self._cadet_branch
2108+
2109+
@property
2110+
def cadet_build_type(self) -> str:
2111+
return self._cadet_build_type
2112+
2113+
@property
2114+
def cadet_commit_hash(self) -> str:
2115+
return self._cadet_commit_hash
2116+
2117+
@property
2118+
def cadet_path(self) -> str | os.PathLike:
2119+
return self._cadet_path

Diff for: cadet/runner.py

+100-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import pathlib
3+
import re
34
import subprocess
45
from abc import ABC, abstractmethod
56
from dataclasses import dataclass
@@ -76,6 +77,31 @@ def load_results(self, sim: "Cadet") -> None:
7677
"""
7778
pass
7879

80+
@property
81+
@abstractmethod
82+
def cadet_version(self) -> str:
83+
pass
84+
85+
@property
86+
@abstractmethod
87+
def cadet_branch(self) -> str:
88+
pass
89+
90+
@property
91+
@abstractmethod
92+
def cadet_build_type(self) -> str:
93+
pass
94+
95+
@property
96+
@abstractmethod
97+
def cadet_commit_hash(self) -> str:
98+
pass
99+
100+
@property
101+
@abstractmethod
102+
def cadet_path(self) -> str:
103+
pass
104+
79105

80106
class CadetCLIRunner(CadetRunnerBase):
81107
"""
@@ -95,7 +121,8 @@ def __init__(self, cadet_path: str | os.PathLike) -> None:
95121
"""
96122
cadet_path = Path(cadet_path)
97123

98-
self.cadet_path = cadet_path
124+
self._cadet_path = cadet_path
125+
self._get_cadet_version()
99126

100127
def run(
101128
self,
@@ -157,3 +184,75 @@ def load_results(self, sim: "Cadet") -> None:
157184
The simulation object where results will be loaded.
158185
"""
159186
sim.load(paths=["/meta", "/output"], update=True)
187+
188+
def _get_cadet_version(self) -> dict:
189+
"""
190+
Get version and branch name of the currently instanced CADET build.
191+
Returns
192+
-------
193+
dict
194+
Dictionary containing: cadet_version as x.x.x, cadet_branch, cadet_build_type, cadet_commit_hash
195+
Raises
196+
------
197+
ValueError
198+
If version and branch name cannot be found in the output string.
199+
RuntimeError
200+
If any unhandled event during running the subprocess occurs.
201+
"""
202+
try:
203+
result = subprocess.run(
204+
[self.cadet_path, '--version'],
205+
check=True,
206+
stdout=subprocess.PIPE,
207+
stderr=subprocess.PIPE,
208+
text=True
209+
)
210+
version_output = result.stdout.strip()
211+
212+
version_match = re.search(
213+
r'cadet-cli version ([\d.]+) \((.*) branch\)\n',
214+
version_output
215+
)
216+
217+
commit_hash_match = re.search(
218+
"Built from commit (.*)\n",
219+
version_output
220+
)
221+
222+
build_variant_match = re.search(
223+
"Build variant (.*)\n",
224+
version_output
225+
)
226+
227+
if version_match:
228+
self._cadet_version = version_match.group(1)
229+
self._cadet_branch = version_match.group(2)
230+
self._cadet_commit_hash = commit_hash_match.group(1)
231+
if build_variant_match:
232+
self._cadet_build_type = build_variant_match.group(1)
233+
else:
234+
self._cadet_build_type = None
235+
else:
236+
raise ValueError("CADET version or branch name missing from output.")
237+
except subprocess.CalledProcessError as e:
238+
raise RuntimeError(f"Command execution failed: {e}")
239+
240+
@property
241+
def cadet_version(self) -> str:
242+
return self._cadet_version
243+
244+
@property
245+
def cadet_branch(self) -> str:
246+
return self._cadet_branch
247+
248+
@property
249+
def cadet_build_type(self) -> str:
250+
return self._cadet_build_type
251+
252+
@property
253+
def cadet_commit_hash(self) -> str:
254+
return self._cadet_commit_hash
255+
256+
@property
257+
def cadet_path(self) -> str | os.PathLike:
258+
return self._cadet_path

0 commit comments

Comments
 (0)