From f898498038874b2cd945cb6c8dad0d14cc449ef7 Mon Sep 17 00:00:00 2001 From: "Bryn M. Reeves" Date: Mon, 23 Jun 2014 15:41:23 +0100 Subject: [PATCH 08/13] [plugin] backport do_path_regex_sub() Backport the following two commits to rhel-6: commit 5447facd2edd3b3e5f1d3ad736b411f8e6406077 Author: Bryn M. Reeves Date: Mon Jun 2 14:51:16 2014 +0100 Make do_path_regex_sub() honour string regex arguments The Plugin.do_path_regex_sub() method to apply regex substitutions to paths matching a pattern documents that it accepts either a compiled re object or a regular expression as a string: '''Apply a regexp substituation to a set of files archived by sos. The set of files to be substituted is generated by matching collected file pathnames against pathexp which may be a regular expression string or compiled re object. The portion of the file to be replaced is specified via regexp and the replacement string is passed in subst.''' It lies. Attempting to pass a string for the 'pathexp' parameter will result in: Traceback (most recent call last): File "/usr/sbin/sosreport", line 23, in main(sys.argv[1:]) File "/usr/lib/python2.6/site-packages/sos/sosreport.py", line 1229, in main sos.execute() AttributeError: 'str' object has no attribute 'match' > /usr/lib/python2.6/site-packages/sos/plugins/__init__.py(219)do_path_regex_sub() -> match = pathexp.match Look to see if the object we are passed has a 'match()' method and call re.compile on it if it does not. Signed-off-by: Bryn M. Reeves commit 6e8c0429cf4cbba8f3dc8001f36d7fb0e245c14e Author: Bryn M. Reeves Date: Thu Apr 3 21:22:50 2014 +0100 Add Plugin.do_path_regex_sub() Add a method to the Plugin class to apply a regex substitution to a set of paths maching a path regex. For e.g.: self.do_path_regex_sub(r'/etc/foo.*', 'pw=(.*)', 'pw=****') The oVirt plugin will use this. Signed-off-by: Bryn M. Reeves --- sos/plugintools.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sos/plugintools.py b/sos/plugintools.py index e985875..1034ac3 100644 --- a/sos/plugintools.py +++ b/sos/plugintools.py @@ -151,6 +151,20 @@ class PluginBase: self.soslog.error(msg % (called['exe'], self.piName, e)) return replacements + def doPathRegexSub(self, pathexp, regexp, subst): + '''Apply a regexp substituation to a set of files archived by + sos. The set of files to be substituted is generated by matchin + collected file pathnames against pathexp which may be a regular + expression string or compiled re object. The portion of the file + to be replaced is specified via regexp and the replacement string + is passed in subst.''' + if not hasattr(pathexp, "match"): + pathexp = re.compile(pathexp) + match = pathexp.match + file_list = [f for f in self.copiedFiles if match(f['srcpath'])] + for file in file_list: + self.doRegexSub(file['srcpath'], regexp, subst) + def doRegexFindAll(self, regex, fname): ''' Return a list of all non overlapping matches in the string(s) ''' -- 1.9.3