hch
2021-05-04 1a0ad714b01722f0a1c9bc4777b8e0d11ed15e01
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# -*- 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
 
 
#-------------------------------------------------------------------------------