Skip to content

Commit 8d60dd1

Browse files
committed
adding xarg to env command, changing output_cpath to root used in the bindings
1 parent 161635b commit 8d60dd1

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

pydra/engine/environments.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,46 +65,53 @@ class Container(Environment):
6565
Tag of the container image
6666
output_cpath : str
6767
Path to the output directory in the container
68-
xargs : dict
68+
xargs : Union[str, List[str]]
6969
Extra arguments to be passed to the container
7070
"""
7171

72-
def __init__(self, image, tag="latest", output_cpath="/output_pydra", xargs=None):
72+
def __init__(self, image, tag="latest", root="/mnt/pydra", xargs=None):
7373
self.image = image
7474
self.tag = tag
75+
if xargs is None:
76+
xargs = []
77+
elif isinstance(xargs, str):
78+
xargs = xargs.split()
7579
self.xargs = xargs
76-
self.output_cpath = output_cpath
80+
self.root = root
7781

7882
@staticmethod
79-
def bind(loc, mode="ro", root="/mnt/pydra"): # TODO
80-
# XXX Failure mode: {loc} overwrites a critical directory in image
81-
# To fix, we'll need to update any args within loc to a new location
82-
# such as /mnt/pydra/loc
83+
def bind(loc, mode="ro", root="/mnt/pydra"):
8384
loc_abs = Path(loc).absolute()
84-
return f"{loc_abs}:{root}{loc_abs}:{mode}" # TODO: moving entire path?
85+
return f"{loc_abs}:{root}{loc_abs}:{mode}"
8586

8687

8788
class Docker(Container):
8889
"""Docker environment."""
8990

90-
def execute(self, task, root="/mnt/pydra"):
91+
def execute(self, task):
9192
docker_img = f"{self.image}:{self.tag}"
9293
# mounting all input locations
93-
mounts = task.get_bindings(root=root)
94+
mounts = task.get_bindings(root=self.root)
9495

9596
# todo adding xargsy etc
96-
docker_args = ["docker", "run", "-v", self.bind(task.cache_dir, "rw")]
97+
docker_args = [
98+
"docker",
99+
"run",
100+
"-v",
101+
self.bind(task.cache_dir, "rw", self.root),
102+
]
103+
docker_args.extend(self.xargs)
97104
docker_args.extend(
98105
" ".join(
99106
[f"-v {key}:{val[0]}:{val[1]}" for (key, val) in mounts.items()]
100107
).split()
101108
)
102-
docker_args.extend(["-w", f"{root}{task.output_dir}"])
109+
docker_args.extend(["-w", f"{self.root}{task.output_dir}"])
103110
keys = ["return_code", "stdout", "stderr"]
104111
# print("\n Docker args", docker_args)
105112

106113
values = execute(
107-
docker_args + [docker_img] + task.command_args(root="/mnt/pydra"),
114+
docker_args + [docker_img] + task.command_args(root=self.root),
108115
strip=task.strip,
109116
)
110117
output = dict(zip(keys, values))
@@ -119,28 +126,29 @@ def execute(self, task, root="/mnt/pydra"):
119126
class Singularity(Container):
120127
"""Singularity environment."""
121128

122-
def execute(self, task, root="/mnt/pydra"):
129+
def execute(self, task):
123130
singularity_img = f"{self.image}:{self.tag}"
124131
# mounting all input locations
125-
mounts = task.get_bindings(root=root)
132+
mounts = task.get_bindings(root=self.root)
126133

127134
# todo adding xargsy etc
128135
singularity_args = [
129136
"singularity",
130137
"exec",
131138
"-B",
132-
self.bind(task.cache_dir, "rw"),
139+
self.bind(task.cache_dir, "rw", self.root),
133140
]
141+
singularity_args.extend(self.xargs)
134142
singularity_args.extend(
135143
" ".join(
136144
[f"-B {key}:{val[0]}:{val[1]}" for (key, val) in mounts.items()]
137145
).split()
138146
)
139-
singularity_args.extend(["--pwd", f"{root}{task.output_dir}"])
147+
singularity_args.extend(["--pwd", f"{self.root}{task.output_dir}"])
140148
keys = ["return_code", "stdout", "stderr"]
141149

142150
values = execute(
143-
singularity_args + [singularity_img] + task.command_args(root="/mnt/pydra"),
151+
singularity_args + [singularity_img] + task.command_args(root=self.root),
144152
strip=task.strip,
145153
)
146154
output = dict(zip(keys, values))

pydra/engine/tests/test_environments.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .utils import no_win, need_docker, need_singularity
1212

1313
import attr
14+
import pytest
1415

1516

1617
def makedir(path, name):
@@ -79,7 +80,15 @@ def test_docker_1(tmp_path):
7980

8081
@no_win
8182
@need_docker
82-
def test_docker_1_subm(tmp_path):
83+
@pytest.mark.parametrize(
84+
"docker",
85+
[
86+
Docker(image="busybox"),
87+
Docker(image="busybox", tag="latest", xargs="--rm"),
88+
Docker(image="busybox", xargs=["--rm"]),
89+
],
90+
)
91+
def test_docker_1_subm(tmp_path, docker):
8392
"""docker env with submitter: simple command, no arguments"""
8493
newcache = lambda x: makedir(tmp_path, x)
8594

0 commit comments

Comments
 (0)