-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprofile.py
443 lines (346 loc) · 15.7 KB
/
profile.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
#
# Authors:
# - Stefan Profanter ([email protected])
#
import logging
import codecs
import sys
import xml.dom.minidom as dom
import json
logger = logging.getLogger(__name__)
from result import TestResult
from status import ImplementationStatus
class Profiles:
"""Class containing all profiles of a project"""
def __init__(self):
self.categories = []
self.projectInfo = ProjectInfo()
self.conformanceGroups = []
self.profiles = []
self.result = []
self.accumulatedTestResult = None
def parseFile(self, profilesFile):
logger.info("Parsing profiles: {}".format(profilesFile.name))
self.__init__()
fileContent = profilesFile.read()
# Remove BOM since the dom parser cannot handle it on python 3 windows
if fileContent.startswith( codecs.BOM_UTF8 ):
fileContent = fileContent.lstrip( codecs.BOM_UTF8 )
if (sys.version_info >= (3, 0)):
fileContent = fileContent.decode("utf-8")
uaProfile = dom.parseString(fileContent).getElementsByTagName("UaProfiles")[0]
self.projectInfo.parseXml(uaProfile.getElementsByTagName("ProjectInfo")[0])
for categoryXml in uaProfile.getElementsByTagName("Categories")[0].childNodes:
if categoryXml.nodeType != categoryXml.ELEMENT_NODE:
continue
cat = ProfileCategory(self)
cat.parseXml(categoryXml)
self.categories.append(cat)
for groupsXml in uaProfile.getElementsByTagName("ConformanceGroups")[0].childNodes:
if groupsXml.nodeType != groupsXml.ELEMENT_NODE:
continue
group = ConformanceGroup()
group.parseXml(groupsXml, self)
self.conformanceGroups.append(group)
for profilesXml in uaProfile.getElementsByTagName("Profiles")[0].childNodes:
if profilesXml.nodeType != profilesXml.ELEMENT_NODE:
continue
profile = Profile()
profile.parseXml(profilesXml, self)
self.profiles.append(profile)
for profile in self.profiles:
profile.delayInit(self)
logger.debug("Profile file parsed.")
#print(json.dumps(mapping_bugs, indent=4))
def getCategory(self, categoryName):
for cat in self.categories:
if cat.name == categoryName:
return cat
logger.warning("Category '{}' not found.".format(categoryName))
return None
def getConformanceGroup(self, groupName):
for grp in self.conformanceGroups:
if grp.name == groupName:
return grp
logger.warning("ConformanceGroup '{}' not found.".format(groupName))
return None
def getConformanceGroupForUnit(self, unitName):
for grp in self.conformanceGroups:
unit = grp.getConformanceUnit(unitName, showWarning=False)
if unit:
return grp
return None
def getProfile(self, profileName, showWarning=False):
for profile in self.profiles:
if profile.name == profileName:
return profile
if showWarning:
logger.warning("Profile '{}' not found.".format(profileName))
return None
def setImplementationStatus(self, status):
for groupName in status:
group = self.getConformanceGroup(groupName)
for unitName in status[groupName]:
unit = group.getConformanceUnit(unitName)
unit.implementationStatus = ImplementationStatus(status[groupName][unitName])
class ProjectInfo:
"""Class containing the project info of a profiles file"""
def __init__(self):
self.type = None
self.profile = None
self.scriptVersion = None
self.version = None
def parseXml(self, xmlelement):
self.type = xmlelement.getAttribute("ProjectType")
self.profile = xmlelement.getAttribute("ProjectProfile")
self.scriptVersion = xmlelement.getAttribute("ScriptVersion")
self.version = xmlelement.getAttribute("Version")
class ProfileCategory:
"""Class describing the profile category"""
def __init__(self, profiles):
self.description = None
self.name = None
self.profiles = profiles
self.accumulatedTestResult = None
self.accumulatedImplementationStatus = None
def parseXml(self, xmlelement):
self.description = xmlelement.getAttribute("description")
self.name = xmlelement.getAttribute("name")
def getResult(self):
if self.accumulatedTestResult:
return self.accumulatedTestResult
self.accumulatedTestResult = TestResult.UNKNOWN
for group in self.profiles.conformanceGroups:
for unit in group.conformanceUnits:
if not unit.category == self:
continue
res = unit.getResult()
if res < self.accumulatedTestResult:
self.accumulatedTestResult = res
return self.accumulatedTestResult
def getImplementationStatus(self):
if self.accumulatedImplementationStatus:
return self.accumulatedImplementationStatus
self.accumulatedImplementationStatus = ImplementationStatus.UNKNOWN
for group in self.profiles.conformanceGroups:
for unit in group.conformanceUnits:
if not unit.category == self:
continue
if ImplementationStatus.UNKNOWN < unit.implementationStatus < self.accumulatedImplementationStatus:
self.accumulatedImplementationStatus = unit.implementationStatus
return self.accumulatedImplementationStatus
class ConformanceGroup:
"""A Conformance group groups together one or multiple conformance units"""
def __init__(self):
self.conformanceUnits = []
self.description = None
self.name = None
self.result = []
self.accumulatedTestResult = None
self.accumulatedImplementationStatus = None
def parseXml(self, xmlelement, profiles):
self.description = xmlelement.getAttribute("description")
self.name = xmlelement.getAttribute("name")
for unitXml in xmlelement.getElementsByTagName("ConformanceUnits")[0].childNodes:
if unitXml.nodeType != unitXml.ELEMENT_NODE:
continue
unit = ConformanceUnit()
unit.parseXml(unitXml, profiles)
self.conformanceUnits.append(unit)
def getConformanceUnit(self, unitName, showWarning=True):
for unit in self.conformanceUnits:
if unit.name == unitName:
return unit
if showWarning:
logger.warning("ConformanceUnit '{}' inside group '{}' not found.".format(unitName, self.name))
return None
def getResult(self):
if self.accumulatedTestResult:
return self.accumulatedTestResult
if self.result and len(self.result) > 0:
self.accumulatedTestResult = self.result.testresult
else:
self.accumulatedTestResult = TestResult.UNKNOWN
for unit in self.conformanceUnits:
res = unit.getResult()
if res < self.accumulatedTestResult:
self.accumulatedTestResult = res
return self.accumulatedTestResult
def getImplementationStatus(self):
if self.accumulatedImplementationStatus:
return self.accumulatedImplementationStatus
self.accumulatedImplementationStatus = ImplementationStatus.UNKNOWN
for unit in self.conformanceUnits:
if ImplementationStatus.UNKNOWN < unit.implementationStatus < self.accumulatedImplementationStatus:
self.accumulatedImplementationStatus = unit.implementationStatus
return self.accumulatedImplementationStatus
class ConformanceUnit:
"""A conformance unit contains test cases"""
def __init__(self):
self.category = None
self.description = None
self.name = None
self.used = None
self.testCases = []
self.initTestCase = None
self.cleanupTestCase = None
self.result = []
self.accumulatedTestResult = None
self.implementationStatus = ImplementationStatus.UNKNOWN
def parseXml(self, xmlelement, profiles):
self.description = xmlelement.getAttribute("description")
self.name = xmlelement.getAttribute("name")
self.used = xmlelement.getAttribute("used") == "true"
cat = xmlelement.getAttribute("category")
self.category = profiles.getCategory(cat) if cat is not '' else None
if len(xmlelement.getElementsByTagName("InitTestCase")) > 0:
self.initTestCase = TestCase()
self.initTestCase.parseXml(xmlelement.getElementsByTagName("InitTestCase")[0])
if len(xmlelement.getElementsByTagName("CleanupTestCase")) > 0:
self.cleanupTestCase = TestCase()
self.cleanupTestCase.parseXml(xmlelement.getElementsByTagName("CleanupTestCase")[0])
for testXml in xmlelement.getElementsByTagName("TestCases")[0].childNodes:
if testXml.nodeType != testXml.ELEMENT_NODE:
continue
testCase = TestCase()
testCase.parseXml(testXml)
self.testCases.append(testCase)
def getTest(self, testName, showWarning=True):
for test in self.testCases:
if test.name == testName:
return test
if showWarning:
logger.warning("Test '{}' inside unit '{}' not found.".format(testName, self.name))
return None
def getResult(self):
if self.accumulatedTestResult:
return self.accumulatedTestResult
if self.result and len(self.result) > 0:
self.accumulatedTestResult = self.result.testresult
else:
self.accumulatedTestResult = TestResult.UNKNOWN
for test in self.testCases:
res = test.getResult()
if res < self.accumulatedTestResult:
self.accumulatedTestResult = res
return self.accumulatedTestResult
class TestCase:
"""A single test case within a conformance unit"""
def __init__(self):
self.description = None
self.name = None
self.filename = None
self.checkedState = None
self.result = []
self.accumulatedTestResult = None
def parseXml(self, xmlelement):
self.description = xmlelement.getAttribute("description")
self.name = xmlelement.getAttribute("name")
self.filename = xmlelement.getAttribute("filename")
self.checkedState = xmlelement.getAttribute("checkedState")
def getResult(self):
if self.accumulatedTestResult:
return self.accumulatedTestResult
if self.result:
self.accumulatedTestResult = self.result.testresult
else:
self.accumulatedTestResult = TestResult.SKIPPED
return self.accumulatedTestResult
mapping_bugs = {}
class IncludedConformanceUnit:
"""Metadata for included conformance unit inside a profile"""
def __init__(self):
self.optional = None
self.group = None
self.unit = None
def parseXml(self, xmlelement, profiles, parentProfile, profile_mapping_bugs=None):
self.optional = xmlelement.getAttribute("optional") == "true"
groupName = xmlelement.getAttribute("group")
unitName = xmlelement.getAttribute("name")
self.group = profiles.getConformanceGroup(groupName)
self.unit = self.group.getConformanceUnit(unitName, showWarning=False)
# There are some bugs in the uaprofile file where conformance units are not mapped to the correct conformance group.
# Try to fix this by searching the correct group here
if not self.unit:
map = mapping_bugs[parentProfile.name] if parentProfile.name in mapping_bugs else {}
units = map['units'] if 'units' in map else {}
self.group = profiles.getConformanceGroupForUnit(unitName)
units[unitName] = {
"wrongGroup": groupName,
"correctGroup": self.group.name
}
map['units'] = units
mapping_bugs[parentProfile.name] = map
self.unit = self.group.getConformanceUnit(unitName)
class Profile:
"""A profile which includes conformance units and other profiles """
def __init__(self):
self.category = None
self.description = None
self.name = None
self.uri = None
self.isFacet = None
self.conformanceUnits = []
self.profiles = []
self._delay_profile_add = []
self.accumulatedTestResult = None
self.accumulatedImplementationStatus = None
def parseXml(self, xmlelement, profiles):
self.category = profiles.getCategory(xmlelement.getAttribute("category"))
self.description = xmlelement.getAttribute("description")
self.name = xmlelement.getAttribute("name")
self.uri = xmlelement.getAttribute("uri")
self.isFacet = self.name.endswith("Facet")
for includeUnitsXml in xmlelement.getElementsByTagName("IncludeConformanceUnits")[0].childNodes:
if includeUnitsXml.nodeType != includeUnitsXml.ELEMENT_NODE:
continue
includeUnit = IncludedConformanceUnit()
includeUnit.parseXml(includeUnitsXml, profiles, self)
self.conformanceUnits.append(includeUnit)
for includeProfileXml in xmlelement.getElementsByTagName("IncludeProfiles")[0].childNodes:
if includeProfileXml.nodeType != includeProfileXml.ELEMENT_NODE:
continue
profile = profiles.getProfile(includeProfileXml.getAttribute("name"))
if profile:
self.profiles.append(profile)
else:
self._delay_profile_add.append(includeProfileXml.getAttribute("name"))
def delayInit(self, profiles):
for includeProfileName in self._delay_profile_add:
profile = profiles.getProfile(includeProfileName, showWarning=True)
if profile:
self.profiles.append(profile)
self._delay_profile_add = []
def getResult(self):
if self.accumulatedTestResult:
return self.accumulatedTestResult
self.accumulatedTestResult = TestResult.UNKNOWN
for selUnit in self.conformanceUnits:
if selUnit.optional:
continue
res = selUnit.unit.getResult()
if res < self.accumulatedTestResult:
self.accumulatedTestResult = res
for profile in self.profiles:
res = profile.getResult()
if res < self.accumulatedTestResult:
self.accumulatedTestResult = res
return self.accumulatedTestResult
def getImplementationStatus(self):
if self.accumulatedImplementationStatus:
return self.accumulatedImplementationStatus
self.accumulatedImplementationStatus = ImplementationStatus.UNKNOWN
for selUnit in self.conformanceUnits:
if ImplementationStatus.UNKNOWN < selUnit.unit.implementationStatus < self.accumulatedImplementationStatus:
self.accumulatedImplementationStatus = selUnit.unit.implementationStatus
for profile in self.profiles:
if ImplementationStatus.UNKNOWN < profile.getImplementationStatus() < self.accumulatedImplementationStatus:
self.accumulatedImplementationStatus = profile.getImplementationStatus()
return self.accumulatedImplementationStatus