Skip to content

Commit

Permalink
Add CADET meta information to Runner classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ronald-jaepel committed Aug 20, 2024
1 parent a1e8148 commit e153f3b
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 7 deletions.
32 changes: 26 additions & 6 deletions cadet/cadet_dll.py
Original file line number Diff line number Diff line change
Expand Up @@ -1625,25 +1625,25 @@ def __init__(self, dll_path: os.PathLike | str) -> None:
dll_path : os.PathLike or str
Path to the CADET DLL.
"""
self.cadet_path = Path(dll_path)
self._lib = ctypes.cdll.LoadLibrary(self.cadet_path.as_posix())
self._cadet_path = Path(dll_path)
self._lib = ctypes.cdll.LoadLibrary(self._cadet_path.as_posix())

# Query meta information
cdtGetLibraryVersion = self._lib.cdtGetLibraryVersion
cdtGetLibraryVersion.restype = ctypes.c_char_p
self.cadet_version = cdtGetLibraryVersion().decode('utf-8')
self._cadet_version = cdtGetLibraryVersion().decode('utf-8')

cdtGetLibraryCommitHash = self._lib.cdtGetLibraryCommitHash
cdtGetLibraryCommitHash.restype = ctypes.c_char_p
self.cadet_commit_hash = cdtGetLibraryCommitHash().decode('utf-8')
self._cadet_commit_hash = cdtGetLibraryCommitHash().decode('utf-8')

cdtGetLibraryBranchRefspec = self._lib.cdtGetLibraryBranchRefspec
cdtGetLibraryBranchRefspec.restype = ctypes.c_char_p
self.cadet_branch = cdtGetLibraryBranchRefspec().decode('utf-8')
self._cadet_branch = cdtGetLibraryBranchRefspec().decode('utf-8')

cdtGetLibraryBuildType = self._lib.cdtGetLibraryBuildType
cdtGetLibraryBuildType.restype = ctypes.c_char_p
self.cadet_build_type = cdtGetLibraryBuildType().decode('utf-8')
self._cadet_build_type = cdtGetLibraryBuildType().decode('utf-8')

# Define the log handler callback type
self.LOG_HANDLER_CLBK = ctypes.CFUNCTYPE(
Expand Down Expand Up @@ -2097,3 +2097,23 @@ def load_meta(self, sim: "Cadet") -> None:
sim.root.meta.cadet_version = self.cadet_version
sim.root.meta.file_format = self.res.file_format()
sim.root.meta.time_sim = self.res.time_sim()

@property
def cadet_version(self) -> str:
return self._cadet_version

@property
def cadet_branch(self) -> str:
return self._cadet_branch

@property
def cadet_build_type(self) -> str:
return self._cadet_build_type

@property
def cadet_commit_hash(self) -> str:
return self._cadet_commit_hash

@property
def cadet_path(self) -> str | os.PathLike:
return self._cadet_path
101 changes: 100 additions & 1 deletion cadet/runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pathlib
import re
import subprocess
from abc import ABC, abstractmethod
from dataclasses import dataclass
Expand Down Expand Up @@ -76,6 +77,31 @@ def load_results(self, sim: "Cadet") -> None:
"""
pass

@property
@abstractmethod
def cadet_version(self) -> str:
pass

@property
@abstractmethod
def cadet_branch(self) -> str:
pass

@property
@abstractmethod
def cadet_build_type(self) -> str:
pass

@property
@abstractmethod
def cadet_commit_hash(self) -> str:
pass

@property
@abstractmethod
def cadet_path(self) -> str:
pass


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

self.cadet_path = cadet_path
self._cadet_path = cadet_path
self._get_cadet_version()

def run(
self,
Expand Down Expand Up @@ -157,3 +184,75 @@ def load_results(self, sim: "Cadet") -> None:
The simulation object where results will be loaded.
"""
sim.load(paths=["/meta", "/output"], update=True)

def _get_cadet_version(self) -> dict:
"""
Get version and branch name of the currently instanced CADET build.
Returns
-------
dict
Dictionary containing: cadet_version as x.x.x, cadet_branch, cadet_build_type, cadet_commit_hash
Raises
------
ValueError
If version and branch name cannot be found in the output string.
RuntimeError
If any unhandled event during running the subprocess occurs.
"""
try:
result = subprocess.run(
[self.cadet_path, '--version'],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
version_output = result.stdout.strip()

version_match = re.search(
r'cadet-cli version ([\d.]+) \((.*) branch\)\n',
version_output
)

commit_hash_match = re.search(
"Built from commit (.*)\n",
version_output
)

build_variant_match = re.search(
"Build variant (.*)\n",
version_output
)

if version_match:
self._cadet_version = version_match.group(1)
self._cadet_branch = version_match.group(2)
self._cadet_commit_hash = commit_hash_match.group(1)
if build_variant_match:
self._cadet_build_type = build_variant_match.group(1)
else:
self._cadet_build_type = None
else:
raise ValueError("CADET version or branch name missing from output.")
except subprocess.CalledProcessError as e:
raise RuntimeError(f"Command execution failed: {e}")

@property
def cadet_version(self) -> str:
return self._cadet_version

@property
def cadet_branch(self) -> str:
return self._cadet_branch

@property
def cadet_build_type(self) -> str:
return self._cadet_build_type

@property
def cadet_commit_hash(self) -> str:
return self._cadet_commit_hash

@property
def cadet_path(self) -> str | os.PathLike:
return self._cadet_path

0 comments on commit e153f3b

Please sign in to comment.