srcanamdw_os/leavescan/test/LET/let.py
changeset 0 83f4b4db085c
child 2 99082257a271
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 #!/usr/bin/python
       
     2 import string
       
     3 import sys
       
     4 import os
       
     5 from os import listdir
       
     6 from os.path import isdir
       
     7 import time
       
     8 from optparse import OptionParser
       
     9 
       
    10 #config
       
    11 LEAVESCAN = 'D:\leavescan.exe '
       
    12 
       
    13 os_type = 'unix'
       
    14 if sys.platform.lower().startswith('win'):
       
    15     os_type = 'windows'
       
    16 
       
    17 class TestResult:
       
    18 	def __init__(self):
       
    19 		self.allOutput=""
       
    20 		self.lines={}
       
    21 
       
    22 	def setAllOutput(self,allOuptput):
       
    23 		self.allOuptput = allOuptput
       
    24 
       
    25 	def parseLines(self):
       
    26 		tmp_lines=self.allOuptput.splitlines()
       
    27 		for tmp_line in tmp_lines:
       
    28 			line_no = getLeaveScanMsgLine(tmp_line)
       
    29 			self.lines[line_no]=tmp_line
       
    30 
       
    31 	def checkResult(self,warnItems):
       
    32 		result = ''
       
    33 		ok=0
       
    34 		for warnItem in warnItems:
       
    35 			warnLine = warnItem.line
       
    36 			if not warnItem.semi:
       
    37 				if self.lines.has_key(warnLine):
       
    38 					outputLine = self.lines[warnLine]
       
    39 					for keyword in warnItem.checkExist:
       
    40 						if outputLine.find(keyword)<0:
       
    41 							result= result+ 'CHECK:"'+keyword+'" is expected in the output:"'+outputLine+'"\n'
       
    42 							ok=1
       
    43 					for keyword in warnItem.checkNoExist:
       
    44 						if outputLine.find(keyword)>=0:
       
    45 							result= result+ 'CHECK:"'+keyword+'" is expected not in the output:"'+outputLine+'"\n'
       
    46 							ok=1
       
    47 				else:
       
    48 					for keyword in warnItem.checkExist:
       
    49 						result= result+ 'CHECK:"'+keyword+'" is expected in the output of line'+str(warnLine)+'\n'
       
    50 						ok = 1
       
    51 			else:
       
    52 				if self.lines.has_key(warnLine):
       
    53 					outputLine = self.lines[warnLine]
       
    54 					for keyword in warnItem.checkExist:
       
    55 						if outputLine.find(keyword)>=0:
       
    56 							result= result+ 'SEMI:"'+keyword+'" is expected in the output:"'+outputLine+'"\n'
       
    57 							ok=1
       
    58 					for keyword in warnItem.checkNoExist:
       
    59 						if outputLine.find(keyword)<0:
       
    60 							result= result+ 'SEMI:"'+keyword+'" is expected not in the output:"'+outputLine+'"\n'
       
    61 							ok=1
       
    62 				else:
       
    63 					for keyword in warnItem.checkNoExist:
       
    64 						result= result+ 'SEMI:"'+keyword+'" is expected in the output of line'+str(warnLine)+'\n'
       
    65 						ok = 1
       
    66 
       
    67 
       
    68 		if ok==0:
       
    69 			return 'OK'
       
    70 		else:
       
    71 			return result;
       
    72 
       
    73 class CheckPoint:
       
    74 	def __init__(self):
       
    75 		self.line = -1
       
    76 		self.checkExist=[]
       
    77 		self.checkNoExist=[]
       
    78 		self.semi = False
       
    79 	def setCheckPoint(self,warnString,semi = False):
       
    80 		warnString=warnString.strip()
       
    81 		if len(warnString)<=0:
       
    82 			return 
       
    83 		keywordList = warnString.split(',')
       
    84 		for keyword in keywordList:
       
    85 			keyword = keyword.strip()
       
    86 			if len(keyword)>0:
       
    87 				if keyword[0]=='+':
       
    88 					self.checkExist.append(keyword[1:])
       
    89 				elif keyword[0]=='-':
       
    90 					self.checkNoExist.append(keyword[1:])
       
    91 				else:
       
    92 					self.checkExist.append(keyword)
       
    93 		self.semi = semi
       
    94 
       
    95 	def printSelf(self):
       
    96 		printType = 0;
       
    97 		print 'line:'+ str(self.line)
       
    98 		if len(self.checkExist)>0:
       
    99 			print '		checkExist'
       
   100 			printType +=1;
       
   101 			for checkWord in self.checkExist:
       
   102 				print '				'+checkWord
       
   103 		if len(self.checkNoExist)>0:
       
   104 			print '		checkNoExist'
       
   105 			printType +=1;
       
   106 			for checkWord in self.checkNoExist:
       
   107 				print '				'+checkWord
       
   108 		if printType == 0:
       
   109 			print  '	nothing to check'
       
   110 
       
   111 
       
   112 
       
   113 class TestcaseConfig:
       
   114 	def __init__(self):
       
   115 		self.desc =''
       
   116 		self.option =''
       
   117 		self.expect =''
       
   118 		self.warnItems =[]
       
   119 		self.author = ''
       
   120 		self.type = ''
       
   121 		self.date =''
       
   122 	def printSelf(self):
       
   123 		print 'desc:'+self.desc
       
   124 		print 'option:'+self.option
       
   125 		print 'author:'+self.author
       
   126 		print 'type:'+self.type
       
   127 		print 'date:'+self.date
       
   128 		print 'expect:'+self.expect
       
   129 		print 'check:'
       
   130 		for warnItem in self.warnItems:
       
   131 			print '	line:'+str(warnItem.line)
       
   132 			printType = 0;
       
   133 			if len(warnItem.checkExist)>0:
       
   134 				print '		checkExist'
       
   135 				printType +=1;
       
   136 				for checkWord in warnItem.checkExist:
       
   137 					print '				'+checkWord
       
   138 			if len(warnItem.checkNoExist)>0:
       
   139 				print '		checkNoExist'
       
   140 				printType +=1;
       
   141 				for checkWord in warnItem.checkNoExist:
       
   142 					print '				'+checkWord
       
   143 			if printType == 0:
       
   144 
       
   145 				print  '	nothing to check'
       
   146 
       
   147 
       
   148 
       
   149 def getTestcaseConfig(fileName):
       
   150 	file = open(fileName,"r")
       
   151 	fileLine = 0
       
   152 	testcaseConfig = TestcaseConfig()
       
   153 	for line in file.readlines():
       
   154 		if not line.endswith('\\'):
       
   155 			fileLine += 1
       
   156 		line = line.strip()
       
   157 		scommnet = line.find('//')
       
   158 		if scommnet == 0:
       
   159 			if (line.find('desc:'))>0:
       
   160 				desc = line[line.find('desc:')+5:].strip()
       
   161 				testcaseConfig.desc=desc
       
   162 			elif line.find('option:')>0:
       
   163 				option = line[line.find('option:')+7:].strip()
       
   164 				testcaseConfig.option=option
       
   165 			elif line.find('expect file:')>0:
       
   166 				expect = line[line.find('expect file:')+12:].strip()
       
   167 				#get the content of the file
       
   168 				data = open(expect,"r")
       
   169 				for dataline in data.readlines():
       
   170 					testcaseConfig.expect += dataline
       
   171 			elif line.find('expect:')>0:
       
   172 				expect = line[line.find('expect:')+7:].strip()
       
   173 				testcaseConfig.expect=expect
       
   174 			elif line.find('author:')>0:
       
   175 				expect = line[line.find('author:')+7:].strip()
       
   176 				testcaseConfig.author=expect
       
   177 			elif line.find('type:')>0:
       
   178 				expect = line[line.find('type:')+5:].strip()
       
   179 				testcaseConfig.type=expect
       
   180 			elif line.find('date:')>0:
       
   181 				expect = line[line.find('date:')+5:].strip()
       
   182 				testcaseConfig.date=expect
       
   183 
       
   184 		if scommnet >=0:
       
   185 			line =line[scommnet+2:]
       
   186 			if line.find('check:')>=0:
       
   187 				line = line[line.find('check:')+6:].strip()
       
   188 				warnItem = CheckPoint()
       
   189 				warnItem.setCheckPoint(line)
       
   190 				warnItem.line = fileLine
       
   191 				testcaseConfig.warnItems.append(warnItem)
       
   192 			elif line.find('semi:')>=0:
       
   193 				line = line[line.find('semi:')+5:].strip()
       
   194 				warnItem = CheckPoint()
       
   195 				warnItem.setCheckPoint(line,True)
       
   196 				warnItem.line = fileLine
       
   197 				testcaseConfig.warnItems.append(warnItem)
       
   198 
       
   199 				
       
   200 	return testcaseConfig
       
   201 
       
   202 def execute(filename , option):
       
   203 	commandLine = LEAVESCAN + option + ' ' +filename
       
   204 	output=''
       
   205 	for outputLine in os.popen(commandLine).readlines():
       
   206 		output += outputLine
       
   207 	return output
       
   208 	
       
   209 def analysis(result,testcaseConfig):
       
   210 	"""
       
   211 	no use in the version
       
   212 	for expect keyword
       
   213 	"""
       
   214 	if not testcaseConfig.expect=='':
       
   215 		if testcaseConfig.expect == result:
       
   216 			return 1;
       
   217 		else:
       
   218 			print testcaseConfig.expect
       
   219 			if DEBUG:
       
   220 				print '------'
       
   221 				print result 
       
   222 				print '========'
       
   223 			return 0;
       
   224 
       
   225 	
       
   226 def excuteCmd(cmdLine):
       
   227 	i, o = os.popen4(cmdLine)
       
   228    	i.close()
       
   229     	out = o.read()
       
   230     	o.close()
       
   231 	return out
       
   232 
       
   233 
       
   234 def getLeaveScanMsgLine(line):
       
   235 	p1=line.find('(')
       
   236 	if(p1<0):
       
   237 		return -2
       
   238 	p2=line.find(')')
       
   239 	if(p2<2):
       
   240 		return -2
       
   241 	return int(line[p1+1:p2])
       
   242 
       
   243 def getFormatTime():
       
   244 	curTime = time.localtime()
       
   245 	return str(curTime[0])+"_"+str(curTime[1])+"_"+str(curTime[2])+"_"+str(curTime[3])+"_"+str(curTime[4])+"_"+str(curTime[5])
       
   246 
       
   247 def getCurTime():
       
   248 	curTime = time.localtime()
       
   249 	return "'"+str(curTime[0])+"-"+str(curTime[1])+"-"+str(curTime[2])+" "+str(curTime[3])+":"+str(curTime[4])+":"+str(curTime[5])+".000000'"
       
   250 
       
   251 def getTestcaseName(fileName):
       
   252 	fileNameList = os.path.splitext(fileName) 
       
   253 	return fileNameList[len(fileNameList)-2]
       
   254 
       
   255 def runTest(testFile,logFile,reportFile):
       
   256 	testcaseConfig = getTestcaseConfig(testFile)
       
   257 	cmdString = LEAVESCAN+testcaseConfig.option+' '+testFile
       
   258 	logFile.write(cmdString+'\n')
       
   259 	startTime = getCurTime()
       
   260 	result = excuteCmd(cmdString)
       
   261 	endtime = getCurTime()
       
   262 	logFile.write(result+'\n')
       
   263 
       
   264 	lr = TestResult()
       
   265 	lr.setAllOutput(result)
       
   266 	lr.parseLines()
       
   267 	
       
   268 	result=lr.checkResult(testcaseConfig.warnItems)
       
   269 	testcaseResult = 'PASS' # for csv file
       
   270 	if not result=='OK':
       
   271 		logFile.write('ERROR:'+testFile+'\n')
       
   272 		logFile.write('DETAIL:'+'\n')
       
   273 		logFile.write(result)
       
   274 		testcaseResult = 'FAILTURE'
       
   275 		print testcaseResult+':'+testFile
       
   276 
       
   277 	else:
       
   278 		logFile.write('PASS\n')
       
   279 	
       
   280 	reportFile.write(getTestcaseName(testFile)+','+startTime+','+endtime+','+testcaseResult+'\n')
       
   281 	if result=='OK':
       
   282 		return 1
       
   283 	else:
       
   284 		return 0
       
   285 
       
   286 
       
   287 if __name__ == '__main__':
       
   288 	#pass argu
       
   289 	testFileList=[]
       
   290 	optParser = OptionParser('let -f file_list -o output_dir')
       
   291 	optParser.add_option("-f","--file",action = "store",type = "string",dest = "fileListName")
       
   292 	optParser.add_option("-o","--outputdir",action = "store",type = "string",dest = "outputName")
       
   293 	optParser.add_option("","--std-in",action = "store_true",dest = "listFromStdin",default =False)
       
   294 	optParser.add_option("-e","--execute",action = "store",type="string", dest = "exePath", default="")
       
   295 	options, args = optParser.parse_args(sys.argv[1:])
       
   296 	#check exe exsitence
       
   297 	if not options.exePath == "":
       
   298 		if os.path.exists(options.exePath):
       
   299 			LEAVESCAN = options.exePath+" "
       
   300 		else:
       
   301 			print "WARN-"+options.exePath+" dosen't exist"
       
   302 			print "WARN-will use default config:"+LEAVESCAN
       
   303 	if not os.path.exists(LEAVESCAN.strip()):
       
   304 		print "WARN-please set Leavescan's address"
       
   305 	else:
       
   306 		fileListName = options.fileListName
       
   307 		outputDirName = options.outputName
       
   308 		if not os.path.exists(outputDirName):
       
   309 			os.makedirs(outputDirName)
       
   310 		#get each testcase in the list 
       
   311 		pwd=os.getcwd()+'\\'
       
   312 		if options.listFromStdin :
       
   313 			listFile = sys.stdin
       
   314 		else:
       
   315 			listFile = open(fileListName,'r')
       
   316 
       
   317 		for curFile in listFile.readlines():
       
   318 			curFile=curFile.strip()
       
   319 			if(curFile.find('#')==0):
       
   320 				1+1
       
   321 			elif os.path.exists(curFile):
       
   322 				testFileList.append(curFile)
       
   323 			else:
       
   324 				print 'WARN-file '+curFile+ 'doesn\'t exist.ignore'
       
   325 		#other config
       
   326 		DEBUG = 0;
       
   327 		totalRun=0;
       
   328 		totalPass=0;
       
   329 		logFileName = outputDirName+'\\'+getFormatTime()+'.log'
       
   330 		reportFileName = outputDirName+'\\'+getFormatTime()+'.csv'
       
   331 		logFile = open(logFileName,'w')
       
   332 		reportFile = open(reportFileName,'w')
       
   333 		reportFile.write('TestCaseId,StartTime,EndTime,Result\n')
       
   334 
       
   335 		for testFileName in testFileList:
       
   336 		#test it
       
   337 			totalRun+=1;
       
   338 			totalPass+=runTest(testFileName,logFile,reportFile)
       
   339 		#summary
       
   340 		print 'RSLT-Total:'+str(totalRun)+';Pass:'+str(totalPass)
       
   341 		#environment
       
   342 		logFile.close()
       
   343 		reportFile.close()
       
   344 	
       
   345