def report(self, reason: str, message: str) -> None: logger.info("[REPORT] Reason: %s. Message: %s", reason, message) try: rc = self._report_command rc = rc.replace("%REASON%", reason) rc = rc.replace("%MESSAGE%", message) exec_shell_command(rc) except Exception as ex: logger.warning("Report failed: %s", ex, exc_info=True)
def _alert_cmd(self, shell_cmd): if not shell_cmd: return try: exec_shell_command(shell_cmd) except Exception as e: logger.warning( "Enable to execute %s trigger command %s:\n%s", self.trigger_name, shell_cmd, e, )
def _get_temp(self) -> Tuple[TempCelsius, TempCelsius, TempCelsius]: temps = [ float(line.strip()) for line in exec_shell_command(self._shell_command).split("\n") if line.strip() ] temp = TempCelsius(temps[0]) if self._min is not None: min_t = self._min else: min_t = TempCelsius(temps[1]) if self._max is not None: max_t = self._max else: max_t = TempCelsius(temps[2]) return temp, min_t, max_t
def _call_hddtemp(self) -> str: # `disk_path` might be a glob, so it has to be executed with a shell. shell_command = "%s -n -u C -- %s" % (self._hddtemp_bin, self._disk_path) return exec_shell_command(shell_command, timeout=10)
def test_exec_shell_command_successful(): assert "42\n" == exec_shell_command("echo 42")
def test_exec_shell_command_expands_glob(temp_path): (temp_path / "sda").write_text("") (temp_path / "sdb").write_text("") expected = "{0}/sda {0}/sdb\n".format(temp_path) assert expected == exec_shell_command('echo "%s/sd"?' % temp_path)
def test_exec_shell_command_raises_for_unicode(): with pytest.raises(ValueError): exec_shell_command("echo привет")
def test_exec_shell_command_erroneous(): with pytest.raises(subprocess.SubprocessError): exec_shell_command("echo 42 && false")
def test_exec_shell_command_ignores_stderr(): assert "42\n" == exec_shell_command("echo 111 >&2; echo 42")
def sensed_exec_shell_command(*args, **kwargs): exec_shell_command_stdout.append(exec_shell_command(*args, **kwargs)) return exec_shell_command_stdout[-1]
def _call_ipmi_sensors(self) -> str: shell_command = "%s %s --sensor-types Fan --comma-separated-output" % ( self._ipmi_sensors_bin, self._ipmi_sensors_extra_args, ) return exec_shell_command(shell_command, timeout=2)