Lines 14-20
func_end_re = re.compile(r'^\}$')
Link Here
|
14 |
|
14 |
|
15 |
var_assign_re = re.compile(r'(^|^declare\s+-\S+\s+|^declare\s+|^export\s+)([^=\s]+)=("|\')?.*$') |
15 |
var_assign_re = re.compile(r'(^|^declare\s+-\S+\s+|^declare\s+|^export\s+)([^=\s]+)=("|\')?.*$') |
16 |
close_quote_re = re.compile(r'(\\"|"|\')\s*$') |
16 |
close_quote_re = re.compile(r'(\\"|"|\')\s*$') |
17 |
readonly_re = re.compile(r'^declare\s+-(\S*)r(\S*)\s+') |
17 |
readonly_re = re.compile(r'^declare\s+-(\S*)r(\S*)\s+([^=\s]+)') |
|
|
18 |
trace_re = re.compile(r'^declare\s+-\S*t\S*\s+') |
18 |
# declare without assignment |
19 |
# declare without assignment |
19 |
var_declare_re = re.compile(r'^declare(\s+-\S+)?\s+([^=\s]+)\s*$') |
20 |
var_declare_re = re.compile(r'^declare(\s+-\S+)?\s+([^=\s]+)\s*$') |
20 |
|
21 |
|
Lines 29-35
def have_end_quote(quote, line):
Link Here
|
29 |
return close_quote_match is not None and \ |
30 |
return close_quote_match is not None and \ |
30 |
close_quote_match.group(1) == quote |
31 |
close_quote_match.group(1) == quote |
31 |
|
32 |
|
32 |
def filter_declare_readonly_opt(line): |
33 |
def filter_declare_readonly_opt(line, options): |
33 |
readonly_match = readonly_re.match(line) |
34 |
readonly_match = readonly_re.match(line) |
34 |
if readonly_match is not None: |
35 |
if readonly_match is not None: |
35 |
declare_opts = '' |
36 |
declare_opts = '' |
Lines 37-50
def filter_declare_readonly_opt(line):
Link Here
|
37 |
group = readonly_match.group(i) |
38 |
group = readonly_match.group(i) |
38 |
if group is not None: |
39 |
if group is not None: |
39 |
declare_opts += group |
40 |
declare_opts += group |
|
|
41 |
var = readonly_match.group(3) |
42 |
if '--report-readonly-variables' in options: |
43 |
sys.stderr.write(var + "\n") |
44 |
if '--preserve-readonly-attribute' in options: |
45 |
declare_opts += 'r' |
40 |
if declare_opts: |
46 |
if declare_opts: |
41 |
line = 'declare -%s %s' % \ |
47 |
line = 'declare -%s %s%s' % \ |
42 |
(declare_opts, line[readonly_match.end():]) |
48 |
(declare_opts, var, line[readonly_match.end():]) |
43 |
else: |
49 |
else: |
44 |
line = 'declare ' + line[readonly_match.end():] |
50 |
line = 'declare ' + var + line[readonly_match.end():] |
45 |
return line |
51 |
return line |
46 |
|
52 |
|
47 |
def filter_bash_environment(pattern, file_in, file_out): |
53 |
def filter_bash_environment(pattern, file_in, file_out, options): |
48 |
# Filter out any instances of the \1 character from variable values |
54 |
# Filter out any instances of the \1 character from variable values |
49 |
# since this character multiplies each time that the environment |
55 |
# since this character multiplies each time that the environment |
50 |
# is saved (strange bash behavior). This can eventually result in |
56 |
# is saved (strange bash behavior). This can eventually result in |
Lines 68-73
def filter_bash_environment(pattern, file_in, file_out):
Link Here
|
68 |
quote = var_assign_match.group(3) |
74 |
quote = var_assign_match.group(3) |
69 |
filter_this = pattern.match(var_assign_match.group(2)) \ |
75 |
filter_this = pattern.match(var_assign_match.group(2)) \ |
70 |
is not None |
76 |
is not None |
|
|
77 |
if not filter_this and '--filter-traced-variables' in options: |
78 |
filter_this = trace_re.match(line) is not None |
71 |
# Exclude the start quote when searching for the end quote, |
79 |
# Exclude the start quote when searching for the end quote, |
72 |
# to ensure that the start quote is not misidentified as the |
80 |
# to ensure that the start quote is not misidentified as the |
73 |
# end quote (happens if there is a newline immediately after |
81 |
# end quote (happens if there is a newline immediately after |
Lines 77-83
def filter_bash_environment(pattern, file_in, file_out):
Link Here
|
77 |
multi_line_quote = quote |
85 |
multi_line_quote = quote |
78 |
multi_line_quote_filter = filter_this |
86 |
multi_line_quote_filter = filter_this |
79 |
if not filter_this: |
87 |
if not filter_this: |
80 |
line = filter_declare_readonly_opt(line) |
88 |
line = filter_declare_readonly_opt(line, options) |
81 |
file_out.write(line.replace("\1", "")) |
89 |
file_out.write(line.replace("\1", "")) |
82 |
continue |
90 |
continue |
83 |
else: |
91 |
else: |
Lines 86-93
def filter_bash_environment(pattern, file_in, file_out):
Link Here
|
86 |
# declare without assignment |
94 |
# declare without assignment |
87 |
filter_this = pattern.match(declare_match.group(2)) \ |
95 |
filter_this = pattern.match(declare_match.group(2)) \ |
88 |
is not None |
96 |
is not None |
|
|
97 |
if not filter_this and '--filter-traced-variables' in options: |
98 |
filter_this = trace_re.match(line) is not None |
89 |
if not filter_this: |
99 |
if not filter_this: |
90 |
line = filter_declare_readonly_opt(line) |
100 |
line = filter_declare_readonly_opt(line, options) |
91 |
file_out.write(line) |
101 |
file_out.write(line) |
92 |
continue |
102 |
continue |
93 |
|
103 |
|
Lines 124-136
if __name__ == "__main__":
Link Here
|
124 |
"while leaving bash function definitions and here-documents " + \ |
134 |
"while leaving bash function definitions and here-documents " + \ |
125 |
"intact. The PATTERN is a space separated list of variable names" + \ |
135 |
"intact. The PATTERN is a space separated list of variable names" + \ |
126 |
" and it supports python regular expression syntax." |
136 |
" and it supports python regular expression syntax." |
127 |
usage = "usage: %s PATTERN" % os.path.basename(sys.argv[0]) |
137 |
usage = "usage: %s [-h|<options>] PATTERN" % os.path.basename(sys.argv[0]) |
128 |
args = sys.argv[1:] |
138 |
args = [] |
129 |
|
139 |
known_options = { |
130 |
if '-h' in args or '--help' in args: |
140 |
'--report-readonly-variables': |
131 |
sys.stdout.write(usage + "\n") |
141 |
"Write names of readonly variables to stderr.", |
132 |
sys.stdout.flush() |
142 |
'--preserve-readonly-attribute': |
133 |
sys.exit(os.EX_OK) |
143 |
"Preserve the '-r' flag in 'declare -r'.", |
|
|
144 |
'--filter-traced-variables': |
145 |
"Filter out variables declared with '-t' attribute." |
146 |
} |
147 |
options = {} |
148 |
for arg in sys.argv[1:]: |
149 |
if arg in known_options.keys(): |
150 |
options[arg] = True |
151 |
continue |
152 |
if '-h' == arg or '--help' == arg: |
153 |
sys.stdout.write(usage + "\n\nKnown <options>:\n\n") |
154 |
for option, descr in known_options.items(): |
155 |
sys.stdout.write(" " + option + "\t" + descr + "\n") |
156 |
sys.stdout.flush() |
157 |
sys.exit(os.EX_OK) |
158 |
args.append(arg) |
134 |
|
159 |
|
135 |
if len(args) != 1: |
160 |
if len(args) != 1: |
136 |
sys.stderr.write(usage + "\n") |
161 |
sys.stderr.write(usage + "\n") |
Lines 154-158
if __name__ == "__main__":
Link Here
|
154 |
|
179 |
|
155 |
var_pattern = "^(%s)$" % "|".join(var_pattern) |
180 |
var_pattern = "^(%s)$" % "|".join(var_pattern) |
156 |
filter_bash_environment( |
181 |
filter_bash_environment( |
157 |
re.compile(var_pattern), file_in, file_out) |
182 |
re.compile(var_pattern), file_in, file_out, options) |
158 |
file_out.flush() |
183 |
file_out.flush() |