Fix process error handling in local.

remotes/origin/4.2
Darko Poljak 9 years ago
parent 95461cded7
commit d48b65ed53
  1. 9
      cdist/exec/local.py
  2. 49
      cdist/exec/util.py

@ -207,14 +207,13 @@ class Local(object):
env.update(message.env)
try:
output = exec_util.call_get_output(command, env=env)
self.log.debug("Local output: {}".format(output))
output, errout = exec_util.call_get_output(command, env=env)
self.log.debug("Local stdout: {}".format(output))
self.log.debug("Local stderr: {}".format(errout))
if return_output:
return output.decode()
except subprocess.CalledProcessError as e:
raise cdist.Error("Command failed: " + " ".join(command)
+ " with returncode: {} and output: {}".format(
e.returncode, e.output))
exec_util.handle_called_process_error(e, command)
except OSError as error:
raise cdist.Error(" ".join(command) + ": " + error.args[1])
finally:

49
cdist/exec/util.py vendored

@ -20,20 +20,61 @@
#
import subprocess
import sys
from tempfile import TemporaryFile
import cdist
STDERR_UNSUPPORTED = 'Not supported in this python version'
def call_get_output(command, env=None):
"""Run the given command with the given environment.
Return the stdout and stderr output as a byte string.
Return the tuple of stdout and stderr output as a byte strings.
"""
assert isinstance(command, (list, tuple)), (
"list or tuple argument expected, got: {}".format(command))
if sys.version_info >= (3, 5):
return call_get_out_err(command, env)
else:
return (call_get_stdout(command, env), STDERR_UNSUPPORTED)
def handle_called_process_error(err, command):
if sys.version_info >= (3, 5):
errout = err.stderr
else:
errout = STDERR_UNSUPPORTED
raise cdist.Error("Command failed: " + " ".join(command)
+ " with returncode: {} and stdout: {}, stderr: {}".format(
err.returncode, err.output, errout))
def call_get_stdout(command, env=None):
"""Run the given command with the given environment.
Return the stdout output as a byte string, stderr is ignored.
"""
assert isinstance(command, (list, tuple)), "list or tuple argument expected, got: {}".format(command)
assert isinstance(command, (list, tuple)), (
"list or tuple argument expected, got: {}".format(command))
with TemporaryFile() as fout:
subprocess.check_call(command, env=env,
stdout=fout, stderr=subprocess.STDOUT)
subprocess.check_call(command, env=env, stdout=fout)
fout.seek(0)
output = fout.read()
return output
def call_get_out_err(command, env=None):
"""Run the given command with the given environment.
Return the tuple of stdout and stderr output as a byte strings.
"""
assert isinstance(command, (list, tuple)), (
"list or tuple argument expected, got: {}".format(command))
with TemporaryFile() as fout, TemporaryFile() as ferr:
subprocess.check_call(command, env=env,
stdout=fout, stderr=ferr)
fout.seek(0)
ferr.seek(0)
output = (fout.read(), ferr.read())
return output

Loading…
Cancel
Save