implement: dont return command output by default

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
Steven Armstrong 2011-10-14 15:51:00 +02:00
parent e715dbb801
commit ab1d3d16f1

View file

@ -87,7 +87,7 @@ class Local(object):
# FIXME: dont set mode here, fix unittest mkdtemp instead
os.makedirs(path, mode=0o700, exist_ok=True)
def run(self, command, env=None):
def run(self, command, env=None, return_output=False):
"""Run the given command with the given environment.
Return the output as a string.
@ -95,13 +95,16 @@ class Local(object):
assert isinstance(command, (list, tuple)), "list or tuple argument expected, got: %s" % command
self.log.debug("Local run: %s", command)
try:
return subprocess.check_output(command, env=env).decode()
if return_output:
return subprocess.check_output(command, env=env).decode()
else:
subprocess.check_call(command, env=env)
except subprocess.CalledProcessError:
raise cdist.Error("Command failed: " + " ".join(command))
except OSError as error:
raise cdist.Error(" ".join(*args) + ": " + error.args[1])
def run_script(self, script, env=None):
def run_script(self, script, env=None, return_output=False):
"""Run the given script with the given environment.
Return the output as a string.
@ -114,7 +117,10 @@ class Local(object):
self.log.debug("Local run script env: %s", env)
try:
return subprocess.check_output(command, env=env).decode()
if return_output:
return subprocess.check_output(command, env=env).decode()
else:
subprocess.check_call(command, env=env)
except subprocess.CalledProcessError as error:
script_content = self.run(["cat", script])
self.log.error("Code that raised the error:\n%s", script_content)