Skip to content
Open
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
39 changes: 15 additions & 24 deletions fast_dp/run_job.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,37 @@
from __future__ import annotations

import os
import shutil
import subprocess


def run_job(executable, arguments=[], stdin=[], working_directory=None):
def run_job(
executable: str,
arguments: list[str] = [],
stdin=[],
working_directory: str | None = None,
) -> list[str]:
"""Run a program with some command-line arguments and some input,
then return the standard output when it is finished.
"""
if working_directory is None:
working_directory = os.getcwd()

command_line = "%s" % executable
for arg in arguments:
command_line += ' "%s"' % arg
command_line = [shutil.which(executable)] + arguments
if command_line[0] is None:
raise RuntimeError(f"Cannot find executable {executable!r}")

popen = subprocess.Popen(
proc = subprocess.run(
command_line,
bufsize=1,
stdin=subprocess.PIPE,
input="\n".join(stdin),
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=working_directory,
universal_newlines=True,
shell=True,
check=True,
)

for record in stdin:
popen.stdin.write("%s\n" % record)

popen.stdin.close()

output = []

while True:
record = popen.stdout.readline()
if not record:
break

output.append(record)

return output
return proc.stdout.splitlines()


if __name__ == "__main__":
Expand Down
Loading