-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtask.py
More file actions
286 lines (239 loc) · 10.3 KB
/
task.py
File metadata and controls
286 lines (239 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from typing import List, Optional
from .common import Input, Output
from .util import WdlBase, KvClass, Meta, ParameterMeta
class Task(WdlBase):
"""
A task definition is a way of encapsulating a UNIX command and environment
and presenting them as functions. Tasks have both inputs and outputs.
Inputs are declared as declarations at the top of the task definition,
while outputs are defined in the output section.
The user must provide a value for these two parameters in order for this task to be runnable.
A task is a declarative construct with a focus on constructing a command from a template.
The command specification is interpreted in an engine specific way, though a typical case
is that a command is a UNIX command line which would be run in a Docker image.
Tasks also define their outputs, which is essential for building dependencies between tasks.
Any other data specified in the task definition (e.g. runtime information and meta-data) is optional.
Documentation: https://github.com/openwdl/get_string/blob/master/versions/draft-2/SPEC.md#task-definition
"""
class Runtime(KvClass):
def add_docker(self, docker):
self.kwargs["docker"] = f'"{docker}"'
def add_cpus(self, cpus):
self.kwargs["cpu"] = cpus
def add_memory(self, memory_gb):
self.kwargs["memory"] = f'"{memory_gb}G"'
def add_gcp_disk(self, disk_size_gb):
self.kwargs["disks"] = f'"local-disk {disk_size_gb} SSD"'
def add_gcp_boot_disk(self, disk_size_gb: int):
self.kwargs["bootDiskSizeGb"] = int(disk_size_gb)
class Command(WdlBase):
"""
Past the regular attributes, I've built the command generation here, because that's where
it logically is. This logic is pretty similar to CWL (and Rabix Composer's visualisation).
Declare a base command, arguments, inputs with positions and prefixes and we can manually assemble the command.
For this reason, there's a bit of double handling, if you add an input to a Task,
you also need to add it here, and that's just unfortunate.
======
Just want a plain and simple command, no worries just don't add inputs or anything else.
"""
class CommandArgument(WdlBase):
def __init__(
self,
prefix: str = None,
value: str = None,
position: int = None,
separate_value_from_prefix: bool = True,
):
self.prefix: Optional[str] = prefix
self.position: Optional[int] = position
self.value = value
self.separate = separate_value_from_prefix
def get_string(self):
pre = self.prefix if self.prefix else ""
sp = " " if self.separate else ""
val = self.value if self.value else ""
return (pre + sp + val).strip()
class CommandInput(CommandArgument):
def __init__(
self,
name: str,
optional: bool = False,
prefix: str = None,
position: int = None,
separate_value_from_prefix: bool = True,
default=None,
separator=None,
true=None,
false=None,
separate_arrays=None,
):
super().__init__(
prefix=prefix,
value=None,
position=position,
separate_value_from_prefix=separate_value_from_prefix,
)
self.name = name
self.optional = optional
self.default = default
self.separator = separator
self.true = true
self.false = false
self.separate_arrays = separate_arrays
@staticmethod
def from_input(inp: Input, prefix: str = None, position: int = None):
return Task.Command.CommandInput(
inp.name, inp.type.optional, prefix, position
)
def get_string(self):
name, array_sep, default, true, false = (
self.name,
self.separator,
self.default,
self.true,
self.false,
)
prefix = self.prefix if self.prefix else ""
bc = prefix + (" " if self.separate and self.prefix else "")
if self.separate_arrays:
if array_sep or default or true or false:
print(
"separate_array take preferences over: separator, default, true, false"
)
if self.optional:
# Ugly optional workaround: https://github.com/openwdl/wdl/issues/25#issuecomment-315424063
# Additional workaround for 'length(select_first({name}, [])' as length requires a non-optional array
internal_pref = f'if defined({name}) && length(select_first([{name}, []])) > 0 then "{bc}" else ""'
return f'~{{{internal_pref}}}~{{sep=" {bc}" {name}}}'
return f'~{{sep=" " prefix("{bc}", {name})}}'
if array_sep and self.optional:
# optional array with separator
# ifdefname = f'(if defined({name}) then {name} else [])'
return f'~{{true="{bc}" false="" defined({name})}}~{{sep="{array_sep}" {name}}}'
options = []
if default:
val = default
if isinstance(default, str):
val = f'"{default}"'
if isinstance(default, bool):
val = "true" if default else "false"
options.append(f"default={val}")
if array_sep:
options.append(f'sep="{array_sep}"')
is_flag = true or false
if is_flag:
options.append(f'true="{true if true else ""}"')
options.append(f'false="{false if false else ""}"')
stroptions = "".join(o + " " for o in options)
if self.optional and not default and not is_flag and bc.strip():
# Option 1: We apply quotes are value, Option 2: We quote whole "prefix + name" combo
if self.separate and self.prefix:
return f'~{{\'"{prefix}" "\' + {name} + \'"\'}}'
else:
return f'~{{\'"{prefix}\' + {name} + \'"\'}}'
else:
return bc + f"~{{{stroptions}{name}}}"
def __init__(
self,
command,
inputs: Optional[List[CommandInput]] = None,
arguments: Optional[List[CommandArgument]] = None,
):
self.command = command
self.inputs = inputs if inputs else []
self.arguments = arguments if arguments else []
def get_string(self, indent: int = 0):
tb = " "
base_command = self.command if self.command else ""
if not (self.inputs or self.arguments):
return indent * tb + base_command
# build up command
args = sorted(
[*self.inputs, *self.arguments],
key=lambda a: a.position if a.position else 0,
)
command: str = base_command if isinstance(base_command, str) else " ".join(
base_command
)
tbed_arg_indent = tb * (indent + 1)
return (
indent * tb
+ command
+ "".join([" \\\n" + tbed_arg_indent + a.get_string() for a in args])
)
def __init__(
self,
name: str,
inputs: List[Input] = None,
outputs: List[Output] = None,
command: Command = None,
runtime: Runtime = None,
version="draft-2",
meta: Meta = None,
parameter_meta: ParameterMeta = None,
):
self.name = name
self.inputs = inputs if inputs else []
self.outputs = outputs if outputs else []
self.command = command
self.runtime = runtime
self.version = version
self.meta = meta
self.param_meta = parameter_meta
self.format = """
version {version}
task {name} {{
{blocks}
}}
""".strip()
def get_string(self):
tb = " "
name = self.name
blocks = []
if self.inputs:
blocks.append(
f"{tb}input {{\n"
+ "\n".join(2 * tb + i.get_string() for i in self.inputs)
+ f"\n{tb}}}"
)
if self.command:
if isinstance(self.command, list):
com = "\n".join(c.get_string(indent=2) for c in self.command)
else:
com = self.command.get_string(indent=2)
blocks.append("{tb}command <<<\n{args}\n{tb}>>>".format(tb=tb, args=com))
if self.runtime:
rt = self.runtime.get_string(indent=2)
blocks.append(
"{tb}runtime {{\n{args}\n{tb}}}".format(
tb=tb,
args=rt,
)
)
if self.meta:
mt = self.meta.get_string(indent=2)
if mt:
blocks.append(
"{tb}meta {{\n{args}\n{tb}}}".format(
tb=tb, args=mt
)
)
if self.param_meta:
pmt = self.param_meta.get_string(indent=2)
if pmt:
blocks.append(
"{tb}parameter_meta {{\n{args}\n{tb}}}".format(
tb=tb,
args=pmt
)
)
if self.outputs:
blocks.append(
"{tb}output {{\n{outs}\n{tb}}}".format(
tb=tb,
outs="\n".join((2 * tb) + o.get_string() for o in self.outputs),
)
)
return self.format.format(
name=name, blocks="\n".join(blocks), version=self.version
)