From 1e68949af3de13b0b871605c41ff46ef0ce32dcf Mon Sep 17 00:00:00 2001 From: Pavel Vondruska Date: Thu, 11 Dec 2025 11:44:27 +0100 Subject: [PATCH] Refactor: move run_command and read_temperature to system_utils --- src/hpc_eff/utils/command_runner.py | 18 ------------ src/hpc_eff/utils/cpu_thermo.py | 16 +---------- src/hpc_eff/utils/frequency_reader.py | 2 +- src/hpc_eff/utils/get_available_attrs.py | 2 +- src/hpc_eff/utils/system_utils.py | 36 ++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 35 deletions(-) delete mode 100644 src/hpc_eff/utils/command_runner.py create mode 100644 src/hpc_eff/utils/system_utils.py diff --git a/src/hpc_eff/utils/command_runner.py b/src/hpc_eff/utils/command_runner.py deleted file mode 100644 index b451820..0000000 --- a/src/hpc_eff/utils/command_runner.py +++ /dev/null @@ -1,18 +0,0 @@ -""" -Centralized command execution utilities. -""" -import subprocess - - -def run_command(cmd: str) -> str: - """ - Execute a shell command and return its output. - - Args: - cmd: Shell command to execute - - Returns: - Command stdout (stripped) - """ - result = subprocess.run(cmd, capture_output=True, text=True, shell=True) - return result.stdout.strip() diff --git a/src/hpc_eff/utils/cpu_thermo.py b/src/hpc_eff/utils/cpu_thermo.py index d3b68d7..0874058 100644 --- a/src/hpc_eff/utils/cpu_thermo.py +++ b/src/hpc_eff/utils/cpu_thermo.py @@ -4,24 +4,10 @@ import time import requests from .set_cpu import log_setting, logger -from .command_runner import run_command +from .system_utils import run_command, read_temperature from .frequency_reader import get_cpu_max_frequency, freq_to_khz, get_cpu_count -def read_temperature(sensor_name: str) -> int | None: - """Try to read temperature via ipmitool sensor reading. Return integer Celsius or None.""" - try: - out = run_command(f'ipmitool sensor reading "{sensor_name}" 2>/dev/null') - if not out: - return None - m = re.search(r"\d+", out) - if not m: - return None - return int(m.group(0)) - except Exception: - return None - - def apply_cpu_thermo(conn=None, log_context=None, config=None): """Apply temperature-based CPU max-frequency settings. diff --git a/src/hpc_eff/utils/frequency_reader.py b/src/hpc_eff/utils/frequency_reader.py index 5b859a1..5be4a48 100644 --- a/src/hpc_eff/utils/frequency_reader.py +++ b/src/hpc_eff/utils/frequency_reader.py @@ -1,7 +1,7 @@ import re import shutil import glob -from .command_runner import run_command +from .system_utils import run_command def freq_to_khz(freq_str: str) -> int: diff --git a/src/hpc_eff/utils/get_available_attrs.py b/src/hpc_eff/utils/get_available_attrs.py index 6bc7200..581dd1e 100644 --- a/src/hpc_eff/utils/get_available_attrs.py +++ b/src/hpc_eff/utils/get_available_attrs.py @@ -2,7 +2,7 @@ import re import glob import shutil -from .command_runner import run_command +from .system_utils import run_command def get_available_frequencies(): diff --git a/src/hpc_eff/utils/system_utils.py b/src/hpc_eff/utils/system_utils.py new file mode 100644 index 0000000..7532d91 --- /dev/null +++ b/src/hpc_eff/utils/system_utils.py @@ -0,0 +1,36 @@ +""" +General system utilities: centralized command execution and hardware sensors. +""" +import subprocess +import re +from typing import Optional + + +def run_command(cmd: str) -> str: + """Execute a shell command and return its stdout (stripped). + + Args: + cmd: Shell command to execute + + Returns: + Command stdout (stripped) + """ + result = subprocess.run(cmd, capture_output=True, text=True, shell=True) + return result.stdout.strip() + + +def read_temperature(sensor_name: str) -> Optional[int]: + """Try to read temperature via `ipmitool sensor reading`. + + Returns integer Celsius on success or None on failure. + """ + try: + out = run_command(f'ipmitool sensor reading "{sensor_name}" 2>/dev/null') + if not out: + return None + m = re.search(r"\d+", out) + if not m: + return None + return int(m.group(0)) + except Exception: + return None