From c85ac13b0af00b0ad3c475e7044b413d82959ad5 Mon Sep 17 00:00:00 2001 From: mryouse Date: Fri, 13 May 2022 04:42:09 +0000 Subject: update shell commands to return list --- std.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/std.py b/std.py index 30d81c9..1786aa5 100644 --- a/std.py +++ b/std.py @@ -125,8 +125,9 @@ def std_literal_to_string(arg): def std_shell(arg): assert isinstance(arg, NebString) lst = arg.value.split(" ") - subprocess.run(lst) - return NebBool(True) + ret = subprocess.run(lst, capture_output=True) + stdout_as_list = ret.stdout.decode("utf-8").split("\n")[:-1] + return NebList([ret.returncode] + stdout_as_list) def std_shell_pipe(args): prev = None @@ -135,9 +136,22 @@ def std_shell_pipe(args): if idx == len(args.items) - 1: stdout = None lst = arg.value.split(" ") - ret = subprocess.run(lst, stdout=stdout, input=prev) - prev = ret.stdout - return NebBool(True) + if prev is None: + if stdout is None: + ret = subprocess.run(lst, capture_output=True) + else: + ret = subprocess.run(lst, stdout=stdout) + else: + if stdout is None: + ret = subprocess.run(lst, input=prev.stdout, capture_output=True) + else: + ret = subprocess.run(lst, stdout=stdout, input=prev.stdout) + + # TODO should we bail here if the return code is non-zero? + prev = ret + + stdout_as_list = prev.stdout.decode("utf-8").split("\n")[:-1] + return NebList([prev.returncode] + stdout_as_list) def evaluate_expression(expr): if not expr.symbol.name in STD: @@ -239,9 +253,9 @@ def build_std(): STD["->string"] = [to_string] # shell - shell_string = FuncImpl(NebFunction("$", [NebString], NebBool), std_shell) + shell_string = FuncImpl(NebFunction("$", [NebString], NebList), std_shell) STD["$"] = [shell_string] - shell_pipe = FuncImpl(NebFunction("$|", [NebList], NebBool), std_shell_pipe) + shell_pipe = FuncImpl(NebFunction("$|", [NebList], NebList), std_shell_pipe) STD["$|"] = [shell_pipe] build_std() -- cgit v1.2.3