1 |
Author: zmedico |
2 |
Date: 2008-03-05 19:51:12 +0000 (Wed, 05 Mar 2008) |
3 |
New Revision: 9440 |
4 |
|
5 |
Modified: |
6 |
main/trunk/bin/filter-bash-environment.py |
7 |
Log: |
8 |
Handle multi-line quoted variable assignments. |
9 |
|
10 |
|
11 |
Modified: main/trunk/bin/filter-bash-environment.py |
12 |
=================================================================== |
13 |
--- main/trunk/bin/filter-bash-environment.py 2008-03-05 19:01:29 UTC (rev 9439) |
14 |
+++ main/trunk/bin/filter-bash-environment.py 2008-03-05 19:51:12 UTC (rev 9440) |
15 |
@@ -15,17 +15,50 @@ |
16 |
func_start_re = re.compile(r'^[-\w]+\s*\(\)\s*$') |
17 |
func_end_re = re.compile(r'^\}$') |
18 |
|
19 |
-var_assign_re = re.compile(r'(^|^declare\s+-\S+\s+|^export\s+)([^=\s]+)=.*$') |
20 |
+var_assign_re = re.compile(r'(^|^declare\s+-\S+\s+|^export\s+)([^=\s]+)=("|\')?.*$') |
21 |
+close_quote_re = re.compile(r'(\\"|"|\')\s*$') |
22 |
|
23 |
def compile_egrep_pattern(s): |
24 |
for k, v in egrep_compat_map.iteritems(): |
25 |
s = s.replace(k, v) |
26 |
return re.compile(s) |
27 |
|
28 |
+def have_end_quote(quote, line): |
29 |
+ """ |
30 |
+ Check if the line has an end quote (useful for handling multi-line |
31 |
+ quotes). This handles escaped double quotes that may occur at the |
32 |
+ end of a line. The posix spec does not allow escaping of single |
33 |
+ quotes inside of single quotes, so that case is not handled. |
34 |
+ """ |
35 |
+ close_quote_match = close_quote_re.search(line) |
36 |
+ return close_quote_match is not None and \ |
37 |
+ close_quote_match.group(0) == quote |
38 |
+ |
39 |
def filter_bash_environment(pattern, file_in, file_out): |
40 |
here_doc_delim = None |
41 |
in_func = None |
42 |
+ multi_line_quote = None |
43 |
+ multi_line_quote_filter = None |
44 |
for line in file_in: |
45 |
+ if multi_line_quote is not None: |
46 |
+ if not multi_line_quote_filter: |
47 |
+ file_out.write(line) |
48 |
+ if have_end_quote(multi_line_quote, line): |
49 |
+ multi_line_quote = None |
50 |
+ multi_line_quote_filter = None |
51 |
+ continue |
52 |
+ if here_doc_delim is None and in_func is None: |
53 |
+ var_assign_match = var_assign_re.match(line) |
54 |
+ if var_assign_match is not None: |
55 |
+ quote = var_assign_match.group(3) |
56 |
+ filter_this = pattern.match(var_assign_match.group(2)) \ |
57 |
+ is not None |
58 |
+ if quote is not None and not have_end_quote(quote, line): |
59 |
+ multi_line_quote = quote |
60 |
+ multi_line_quote_filter = filter_this |
61 |
+ if not filter_this: |
62 |
+ file_out.write(line) |
63 |
+ continue |
64 |
if here_doc_delim is not None: |
65 |
if here_doc_delim.match(line): |
66 |
here_doc_delim = None |
67 |
@@ -48,13 +81,9 @@ |
68 |
if in_func is not None: |
69 |
file_out.write(line) |
70 |
continue |
71 |
- var_assign_match = var_assign_re.match(line) |
72 |
- if var_assign_match is not None: |
73 |
- if pattern.match(var_assign_match.group(2)) is None: |
74 |
- file_out.write(line) |
75 |
- continue |
76 |
- # TODO: properly handle multi-line variable assignments |
77 |
- # like those which the 'export' builtin can produce. |
78 |
+ # This line is not recognized as part of a variable assignment, |
79 |
+ # function definition, or here document, so just allow it to |
80 |
+ # pass through. |
81 |
file_out.write(line) |
82 |
|
83 |
if __name__ == "__main__": |
84 |
|
85 |
-- |
86 |
gentoo-commits@l.g.o mailing list |