Suppress subprocess script output in quiet mode.

This commit is contained in:
Darko Poljak 2017-06-29 10:18:46 +02:00 committed by Steven Armstrong
parent a722f3c634
commit a37d286d67
4 changed files with 32 additions and 11 deletions

View file

@ -220,13 +220,15 @@ class Config(object):
host_dir_name=host_dir_name, host_dir_name=host_dir_name,
initial_manifest=args.manifest, initial_manifest=args.manifest,
add_conf_dirs=args.conf_dir, add_conf_dirs=args.conf_dir,
cache_path_pattern=args.cache_path_pattern) cache_path_pattern=args.cache_path_pattern,
quiet_mode=args.quiet)
remote = cdist.exec.remote.Remote( remote = cdist.exec.remote.Remote(
target_host=target_host, target_host=target_host,
remote_exec=remote_exec, remote_exec=remote_exec,
remote_copy=remote_copy, remote_copy=remote_copy,
base_path=args.remote_out_path) base_path=args.remote_out_path,
quiet_mode=args.quiet)
c = cls(local, remote, dry_run=args.dry_run, jobs=args.jobs) c = cls(local, remote, dry_run=args.dry_run, jobs=args.jobs)
c.run() c.run()

View file

@ -54,7 +54,8 @@ class Local(object):
exec_path=sys.argv[0], exec_path=sys.argv[0],
initial_manifest=None, initial_manifest=None,
add_conf_dirs=None, add_conf_dirs=None,
cache_path_pattern=None): cache_path_pattern=None,
quiet_mode=False):
self.target_host = target_host self.target_host = target_host
self.hostdir = host_dir_name self.hostdir = host_dir_name
@ -64,6 +65,7 @@ class Local(object):
self.custom_initial_manifest = initial_manifest self.custom_initial_manifest = initial_manifest
self._add_conf_dirs = add_conf_dirs self._add_conf_dirs = add_conf_dirs
self.cache_path_pattern = cache_path_pattern self.cache_path_pattern = cache_path_pattern
self.quiet_mode = quiet_mode
self._init_log() self._init_log()
self._init_permissions() self._init_permissions()
@ -212,8 +214,13 @@ class Local(object):
self.log.trace("Local run: %s", command) self.log.trace("Local run: %s", command)
try: try:
if self.quiet_mode:
stderr = subprocess.DEVNULL
else:
stderr = None
if save_output: if save_output:
output, errout = exec_util.call_get_output(command, env=env) output, errout = exec_util.call_get_output(
command, env=env, stderr=stderr)
self.log.trace("Local stdout: {}".format(output)) self.log.trace("Local stdout: {}".format(output))
# Currently, stderr is not captured. # Currently, stderr is not captured.
# self.log.trace("Local stderr: {}".format(errout)) # self.log.trace("Local stderr: {}".format(errout))
@ -223,7 +230,12 @@ class Local(object):
# In some cases no output is saved. # In some cases no output is saved.
# This is used for shell command, stdout and stderr # This is used for shell command, stdout and stderr
# must not be catched. # must not be catched.
subprocess.check_call(command, env=env) if self.quiet_mode:
stdout = subprocess.DEVNULL
else:
stdout = None
subprocess.check_call(command, env=env, stderr=stderr,
stdout=stdout)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
exec_util.handle_called_process_error(e, command) exec_util.handle_called_process_error(e, command)
except OSError as error: except OSError as error:

View file

@ -62,7 +62,8 @@ class Remote(object):
target_host, target_host,
remote_exec, remote_exec,
remote_copy, remote_copy,
base_path=None): base_path=None,
quiet_mode=None):
self.target_host = target_host self.target_host = target_host
self._exec = remote_exec self._exec = remote_exec
self._copy = remote_copy self._copy = remote_copy
@ -71,6 +72,7 @@ class Remote(object):
self.base_path = base_path self.base_path = base_path
else: else:
self.base_path = "/var/lib/cdist" self.base_path = "/var/lib/cdist"
self.quiet_mode = quiet_mode
self.conf_path = os.path.join(self.base_path, "conf") self.conf_path = os.path.join(self.base_path, "conf")
self.object_path = os.path.join(self.base_path, "object") self.object_path = os.path.join(self.base_path, "object")
@ -228,7 +230,12 @@ class Remote(object):
self.log.trace("Remote run: %s", command) self.log.trace("Remote run: %s", command)
try: try:
output, errout = exec_util.call_get_output(command, env=os_environ) if self.quiet_mode:
stderr = subprocess.DEVNULL
else:
stderr = None
output, errout = exec_util.call_get_output(
command, env=os_environ, stderr=stderr)
self.log.trace("Remote stdout: {}".format(output)) self.log.trace("Remote stdout: {}".format(output))
# Currently, stderr is not captured. # Currently, stderr is not captured.
# self.log.trace("Remote stderr: {}".format(errout)) # self.log.trace("Remote stderr: {}".format(errout))

View file

@ -116,14 +116,14 @@ import cdist
# return (result.stdout, result.stderr) # return (result.stdout, result.stderr)
def call_get_output(command, env=None): def call_get_output(command, env=None, stderr=None):
"""Run the given command with the given environment. """Run the given command with the given environment.
Return the tuple of stdout and stderr output as a byte strings. Return the tuple of stdout and stderr output as a byte strings.
""" """
assert isinstance(command, (list, tuple)), ( assert isinstance(command, (list, tuple)), (
"list or tuple argument expected, got: {}".format(command)) "list or tuple argument expected, got: {}".format(command))
return (_call_get_stdout(command, env), None) return (_call_get_stdout(command, env, stderr), None)
def handle_called_process_error(err, command): def handle_called_process_error(err, command):
@ -140,7 +140,7 @@ def handle_called_process_error(err, command):
err.returncode, err.output)) err.returncode, err.output))
def _call_get_stdout(command, env=None): def _call_get_stdout(command, env=None, stderr=None):
"""Run the given command with the given environment. """Run the given command with the given environment.
Return the stdout output as a byte string, stderr is ignored. Return the stdout output as a byte string, stderr is ignored.
""" """
@ -148,7 +148,7 @@ def _call_get_stdout(command, env=None):
"list or tuple argument expected, got: {}".format(command)) "list or tuple argument expected, got: {}".format(command))
with TemporaryFile() as fout: with TemporaryFile() as fout:
subprocess.check_call(command, env=env, stdout=fout) subprocess.check_call(command, env=env, stdout=fout, stderr=stderr)
fout.seek(0) fout.seek(0)
output = fout.read() output = fout.read()