-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbash.py
More file actions
405 lines (325 loc) · 10.3 KB
/
bash.py
File metadata and controls
405 lines (325 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# Copyright (c) 2021 The Toltec Contributors
# SPDX-License-Identifier: MIT
"""Bridge Bash with Python."""
import os
import shlex
import subprocess
import logging
from collections import deque
from typing import Deque, Dict, Generator, List, Optional, Tuple, Union
AssociativeArray = Dict[str, str]
IndexedArray = List[Optional[str]]
LogGenerator = Generator[str, None, None]
Any = Union[str, AssociativeArray, IndexedArray]
Variables = Dict[str, Optional[Any]]
Functions = Dict[str, str]
class ScriptError(Exception):
"""Raised when a launched Bash script exits with a non-zero code."""
# Variables which are defined by default by Bash. Those variables are excluded
# from the result of `get_declarations()`. Subset of the list at:
# <https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html>
default_variables = {
"BASH",
"BASHOPTS",
"BASHPID",
"BASH_ALIASES",
"BASH_ARGC",
"BASH_ARGV",
"BASH_ARGV0",
"BASH_CMDS",
"BASH_COMMAND",
"BASH_LINENO",
"BASH_SOURCE",
"BASH_SUBSHELL",
"BASH_VERSINFO",
"BASH_VERSION",
"COLUMNS",
"COMP_WORDBREAKS",
"DIRSTACK",
"EPOCHREALTIME",
"EPOCHSECONDS",
"EUID",
"FUNCNAME",
"GROUPS",
"HISTCMD",
"HISTFILE",
"HISTFILESIZE",
"HISTSIZE",
"HOSTNAME",
"HOSTTYPE",
"IFS",
"LINENO",
"LINES",
"MACHTYPE",
"MAILCHECK",
"OLDPWD",
"OPTERR",
"OPTIND",
"OSTYPE",
"PATH",
"PIPESTATUS",
"PPID",
"PS1",
"PS2",
"PS4",
"PWD",
"RANDOM",
"SECONDS",
"SHELL",
"SHELLOPTS",
"SHLVL",
"SRANDOM",
"TERM",
"UID",
"_",
}
def get_declarations(src: str) -> Tuple[Variables, Functions]:
"""
Extract all variables and functions defined by a Bash script.
If a function or a variable is defined or assigned multiple times
in the script, only the final value is extracted. The script must not
output anything on the standard output stream.
:param src: source string of the considered Bash string
:returns: a tuple containing the declared variables and functions
"""
src += """
declare -f
declare -p
"""
env: Dict[str, str] = {
"PATH": os.environ["PATH"],
}
declarations_subshell = (
subprocess.run( # pylint:disable=subprocess-run-check
["/usr/bin/env", "bash"],
input=src.encode(),
capture_output=True,
env=env,
)
)
errors = declarations_subshell.stderr.decode()
if declarations_subshell.returncode == 2 or "syntax error" in errors:
raise ScriptError(f"Bash syntax error\n{errors}")
if declarations_subshell.returncode != 0 or errors:
raise ScriptError(f"Bash error\n{errors}")
declarations = declarations_subshell.stdout.decode()
# Parse `declare` statements and function statements
lexer = shlex.shlex(declarations, posix=True)
lexer.wordchars = lexer.wordchars + "-"
variables = {}
functions = {}
while True:
token = lexer.get_token()
if token == lexer.eof:
break
next_token = lexer.get_token()
if token == "declare" and next_token[0] == "-":
lexer.push_token(next_token)
name, value = _parse_var(lexer)
if name not in default_variables:
variables[name] = value
else:
assert next_token == "("
assert lexer.get_token() == ")"
start, end = _parse_func(lexer)
functions[token] = declarations[start:end].strip(" ")
return variables, functions
def put_variables(variables: Variables) -> str:
"""
Generate a Bash script fragment which defines a set of variables.
:param variables: set of variables to define
:returns: generated Bash fragment
"""
result = ""
for name, value in variables.items():
if value is None:
result += f"declare -- {name}\n"
elif isinstance(value, str):
result += f"declare -- {name}={_generate_string(value)}\n"
elif isinstance(value, list):
result += f"declare -a {name}={_generate_indexed(value)}\n"
elif isinstance(value, dict):
result += f"declare -A {name}={_generate_assoc(value)}\n"
else:
raise ValueError(
f"Unsupported type {type(value)} for variable \
{name}"
)
return result
def put_functions(functions: Functions) -> str:
"""
Generate a Bash script which defines a set of functions.
:param functions: set of functions to define
:returns: generated Bash fragment
"""
result = ""
for name, value in functions.items():
result += f"{name}() {{\n{value}\n}}\n"
return result
def _parse_string(token: str) -> str:
"""Remove escape sequences from a Bash string."""
return token.replace("\\$", "$")
def _generate_string(string: str) -> str:
"""Generate a Bash string."""
return shlex.quote(string)
def _parse_indexed(lexer: shlex.shlex) -> IndexedArray:
"""Parse an indexed Bash array."""
assert lexer.get_token() == "("
result: List[Optional[str]] = []
while True:
token = lexer.get_token()
assert token != lexer.eof
if token == ")":
break
assert token == "["
index = int(lexer.get_token())
assert lexer.get_token() == "]"
assert lexer.get_token() == "="
value = _parse_string(lexer.get_token())
# Grow the result array so that the index exists
if index >= len(result):
result.extend([None] * (index - len(result) + 1))
result[index] = value
return result
def _generate_indexed(array: IndexedArray) -> str:
"""Generate an indexed Bash array."""
return (
"("
+ " ".join(
f"[{index}]={_generate_string(value)}"
for index, value in enumerate(array)
if value is not None
)
+ ")"
)
def _parse_assoc(lexer: shlex.shlex) -> AssociativeArray:
"""Parse an associative Bash array."""
assert lexer.get_token() == "("
result = {}
while True:
token = lexer.get_token()
assert token != lexer.eof
if token == ")":
break
assert token == "["
key = lexer.get_token()
assert lexer.get_token() == "]"
assert lexer.get_token() == "="
value = _parse_string(lexer.get_token())
result[key] = value
return result
def _generate_assoc(array: AssociativeArray) -> str:
"""Generate an associative Bash array."""
return (
"("
+ " ".join(
f"[{_generate_string(key)}]={_generate_string(value)}"
for key, value in array.items()
)
+ ")"
)
def _parse_var(lexer: shlex.shlex) -> Tuple[str, Optional[Any]]:
"""Parse a variable declaration."""
flags_token = lexer.get_token()
if flags_token != "--":
var_flags = set(flags_token[1:])
else:
var_flags = set()
var_name = lexer.get_token()
var_value: Optional[Any] = None
lookahead = lexer.get_token()
if lookahead == "=":
if "a" in var_flags:
var_value = _parse_indexed(lexer)
elif "A" in var_flags:
var_value = _parse_assoc(lexer)
else:
var_value = _parse_string(lexer.get_token())
else:
lexer.push_token(lookahead)
return var_name, var_value
def _parse_func(lexer: shlex.shlex) -> Tuple[int, int]:
"""Find the starting and end bounds of a function declaration."""
assert lexer.get_token() == "{"
brace_depth = 1
start_byte = lexer.instream.tell()
while brace_depth > 0:
token = lexer.get_token()
assert token != lexer.eof
if token == "{":
brace_depth += 1
elif token == "}":
brace_depth -= 1
end_byte = lexer.instream.tell() - 1
return start_byte, end_byte
def run_script(variables: Variables, script: str) -> LogGenerator:
"""
Run a Bash script and stream its output.
:param variables: Bash variables to set before running the script
:param script: Bash script to execute
:returns: generator yielding output lines from the script
:raises ScriptError: if the script exits with a non-zero code
"""
with subprocess.Popen(
["/usr/bin/env", "bash"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
) as process:
assert process.stdin is not None
assert process.stdout is not None
process.stdin.write(
"\n".join(
(
"set -euo pipefail",
put_variables(variables),
"script() {",
script,
"}",
"script",
)
).encode()
)
process.stdin.close()
while process.poll() is None:
line = process.stdout.readline()
if line:
yield line.decode().strip()
if process.returncode != 0:
raise ScriptError(f"Script exited with code {process.returncode}")
def pipe_logs(
logger: logging.Logger,
logs: LogGenerator,
prefix: str = "",
max_lines_on_fail: int = 50,
) -> None:
"""
Pipe logs from a script to the debug output of a Python logger.
Print the last :param:`max_lines_on_fail` log lines to the error output in
case a ScriptError is caught.
:param logs: generator of log lines
:param prefix: log prefix
:param max_lines_on_fail: number of context lines to print
in non-debug mode
"""
log_buffer: Deque[str] = deque()
try:
for line in logs:
if logger.getEffectiveLevel() <= logging.DEBUG:
logger.debug("%s%s", prefix + ": ", line)
else:
if len(log_buffer) == max_lines_on_fail:
log_buffer.popleft()
log_buffer.append(line)
except ScriptError as err:
if len(log_buffer) > 0:
logger.info(
"Only showing up to %s lines of context. "
"Use --verbose for the full output.",
max_lines_on_fail,
)
for line in log_buffer:
logger.error("%s%s", prefix + ": ", line)
if prefix:
logger.error("%s failed", prefix)
raise err