4
4
5
5
6
6
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
+
7
14
def setup (self ):
8
15
pass
9
16
10
17
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
+ """
11
31
raise NotImplementedError
12
32
13
33
def teardown (self ):
14
34
pass
15
35
16
36
17
37
class Native (Environment ):
38
+ """
39
+ Native environment, i.e. the tasks are executed in the current python environment.
40
+ """
41
+
18
42
def execute (self , task ):
19
- # breakpoint()
20
- # args = task.render_arguments_in_root()
21
43
keys = ["return_code" , "stdout" , "stderr" ]
22
44
values = execute (task .command_args (), strip = task .strip )
23
45
output = dict (zip (keys , values ))
@@ -31,7 +53,22 @@ def execute(self, task):
31
53
return output
32
54
33
55
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
+
35
72
def __init__ (self , image , tag = "latest" , output_cpath = "/output_pydra" , xargs = None ):
36
73
self .image = image
37
74
self .tag = tag
@@ -46,13 +83,13 @@ def bind(loc, mode="ro", root="/mnt/pydra"): # TODO
46
83
loc_abs = Path (loc ).absolute ()
47
84
return f"{ loc_abs } :{ root } { loc_abs } :{ mode } " # TODO: moving entire path?
48
85
86
+
87
+ class Docker (Container ):
88
+ """Docker environment."""
89
+
49
90
def execute (self , task , root = "/mnt/pydra" ):
50
- # XXX Need to mount all input locations
51
91
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
56
93
mounts = task .get_bindings (root = root )
57
94
58
95
# todo adding xargsy etc
@@ -76,20 +113,15 @@ def execute(self, task, root="/mnt/pydra"):
76
113
raise RuntimeError (output ["stderr" ])
77
114
else :
78
115
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
82
116
return output
83
117
84
118
85
- class Singularity (Docker ):
119
+ class Singularity (Container ):
120
+ """Singularity environment."""
121
+
86
122
def execute (self , task , root = "/mnt/pydra" ):
87
- # XXX Need to mount all input locations
88
123
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
93
125
mounts = task .get_bindings (root = root )
94
126
95
127
# todo adding xargsy etc
@@ -117,7 +149,4 @@ def execute(self, task, root="/mnt/pydra"):
117
149
raise RuntimeError (output ["stderr" ])
118
150
else :
119
151
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
123
152
return output
0 commit comments