-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxnat_workflow
executable file
·366 lines (339 loc) · 12 KB
/
xnat_workflow
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
#!/usr/bin/python
# Copyright (c) 2011 Christian Haselgrove
# Licensed under the BSD license: http://www.opensource.org/licenses/BSD-2-Clause
import sys
import os
import getopt
import getpass
import datetime
import urllib2
import xml.dom.minidom
import suds.client
import suds.xsd.doctor
import suds.transport.http
class XNATSoapCaller:
def __init__(self, username, password):
self.cookiejar = None
result = self.call('CreateServiceSession.jws',
'execute',
(),
auth=(username, password))
self.session = str(result)
return
def close(self):
self.call('CloseServiceSession.jws', 'execute', ())
return
def call(self, jws, operation, inputs, fix_import=False, auth=None):
url = '%s/axis/%s' % ('http://candi-store/xnat/', jws)
if auth:
t = suds.transport.http.HttpAuthenticated(username=auth[0],
password=auth[1])
else:
t = suds.transport.http.HttpTransport()
if self.cookiejar:
t.cookiejar = self.cookiejar
if fix_import:
xsd_url = 'http://schemas.xmlsoap.org/soap/encoding/'
imp = suds.xsd.doctor.Import(xsd_url)
doctor = suds.xsd.doctor.ImportDoctor(imp)
client = suds.client.Client('%s?wsdl' % url,
transport=t,
doctor=doctor)
else:
client = suds.client.Client('%s?wsdl' % url, transport=t)
typed_inputs = []
for (dtype, val) in inputs:
ti = client.factory.create(dtype)
ti.value = val
typed_inputs.append(ti)
# the WSDL returns the local IP address in the URLs; these need
# to be corrected if XNAT is behind a proxy
client.set_options(location=url)
f = getattr(client.service, operation)
result = f(*typed_inputs)
if not self.cookiejar:
self.cookiejar = t.cookiejar
return result
def report_error(msg):
sys.stderr.write('%s: %s\n' % (progname, msg))
sys.stderr.write('run %s with no arguments for usage\n' % progname)
return
def node_text(node):
s = ''
for cn in node.childNodes:
if cn.nodeType == cn.TEXT_NODE:
s += cn.data
return s
def print_pairs(pairs):
if not pairs:
return
max_name_length = max([ len(name) for (name, value) in pairs ])
fmt = '%%-%ds %%s' % max_name_length
for pair in pairs:
print fmt % pair
def get_workflow_xml(xsc, experiment_id, w_id):
args = (('ns1:string', xsc.session),
('ns1:string', 'wrk:workflowData.ID'),
('ns1:string', '='),
('ns1:string', experiment_id),
('ns1:string', 'wrk:workflowData'))
w_ids = [ int(v) for v in xsc.call('GetIdentifiers.jws', 'search', args) ]
if w_id not in w_ids:
sys.stderr.write("%s: can't find ID %d for %s (maybe try '%s list %s')\n" % (progname, w_id, experiment_id, progname, experiment_id))
sys.exit(1)
url = '%s/app/template/XMLSearch.vm/id/%s/data_type/wrk:workflowData' % (host, str(w_id))
r = urllib2.Request(url)
xsc.cookiejar.add_cookie_header(r)
return urllib2.urlopen(r).read()
def update(xsc, doc):
inputs = (('ns0:string', xsc.session),
('ns0:string', doc.toxml()),
('ns0:boolean', False),
('ns0:boolean', True))
xsc.call('StoreXML.jws', 'store', inputs, fix_import=True)
return
progname = os.path.basename(sys.argv[0])
try:
host = os.environ['XNAT_HOST']
except KeyError:
host = None
try:
user_name = os.environ['XNAT_USER']
except KeyError:
user_name = None
try:
password = os.environ['XNAT_PASSWORD']
except KeyError:
password = None
if len(sys.argv) == 1:
print
print 'usage: %s [options] <command> [command arguments]' % progname
print
print 'XNAT workflow tool'
print
print 'options are:'
print
print ' -h <host>'
print ' -u <user name>'
print ' -p <password>'
print
print 'option values may be given by environment variables:'
print
if host is None:
print ' host: XNAT_HOST'
else:
print ' host: XNAT_HOST (set to %s)' % host
if user_name is None:
print ' user name: XNAT_USER'
else:
print ' user name: XNAT_USER (set to %s)' % user_name
if password is None:
print ' password: XNAT_PASSWORD'
else:
print ' password: XNAT_PASSWORD (currently set)'
print
print '%s will prompt for a missing user name, password, or host' % progname
print
print 'commands are:'
print
print ' list <experiment identifier>'
print ' info <experiment identifier> <workflow ID>'
print ' xml <experiment identifier> <workflow ID>'
print ' update <experiment identifier> <workflow ID> <step ID> <step description> <percentage complete>'
print ' complete <experiment identifier> <workflow ID>'
print ' fail <experiment identifier> <workflow ID> [step description]'
print
sys.exit(1)
try:
(opts, args) = getopt.getopt(sys.argv[1:], 'h:u:p:')
except getopt.error, data:
report_error(data)
sys.exit(1)
if not args:
report_error('not enough non-option arguments')
sys.exit(1)
for (option, value) in opts:
if option == '-h':
host = value
if option == '-u':
user_name = value
if option == '-p':
password = value
if host is None:
sys.stdout.write('Host: ')
sys.stdout.flush()
host = sys.stdin.readline().strip()
if user_name is None:
sys.stdout.write('User name: ')
sys.stdout.flush()
user_name = sys.stdin.readline().strip()
if password is None:
password = getpass.getpass()
host = host.rstrip('/')
command = args.pop(0)
if command == 'list':
if not args:
report_error('"list" requires an argument')
sys.exit(0)
experiment_id = args[0]
xsc = XNATSoapCaller(user_name, password)
args = (('ns1:string', xsc.session),
('ns1:string', 'wrk:workflowData.ID'),
('ns1:string', '='),
('ns1:string', experiment_id),
('ns1:string', 'wrk:workflowData'))
for w_id in xsc.call('GetIdentifiers.jws', 'search', args):
url = '%s/app/template/XMLSearch.vm/id/%s/data_type/wrk:workflowData' % (host, str(w_id))
r = urllib2.Request(url)
xsc.cookiejar.add_cookie_header(r)
data = urllib2.urlopen(r).read()
doc = xml.dom.minidom.parseString(data)
workflow_node = doc.getElementsByTagName('wrk:Workflow')[0]
print w_id, \
workflow_node.getAttribute('ExternalID'), \
workflow_node.getAttribute('ID'), \
workflow_node.getAttribute('status'), \
workflow_node.getAttribute('pipeline_name'), \
workflow_node.getAttribute('launch_time'), \
workflow_node.getAttribute('percentageComplete')
elif command == 'xml':
if len(args) != 2:
report_error('"xml" requires two arguments')
sys.exit(0)
experiment_id = args[0]
try:
w_id = int(args[1])
except ValueError:
report_error('bad workflow ID "%s"' % args[1])
sys.exit(1)
xsc = XNATSoapCaller(user_name, password)
print get_workflow_xml(xsc, experiment_id, w_id)
elif command == 'info':
if len(args) != 2:
report_error('"info" requires two arguments')
sys.exit(0)
experiment_id = args[0]
try:
w_id = int(args[1])
except ValueError:
report_error('bad workflow ID "%s"' % args[1])
sys.exit(1)
xsc = XNATSoapCaller(user_name, password)
doc = xml.dom.minidom.parseString(get_workflow_xml(xsc, experiment_id, w_id))
workflow_node = doc.getElementsByTagName('wrk:Workflow')[0]
print '--- basic info'
print
for attr in ('data_type',
'ID',
'ExternalID',
'status',
'pipeline_name',
'step_description',
'launch_time',
'current_step_id',
'current_step_launch_time',
'percentageComplete'):
print '%-25s %s' % (attr + ':', workflow_node.getAttribute(attr))
print
environment_nodes = doc.getElementsByTagName('wrk:Workflow')
if environment_nodes:
print '--- execution environment'
print
pairs = []
for tag in ('pipeline',
'xnatuser',
'host',
'notify',
'dataType',
'id',
'supressNotification'):
elements = environment_nodes[0].getElementsByTagName('wrk:' + tag)
if not elements:
continue
pairs.append((tag+':', node_text(elements[0])))
print_pairs(pairs)
print
print '--- parameters'
print
pairs = []
for pn in environment_nodes[0].getElementsByTagName('wrk:parameter'):
name = pn.getAttribute('name')
value = node_text(pn)
pairs.append((name+':', value))
print_pairs(pairs)
print
xsc.close()
elif command == 'update':
if len(args) != 5:
report_error('"update" requires five arguments')
sys.exit(0)
experiment_id = args[0]
try:
w_id = int(args[1])
except ValueError:
report_error('bad workflow ID "%s"' % args[1])
sys.exit(1)
xsc = XNATSoapCaller(user_name, password)
doc = xml.dom.minidom.parseString(get_workflow_xml(xsc, experiment_id, w_id))
workflow_node = doc.getElementsByTagName('wrk:Workflow')[0]
workflow_node.setAttribute('status', 'Running')
t = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
workflow_node.setAttribute('current_step_launch_time', t)
workflow_node.setAttribute('current_step_id', args[2])
workflow_node.setAttribute('step_description', args[3])
workflow_node.setAttribute('percentageComplete', args[4])
update(xsc, doc)
xsc.close()
print 'updated workflow %d for %s' % (w_id, experiment_id)
elif command == 'complete':
if len(args) != 2:
report_error('"complete" requires two arguments')
sys.exit(0)
experiment_id = args[0]
try:
w_id = int(args[1])
except ValueError:
report_error('bad workflow ID "%s"' % args[1])
sys.exit(1)
xsc = XNATSoapCaller(user_name, password)
doc = xml.dom.minidom.parseString(get_workflow_xml(xsc, experiment_id, w_id))
workflow_node = doc.getElementsByTagName('wrk:Workflow')[0]
workflow_node.setAttribute('status', 'Complete')
t = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
workflow_node.setAttribute('current_step_launch_time', t)
workflow_node.setAttribute('percentageComplete', '100.0')
try:
workflow_node.removeAttribute('current_step_id')
except xml.dom.NotFoundErr:
pass
try:
workflow_node.removeAttribute('step_description')
except xml.dom.NotFoundErr:
pass
update(xsc, doc)
xsc.close()
print 'closed (completed) workflow %d for %s' % (w_id, experiment_id)
elif command == 'fail':
if len(args) < 2 or len(args) > 3:
report_error('"fail" requires two or three arguments')
sys.exit(0)
experiment_id = args[0]
try:
w_id = int(args[1])
except ValueError:
report_error('bad workflow ID "%s"' % args[1])
sys.exit(1)
xsc = XNATSoapCaller(user_name, password)
doc = xml.dom.minidom.parseString(get_workflow_xml(xsc, experiment_id, w_id))
workflow_node = doc.getElementsByTagName('wrk:Workflow')[0]
workflow_node.setAttribute('status', 'Failed')
if len(args) > 2:
workflow_node.setAttribute('step_description', args[2])
update(xsc, doc)
xsc.close()
print 'closed (failed) workflow %d for %s' % (w_id, experiment_id)
else:
report_error('unknown command "%s"' % command)
sys.exit(1)
sys.exit(0)
# eof