-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctsparsinglib.py
executable file
·136 lines (113 loc) · 3.55 KB
/
ctsparsinglib.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from os import walk
import fnmatch, re
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
#== Constants 常數 ==
cTestPkg = "TestPackage"
cTestSuite = "TestSuite"
cTestCase = "TestCase"
cTest = "Test"
cEntry = "Entry"
cPkgAttr = "appPackageName"
cNameAttr = "name"
cUriAttr = "uri"
kSuccess = 'success'
testplanDefPath = "./../repository/plans/"
ctsplanFileName = "CTS.xml"
testcaseDefPath = "./../repository/testcases/"
#== String Comparing 字串比對 ==
# is it TestPackage tag?
def isTestPkg(name):
return name == cTestPkg
# is it TestSuite tag?
def isTestSuite(name):
return name == cTestSuite
# is it TestCase tag?
def isTestCase(name):
return name == cTestCase
# is it Test tag?
def isTest(name):
return name == cTest
def isThisFileRightPkg(name, target):
return name == target
def isXMLFile(name):
return fnmatch.fnmatch(name, '*.xml')
#== Utils 小工具 ==
def combineByDot(a,b):
return a + '.' + b
#== Real Parsing actions 真實分析動作 ==
def recursivelyParseTestElementChild(element, result, path):
for child in element:
parseRecursiveToBuildTestcaseTestFullName(child, result, path)
def parseRecursiveToBuildTestcaseTestFullName(element, result, path):
attrName = element.get(cNameAttr)
tagName = element.tag
if isTest(tagName):
TestDataWithClassAndMethod = {}
TestDataWithClassAndMethod[cTest] = attrName
TestDataWithClassAndMethod[cTestCase] = path
result[cTest].append(TestDataWithClassAndMethod)
elif isTestCase(tagName):
path = combineByDot(path, attrName)
result[cTestCase].append(path)
recursivelyParseTestElementChild(element, result, path)
elif isTestSuite(tagName):
if path == '':
path = path + attrName
else:
path = combineByDot(path, attrName)
recursivelyParseTestElementChild(element, result, path)
elif isTestPkg(tagName):
recursivelyParseTestElementChild(element, result, path)
def parsingFileAndFindTarget(filename, target):
isCurrectTestPkg = False # Default False
testsuiteList = [] # No Use
testcaseList = []
testList = []
result = {}
result[cTestSuite] = testsuiteList # No Use
result[cTestCase] = testcaseList
result[cTest] = testList
tree = ET.ElementTree(file=filename)
root = tree.getroot()
parseRecursiveToBuildTestcaseTestFullName(root, result, '');
isCurrectTestPkg = isThisFileRightPkg(root.get(cPkgAttr), target)
result[kSuccess] = isCurrectTestPkg
return result
def parsingPackagesInPlanFile(filename):
result = {}
result[cEntry] = []
for event, element in ET.iterparse(filename):
if event == 'end':
tagName = element.tag
if tagName == cEntry:
attrUri = element.get(cUriAttr)
result[cEntry].append(attrUri)
element.clear() # discard the element
return result;
def getAllFile(path):
f = []
for (dirpath, dirnames, filenames) in walk(path):
f.extend(filenames)
break
return f
def getXMLFiles():
f = getAllFile(testcaseDefPath)
result = []
for file in f:
if isXMLFile(file):
result.append(file)
return result
def findTargetInAllFiles(target):
result = None
for file in getXMLFiles():
temp = parsingFileAndFindTarget(testcaseDefPath + file, target)
if temp[kSuccess] == True:
result = temp
break
return result