Home | History | Annotate | Download | only in scripts
      1 # -*- coding: utf-8 -*-
      2 
      3 import re
      4 import sys
      5 from fnmatch import fnmatch
      6 
      7 def fail (msg):
      8 	print "ERROR: " + msg
      9 	sys.exit(-1)
     10 
     11 # filename -> [case name]
     12 def readCaseList (filename):
     13 	f = open(filename, 'rb')
     14 	cases = []
     15 	for line in f:
     16 		if line[0:6] == "TEST: ":
     17 			cases.append(line[6:].strip())
     18 	f.close()
     19 	return cases
     20 
     21 # filename -> [(filter, min, recommended)]
     22 def readResolutionList (filename):
     23 	f = open(filename, 'rb')
     24 	resList = []
     25 	for line in f:
     26 		line = line.strip()
     27 		params = line.split('\t')
     28 		if len(params) == 3:
     29 			resList.append((params[0], params[1], params[2]))
     30 		elif len(params) != 0:
     31 			fail("Invalid line in resolution list: '%s'" % line)
     32 	f.close()
     33 	return resList
     34 
     35 def getMatchingCases (cases, pattern):
     36 	matching = []
     37 	for case in cases:
     38 		if fnmatch(case, pattern):
     39 			matching.append(case)
     40 	return matching
     41 
     42 def isResolutionOk (res):
     43 	return re.match('^[1-9][0-9]*x[1-9][0-9]*$', res) != None
     44 
     45 if __name__ == "__main__":
     46 	if len(sys.argv) != 3:
     47 		print "%s: [caselist] [resolution list]" % sys.argv[0]
     48 		sys.exit(-1)
     49 
     50 	caseList	= readCaseList(sys.argv[1])
     51 	resList		= readResolutionList(sys.argv[2])
     52 
     53 	# Pass 1: sanity check for resolution values
     54 	for pattern, minRes, recRes in resList:
     55 		if not isResolutionOk(minRes) or not isResolutionOk(recRes):
     56 			fail("Invalid resolution: '%s %s %s'" % (pattern, minRes, recRes))
     57 
     58 	# Pass 2: check that each case is specified by one rule
     59 	markedCases = set()
     60 	for pattern, minRes, recRes in resList:
     61 		matchingCases = getMatchingCases(caseList, pattern)
     62 
     63 		if len(matchingCases) == 0:
     64 			fail("Pattern '%s' does not match any cases" % pattern)
     65 
     66 		for case in matchingCases:
     67 			if case in markedCases:
     68 				fail("Case '%s' specified multiple times (when processing '%s')" % (case, pattern))
     69 			markedCases.add(case)
     70 
     71 	for case in caseList:
     72 		if not case in markedCases:
     73 			fail("Case '%s' not specified by any rule" % case)
     74