-
Notifications
You must be signed in to change notification settings - Fork 2
/
xml_utils.py
294 lines (232 loc) · 10.2 KB
/
xml_utils.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
import xml.dom.minidom as minidom
import time
import os
verbose: bool = False # Verbose output here and in cmr.py
detail_xsl = "/NGAP-PROD-tests/details.v2.xsl"
def write_xml_documents(path, version, results):
if results.misc_results:
results.misc_path = write_misc_doc(results.provider, version, results.misc_results,
str(results.misc_total), str(results.error_count),
str(results.info_count), str(results.timeout_count))
if results.dmr_results:
results.dmr_path = write_dmr_doc(results.provider, version, results.dmr_results,
str(results.dmr_total), str(results.dmr_pass), str(results.dmr_fail))
if results.dap_results:
results.dap_path = write_dap_doc(results.provider, version, results.dap_results,
str(results.dap_total), str(results.dap_pass), str(results.dap_fail))
if results.dap_var_results:
results.dap_var_path = write_var_doc(results.provider, version, results.dap_var_results,
str(results.dap_var_total), str(results.dap_var_pass),
str(results.dap_var_fail))
if results.netcdf_results:
results.netcdf_path = write_netcdf_doc(results.provider, version, results.netcdf_results,
str(results.netcdf_total), str(results.netcdf_pass),
str(results.netcdf_fail))
update_summary(path, results.provider, results)
def write_misc_doc(provider, version, misc_list, total, err, info, timeout):
# make the response document
root = minidom.Document()
xsl_element = root.createProcessingInstruction("xml-stylesheet",
"type='text/xsl' href='" + detail_xsl + "'")
root.appendChild(xsl_element)
prov = root.createElement('Provider')
prov.setAttribute('name', provider)
prov.setAttribute('date', time.asctime())
prov.setAttribute('total', total)
prov.setAttribute('err_count', err)
prov.setAttribute('info_count', info)
prov.setAttribute('time_count', timeout)
root.appendChild(prov)
for item in misc_list:
test = create_attribute(root, item.type, item)
prov.appendChild(test)
# Save the XML
xml_str = root.toprettyxml(indent="\t")
directory = "Exports/" + time.strftime("%m.%d.%y") + "/"
isExist = os.path.exists(directory)
if not isExist:
os.makedirs(directory)
url = provider + time.strftime("-%m.%d.%Y-") + version + ".err.xml"
save_path_file = directory + url
with open(save_path_file, "w") as f:
f.write(xml_str)
return url
def write_dmr_doc(provider, version, dmr_list, total, pas, fail):
# make the response document
root = minidom.Document()
xsl_element = root.createProcessingInstruction("xml-stylesheet",
"type='text/xsl' href='" + detail_xsl + "'")
root.appendChild(xsl_element)
prov = root.createElement('Provider')
prov.setAttribute('name', provider)
prov.setAttribute('date', time.asctime())
prov.setAttribute('total', total)
prov.setAttribute('pass_count', pas)
prov.setAttribute('fail_count', fail)
root.appendChild(prov)
for item in dmr_list:
test = create_attribute(root, "Test", item)
prov.appendChild(test)
# Save the XML
xml_str = root.toprettyxml(indent="\t")
directory = "Exports/" + time.strftime("%m.%d.%y") + "/"
isExist = os.path.exists(directory)
if not isExist:
os.makedirs(directory)
url = provider + time.strftime("-%m.%d.%Y-") + version + ".dmr.xml"
save_path_file = directory + url
with open(save_path_file, "w") as f:
f.write(xml_str)
return url
def write_dap_doc(provider, version, dap_list, total, pas, fail):
# make the response document
root = minidom.Document()
xsl_element = root.createProcessingInstruction("xml-stylesheet",
"type='text/xsl' href='" + detail_xsl + "'")
root.appendChild(xsl_element)
prov = root.createElement('Provider')
prov.setAttribute('name', provider)
prov.setAttribute('date', time.asctime())
prov.setAttribute('total', total)
prov.setAttribute('pass_count', pas)
prov.setAttribute('fail_count', fail)
root.appendChild(prov)
for item in dap_list:
test = create_attribute(root, "Test", item)
prov.appendChild(test)
# Save the XML
xml_str = root.toprettyxml(indent="\t")
directory = "Exports/" + time.strftime("%m.%d.%y") + "/"
isExist = os.path.exists(directory)
if not isExist:
os.makedirs(directory)
url = provider + time.strftime("-%m.%d.%Y-") + version + ".dap.xml"
save_path_file = directory + url
with open(save_path_file, "w") as f:
f.write(xml_str)
return url
def write_var_doc(provider, version, var_list, total, pas, fail):
# make the response document
root = minidom.Document()
xsl_element = root.createProcessingInstruction("xml-stylesheet",
"type='text/xsl' href='" + detail_xsl + "'")
root.appendChild(xsl_element)
prov = root.createElement('Provider')
prov.setAttribute('name', provider)
prov.setAttribute('date', time.asctime())
prov.setAttribute('total', total)
prov.setAttribute('pass_count', pas)
prov.setAttribute('fail_count', fail)
root.appendChild(prov)
for item in var_list:
test = create_attribute(root, "Test", item)
prov.appendChild(test)
# Save the XML
xml_str = root.toprettyxml(indent="\t")
directory = "Exports/" + time.strftime("%m.%d.%y") + "/"
isExist = os.path.exists(directory)
if not isExist:
os.makedirs(directory)
url = provider + time.strftime("-%m.%d.%Y-") + version + ".var.xml"
save_path_file = directory + url
with open(save_path_file, "w") as f:
f.write(xml_str)
return url
def write_netcdf_doc(provider, version, netcdf_list, total, pas, fail):
# make the response document
root = minidom.Document()
xsl_element = root.createProcessingInstruction("xml-stylesheet",
"type='text/xsl' href='" + detail_xsl + "'")
root.appendChild(xsl_element)
prov = root.createElement('Provider')
prov.setAttribute('name', provider)
prov.setAttribute('date', time.asctime())
prov.setAttribute('total', total)
prov.setAttribute('pass_count', pas)
prov.setAttribute('fail_count', fail)
root.appendChild(prov)
for item in netcdf_list:
test = create_attribute(root, "Test", item)
prov.appendChild(test)
# Save the XML
xml_str = root.toprettyxml(indent="\t")
directory = "Exports/" + time.strftime("%m.%d.%y") + "/"
isExist = os.path.exists(directory)
if not isExist:
os.makedirs(directory)
url = provider + time.strftime("-%m.%d.%Y-") + version + ".net.xml"
save_path_file = directory + url
with open(save_path_file, "w") as f:
f.write(xml_str)
return url
def update_summary(path, provider, results):
# make the response document
root = minidom.parse(path)
pros = root.getElementsByTagName('Provider')
pro = None
for p in pros:
if p.getAttribute('name') == provider:
p.setAttribute('run', str(results.run))
p.setAttribute('total', str(results.total))
p.setAttribute('time', str(results.time))
pro = p
break
if results.misc_results:
misc = root.createElement('Error')
misc.setAttribute('url', results.misc_path)
misc.setAttribute('total', str(results.misc_total))
misc.setAttribute('error', str(results.error_count))
misc.setAttribute('info', str(results.info_count))
misc.setAttribute('time', str(results.timeout_count))
pro.appendChild(misc)
if results.dmr_results:
dmr = root.createElement('Test')
dmr.setAttribute('name', 'DMR Tests')
dmr.setAttribute('url', results.dmr_path)
dmr.setAttribute('total', str(results.dmr_total))
dmr.setAttribute('pass', str(results.dmr_pass))
dmr.setAttribute('fail', str(results.dmr_fail))
pro.appendChild(dmr)
if results.dap_results:
dap = root.createElement('Test')
dap.setAttribute('name', 'DAP Tests')
dap.setAttribute('url', results.dap_path)
dap.setAttribute('total', str(results.dap_total))
dap.setAttribute('pass', str(results.dap_pass))
dap.setAttribute('fail', str(results.dap_fail))
pro.appendChild(dap)
if results.dap_var_results:
dap_var = root.createElement('Test')
dap_var.setAttribute('name', 'DAP Variable Tests')
dap_var.setAttribute('url', results.dap_var_path)
dap_var.setAttribute('total', str(results.dap_var_total))
dap_var.setAttribute('pass', str(results.dap_var_pass))
dap_var.setAttribute('fail', str(results.dap_var_fail))
pro.appendChild(dap_var)
if results.netcdf_results:
netcdf = root.createElement('Test')
netcdf.setAttribute('name', 'NETCDF Tests')
netcdf.setAttribute('url', results.netcdf_path)
netcdf.setAttribute('total', str(results.netcdf_total))
netcdf.setAttribute('pass', str(results.netcdf_pass))
netcdf.setAttribute('fail', str(results.netcdf_fail))
pro.appendChild(netcdf)
xml_str = root.toprettyxml(indent="\t")
xml_str = '\n'.join([s for s in xml_str.splitlines() if s.strip()])
with open(path, "w") as f:
f.write(xml_str)
def create_attribute(root, name, result):
test = root.createElement(name)
# collection level information
test.setAttribute('ccid', result.ccid)
test.setAttribute('title', result.title)
# granule level information
test.setAttribute('gid', result.gid)
test.setAttribute('url', result.url)
test.setAttribute('murl', result.murl)
# test results
test.setAttribute('type', result.type)
test.setAttribute('status', result.status)
test.setAttribute('code', str(result.code))
test.setAttribute('payload', result.payload)
return test