Suppress subprocess script output in quiet mode.
This commit is contained in:
		
					parent
					
						
							
								a722f3c634
							
						
					
				
			
			
				commit
				
					
						a37d286d67
					
				
			
		
					 4 changed files with 32 additions and 11 deletions
				
			
		| 
						 | 
				
			
			@ -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()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue