Home | History | Annotate | Download | only in scripts
      1 #-------------------------------------------------------------------------
      2 # drawElements Quality Program utilities
      3 # --------------------------------------
      4 #
      5 # Copyright 2015 The Android Open Source Project
      6 #
      7 # Licensed under the Apache License, Version 2.0 (the "License");
      8 # you may not use this file except in compliance with the License.
      9 # You may obtain a copy of the License at
     10 #
     11 #      http://www.apache.org/licenses/LICENSE-2.0
     12 #
     13 # Unless required by applicable law or agreed to in writing, software
     14 # distributed under the License is distributed on an "AS IS" BASIS,
     15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 # See the License for the specific language governing permissions and
     17 # limitations under the License.
     18 #
     19 #-------------------------------------------------------------------------
     20 
     21 import re
     22 import sys
     23 from xml.dom.minidom import Document
     24 
     25 class TestCase:
     26 	def __init__(self, casePath, description, caseType):
     27 		self.casePath		= casePath
     28 		self.description	= description
     29 		self.caseType		= caseType
     30 		self.children		= []
     31 
     32 def findAllMatches(haystack, needle):
     33 	matches = []
     34 	ndx = -1
     35 	while True:
     36 		ndx = haystack.find(needle, ndx+1)
     37 		if (ndx == -1):
     38 			break
     39 		matches.append(ndx)
     40 	return matches
     41 
     42 def createAncestors(casePath):
     43 	parentCase = None
     44 	for dotNdx in findAllMatches(casePath, "."):
     45 		ancestorPath = casePath[:dotNdx]
     46 		if ancestorPath not in caseNameHash:
     47 			case = TestCase(ancestorPath, "Test Group", "TestGroup")
     48 			parentCase.children.append(case)
     49 			caseNameHash[ancestorPath] = case
     50 			parentCase = case
     51 		parentCase = caseNameHash[ancestorPath]
     52 	return parentCase
     53 
     54 def exportCase (doc, parent, testCase):
     55 	#print testCase.name, testCase.caseType
     56 	element = doc.createElement("TestCase")
     57 	element.setAttribute("Name", testCase.casePath.rsplit(".", 2)[-1])
     58 	element.setAttribute("Description", testCase.description)
     59 	element.setAttribute("CaseType", testCase.caseType)
     60 	parent.appendChild(element)
     61 	for child in testCase.children:
     62 		exportCase(doc, element, child)
     63 
     64 # Main.
     65 
     66 packageName = sys.argv[1]
     67 
     68 rootCase = TestCase(packageName, packageName, "TestPackage" )
     69 caseNameHash = { packageName:rootCase }
     70 caseRE = re.compile(r"^\s*([a-zA-Z0-9_\.\-]+) '([^']*)' (\w+)\s*$".replace(" ", r"\s+"))
     71 
     72 lines = open(packageName + ".cases").readlines()
     73 numMatches = 0
     74 for line in lines:
     75 	line = line[:-1]
     76 	if line.startswith(packageName + "."):
     77 		m = caseRE.match(line)
     78 		if m:
     79 			casePath	= m.group(1)
     80 			description	= m.group(2)
     81 			caseType	= m.group(3)
     82 			parent = createAncestors(casePath)
     83 			parent.children.append(TestCase(casePath, description, caseType))
     84 			numMatches += 1
     85 
     86 # Create XML document.
     87 doc = Document()
     88 element = doc.createElement("TestCaseList")
     89 doc.appendChild(element)
     90 for testCase in rootCase.children:
     91 	exportCase(doc, element, testCase)
     92 
     93 # Dump XML document.
     94 xml = doc.toprettyxml(indent="  ")
     95 open(packageName + "-cases.xml", "wt").write(xml)
     96 
     97 print "%d cases converted." % numMatches
     98 
     99