diff options
-rwxr-xr-x | bin/filter-bash-environment.py | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/bin/filter-bash-environment.py b/bin/filter-bash-environment.py index 691d406da..0c1e67911 100755 --- a/bin/filter-bash-environment.py +++ b/bin/filter-bash-environment.py @@ -12,6 +12,8 @@ egrep_compat_map = { } here_doc_re = re.compile(r'.*\s<<[-]?(\w+)$') +func_start_re = re.compile(r'^\s*[-\w]*\s*\(\)\s*$') +func_end_re = re.compile(r'^\}$') def compile_egrep_pattern(s): for k, v in egrep_compat_map.iteritems(): @@ -20,6 +22,7 @@ def compile_egrep_pattern(s): def filter_bash_environment(pattern, file_in, file_out): here_doc_delim = None + in_func = None for line in file_in: if here_doc_delim is not None: if here_doc_delim.match(line): @@ -31,13 +34,26 @@ def filter_bash_environment(pattern, file_in, file_out): here_doc_delim = re.compile("^%s$" % here_doc.group(1)) file_out.write(line) continue + # Note: here-documents are handled before fuctions since otherwise + # it would be possible for the content of a here-document to be + # mistaken as the end of a function. + if in_func: + if func_end_re.match(line) is not None: + in_func = None + file_out.write(line) + continue + in_func = func_start_re.match(line) + if in_func is not None: + 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 " + \ + "while leaving bash function definitions and here-documents " + \ + "intact. The PATTERN should use python regular expression syntax" + \ + " but [:digit:], [:space:] and " + \ "[:alnum:] character classes will be automatically translated " + \ "for compatibility with egrep syntax." usage = "usage: %s PATTERN" % os.path.basename(sys.argv[0]) |