From 7b2885740216f8e4076781e088d37cb7b4f96f63 Mon Sep 17 00:00:00 2001 From: John Haxby Date: Wed, 28 Jan 2015 19:30:29 +0000 Subject: [PATCH] [plugins,utilities] Allow commands to discard stderr Just add "stderr=False" to add_cmd_output() if you don't want the command's stderr in the sosreport. Signed-off-by: John Haxby Minor edits for pep8 conformance and to shorten the argument name. Signed-off-by: Bryn M. Reeves --- sos/plugins/__init__.py | 29 +++++++++++++++++------------ sos/utilities.py | 5 +++-- 2 files changed, 20 insertions(+), 14 deletions(-) --- sos/plugins/__init__.py | 33 +++++++++++++++++++++------------ sos/utilities.py | 5 +++-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py index 4fd85be..9d126a5 100644 --- a/sos/plugins/__init__.py +++ b/sos/plugins/__init__.py @@ -460,8 +460,9 @@ class Plugin(object): self.copy_paths.update(copy_paths) self._log_info("added copyspec '%s'" % copyspec) - def get_command_output(self, prog, timeout=300, runat=None): - result = sos_get_command_output(prog, timeout=timeout, runat=runat) + def get_command_output(self, prog, timeout=300, runat=None, stderr=True): + result = sos_get_command_output(prog, timeout=timeout, runat=runat, + stderr=stderr) if result['status'] == 124: self._log_warn("command '%s' timed out after %ds" % (prog, timeout)) @@ -470,11 +471,12 @@ class Plugin(object): self._log_debug("could not run '%s': command not found" % prog) return result - def call_ext_prog(self, prog, timeout=300, runat=None): + def call_ext_prog(self, prog, timeout=300, runat=None, stderr=True): """Execute a command independantly of the output gathering part of sosreport. """ - return self.get_command_output(prog, timeout=timeout, runat=runat) + return self.get_command_output(prog, timeout=timeout, runat=runat, + stderr=True) def check_ext_prog(self, prog): """Execute a command independently of the output gathering part of @@ -484,15 +486,19 @@ class Plugin(object): return self.call_ext_prog(prog)['status'] == 0 def add_cmd_output(self, cmds, suggest_filename=None, - root_symlink=None, timeout=300, runat=None): + root_symlink=None, timeout=300, runat=None, + stderr=True): """Run a program or a list of programs and collect the output""" if isinstance(cmds, six.string_types): cmds = [cmds] if len(cmds) > 1 and (suggest_filename or root_symlink): self._log_warn("ambiguous filename or symlink for command list") for cmd in cmds: - cmdt = (cmd, suggest_filename, root_symlink, timeout, runat) - _logstr = "packed command tuple: ('%s', '%s', '%s', %s, '%s')" + cmdt = ( + cmd, suggest_filename, root_symlink, timeout, runat, stderr + ) + _tuplefmt = "('%s', '%s', '%s', %s, '%s', '%s')" + _logstr = "packed command tuple: " + _tuplefmt self._log_debug(_logstr % cmdt) self.collect_cmds.append(cmdt) self._log_info("added cmd output '%s'" % cmd) @@ -547,12 +553,13 @@ class Plugin(object): def get_cmd_output_now(self, exe, suggest_filename=None, root_symlink=False, timeout=300, - runat=None): + runat=None, stderr=True): """Execute a command and save the output to a file for inclusion in the report. """ start = time() - result = self.get_command_output(exe, timeout=timeout, runat=runat) + result = self.get_command_output(exe, timeout=timeout, runat=runat, + stderr=stderr) # 126 means 'found but not executable' if result['status'] == 126 or result['status'] == 127: return None @@ -601,13 +608,15 @@ class Plugin(object): def _collect_cmd_output(self): for progs in zip(self.collect_cmds): - prog, suggest_filename, root_symlink, timeout, runat = progs[0] + (prog, suggest_filename, root_symlink, timeout, runat, stderr + ) = progs[0] self._log_debug("unpacked command tuple: " - + "('%s', '%s', '%s', %s, '%s')" % progs[0]) + + "('%s', '%s', '%s', %s, '%s', %s)" % progs[0]) self._log_info("collecting output of '%s'" % prog) self.get_cmd_output_now(prog, suggest_filename=suggest_filename, root_symlink=root_symlink, - timeout=timeout, runat=runat) + timeout=timeout, runat=runat, + stderr=stderr) def _collect_strings(self): for string, file_name in self.copy_strings: diff --git a/sos/utilities.py b/sos/utilities.py index 2638ce4..db340f8 100644 --- a/sos/utilities.py +++ b/sos/utilities.py @@ -120,7 +120,7 @@ def is_executable(command): return any(os.access(path, os.X_OK) for path in candidates) -def sos_get_command_output(command, timeout=300, runat=None): +def sos_get_command_output(command, timeout=300, runat=None, stderr=True): """Execute a command through the system shell. First checks to see if the requested command is executable. Returns (returncode, stdout, 0)""" def _child_chdir(): @@ -142,7 +142,8 @@ def sos_get_command_output(command, timeout=300, runat=None): command = command.encode('utf-8', 'ignore') args = shlex.split(command) try: - p = Popen(args, shell=False, stdout=PIPE, stderr=STDOUT, + p = Popen(args, shell=False, stdout=PIPE, + stderr=STDOUT if stderr else PIPE, bufsize=-1, env=cmd_env, close_fds=True, preexec_fn=_child_chdir) except OSError as e: -- 1.8.3.1