From 677c85fc60f3ce0434d0463516e987d1eb86f47b Mon Sep 17 00:00:00 2001 From: Cody Scott Date: Mon, 4 Dec 2023 18:11:24 -0500 Subject: [PATCH] Cleanup execute_command by using CalledProcessError --- docs/source/conf.py | 37 ++++++++-------- volttron/platform/agent/utils.py | 72 +++++++++----------------------- 2 files changed, 38 insertions(+), 71 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 80eaeb23f6..d0410d2fcc 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -37,7 +37,7 @@ def execute_command(cmds, raise a RuntimeError. If logger is specified then write the exception to the logger otherwise this call will remain silent. - :param cmds:list of commands to pass to subprocess.run + :param cmds: list of commands to pass to subprocess.run :param env: environment to run the command with :param cwd: working directory for the command :param logger: a logger to use if errors occure @@ -46,26 +46,27 @@ def execute_command(cmds, :raises RuntimeError: if the return code is not 0 from suprocess.run """ - - results = subprocess.run(cmds, - env=env, - cwd=cwd, - stderr=subprocess.PIPE, - stdout=subprocess.PIPE, - shell=use_shell) - if results.returncode != 0: + try: + results = subprocess.run( + cmds, + env=env, + cwd=cwd, + stderr=subprocess.PIPE, + stdout=subprocess.PIPE, + shell=use_shell, + check=True + ) + return results.stdout.decode('utf-8') + except subprocess.CalledProcessError as e: err_prefix = err_prefix if err_prefix is not None else "Error executing command" - err_message = "\n{}: Below Command failed with non zero exit code.\n" \ - "Command:{} \nStderr:\n{}\n".format(err_prefix, - results.args, - results.stderr) + err_message = f"\n{err_prefix}: Command failed with non-zero exit code.\n" \ + f"Command: {e.cmd}\n" \ + f"Return Code: {e.returncode}\n" \ + f"Stdout:\n{e.stdout.decode('utf-8')}\n" \ + f"Stderr:\n{e.stderr.decode('utf-8')}\n" if logger: logger.exception(err_message) - raise RuntimeError(err_message) - else: - raise RuntimeError(err_message) - - return results.stdout.decode('utf-8') + raise RuntimeError(err_message) class Mock(MagicMock): diff --git a/volttron/platform/agent/utils.py b/volttron/platform/agent/utils.py index e1f62125c3..96652b47f2 100644 --- a/volttron/platform/agent/utils.py +++ b/volttron/platform/agent/utils.py @@ -838,7 +838,7 @@ def execute_command(cmds, raise a RuntimeError. If logger is specified then write the exception to the logger otherwise this call will remain silent. - :param cmds:list of commands to pass to subprocess.run + :param cmds: list of commands to pass to subprocess.run :param env: environment to run the command with :param cwd: working directory for the command :param logger: a logger to use if errors occure @@ -848,61 +848,27 @@ def execute_command(cmds, :raises RuntimeError: if the return code is not 0 from suprocess.run """ - results = subprocess.run(cmds, - env=env, - cwd=cwd, - stderr=subprocess.PIPE, - stdout=subprocess.PIPE, - shell=use_shell) - if results.returncode != 0: + try: + results = subprocess.run( + cmds, + env=env, + cwd=cwd, + stderr=subprocess.PIPE, + stdout=subprocess.PIPE, + shell=use_shell, + check=True + ) + return results.stdout.decode('utf-8') + except subprocess.CalledProcessError as e: err_prefix = err_prefix if err_prefix is not None else "Error executing command" - err_message = "\n{}: Below Command failed with non zero exit code.\n" \ - "Command:{} \nStderr:\n{}\n".format(err_prefix, - results.args, - results.stderr) + err_message = f"\n{err_prefix}: Command failed with non-zero exit code.\n" \ + f"Command: {e.cmd}\n" \ + f"Return Code: {e.returncode}\n" \ + f"Stdout:\n{e.stdout.decode('utf-8')}\n" \ + f"Stderr:\n{e.stderr.decode('utf-8')}\n" if logger: logger.exception(err_message) - raise RuntimeError(err_message) - else: - raise RuntimeError(err_message) - - return results.stdout.decode('utf-8') - - -# -# def execute_command_p(cmds, env=None, cwd=None, logger=None, err_prefix=None): -# """ Executes a given command using a subprocess. -# -# Returns the return code and stdout of the call. -# -# :param cmds: -# :param env: -# :param cwd: -# :param logger: -# :param err_prefix: -# :return: -# """ -# if cwd is None: -# cwd = os.getcwd() -# -# # try: -# results = subprocess.run(cmds, env=env, cwd=cwd, -# stderr=subprocess.PIPE, stdout=subprocess.PIPE) -# if results.returncode != 0: -# err_prefix = "Error executing command" -# err_message = "\n{}: Below Command failed with non zero exit code.\n" \ -# "Command:{} \nStderr:\n{}\n".format(err_prefix, -# results.args, -# results.stderr) -# if logger: -# logger.exception(err_message) -# raise RuntimeError() -# else: -# raise RuntimeError(err_message) -# return results.returncode, results.stdout.decode('utf-8') -# # except BaseException as e: -# # _log.error("Exception running cmd: {} . Exception: {}".format(cmds, e)) -# # raise e + raise RuntimeError(err_message) def is_volttron_running(volttron_home):