Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup execute_command by using CalledProcessError #3146

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading