Skip to content

Commit

Permalink
[one-cmds] Introduce run_ret method in utils
Browse files Browse the repository at this point in the history
This will introduce run_ret method in utils which will return with
process return code.

ONE-DCO-1.0-Signed-off-by: SaeHie Park <[email protected]>
  • Loading branch information
seanshpark committed Sep 2, 2024
1 parent 6e9485f commit 1c59719
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions compiler/one-cmds/onelib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,48 @@ def safemain(main, mainpath):
sys.exit(255)


def run_ret(cmd, *, one_cmd: str = None, err_prefix=None, logfile=None):
"""Execute command in subprocess
Args:
one_cmd: subtool name to execute with given `cmd`
cmd: command to be executed in subprocess
err_prefix: prefix to be put before every stderr lines
logfile: file stream to which both of stdout and stderr lines will be written
Return:
Process execution return code; 0 if success and others for error.
"""
if one_cmd:
assert one_cmd in one_cmd_list(), f'Invalid ONE COMMAND: {one_cmd}'
dir_path = os.path.dirname(os.path.dirname(
os.path.realpath(__file__))) # bin = onelib/../
driver_path = os.path.join(dir_path, f'one-{one_cmd}')
cmd = [driver_path] + cmd

with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
import select
inputs = set([p.stdout, p.stderr])
while inputs:
readable, _, _ = select.select(inputs, [], [])
for x in readable:
line = x.readline()
if len(line) == 0:
inputs.discard(x)
continue
if x == p.stdout:
out = sys.stdout
if x == p.stderr:
out = sys.stderr
if err_prefix:
line = f"{err_prefix}: ".encode() + line
out.buffer.write(line)
out.buffer.flush()
if logfile != None:
logfile.write(line)
return p.returncode


# TODO make run call run_ret
def run(cmd, *, one_cmd: str = None, err_prefix=None, logfile=None):
"""Execute command in subprocess
Expand Down

0 comments on commit 1c59719

Please sign in to comment.