diff options
author | Bryn M. Reeves <bmr@errorists.org> | 2012-12-05 15:20:34 +0000 |
---|---|---|
committer | Bryn M. Reeves <bmr@redhat.com> | 2012-12-05 19:50:33 +0000 |
commit | 3a982ffb7a63710d8fdbbfb3d885fd6704c8b6fe (patch) | |
tree | 7140c13538f8bd6da9334d95bd7221024907ad1b | |
parent | 23199d56b8d99cd2beed229939de45acb45ef2ba (diff) | |
download | sos-3a982ffb7a63710d8fdbbfb3d885fd6704c8b6fe.tar.gz |
Add implementation of command output post-processing
Add a doRegexExtOutputSub() function to mirror doRegExSub(). This
allows modukes ti apply an arbitrary regular expression
substitution to the output collected from external commands.
-rw-r--r-- | sos/plugins/__init__.py | 29 | ||||
-rw-r--r-- | sos/utilities.py | 3 |
2 files changed, 31 insertions, 1 deletions
diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py index bf9f5a0b..ad4e5839 100644 --- a/sos/plugins/__init__.py +++ b/sos/plugins/__init__.py @@ -40,6 +40,7 @@ from time import time from itertools import * import logging import urllib2 +import fnmatch try: import json @@ -164,6 +165,34 @@ class Plugin(object): '''Is the package $package_name installed?''' return (self.policy().pkgByName(package_name) is not None) + def doRegexExtOutputSub(self, cmd, regexp, subst): + '''Apply a regexp substitution to command output archived by sosreport. + cmd is the command name from which output is collected (i.e. excluding + parameters). The regexp can be a string or a compiled re object. The + substitution string, subst, is a string that replaces each occurrence + of regexp in each file collected from cmd. Internally 'cmd' is treated + as a glob with a trailing '*' and each matching file from the current + module's command list is subjected to the replacement. + + This function returns the number of replacements made. + ''' + globstr = '*' + cmd + '*' + cmdpath = os.path.join(self.cInfo['cmddir'], "foo") + try: + for called in self.executedCommands: + if fnmatch.fnmatch(called['exe'], globstr): + path = os.path.join(self.cInfo['cmddir'], called['file']) + readable = self.archive.open_file(path) + result, replacements = re.subn( + regexp, subst, readable.read()) + if replacements: + self.archive.add_string(result, path) + return replacements + else: + return 0 + except Exception, e: + return 0 + def doRegexSub(self, srcpath, regexp, subst): '''Apply a regexp substitution to a file archived by sosreport. srcpath is the path in the archive where the file can be found. regexp diff --git a/sos/utilities.py b/sos/utilities.py index 9aafacb3..3b254c7c 100644 --- a/sos/utilities.py +++ b/sos/utilities.py @@ -25,7 +25,6 @@ from __future__ import with_statement import os import re import string -import fnmatch import inspect from stat import * #from itertools import * @@ -36,6 +35,8 @@ import zipfile import tarfile import hashlib import logging +import fnmatch + from contextlib import closing try: from cStringIO import StringIO |