2026-05-28 00:20:08 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-06-12 09:01:49 -04:00
|
|
|
__lazy_modules__ = {"bashlex", "subprocess"}
|
|
|
|
|
|
2025-05-28 05:41:26 -04:00
|
|
|
import dataclasses
|
2019-11-12 23:51:27 +00:00
|
|
|
import subprocess
|
2020-04-08 00:16:25 +02:00
|
|
|
|
2021-01-02 14:32:55 -05:00
|
|
|
import bashlex
|
2017-09-02 22:09:32 +01:00
|
|
|
|
2026-05-28 00:20:08 +02:00
|
|
|
TYPE_CHECKING = False
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from collections.abc import (
|
|
|
|
|
Callable,
|
|
|
|
|
Iterable,
|
|
|
|
|
Mapping,
|
|
|
|
|
Sequence,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# a function that takes a command and the environment, and returns the result
|
|
|
|
|
EnvironmentExecutor = Callable[[list[str], dict[str, str]], str]
|
2020-06-23 19:46:23 +01:00
|
|
|
|
|
|
|
|
|
2023-04-18 13:06:17 -04:00
|
|
|
def local_environment_executor(command: Sequence[str], env: Mapping[str, str]) -> str:
|
2022-07-14 12:47:01 +02:00
|
|
|
return subprocess.run(command, env=env, text=True, stdout=subprocess.PIPE, check=True).stdout
|
2020-06-23 19:46:23 +01:00
|
|
|
|
|
|
|
|
|
2025-05-28 05:41:26 -04:00
|
|
|
@dataclasses.dataclass(frozen=True, kw_only=True)
|
2022-04-28 09:19:27 -04:00
|
|
|
class NodeExecutionContext:
|
2022-07-14 13:36:57 +02:00
|
|
|
environment: dict[str, str]
|
2020-04-10 01:53:06 +02:00
|
|
|
input: str
|
2020-06-23 19:46:23 +01:00
|
|
|
executor: EnvironmentExecutor
|
2017-09-02 22:09:32 +01:00
|
|
|
|
2019-11-12 23:51:27 +00:00
|
|
|
|
2021-04-30 17:56:34 -04:00
|
|
|
def evaluate(
|
2023-04-18 13:06:17 -04:00
|
|
|
value: str, environment: Mapping[str, str], executor: EnvironmentExecutor | None = None
|
2021-04-30 17:56:34 -04:00
|
|
|
) -> str:
|
2017-09-02 22:09:32 +01:00
|
|
|
if not value:
|
|
|
|
|
# empty string evaluates to empty string
|
|
|
|
|
# (but trips up bashlex)
|
2021-05-03 11:45:43 -04:00
|
|
|
return ""
|
2017-09-02 22:09:32 +01:00
|
|
|
|
|
|
|
|
command_node = bashlex.parsesingle(value)
|
|
|
|
|
|
|
|
|
|
if len(command_node.parts) != 1:
|
2022-09-05 13:11:46 -04:00
|
|
|
msg = f"{value!r} has too many parts"
|
|
|
|
|
raise ValueError(msg)
|
2017-09-02 22:09:32 +01:00
|
|
|
|
|
|
|
|
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,
|
2021-04-30 17:56:34 -04:00
|
|
|
context=NodeExecutionContext(
|
2023-04-18 13:06:17 -04:00
|
|
|
environment=dict(environment),
|
|
|
|
|
input=value,
|
|
|
|
|
executor=executor or local_environment_executor,
|
2021-04-30 17:56:34 -04:00
|
|
|
),
|
2017-09-02 22:09:32 +01:00
|
|
|
)
|
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:
|
2021-05-03 11:45:43 -04:00
|
|
|
if node.kind == "word":
|
2020-04-08 00:51:04 +02:00
|
|
|
return evaluate_word_node(node, context=context)
|
2021-05-03 11:45:43 -04:00
|
|
|
elif node.kind == "commandsubstitution":
|
2020-06-28 21:50:26 +01:00
|
|
|
node_result = evaluate_command_node(node.command, context=context)
|
|
|
|
|
# bash removes training newlines in command substitution
|
|
|
|
|
return node_result.rstrip()
|
2021-05-03 11:45:43 -04: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:
|
2022-09-05 13:11:46 -04:00
|
|
|
msg = f"Unsupported bash construct: {node.kind!r}"
|
|
|
|
|
raise ValueError(msg)
|
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:
|
2021-01-02 14:32:55 -05:00
|
|
|
value: str = node.word
|
2017-09-01 13:24:12 +01:00
|
|
|
|
|
|
|
|
for part in node.parts:
|
2021-04-30 17:56:34 -04:00
|
|
|
part_string = context.input[part.pos[0] : part.pos[1]]
|
2020-07-15 21:07:33 +01:00
|
|
|
part_value = evaluate_node(part, context=context)
|
2017-09-01 13:24:12 +01:00
|
|
|
|
2020-07-15 21:07:33 +01:00
|
|
|
if part_string not in value:
|
2022-09-05 13:11:46 -04:00
|
|
|
msg = f"bash parse failed. part {part_string!r} not found in {value!r}. Word was {node.word!r}. Full input was {context.input!r}"
|
|
|
|
|
raise RuntimeError(msg)
|
2017-09-01 13:24:12 +01:00
|
|
|
|
2020-07-15 21:07:33 +01:00
|
|
|
value = value.replace(part_string, part_value, 1)
|
2017-09-02 22:09:32 +01:00
|
|
|
|
2020-07-15 21:07:33 +01:00
|
|
|
return 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:
|
2021-05-03 11:45:43 -04:00
|
|
|
if any(n.kind == "operator" for n in node.parts):
|
2020-06-26 12:59:26 +01:00
|
|
|
return evaluate_nodes_as_compound_command(node.parts, context=context)
|
|
|
|
|
else:
|
|
|
|
|
return evaluate_nodes_as_simple_command(node.parts, context=context)
|
|
|
|
|
|
|
|
|
|
|
2021-04-30 17:56:34 -04:00
|
|
|
def evaluate_nodes_as_compound_command(
|
|
|
|
|
nodes: Sequence[bashlex.ast.node], context: NodeExecutionContext
|
|
|
|
|
) -> str:
|
2020-06-26 12:59:26 +01:00
|
|
|
# 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!
|
|
|
|
|
|
2021-05-03 11:45:43 -04:00
|
|
|
result = ""
|
2020-06-26 12:59:26 +01:00
|
|
|
for node in nodes:
|
2021-05-03 11:45:43 -04:00
|
|
|
if node.kind == "command":
|
2020-06-26 12:59:26 +01:00
|
|
|
result += evaluate_command_node(node, context=context)
|
2021-05-03 11:45:43 -04:00
|
|
|
elif node.kind == "operator":
|
|
|
|
|
if node.op != ";":
|
2022-09-05 13:11:46 -04:00
|
|
|
msg = f"Unsupported bash operator: {node.op!r}"
|
|
|
|
|
raise ValueError(msg)
|
2020-06-26 12:59:26 +01:00
|
|
|
else:
|
2022-09-05 13:11:46 -04:00
|
|
|
msg = f"Unsupported bash node in compound command: {node.kind!r}"
|
|
|
|
|
raise ValueError(msg)
|
2020-06-26 12:59:26 +01:00
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
2021-04-30 17:56:34 -04:00
|
|
|
def evaluate_nodes_as_simple_command(
|
2023-04-18 13:06:17 -04:00
|
|
|
nodes: Iterable[bashlex.ast.node], context: NodeExecutionContext
|
2021-04-30 17:56:34 -04:00
|
|
|
) -> str:
|
2020-07-19 17:37:25 +02:00
|
|
|
command = [evaluate_node(part, context=context) for part in nodes]
|
2020-06-23 19:46:23 +01:00
|
|
|
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:
|
2021-05-03 11:45:43 -04:00
|
|
|
return context.environment.get(node.value, "")
|