From 820724b418ac0020394bfbc8f17ac9bcbb4926e5 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Tue, 4 Mar 2008 07:44:11 +0000 Subject: [PATCH] Add support for idendification of function definitions since it's needed in some cases in order to prevent some odd function contents from being mistakenly identified as invalid variable assignments. For example, this line from _gcc-specs-directive_raw() is commonly found in environment.bz2 files: $1=="*"directive":" { pspec=spec; spec=""; outside=0; next } svn path=/main/trunk/; revision=9431 --- bin/filter-bash-environment.py | 20 ++++++++++++++++++-- 1 file 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]) -- 2.26.2