Bug #302937 - Handle declare -r without assignment. (trunk r15304)
authorZac Medico <zmedico@gentoo.org>
Tue, 2 Mar 2010 19:51:14 +0000 (19:51 -0000)
committerZac Medico <zmedico@gentoo.org>
Tue, 2 Mar 2010 19:51:14 +0000 (19:51 -0000)
svn path=/main/branches/2.1.7/; revision=15539

bin/filter-bash-environment.py

index e919572c9486506e8a704490386c32cb35ce5c2e..8c0fdb9da88dc750b4605521664f250ff67acaf0 100755 (executable)
@@ -13,6 +13,8 @@ func_end_re = re.compile(r'^\}$')
 var_assign_re = re.compile(r'(^|^declare\s+-\S+\s+|^declare\s+|^export\s+)([^=\s]+)=("|\')?.*$')
 close_quote_re = re.compile(r'(\\"|"|\')\s*$')
 readonly_re = re.compile(r'^declare\s+-(\S*)r(\S*)\s+')
+# declare without assignment
+var_declare_re = re.compile(r'^declare(\s+-\S+)?\s+([^=\s]+)\s*$')
 
 def have_end_quote(quote, line):
        """
@@ -25,6 +27,21 @@ def have_end_quote(quote, line):
        return close_quote_match is not None and \
                close_quote_match.group(1) == quote
 
+def filter_declare_readonly_opt(line):
+       readonly_match = readonly_re.match(line)
+       if readonly_match is not None:
+               declare_opts = ''
+               for i in (1, 2):
+                       group = readonly_match.group(i)
+                       if group is not None:
+                               declare_opts += group
+               if declare_opts:
+                       line = 'declare -%s %s' % \
+                               (declare_opts, line[readonly_match.end():])
+               else:
+                       line = 'declare ' + line[readonly_match.end():]
+       return line
+
 def filter_bash_environment(pattern, file_in, file_out):
        # Filter out any instances of the \1 character from variable values
        # since this character multiplies each time that the environment
@@ -58,20 +75,20 @@ def filter_bash_environment(pattern, file_in, file_out):
                                        multi_line_quote = quote
                                        multi_line_quote_filter = filter_this
                                if not filter_this:
-                                       readonly_match = readonly_re.match(line)
-                                       if readonly_match is not None:
-                                               declare_opts = ""
-                                               for i in (1, 2):
-                                                       group = readonly_match.group(i)
-                                                       if group is not None:
-                                                               declare_opts += group
-                                               if declare_opts:
-                                                       line = "declare -%s %s" % \
-                                                               (declare_opts, line[readonly_match.end():])
-                                               else:
-                                                       line = "declare " + line[readonly_match.end():]
+                                       line = filter_declare_readonly_opt(line)
                                        file_out.write(line.replace("\1", ""))
                                continue
+                       else:
+                               declare_match = var_declare_re.match(line)
+                               if declare_match is not None:
+                                       # declare without assignment
+                                       filter_this = pattern.match(declare_match.group(2)) \
+                                               is not None
+                                       if not filter_this:
+                                               line = filter_declare_readonly_opt(line)
+                                               file_out.write(line)
+                                       continue
+
                if here_doc_delim is not None:
                        if here_doc_delim.match(line):
                                here_doc_delim = None