Files
cibuildwheel/cibuildwheel/bashlex_eval.py
T

113 lines
3.9 KiB
Python
Raw Normal View History

2019-11-12 23:51:27 +00:00
import shlex
import subprocess
2020-06-26 20:22:49 +01:00
from typing import Callable, Dict, List, NamedTuple, Optional, Sequence
import bashlex # type: ignore
# a function that takes a shell command and the environment, and returns the result
EnvironmentExecutor = Callable[[str, Dict[str, str]], str]
def local_environment_executor(command: str, env: Dict[str, str]) -> str:
return subprocess.check_output(shlex.split(command), env=env, universal_newlines=True)
class NodeExecutionContext(NamedTuple):
environment: Dict[str, str]
input: str
executor: EnvironmentExecutor
2019-11-12 23:51:27 +00:00
def evaluate(value: str, environment: Dict[str, str], executor: Optional[EnvironmentExecutor] = None) -> str:
if not value:
# empty string evaluates to empty string
# (but trips up bashlex)
return ''
command_node = bashlex.parsesingle(value)
if len(command_node.parts) != 1:
2020-05-09 10:34:59 +02:00
raise ValueError(f'"{value}" has too many parts')
value_word_node = command_node.parts[0]
2019-11-12 23:51:27 +00:00
2020-04-08 00:51:04 +02:00
return evaluate_node(
2019-11-12 23:51:27 +00:00
value_word_node,
context=NodeExecutionContext(environment=environment, input=value, executor=executor or local_environment_executor)
)
2017-09-01 13:24:12 +01:00
2020-04-08 00:51:04 +02:00
def evaluate_node(node: bashlex.ast.node, context: NodeExecutionContext) -> str:
2017-09-01 13:24:12 +01:00
if node.kind == 'word':
2020-04-08 00:51:04 +02:00
return evaluate_word_node(node, context=context)
2017-09-01 13:24:12 +01:00
elif node.kind == 'commandsubstitution':
node_result = evaluate_command_node(node.command, context=context)
# bash removes training newlines in command substitution
return node_result.rstrip()
2017-09-01 13:24:12 +01:00
elif node.kind == 'parameter':
2020-04-08 00:51:04 +02:00
return evaluate_parameter_node(node, context=context)
2017-09-01 13:24:12 +01:00
else:
raise ValueError(f'Unsupported bash construct: "{node.kind}"')
2017-09-01 13:24:12 +01:00
2020-04-08 00:51:04 +02:00
def evaluate_word_node(node: bashlex.ast.node, context: NodeExecutionContext) -> str:
word_start = node.pos[0]
word_end = node.pos[1]
word_string = context.input[word_start:word_end]
2020-04-10 20:24:52 +02:00
letters = list(word_string)
2017-09-01 13:24:12 +01:00
for part in node.parts:
part_start = part.pos[0] - word_start
part_end = part.pos[1] - word_start
2017-09-01 13:24:12 +01:00
# Set all the characters in the part to None
for i in range(part_start, part_end):
2020-04-10 20:24:52 +02:00
letters[i] = ''
2017-09-01 13:24:12 +01:00
2020-04-08 00:51:04 +02:00
letters[part_start] = evaluate_node(part, context=context)
2017-09-01 13:24:12 +01:00
# remove the None letters and concat
2020-04-10 20:24:52 +02:00
value = ''.join(letters)
2020-07-12 12:45:59 +01:00
print('node value:', value)
# apply bash-like quotes/whitespace treatment
return ' '.join(word.strip() for word in shlex.split(value))
2017-09-01 13:24:12 +01:00
2020-04-08 00:51:04 +02:00
def evaluate_command_node(node: bashlex.ast.node, context: NodeExecutionContext) -> str:
if any(n.kind == 'operator' for n in node.parts):
return evaluate_nodes_as_compound_command(node.parts, context=context)
else:
return evaluate_nodes_as_simple_command(node.parts, context=context)
def evaluate_nodes_as_compound_command(nodes: Sequence[bashlex.ast.node], context: NodeExecutionContext) -> str:
# bashlex doesn't support any operators besides ';' inside command
# substitutions, so we only need to handle that case. We do so assuming
# that `set -o errexit` is on, because it's easier to code!
result = ''
for node in nodes:
if node.kind == 'command':
result += evaluate_command_node(node, context=context)
elif node.kind == 'operator':
if node.op == ';':
pass
else:
raise ValueError(f'Unsupported bash operator: "{node.op}"')
else:
raise ValueError(f'Unsupported bash node in compound command: "{node.kind}"')
return result
def evaluate_nodes_as_simple_command(nodes: List[bashlex.ast.node], context: NodeExecutionContext):
words = [evaluate_node(part, context=context) for part in nodes]
2017-09-01 13:24:12 +01:00
command = ' '.join(words)
return context.executor(command, context.environment)
2017-09-01 13:24:12 +01:00
2020-04-08 00:51:04 +02:00
def evaluate_parameter_node(node: bashlex.ast.node, context: NodeExecutionContext) -> str:
return context.environment.get(node.value, '')