-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxnat_workflow_info
executable file
·274 lines (209 loc) · 10.3 KB
/
xnat_workflow_info
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
#!/usr/bin/env python
# Copyright (c) 2015 Washington University
# Author(s) Timothy B. Brown (tbbrown at wustl.edu)
#
# Command line tool for displaying and altering XNAT workflow information
#
# This utility is loosely based on, and owes some design acknowledgment to,
# the xnat_workflow Python script found in Christian Haselgrove's XNAT
# extras repository at: https://github.com/chaselgrove/xnat_extras.git
# along with a bash shell script called 'xnat_workflow' that has similar
# functionality that was also supplied by Christian Haselgrove.
import requests
import argparse
import json
import datetime
def xnat_workflow_info_show(args):
"""
Display on stdout information about a specified workflow.
This function is called in response to use of the 'show' sub-command.
The main argument parser and the sub-parser for the 'show' sub-command
enforce rules about what command line options are required.
Example invocation:
xnat_workflow_info --username=<your-username> --password=<your-password> --workflow-id=3912918 show
"""
request_url = "http://" + args.server + "/data/services/workflows/workflowid/" + args.workflow_id + "?format=json"
print("xnat_workflow_info show: request_url: " + request_url)
response = requests.get(request_url, auth=(args.username, args.password))
if (response.status_code != 200):
print("Cannot get response from request: " + request_url)
sys.exit(1)
json_response = json.loads(response.text)
json_items = json_response['items']
i = 0
for json_item in json_items:
i = i + 1
print i
# meta
json_meta = json_item['meta']
isHistory = json_meta['isHistory']
type = json_meta['xsi:type']
start_date = json_meta['start_date']
print " isHistory: " + str(isHistory)
print " type: " + type
print " start_date: " + start_date
# children
#json_children = json_item['children']
#print " children"
#print json_children
# data_fields
json_data_fields = json_item['data_fields']
status = json_data_fields['status']
workflow_id = json_data_fields['wrk_workflowData_id']
data_type = json_data_fields['data_type']
launch_time = json_data_fields['launch_time']
ExternalID = json_data_fields['ExternalID']
pipeline_name = json_data_fields['pipeline_name']
ID = json_data_fields['ID']
print " status: " + status
print " workflow_id: " + str(workflow_id)
print " data_type: " + data_type
print " launch_time: " + launch_time
print " ExternalID: " + ExternalID
print " pipeline_name: " + pipeline_name
print " ID: " + ID
print " All Data Fields:"
print " " + str(json_data_fields)
def xnat_workflow_info_update(args):
"""
Update the information about the specified workflow.
The status is set to 'In Progress'
The current_step_id is set to the args.step_id
The step_description is set to args.step_description
The percentageComplete is set to args.percent_complete
The current_step_launch_time is set to the current time
This function is called in response to use of the 'update' sub-command.
The main argument parser and the sub-parser for the 'update' sub-command
enforce rules about what command line options are required.
Example invocation:
xnat_workflow_info --username=<your-username> --password=<your-password> --workflow-id=3912918 update \
--step-id=1 --step-description='do this' --percent-complete=10
"""
request_url = "http://" + args.server + "/data/services/workflows/workflowid/" + args.workflow_id + "?format=json"
print("xnat_workflow_info update: request_url: " + request_url)
response = requests.get(request_url, auth=(args.username, args.password))
json_response = json.loads(response.text)
json_items = json_response['items']
json_item = json_items[0]
json_data_fields = json_item['data_fields']
put_url = "http://" + args.server + "/REST/workflows"
# workflow identifying information
put_url += "?wrk:workflowData/id=" + json_data_fields['ID']
put_url += "&wrk:workflowData/pipeline_name=" + json_data_fields['pipeline_name']
put_url += "&wrk:workflowData/launch_time=" + json_data_fields['launch_time']
put_url += "&wrk:workflowData/data_type=" + json_data_fields['data_type']
# workflow information to be updated
put_url += "&wrk:workflowData/status=" + "In Progress"
put_url += "&wrk:workflowData/current_step_id=" + args.step_id
put_url += "&wrk:workflowData/step_description=" + args.step_description
put_url += "&wrk:workflowData/percentageComplete=" + args.percent_complete
put_url += "&wrk:workflowData/current_step_launch_time=" + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
put_url = put_url.replace(" ", "%20");
print("xnat_workflow_info update: put_url: " + put_url)
response = requests.put(put_url, auth=(args.username, args.password))
if (response.status_code != 200):
print("Cannot update workflow")
print("response.status_code: " + str(response.status_code))
xnat_workflow_info_show(args)
def xnat_workflow_info_complete(args):
"""
Update the information about the specified workflow to reflect the
successful completion of the job.
The status is set to 'Complete'
The current_step_id is set to -1
The step_description is set to 'End'
The percentageComplete is set to 100.0
The current_step_launch_time is set to the current time
This function is called in response to use of the 'complete' sub-command.
The main argument parser and the sub-parser for the 'complete' sub-command
enforce rules about what command line options are required.
Example invocation:
xnat_workflow_info --username=<your-username> --password=<your-password> --workflow-id=3912918 complete
"""
request_url = "http://" + args.server + "/data/services/workflows/workflowid/" + args.workflow_id + "?format=json"
print("xnat_workflow_info complete: request_url: " + request_url)
response = requests.get(request_url, auth=(args.username, args.password))
json_response = json.loads(response.text)
json_items = json_response['items']
json_item = json_items[0]
json_data_fields = json_item['data_fields']
put_url = "http://" + args.server + "/REST/workflows"
# workflow identifying information
put_url += "?wrk:workflowData/id=" + json_data_fields['ID']
put_url += "&wrk:workflowData/pipeline_name=" + json_data_fields['pipeline_name']
put_url += "&wrk:workflowData/launch_time=" + json_data_fields['launch_time']
put_url += "&wrk:workflowData/data_type=" + json_data_fields['data_type']
# workflow information to be updated
put_url += "&wrk:workflowData/status=" + "Complete"
put_url += "&wrk:workflowData/current_step_id=" + "-1"
put_url += "&wrk:workflowData/step_description=" + "End"
put_url += "&wrk:workflowData/percentageComplete=" + "100.0"
put_url += "&wrk:workflowData/current_step_launch_time=" + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
put_url = put_url.replace(" ", "%20");
print("xnat_workflow_info complete: put_url: " + put_url)
response = requests.put(put_url, auth=(args.username, args.password))
if (response.status_code != 200):
print("Cannot update workflow")
print("response.status_code: " + str(response.status_code))
xnat_workflow_info_show(args)
def xnat_workflow_info_fail(args):
"""
Update the information about the specified workflow to reflect the
failure of the job.
The status is set to 'Failed'
This function is called in response to use of the 'fail' sub-command.
The main argument parser and the sub-parser for the 'fail' sub-command
enforce rules about what command line options are required.
Example invocation:
xnat_workflow_info --username=<your-username> --password=<your-password> --workflow-id=3912918 fail
"""
request_url = "http://" + args.server + "/data/services/workflows/workflowid/" + args.workflow_id + "?format=json"
print("xnat_workflow_info fail: request_url: " + request_url)
response = requests.get(request_url, auth=(args.username, args.password))
json_response = json.loads(response.text)
json_items = json_response['items']
json_item = json_items[0]
json_data_fields = json_item['data_fields']
put_url = "http://" + args.server + "/REST/workflows"
# workflow identifying information
put_url += "?wrk:workflowData/id=" + json_data_fields['ID']
put_url += "&wrk:workflowData/pipeline_name=" + json_data_fields['pipeline_name']
put_url += "&wrk:workflowData/launch_time=" + json_data_fields['launch_time']
put_url += "&wrk:workflowData/data_type=" + json_data_fields['data_type']
# workflow information to be updated
put_url += "&wrk:workflowData/status=" + "Failed"
put_url = put_url.replace(" ", "%20");
print("xnat_workflow_info fail: put_url: " + put_url)
response = requests.put(put_url, auth=(args.username, args.password))
if (response.status_code != 200):
print("Cannot update workflow")
print("response.status_code: " + str(response.status_code))
xnat_workflow_info_show(args)
def main():
parser = argparse.ArgumentParser(prog="xnat_workflow_info",
description="Retrieve and update XNAT workflow information")
parser.add_argument("-s", "--server", dest="server", default="db.humanconnectome.org", type=str)
parser.add_argument("-u", "--username", dest="username", required=True, type=str)
parser.add_argument("-p", "--password", dest="password", required=True, type=str)
parser.add_argument("-w", "--workflow-id", dest="workflow_id", required=True, type=str)
subparsers = parser.add_subparsers(help='workflow information sub-commands')
# setup the show sub-command
parser_info = subparsers.add_parser('show', help='show workflow information')
parser_info.set_defaults(func=xnat_workflow_info_show)
# setup the update sub-command
parser_update = subparsers.add_parser('update', help='update workflow information')
parser_update.add_argument("-t", "--step-id", dest="step_id", required=True, type=str)
parser_update.add_argument("-d", "--step-description", dest="step_description", required=True, type=str)
parser_update.add_argument("-c", "--percent-complete", dest="percent_complete", required=True, type=str)
parser_update.set_defaults(func=xnat_workflow_info_update)
# setup the complete sub-command
parser_complete = subparsers.add_parser('complete', help='complete workflow')
parser_complete.set_defaults(func=xnat_workflow_info_complete)
# setup the fail sub-command
parser_fail = subparsers.add_parser('fail', help='fail workflow')
parser_fail.set_defaults(func=xnat_workflow_info_fail)
# parse the arguments and call whatever function was selected
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
main()