Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions python/bart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tempfile as tmp
import os
import sys
import threading

if __spec__.parent:
from . import cfl
Expand Down Expand Up @@ -171,37 +172,38 @@ def bart_postprocess(nargout, ERR, infiles, infiles_kw, outfiles):
def execute_cmd(cmd):
"""
Execute a command in a shell.
Print and catch the output.
Print and catch the output (stdout + stderr) in real time.
"""

errcode = 0
stdout = ""
stderr = ""

# remove empty strings from cmd
cmd = [item for item in cmd if len(item)]

# execute cmd
proc = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE, universal_newlines=True)

# print to stdout
for stdout_line in iter(proc.stdout.readline, ""):
stdout += stdout_line
print(stdout_line, end="")
proc.stdout.close()
def read_stream(stream, buffer):
for line in iter(stream.readline, ""):
buffer.append(line)
print(line, end="")
stream.close()

# in case of error, print to stderr
errcode = proc.wait()
if errcode:
stderr = "".join(proc.stderr.readlines())
print(stderr)
proc.stderr.close()
stdout_lines, stderr_lines = [], []

return errcode, stdout, stderr
t_out = threading.Thread(target=read_stream, args=(proc.stdout, stdout_lines))
t_err = threading.Thread(target=read_stream, args=(proc.stderr, stderr_lines))

t_out.start()
t_err.start()

t_out.join()
t_err.join()

errcode = proc.wait()
return errcode, "".join(stdout_lines), "".join(stderr_lines)

wasm_bart_ok = False;


wasm_bart_ok = False

async def get_wasm_cfl(name):
await wasm_async_call(f"get_cfl('{name}')")
Expand All @@ -218,7 +220,7 @@ async def wasm_load_bart():
wasm_bart_ok = True

async def run_wasm_cmd(shell_cmd, infiles, infiles_kw, outfiles):
global wasm_bart_ok;
global wasm_bart_ok
try:
if not wasm_bart_ok:
await wasm_load_bart()
Expand Down Expand Up @@ -265,7 +267,7 @@ async def wasm_async_call(cmd):
if 0 != ret[0]:
raise Exception(f"Error in JS call: {ret[1]}")

return ret[1];
return ret[1]


if isWASM:
Expand Down