Skip to content

Commit ec87910

Browse files
committed
rolled "base.execute" into "read_and_display" including error common handling performed in Environment.execute
1 parent 058d793 commit ec87910

File tree

4 files changed

+36
-69
lines changed

4 files changed

+36
-69
lines changed

pydra/environments/base.py

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -200,52 +200,46 @@ def map_path(fileset: os.PathLike | FileSet) -> Path:
200200
return bindings, values
201201

202202

203-
def execute(cmd, strip=False):
204-
"""
205-
Run the event loop with coroutine.
206-
207-
Uses :func:`read_and_display_async` unless a loop is
208-
already running, in which case :func:`read_and_display`
209-
is used.
203+
def read_and_display(
204+
*cmd: str, strip: bool = False, hide_display: bool = False
205+
) -> dict[str, int | str]:
206+
"""Capture a process' standard output.
210207
211208
Parameters
212209
----------
213-
cmd : :obj:`list` or :obj:`tuple`
214-
The command line to be executed.
215-
strip : :obj:`bool`
216-
TODO
217-
218-
"""
219-
rc, stdout, stderr = read_and_display(*cmd, strip=strip)
220-
"""
221-
loop = get_open_loop()
222-
if loop.is_running():
223-
rc, stdout, stderr = read_and_display(*cmd, strip=strip)
224-
else:
225-
rc, stdout, stderr = loop.run_until_complete(
226-
read_and_display_async(*cmd, strip=strip)
227-
)
210+
cmd : str
211+
The command to execute, as a list of strings.
212+
strip : bool, optional
213+
If True, the output will be stripped of leading and trailing whitespace.
214+
hide_display : bool, optional
215+
If True, the output will not be displayed.
216+
217+
Returns
218+
-------
219+
dict[str, Any]
220+
A dictionary containing the return code, standard output, and standard error.
221+
222+
Raises
223+
------
224+
RuntimeError
225+
If the return code is not 0, a RuntimeError is raised with a formatted
226+
error message.
228227
"""
229-
return rc, stdout, stderr
230-
231-
232-
def read_and_display(*cmd, strip=False, hide_display=False):
233-
"""Capture a process' standard output."""
234228
try:
235229
process = sp.run(cmd, stdout=sp.PIPE, stderr=sp.PIPE)
236230
except Exception:
237231
# TODO editing some tracing?
238232
raise
239233

234+
stdout = process.stdout.decode("utf-8")
240235
if strip:
241-
return (
242-
process.returncode,
243-
process.stdout.decode("utf-8").strip(),
244-
process.stderr.decode("utf-8"),
245-
)
246-
else:
247-
return (
248-
process.returncode,
249-
process.stdout.decode("utf-8"),
250-
process.stderr.decode("utf-8"),
251-
)
236+
stdout = stdout.strip()
237+
stderr = process.stderr.decode("utf-8")
238+
if process.returncode:
239+
msg = f"Error executing command {cmd} (code: {process.returncode}):"
240+
if stdout:
241+
msg += "\n\nstderr:\n" + stderr
242+
if stdout:
243+
msg += "\n\nstdout:\n" + stdout
244+
raise RuntimeError(msg)
245+
return {"return_code": process.returncode, "stdout": stdout, "stderr": stderr}

pydra/environments/docker.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,11 @@ def execute(self, job: "Job[shell.Task]") -> dict[str, ty.Any]:
2828
).split()
2929
)
3030
docker_args.extend(["-w", f"{self.root}{job.cache_dir}"])
31-
keys = ["return_code", "stdout", "stderr"]
3231

3332
job.cache_dir.mkdir(exist_ok=True)
34-
values = base.execute(
35-
docker_args + [docker_img] + job.task._command_args(values=values),
33+
return base.read_and_display(
34+
*(docker_args + [docker_img] + job.task._command_args(values=values)),
3635
)
37-
output = dict(zip(keys, values))
38-
if output["return_code"]:
39-
if output["stderr"]:
40-
raise RuntimeError(output["stderr"])
41-
else:
42-
raise RuntimeError(output["stdout"])
43-
return output
4436

4537

4638
# Alias so it can be referred to as docker.Environment

pydra/environments/native.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,8 @@ class Native(base.Environment):
1515
"""
1616

1717
def execute(self, job: "Job[shell.Task]") -> dict[str, ty.Any]:
18-
keys = ["return_code", "stdout", "stderr"]
1918
cmd_args = job.task._command_args(values=job.inputs)
20-
values = base.execute(cmd_args)
21-
output = dict(zip(keys, values))
22-
if output["return_code"]:
23-
msg = f"Error running '{job.name}' job with {cmd_args}:"
24-
if output["stderr"]:
25-
msg += "\n\nstderr:\n" + output["stderr"]
26-
if output["stdout"]:
27-
msg += "\n\nstdout:\n" + output["stdout"]
28-
raise RuntimeError(msg)
29-
return output
19+
return base.read_and_display(cmd_args)
3020

3121

3222
# Alias so it can be referred to as native.Environment

pydra/environments/singularity.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,12 @@ def execute(self, job: "Job[shell.Task]") -> dict[str, ty.Any]:
3131
singularity_args.extend(
3232
["--pwd", f"{self.root.rstrip('/')}{job.cache_dir.absolute()}"]
3333
)
34-
keys = ["return_code", "stdout", "stderr"]
35-
3634
job.cache_dir.mkdir(exist_ok=True)
37-
values = base.execute(
35+
return base.read_and_display(
3836
singularity_args
3937
+ [singularity_img]
4038
+ job.task._command_args(values=values),
4139
)
42-
output = dict(zip(keys, values))
43-
if output["return_code"]:
44-
if output["stderr"]:
45-
raise RuntimeError(output["stderr"])
46-
else:
47-
raise RuntimeError(output["stdout"])
48-
return output
4940

5041

5142
# Alias so it can be referred to as singularity.Environment

0 commit comments

Comments
 (0)