1
1
import os
2
2
import pathlib
3
+ import re
3
4
import subprocess
4
5
from abc import ABC , abstractmethod
5
6
from dataclasses import dataclass
@@ -76,6 +77,31 @@ def load_results(self, sim: "Cadet") -> None:
76
77
"""
77
78
pass
78
79
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
+
79
105
80
106
class CadetCLIRunner (CadetRunnerBase ):
81
107
"""
@@ -95,7 +121,8 @@ def __init__(self, cadet_path: str | os.PathLike) -> None:
95
121
"""
96
122
cadet_path = Path (cadet_path )
97
123
98
- self .cadet_path = cadet_path
124
+ self ._cadet_path = cadet_path
125
+ self ._get_cadet_version ()
99
126
100
127
def run (
101
128
self ,
@@ -157,3 +184,75 @@ def load_results(self, sim: "Cadet") -> None:
157
184
The simulation object where results will be loaded.
158
185
"""
159
186
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