-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.py
executable file
·381 lines (318 loc) · 14.1 KB
/
builder.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python
import os
import re
import ast
import sys
import pdb
import dxpy
import json
import stat
import shutil
import fnmatch
import logging
import argparse
import datetime
import subprocess
from dxpy import app_builder
#global workflow_logger
#global applet_logger
class WorkflowBuild:
def __init__(
self,
workflow_config_path,
region,
project_dxid,
dx_folder,
dry_run):
self.region = region
self.project_dxid = project_dxid
self.dx_folder = dx_folder
self.applet_dxids = {}
self.name = None
self.object = None
self.object_dxid = None
self.edit_version = None
# Configure loggers for BuildWorkflow and Applet classes
workflow_config_name = os.path.basename(workflow_config_path)
self.logger = config_logger('Build Workflow')
# Logic for choosing applet path in DXProject; used by Applet:write_config_file()
self.logger.info('Designated workflow path: {}:{}'.format(
project_dxid,
dx_folder))
# Create workflow configuration object
with open(workflow_config_path, 'r') as CONFIG:
workflow_config = json.load(CONFIG)
self.stages = workflow_config['stages']
self.name = workflow_config['name']
# Build all applets listed in workflow
for stage_index in self.stages:
applet_name = workflow_config['stages'][stage_index]['executable']
#print applet
applet = AppletBuild(
applet_path = applet_name,
region = region,
project_dxid = project_dxid,
dx_folder = dx_folder,
dry_run = dry_run)
self.applet_dxids[applet_name] = applet.dxid
# Create workflow
workflow_details = {
'name': workflow_config['name'],
'version': workflow_config['version'],
}
# Create DXWorkflow object on DNAnexus
self.create_workflow_object(details=workflow_details)
# Add executables to each workflow stage
for stage_index in range(0, len(self.stages)):
self.logger.info('Setting executable for stage {}'.format(stage_index))
self.add_stage_executable(str(stage_index))
# Add applet inputs to each workflow stage
for stage_index in range(0, len(self.stages)):
self.logger.info('Setting inputs for stage {}'.format(stage_index))
self.set_stage_inputs(str(stage_index))
dxpy.api.workflow_close(self.object_dxid)
self.logger.info('Build complete: {} ,'.format(self.name) +
'workflow id: {}:{}'.format(
self.project_dxid,
self.object_dxid))
def create_workflow_object(self, environment=None, properties=None, details=None):
''' Description: In development environment, find and delete any old workflow
object and create new one every time. If there is an existing workflow in
the production environment, throw an error. Never delete an existing
production workflow or writing two production workflows to the same project
folder.
'''
# Create new workflow
self.object = dxpy.new_dxworkflow(title = self.name,
name = self.name,
project = self.project_dxid,
folder = self.dx_folder,
properties = properties,
details = details
)
self.object_dxid = self.object.describe()['id']
def update_stage_executable(self, stage_index):
''' Description: Not in use since current strategy is to always create
new workflow objects.
'''
self.edit_version = self.object.describe()['editVersion']
output_folder = self.stages[stage_index]['folder']
applet_name = self.stages[stage_index]['executable']
applet_dxid = self.applet_dxids[applet_name]
self.object.update_stage(
stage = stage_index,
edit_version = self.edit_version,
executable = applet_dxid,
folder = output_folder)
def add_stage_executable(self, stage_index):
self.edit_version = self.object.describe()['editVersion']
output_folder = self.stages[stage_index]['folder']
applet_name = self.stages[stage_index]['executable']
applet_dxid = self.applet_dxids[applet_name]
stage_dxid = self.object.add_stage(
edit_version = self.edit_version,
executable = applet_dxid,
folder = output_folder)
self.stages[stage_index]['dxid'] = stage_dxid
def set_stage_inputs(self, stage_index):
if not self.stages[stage_index]['dxid']:
logger.error('Stage %s has not yet been created' % stage_index)
stage_input = {}
standard_inputs = self.stages[stage_index]['input']
for name in standard_inputs:
value = self.stages[stage_index]['input'][name]
if value == '$dnanexus_link':
print value
project = value['project']
dxid = value['id']
dxlink = dxpy.dxlink(dxid, project)
value = dxlink
stage_input[name] = value
linked_inputs = self.stages[stage_index]['linked_input']
for field_name in linked_inputs:
linked_input = linked_inputs[field_name]
if type(linked_input) is dict:
field_type = linked_input['field']
input_stage_index = linked_input['stage']
input_stage_dxid = self.stages[input_stage_index]['dxid']
stage_input[field_name] = {'$dnanexus_link': {
'stage': input_stage_dxid,
field_type: field_name
}
}
elif type(linked_input) is list:
stage_input[field_name] = []
for list_input in linked_input:
field_type = list_input['field']
input_stage_index = list_input['stage']
input_stage_dxid = self.stages[input_stage_index]['dxid']
stage_input[field_name].append({
'$dnanexus_link': {
'stage': input_stage_dxid,
field_type: field_name
}
})
self.edit_version = self.object.describe()['editVersion']
self.object.update_stage(
stage = stage_index,
edit_version = self.edit_version,
stage_input = stage_input)
class AppletBuild:
def __init__(self, applet_path, region, project_dxid, dx_folder, dry_run):
self.logger = config_logger('Build {}'.format(applet_path))
self.dxid = None
# GET VERSION INFO FROM dxapp.json FILE
dxapp_path = os.path.join(applet_path, 'dxapp.json')
with open(dxapp_path, 'r') as DXAPP:
dxapp_json = json.load(DXAPP)
#version = dxapp_json['details']['upstreamVersion']
name = dxapp_json['name']
dx_folder = os.path.join(dx_folder)
dx_applet_path = os.path.join(dx_folder, name)
# Ensure folder exists on DNAnexus
dx_project = dxpy.DXProject(project_dxid)
try:
dx_project.new_folder(dx_folder)
except:
self.logger.info(
"Folder '%s'" % dx_folder +
" in project: '%s'" % project_dxid +
' already exists.')
build_args = [
'dx',
'build',
'%s' % applet_path,
'-d=%s:%s' % (project_dxid, dx_applet_path),
'-f']
if dry_run:
build_args.append('-n')
self.logger.info(build_args)
result = subprocess.check_output(build_args)
if dry_run:
print result
return
result = ast.literal_eval(result)
self.dxid = result['id']
applet_name = os.path.basename(applet_path)
if dry_run:
self.logger.info(result)
else:
self.logger.info('Build complete. {} applet id: {}.'.format(
name,
result['id']))
def config_logger(name):
'''Create simple logger.
https://docs.python.org/2/howto/logging.html
'''
# create logger
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
return logger
def _make_new_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def parse_args(args):
parser = argparse.ArgumentParser()
parser.add_argument(
'-a',
'--applet',
dest='applet_path',
type=str,
help='Name of applet folder.')
parser.add_argument(
'-w',
'--workflow',
dest='workflow_path',
type=str,
help='Path of workflow JSON file.')
parser.add_argument(
'-e',
'--environment',
dest = 'env',
type = str,
choices = ['develop', 'production'],
default = 'develop',
help = 'Select DNAnexus environment [develop, production]')
parser.add_argument(
'-d',
'--dry-run',
dest = 'dry_run',
action = 'store_true',
default = False,
help = 'Does not actually create applet or workflow')
parser.add_argument(
'-r',
'--region',
dest = 'region',
type = str,
choices = ['azure:westus','aws:us-east-1'],
default = 'azure:westus',
help = 'Choose DNAnexus region')
if len(sys.argv[1:]) < 1:
#logger.warning('No arguments specified')
parser.print_help()
#sys.exit()
return None
args = parser.parse_args(args)
return(args)
def main():
logger = config_logger('Main')
home = os.path.dirname(os.path.abspath(__file__))
build_json = os.path.join(home, 'builder.json')
# Parse arguments
args = parse_args(sys.argv[1:])
if not args:
logger.error('No arguments provided')
sys.exit()
else:
logger.info('Args: %s' % args)
if args.applet_path and args.workflow_path:
main_logger.error(
'Applet and workflow arguments passed to builder. ' +
'Can only build one object at once')
sys.exit()
elif not args.applet_path and not args.workflow_path:
main_logger.error('No valid DNAnexus objects specified for building')
sys.exit()
if args.applet_path:
type = 'applet'
elif args.workflow_path:
type = 'workflow'
else:
print 'No object type found to build'
# Initiate path list and global resource manager objects
#path_list = PathList()
# Read 'builder.json' configuration file for building workflows/applets
with open(build_json, 'r') as json_fh:
build_config = json.load(json_fh)
project_dxid = build_config['region'][args.region][type][args.env]['dxid']
dx_folder = build_config['region'][args.region][type][args.env]['folder']
# Create build object
if args.applet_path:
logger.info('Building applet: %s' % args.applet_path)
builder = AppletBuild(
applet_path = args.applet_path,
region = args.region,
project_dxid = project_dxid,
dx_folder = dx_folder,
dry_run = args.dry_run)
elif args.workflow_path:
logger.info('Building workflow: %s' % args.workflow_path)
builder = WorkflowBuild(
workflow_config_path = args.workflow_path,
region = args.region,
project_dxid = project_dxid,
dx_folder = dx_folder,
#path_list = path_list,
dry_run = args.dry_run)
if __name__ == "__main__":
main()