def execute_command_subprocess()

in src/module_utils/sap_automation_qa.py [0:0]


    def execute_command_subprocess(self, command: str, shell_command: bool = False) -> str:
        """
        Executes a shell command using subprocess with a timeout and logs output or errors.

        :param command: Shell command to execute
        :type command: str
        :param shell_command: Whether the command is a shell command
        :type shell_command: bool
        :return: Standard output from the command
        :rtype: str
        """
        command_string = command if isinstance(command, str) else " ".join(command).replace("'", "")
        self.log(
            logging.INFO,
            f"Executing command: {command_string}",
        )
        try:
            command_output = subprocess.run(
                command,
                timeout=30,
                check=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                shell=shell_command,
            )
            stdout = command_output.stdout.decode("utf-8")
            stderr = command_output.stderr.decode("utf-8")
            return stdout if not stderr else stderr
        except subprocess.TimeoutExpired as ex:
            self.handle_error(ex, "Command timed out")
        except subprocess.CalledProcessError as ex:
            self.handle_error(ex, ex.stderr.decode("utf-8").strip())
        except Exception as ex:
            self.handle_error(ex, "")
        return ""