-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhelper.py
349 lines (283 loc) · 10.5 KB
/
helper.py
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Copyright 2023, Seqera
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This file contains helper functions for the library.
Including handling methods for each block in the YAML file, and parsing
methods for each block in the YAML file.
"""
import yaml
from seqerakit import utils
def parse_yaml_block(yaml_data, block_name):
# Get the name of the specified block/resource.
block = yaml_data.get(block_name)
# If block is not found in the YAML, return an empty list.
if not block:
return block_name, []
# Initialize an empty list to hold the lists of command line arguments.
cmd_args_list = []
# Initialize a set to track the --name values within the block.
name_values = set()
# Iterate over each item in the block.
for item in block:
cmd_args = parse_block(block_name, item)
name = find_name(cmd_args)
if name in name_values:
raise ValueError(
f" Duplicate 'name' specified in config file"
f" for {block_name}: {name}. Please specify a unique name."
)
name_values.add(name)
cmd_args_list.append(cmd_args)
# Return the block name and list of command line argument lists.
return block_name, cmd_args_list
def parse_all_yaml(file_paths, destroy=False):
# If multiple yamls, merge them into one dictionary
merged_data = {}
for file_path in file_paths:
with open(file_path, "r") as f:
data = yaml.safe_load(f)
# Check if the YAML file is empty or contains no valid data
if data is None or not data:
raise ValueError(
f" The file '{file_path}' is empty or does not contain valid data."
)
for key, value in data.items():
if key in merged_data:
try:
merged_data[key].extend(value)
except AttributeError:
merged_data[key] = [merged_data[key], value]
else:
merged_data[key] = value
block_names = list(merged_data.keys())
# Define the order in which the resources should be created.
resource_order = [
"organizations",
"teams",
"workspaces",
"participants",
"credentials",
"compute-envs",
"secrets",
"actions",
"datasets",
"pipelines",
"launch",
]
# Reverse the order of resources if destroy is True
if destroy:
resource_order = resource_order[:-1][::-1]
# Initialize an empty dictionary to hold all the command arguments.
cmd_args_dict = {}
# Iterate over each block name in the desired order.
for block_name in resource_order:
if block_name in block_names:
# Parse the block and add its command line arguments to the dictionary.
block_name, cmd_args_list = parse_yaml_block(merged_data, block_name)
cmd_args_dict[block_name] = cmd_args_list
# Return the dictionary of command arguments.
return cmd_args_dict
def parse_block(block_name, item):
# Define the mapping from block names to functions.
block_to_function = {
"credentials": parse_type_block,
"compute-envs": parse_type_block,
"actions": parse_type_block,
"teams": parse_teams_block,
"datasets": parse_datasets_block,
"pipelines": parse_pipelines_block,
"launch": parse_launch_block,
}
# Use the generic block function as a default.
parse_fn = block_to_function.get(block_name, parse_generic_block)
overwrite = item.pop("overwrite", False)
# Call the appropriate function and return its result along with overwrite value.
cmd_args = parse_fn(item)
return {"cmd_args": cmd_args, "overwrite": overwrite}
# Parsers for certain blocks of yaml that require handling
# for structuring command line arguments in a certain way
def parse_generic_block(item):
cmd_args = []
for key, value in item.items():
cmd_args.extend([f"--{key}", str(value)])
return cmd_args
def parse_type_block(item, priority_keys=["type", "config-mode", "file-path"]):
cmd_args = []
# Ensure at least one of 'type' or 'file-path' is present
if not any(key in item for key in ["type", "file-path"]):
raise ValueError(
"Please specify at least 'type' or 'file-path' for creating the resource."
)
# Process priority keys first
for key in priority_keys:
if key in item:
cmd_args.append(str(item[key]))
del item[key] # Remove the key to avoid repeating in args
for key, value in item.items():
if isinstance(value, bool):
if value:
cmd_args.append(f"--{key}")
elif key == "params":
temp_file_name = utils.create_temp_yaml(value)
cmd_args.extend(["--params-file", temp_file_name])
else:
cmd_args.extend([f"--{key}", str(value)])
return cmd_args
def parse_teams_block(item):
# Keys for each list
cmd_keys = ["name", "organization", "description"]
members_keys = ["name", "organization", "members"]
cmd_args = []
members_cmd_args = []
for key, value in item.items():
if key in cmd_keys:
cmd_args.extend([f"--{key}", str(value)])
elif key in members_keys and key == "members":
for member in value:
members_cmd_args.append(
[
"--team",
str(item["name"]),
"--organization",
str(item["organization"]),
"add",
"--member",
str(member),
]
)
return (cmd_args, members_cmd_args)
def parse_datasets_block(item):
cmd_args = []
for key, value in item.items():
if key == "file-path":
cmd_args.extend(
[
str(item["file-path"]),
"--name",
str(item["name"]),
"--workspace",
str(item["workspace"]),
"--description",
str(item["description"]),
]
)
if key == "header" and value is True:
cmd_args.append("--header")
return cmd_args
def parse_pipelines_block(item):
cmd_args = []
repo_args = []
params_args = []
params_file_path = None
for key, value in item.items():
if key == "url":
repo_args.extend([str(value)])
elif key == "params":
params_dict = value
elif key == "params-file":
params_file_path = str(value)
elif key == "file-path":
repo_args.extend([str(value)])
elif isinstance(value, bool):
if value:
cmd_args.append(f"--{key}")
else:
cmd_args.extend([f"--{key}", str(value)])
# Create the temporary YAML file after processing all items
if "params" in item:
temp_file_name = utils.create_temp_yaml(
params_dict, params_file=params_file_path
)
params_args.extend(["--params-file", temp_file_name])
if params_file_path and "params" not in item:
params_args.extend(["--params-file", params_file_path])
combined_args = cmd_args + repo_args + params_args
return combined_args
def parse_launch_block(item):
repo_args = []
cmd_args = []
params_args = []
params_file_path = None
for key, value in item.items():
if key == "pipeline" or key == "url":
repo_args.extend([str(value)])
elif key == "params":
params_dict = value
elif key == "params-file":
params_file_path = str(value)
elif isinstance(value, bool):
if value:
cmd_args.append(f"--{key}")
else:
cmd_args.extend([f"--{key}", str(value)])
if "params" in item:
temp_file_name = utils.create_temp_yaml(
params_dict, params_file=params_file_path
)
params_args.extend(["--params-file", temp_file_name])
if params_file_path and "params" not in item:
params_args.extend(["--params-file", params_file_path])
cmd_args = cmd_args + repo_args + params_args
return cmd_args
# Handlers to call the actual sp method,based on the block name.
# Certain blocks required special handling and combination of methods.
def handle_generic_block(sp, block, args, method_name="add"):
# Generic handler for most blocks, with optional method name
method = getattr(sp, block)
if method_name is None:
method(*args)
else:
method(method_name, *args)
def handle_teams(sp, args):
cmd_args, members_cmd_args = args
sp.teams("add", *cmd_args)
for sublist in members_cmd_args:
sp.teams("members", *sublist)
def handle_participants(sp, args):
# Generic handler for blocks with a key to skip
method = getattr(sp, "participants")
skip_key = "--role"
new_args = [
arg
for i, arg in enumerate(args)
if not (args[i - 1] == skip_key or arg == skip_key)
]
method("add", *new_args)
method("update", *args)
def handle_compute_envs(sp, args):
json_file = any(".json" in arg for arg in args)
method = getattr(sp, "compute_envs")
if json_file:
method("import", *args)
else:
method("add", *args)
def handle_pipelines(sp, args):
method = getattr(sp, "pipelines")
for arg in args:
# Check if arg is a url or a json file.
# If it is, use the appropriate method and break.
if utils.is_url(arg):
method("add", *args)
break
elif ".json" in arg:
method("import", *args)
def find_name(cmd_args):
"""
Find and return the value associated with --name in the cmd_args list.
"""
args_list = cmd_args.get("cmd_args", [])
for i in range(len(args_list) - 1):
if args_list[i] == "--name":
return args_list[i + 1]
return None