Skip to content

Commit

Permalink
Cleanup execute_command by using CalledProcessError
Browse files Browse the repository at this point in the history
  • Loading branch information
Siecje committed Dec 4, 2023
1 parent c3d2768 commit 677c85f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 71 deletions.
37 changes: 19 additions & 18 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
72 changes: 19 additions & 53 deletions volttron/platform/agent/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 677c85f

Please sign in to comment.