diff options
author | Zac Medico <zmedico@gentoo.org> | 2007-12-13 05:54:49 +0000 |
---|---|---|
committer | Zac Medico <zmedico@gentoo.org> | 2007-12-13 05:54:49 +0000 |
commit | 78fb2c22e9b81dd5cd2e0d35a1d7d8960bc363e7 (patch) | |
tree | 3092c12edac23c3c087dcea1942f0c8a107af988 /bin/filter-bash-environment.py | |
parent | fcc332a75cf153b9912bc4b26f1d4f028daa7ff3 (diff) | |
download | portage-78fb2c22e9b81dd5cd2e0d35a1d7d8960bc363e7.tar.gz portage-78fb2c22e9b81dd5cd2e0d35a1d7d8960bc363e7.tar.bz2 portage-78fb2c22e9b81dd5cd2e0d35a1d7d8960bc363e7.zip |
* In filter_readonly_variables(), replace 'declare -r ' with 'declare '
instead of removing it completely.
* Bug #202068 - In order to filter unwanted variable assignments out
of the bash environment, use a filter-bash-environment.py script
that behaves similar to egrep -v except that it leaves bash here-
documents intact.
* Use the sed -r option to enable extended regular expressions so that
commonly used characters like (, ), and + don't have to be escaped.
(trunk r8890:8893)
svn path=/main/branches/2.1.2/; revision=8899
Diffstat (limited to 'bin/filter-bash-environment.py')
-rwxr-xr-x | bin/filter-bash-environment.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/bin/filter-bash-environment.py b/bin/filter-bash-environment.py new file mode 100755 index 000000000..83b250b69 --- /dev/null +++ b/bin/filter-bash-environment.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# Copyright 1999-2007 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id$ + +import os, re, sys + +egrep_compat_map = { + "[:alnum:]" : r'\w', + "[:space:]" : r'\s', +} + +here_doc_re = re.compile(r'.*\s<<[-]?(\w+)$') + +def compile_egrep_pattern(s): + for k, v in egrep_compat_map.iteritems(): + s = s.replace(k, v) + return re.compile(s) + +def filter_bash_environment(pattern, file_in, file_out): + here_doc_delim = None + for line in file_in: + if here_doc_delim is not None: + if here_doc_delim.match(line): + here_doc_delim = None + file_out.write(line) + continue + here_doc = here_doc_re.match(line) + if here_doc is not None: + here_doc_delim = re.compile("^%s$" % here_doc.group(1)) + file_out.write(line) + continue + if pattern.match(line) is None: + file_out.write(line) + +if __name__ == "__main__": + description = "Filter out any lines that match a given PATTERN " + \ + "while leaving bash here-documents intact. The PATTERN should " + \ + "use python regular expression syntax but [:space:] and " + \ + "[:alnum:] character classes will be automatically translated " + \ + "for compatibility with egrep syntax." + usage = "usage: %s PATTERN" % os.path.basename(sys.argv[0]) + from optparse import OptionParser + parser = OptionParser(description=description, usage=usage) + options, args = parser.parse_args(sys.argv[1:]) + if len(args) != 1: + parser.error("Missing required PATTERN argument.") + file_in = sys.stdin + file_out = sys.stdout + filter_bash_environment( + compile_egrep_pattern(args[0]), file_in, file_out) + file_out.flush() |