Skip to content

Commit 2dd5603

Browse files
committed
creating Container class
1 parent 979bb60 commit 2dd5603

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

pydra/engine/environments.py

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,42 @@
44

55

66
class Environment:
7+
"""
8+
Base class for environments that are used to execute tasks.
9+
Right now it is asssumed that the environment, including container images,
10+
are available and are not removed at the end
11+
TODO: add setup and teardown methods
12+
"""
13+
714
def setup(self):
815
pass
916

1017
def execute(self, task):
18+
"""
19+
Execute the task in the environment.
20+
21+
Parameters
22+
----------
23+
task : TaskBase
24+
the task to execute
25+
26+
Returns
27+
-------
28+
output
29+
Output of the task.
30+
"""
1131
raise NotImplementedError
1232

1333
def teardown(self):
1434
pass
1535

1636

1737
class Native(Environment):
38+
"""
39+
Native environment, i.e. the tasks are executed in the current python environment.
40+
"""
41+
1842
def execute(self, task):
19-
# breakpoint()
20-
# args = task.render_arguments_in_root()
2143
keys = ["return_code", "stdout", "stderr"]
2244
values = execute(task.command_args(), strip=task.strip)
2345
output = dict(zip(keys, values))
@@ -31,7 +53,22 @@ def execute(self, task):
3153
return output
3254

3355

34-
class Docker(Environment):
56+
class Container(Environment):
57+
"""
58+
Base class for container environments used by Docker and Singularity.
59+
60+
Parameters
61+
----------
62+
image : str
63+
Name of the container image
64+
tag : str
65+
Tag of the container image
66+
output_cpath : str
67+
Path to the output directory in the container
68+
xargs : dict
69+
Extra arguments to be passed to the container
70+
"""
71+
3572
def __init__(self, image, tag="latest", output_cpath="/output_pydra", xargs=None):
3673
self.image = image
3774
self.tag = tag
@@ -46,13 +83,13 @@ def bind(loc, mode="ro", root="/mnt/pydra"): # TODO
4683
loc_abs = Path(loc).absolute()
4784
return f"{loc_abs}:{root}{loc_abs}:{mode}" # TODO: moving entire path?
4885

86+
87+
class Docker(Container):
88+
"""Docker environment."""
89+
4990
def execute(self, task, root="/mnt/pydra"):
50-
# XXX Need to mount all input locations
5191
docker_img = f"{self.image}:{self.tag}"
52-
# TODO ?
53-
# Skips over any inputs in task.cache_dir
54-
# Needs to include `out_file`s when not relative to working dir
55-
# Possibly a `TargetFile` type to distinguish between `File` and `str`?
92+
# mounting all input locations
5693
mounts = task.get_bindings(root=root)
5794

5895
# todo adding xargsy etc
@@ -76,20 +113,15 @@ def execute(self, task, root="/mnt/pydra"):
76113
raise RuntimeError(output["stderr"])
77114
else:
78115
raise RuntimeError(output["stdout"])
79-
# Any outputs that have been created with a re-rooted path need
80-
# to be de-rooted
81-
# task.finalize_outputs("/mnt/pydra") TODO: probably don't need it
82116
return output
83117

84118

85-
class Singularity(Docker):
119+
class Singularity(Container):
120+
"""Singularity environment."""
121+
86122
def execute(self, task, root="/mnt/pydra"):
87-
# XXX Need to mount all input locations
88123
singularity_img = f"{self.image}:{self.tag}"
89-
# TODO ?
90-
# Skips over any inputs in task.cache_dir
91-
# Needs to include `out_file`s when not relative to working dir
92-
# Possibly a `TargetFile` type to distinguish between `File` and `str`?
124+
# mounting all input locations
93125
mounts = task.get_bindings(root=root)
94126

95127
# todo adding xargsy etc
@@ -117,7 +149,4 @@ def execute(self, task, root="/mnt/pydra"):
117149
raise RuntimeError(output["stderr"])
118150
else:
119151
raise RuntimeError(output["stdout"])
120-
# Any outputs that have been created with a re-rooted path need
121-
# to be de-rooted
122-
# task.finalize_outputs("/mnt/pydra") TODO: probably don't need it
123152
return output

0 commit comments

Comments
 (0)