From a37d286d6759aeb8d58943da97d28e5eb5da7701 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Thu, 29 Jun 2017 10:18:46 +0200 Subject: [PATCH] Suppress subprocess script output in quiet mode. --- cdist/config.py | 6 ++++-- cdist/exec/local.py | 18 +++++++++++++++--- cdist/exec/remote.py | 11 +++++++++-- cdist/exec/util.py | 8 ++++---- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/cdist/config.py b/cdist/config.py index 4109fa85..b17ec67f 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -220,13 +220,15 @@ class Config(object): host_dir_name=host_dir_name, initial_manifest=args.manifest, 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( target_host=target_host, remote_exec=remote_exec, 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.run() diff --git a/cdist/exec/local.py b/cdist/exec/local.py index 2ff31466..06127660 100644 --- a/cdist/exec/local.py +++ b/cdist/exec/local.py @@ -54,7 +54,8 @@ class Local(object): exec_path=sys.argv[0], initial_manifest=None, add_conf_dirs=None, - cache_path_pattern=None): + cache_path_pattern=None, + quiet_mode=False): self.target_host = target_host self.hostdir = host_dir_name @@ -64,6 +65,7 @@ class Local(object): self.custom_initial_manifest = initial_manifest self._add_conf_dirs = add_conf_dirs self.cache_path_pattern = cache_path_pattern + self.quiet_mode = quiet_mode self._init_log() self._init_permissions() @@ -212,8 +214,13 @@ class Local(object): self.log.trace("Local run: %s", command) try: + if self.quiet_mode: + stderr = subprocess.DEVNULL + else: + stderr = None 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)) # Currently, stderr is not captured. # self.log.trace("Local stderr: {}".format(errout)) @@ -223,7 +230,12 @@ class Local(object): # In some cases no output is saved. # This is used for shell command, stdout and stderr # 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: exec_util.handle_called_process_error(e, command) except OSError as error: diff --git a/cdist/exec/remote.py b/cdist/exec/remote.py index ff4ad30a..2a27bae4 100644 --- a/cdist/exec/remote.py +++ b/cdist/exec/remote.py @@ -62,7 +62,8 @@ class Remote(object): target_host, remote_exec, remote_copy, - base_path=None): + base_path=None, + quiet_mode=None): self.target_host = target_host self._exec = remote_exec self._copy = remote_copy @@ -71,6 +72,7 @@ class Remote(object): self.base_path = base_path else: self.base_path = "/var/lib/cdist" + self.quiet_mode = quiet_mode self.conf_path = os.path.join(self.base_path, "conf") self.object_path = os.path.join(self.base_path, "object") @@ -228,7 +230,12 @@ class Remote(object): self.log.trace("Remote run: %s", command) 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)) # Currently, stderr is not captured. # self.log.trace("Remote stderr: {}".format(errout)) diff --git a/cdist/exec/util.py b/cdist/exec/util.py index 864a73a3..9ed7103b 100644 --- a/cdist/exec/util.py +++ b/cdist/exec/util.py @@ -116,14 +116,14 @@ import cdist # 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. 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)) - return (_call_get_stdout(command, env), None) + return (_call_get_stdout(command, env, stderr), None) def handle_called_process_error(err, command): @@ -140,7 +140,7 @@ def handle_called_process_error(err, command): 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. 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)) 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) output = fout.read()