| # -*- coding: GBK -*-    | 
| #write:zfl  | 
| #  | 
| ##@package ReportAnalyzer.py  | 
| # Ä£¿éµÄ¼òҪ˵Ã÷:½âÎö´úÂëÖ´ÐÐÂʱ¨¸æ  | 
| # @author:zfl  | 
| # @date 2010-11-03 10:00  | 
| # @version 1.0  | 
| #  | 
| # ÐÞ¸Äʱ¼ä ÐÞ¸ÄÈË ÐÞ¸ÄÄÚÈÝ  | 
| #  | 
| # Ä£¿éÏêϸ˵Ã÷:½âÎö´úÂëÖ´ÐÐÂʱ¨¸æ  | 
| #  | 
| #-------------------------------------------------------------------------------  | 
|   | 
| """Version = 2010-11-03 10:00"""  | 
|   | 
| #-------------------------------------------------------------------------------  | 
| #µ¼Èë  | 
| import os  | 
| import time  | 
| import traceback  | 
| import PyTrackObj  | 
|   | 
| #-------------------------------------------------------------------------------  | 
| #È«¾Ö±äÁ¿  | 
|   | 
| FORM_NAME = "ReportAnalyzer"  | 
|   | 
| #---µ÷ÊÔº¯Êý--------------------------------------------------------------------  | 
|   | 
| NOW_TIME = 'log%s%s%s%s%s%s.txt'%tuple(['%02d'%time.localtime()[i] for i in range(6)])  | 
|   | 
| LOG_PATH = os.getcwd() + "\\log\\"      # log·¾¶  | 
|   | 
| FileEndStr = "_Report.txt"  | 
| FileNameConnector = "!"  | 
| ReportConnector = "_"  | 
| ReportMDVConnector = "²âÊÔ±¨¸æMD5"  | 
| PyMDVConnector = "PyÎļþMD5"  | 
|   | 
| ## Ð´logº¯Êý  | 
| #  @param *varlist ²»¶¨²Î  | 
| #  @return ÎÞ·µ»ØÖµ  | 
| #  @remarks ½«´«ÈëµÄlogÐÅϢдÈëÖ¸¶¨logÎļþ  | 
| def LogEx(*varlist):  | 
|     info = '[%s%s%s%s%s%s]'%tuple(['%02d'%time.localtime()[i] for i in range(6)])  | 
|     for i in range(1, len(varlist)):  | 
|         curValue = varlist[i]  | 
|         info += "( "+str(type(curValue)) + ":" + str(curValue) + " ),"  | 
|       | 
|     if not os.path.isdir(LOG_PATH):  | 
|         os.mkdir(LOG_PATH)  | 
|           | 
|     if os.path.isfile(LOG_PATH +  NOW_TIME):  | 
|         logfile = open(LOG_PATH +  NOW_TIME, 'a')  | 
|     else:  | 
|         logfile = file(LOG_PATH +  NOW_TIME, 'w')  | 
|       | 
|     logfile.write('\t'*int(varlist[0])+info+'\n')  | 
|     logfile.close()  | 
|       | 
| LogEx(0, "CurPath=%s"%os.getcwd())  | 
|   | 
|   | 
| ## »ñµÃkey¶ÔÓ¦µÄÊý¾Ý  | 
| #  @param keyList: ¹Ø¼ü´ÊÁÐ±í  | 
| #  @param line: Ò»Ðб¨¸æÄÚÈÝ  | 
| #  @return ·µ»Økey¶ÔÓ¦µÄÊý¾ÝÁÐ±í  | 
| #  @remarks »ñµÃkey¶ÔÓ¦µÄÊý¾Ý  | 
| def GetValueByKey(keyList, line):  | 
|       | 
|     valueList = []  | 
|     for key in keyList:  | 
|         keyPos = line.find(key)  | 
|         # ÕÒ²»µ½Ê±£¬¼ÓÈë¿Õ´®  | 
|         if keyPos == -1:  | 
|             valueList.append("")  | 
|             LogEx(0, "key = %s: Key Erorr!"%key)  | 
|             continue  | 
|           | 
|         line = line[keyPos:]  | 
|         #½âÎö³öÊý¾Ý£¬¼ÓÈëÁÐ±í  | 
|         valueList.append(line[line.find("[")+1:line.find("]")])  | 
|       | 
|     return valueList  | 
|   | 
|       | 
| ## ¸²¸ÇÂʱ¨¸æ½âÎöÆ÷  | 
| #  @param fileName: ±¨¸æÎļþÃû  | 
| #  @param MD5Key: MD5ÃÜÔ¿  | 
| #  @param contentList: ±¨¸æÄÚÈÝ  | 
| #  @return ·µ»ØPyObj  | 
| #  @remarks ¸²¸ÇÂʱ¨¸æ½âÎöÆ÷  | 
| def ReportAnalyzer(fileName, contentList, MD5Key):  | 
|       | 
|     # ÎļþÃû³ö´í  | 
|     if not fileName.endswith(FileEndStr):  | 
|         # ×ܱ¨¸æ£¬²»½âÎö  | 
|         if not fileName.find(".py") and not fileName.find(".PY"):  | 
|             return  | 
|         LogEx(0, "fileName = %s: FileName Erorr!"%fileName)  | 
|         return  | 
|       | 
|     # È¥µô·¾¶Ç°×º  | 
|     tName = fileName[fileName.rfind("\\")+1:]  | 
|     # ½âÎöPyÃû  | 
|     connectPos = tName.find(FileNameConnector)  | 
|     if connectPos == -1:  | 
|         connectPos = tName.rfind(ReportConnector)  | 
|     pyName = tName[:connectPos]  # pyÃû  | 
|       | 
|     state = GetValueByKey(["state"], contentList[0])[0]  | 
|       | 
|     # ³õʼ»¯  | 
|     pyObj = PyTrackObj.GetPyObj(pyName, MD5Key, "", False)  | 
|     funcObjList = []     # º¯Êý¶ÔÏóÁÐ±í  | 
|     pyCodeList = []      # PyÄÚÈÝÁÐ±í  | 
|       | 
|     pyExecCount = int(state[:state.find("/")])     # pyÒÑÖ´ÐÐÐÐÊý   | 
|     pyTotalCount = int(state[state.find("/")+1:])  # py×ÜÓÐЧÐÐÊý  | 
|       | 
| #    funcObj = None       # º¯Êý¶ÔÏó  | 
|     className = ""       # ËùÊôÀàÃû  | 
|     funcName = ""        # º¯ÊýÃû    | 
|     funcComment = ""     # º¯Êý×¢ÊÍ   | 
| #    execRowCount = 0     # ÒÑÖ´ÐÐÐÐÊý  | 
| #    totalRowCount = 0    # ×ÜÐÐÊý  | 
|     startIndex = 0       # º¯ÊýÆðʼ  | 
|     endIndex = 0         # º¯Êý½áÊø  | 
|     returnValue = None   # ·µ»ØÖµ  | 
|     funcMD5 = ""         # º¯ÊýMD5  | 
|     reportMD5 = ""           # PyÎļþMD5  | 
|     rowInfoList = []     # ÐжÔÏóÁÐ±í  | 
|       | 
|     iCount = 0           # ÌøÐмÆÊý  | 
|       | 
| #    if 1:  | 
|     try:  | 
|         for i in range(1, len(contentList)):  | 
| #            print contentList[i]  | 
|             # È¥µô¶àÓڵĻ»ÐÐ  | 
|             contentList[i] = contentList[i][:-1]  | 
|             line = contentList[i]  | 
|               | 
|             # Ìø¹ýPyÎļþµÄMD5  | 
|             if line.startswith(PyMDVConnector):  | 
|                 pyMD5 = GetValueByKey(["MD5"], line)[0]  | 
|                 continue  | 
|               | 
|             # ±¨¸æ½áÊø  | 
|             if line.startswith(ReportMDVConnector):  | 
|                 # ÒѾ»ñµÃº¯ÊýÐÅÏ¢  | 
|                 if funcName:  | 
|                     # ÉèÖÃFuncObjÐÅÏ¢  | 
|                     funcObj = PyTrackObj.GetFuncObj(className, funcName, funcComment, rowInfoList, \  | 
|                                                     startIndex, endIndex, funcMD5, returnValue)  | 
|                     # ¼ÓÈ뺯Êý¶ÔÏóÁÐ±í  | 
|                     funcObjList.append(funcObj)  | 
|                     # È¥µôÊ×ÐеĻ»Ðзû  | 
|                     contentList[0] = contentList[0][:-1]  | 
|                       | 
|                     reportMD5 = GetValueByKey(["MD5"], line)[0]  | 
|   | 
|                     # ÑéÖ¤PyÎļþµÄMD5  | 
|                     if reportMD5 != PyTrackObj.CalcListMD5(contentList[:i-1], MD5Key):  | 
|                         LogEx(0, "fileName = %s: MD5 Erorr!"%fileName)  | 
| #                        print "fileName = %s: MD5 Erorr!"%fileName  | 
|                         return None  | 
|                       | 
| #                    print "fileName = %s: PyMD5 OK!"%fileName  | 
|                       | 
|                     # ÉèÖÃPyObjÐÅÏ¢  | 
|                     pyObj.SetPyInfo(pyName, pyExecCount, pyTotalCount, funcObjList, pyCodeList, pyMD5)  | 
|                 else:  | 
|                     LogEx(0, "fileName = %s: No Code!"%fileName)  | 
|                 break  | 
|               | 
|             # ÅжÏÊÇ·ñÊǺ¯ÊýÍ·  | 
|             if line.find("]:"+" <") != -1 or line.find("]:"+":<") != -1:  | 
|                   | 
|                 # ÒѾ»ñµÃº¯ÊýÐÅÏ¢  | 
|                 if funcName:  | 
|                           | 
|                     # ÉèÖÃFuncObjÐÅÏ¢  | 
|                     funcObj = PyTrackObj.GetFuncObj(className, funcName, funcComment, rowInfoList, \  | 
|                                                     startIndex, endIndex, funcMD5, returnValue)  | 
|                     # ¼ÓÈ뺯Êý¶ÔÏóÁÐ±í  | 
|                     funcObjList.append(funcObj)  | 
|                   | 
|                 funcObj = None  | 
|                 rowInfoList = []  | 
|                   | 
|                 # ÅжÏÓÐûÓÐÀàÃû  | 
|                 if line.find("]:"+":<") != -1:  | 
|                     className = line[line.find("[") + 1:line.find("]")]  | 
|                 else:  | 
|                     className = ""        # ËùÊôÀàÃû  | 
|                   | 
|                 #---»ñµÃº¯ÊýÐÅÏ¢---  | 
|                 funcName = line[line.find("<") + 1:line.find(">")]  | 
|                 funcMD5Pos = contentList[i - 1].find("MD5:[")  | 
|                 funcMD5Pos = (funcMD5Pos == -1) and len(contentList[i - 1]) or funcMD5Pos  | 
|                 funcMD5 = GetValueByKey(["MD5"], contentList[i - 1])[0]  | 
|                 funcComment = contentList[i - 1][:funcMD5Pos - 4]  | 
|                   | 
|                 # returnValue×Ö·û´®£¬²»È«ÔÚ±¾ÐÐÄÚ  | 
|                 if contentList[i].find("type") == -1:  | 
|                     # returnValue×Ö·û´®Öв¹»»Ðзû  | 
|                     contentList[i] += "\n"  | 
|                     line = contentList[i]  | 
|                       | 
|                 # returnValueÖÐÓÐÐл»·û£¬½«returnValueËùÓÐÐÐÄÚÈÝ×éºÏÆðÀ´  | 
|                 for j in range(i+1, len(contentList)):  | 
|                     if line.find("]  "+"type"+":[") != -1:  | 
|                         break   | 
|                       | 
|                     line += contentList[j]  | 
|                     iCount += 1  | 
|                   | 
|                 # ½âÎöº¯ÊýÐÅÏ¢  | 
|                 valueList = GetValueByKey(["state", "range", "returnValue", "type"], line)  | 
|                   | 
|                 execRowCount = int(valueList[0][:valueList[0].find("/")])  | 
|                 totalRowCount = int(valueList[0][valueList[0].find("/")+1:])  | 
|                 startIndex = int(valueList[1][:valueList[1].find(":")])  | 
|                 endIndex = int(valueList[1][valueList[1].find(":")+1:])  | 
|                 returnValueType = valueList[3][valueList[3].find("'")+1:valueList[3].rfind("'")]  | 
|                   | 
|                 if returnValueType == "str":  | 
|                     returnValue = valueList[2]  | 
|                 elif returnValueType == "NoneType":  | 
|                     returnValue = None  | 
|                 else:  | 
|                     returnValue = eval("%s('''%s''')"%(returnValueType, valueList[2]))  | 
|                 continue  | 
|               | 
|             # Ìø¹ý±»·Ö¸îµÄreturnValue×Ö·û´®  | 
|             if iCount > 0:  | 
|                 if iCount > 1:  | 
|                     # ¶ÔreturnValue×Ö·û´®²¹»»Ðзû  | 
|                     contentList[i] = contentList[i] + "\n"  | 
|                 iCount -= 1  | 
|                 continue  | 
|                       | 
|             # Èç¹û·Ç¿ÕÐлòº¯Êý×¢ÊÍ  | 
|             if len(line.strip()) > 0 and not line.strip().startswith("##") and not line.strip().startswith("MD5"):  | 
|                 rowIndex = int(line[line.find("[")+1:line.find("]")])  | 
|                   | 
|                 # ÅжÏÊÇ·ñÖ´Ðйý  | 
|                 if line.startswith("*"):  | 
|                     rowState = 0  | 
|                 else:  | 
|                     rowState = 1  | 
|                   | 
|                 line = line[line.find("]")+2:]  | 
|                    | 
|                 # ²¹×ãÐÐÊý  | 
|                 for j in range(len(pyCodeList), rowIndex-1):  | 
|                     pyCodeList.append("\n")   | 
|                 pyCodeList.append(line + "\n")  | 
|                   | 
|                 offset = rowIndex - startIndex  | 
|                   | 
|                 # ×¢ÊͺͿɺöÂÔ´úÂ룬²»¼Ç¼ִÐÐÇé¿ö  | 
|                 if line.strip().startswith("#") or PyTrackObj.IsLineNeedIgnore(line):  | 
|                     rowInfoList.append([rowIndex, offset, 2, contentList[i][contentList[i].find("]")+2:]])  | 
|                     continue  | 
|                   | 
|                 rowInfoList.append([rowIndex, offset, rowState, contentList[i][contentList[i].find("]")+2:]])  | 
|       | 
|     except:  | 
|         LogEx(0, "Error--%s"%traceback.format_exc())  | 
|         return None  | 
|   | 
| #    print pyObj.GetTestReport("", 1, 1)  | 
|        | 
|     return pyObj  | 
|   | 
|   | 
| #-------------------------------------------------------------------------------  |