Add support for idendification of function definitions since it's needed
authorZac Medico <zmedico@gentoo.org>
Tue, 4 Mar 2008 07:44:11 +0000 (07:44 -0000)
committerZac Medico <zmedico@gentoo.org>
Tue, 4 Mar 2008 07:44:11 +0000 (07:44 -0000)
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

index 691d406da7f06e8f32e4e948baaafd0e63ffcfa2..0c1e67911ea0e10cdb3eb0f2bc37c5aad03ab8c7 100755 (executable)
@@ -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])