Parser.py 9.66 KB
Newer Older
1
#!/usr/bin/env python3
2
3
#SCT log parser

4

5
import sys
Vincent Stehlé's avatar
Vincent Stehlé committed
6
import argparse
Vincent Stehlé's avatar
Vincent Stehlé committed
7
import csv
Vincent Stehlé's avatar
Vincent Stehlé committed
8

9

10
#based loosley on https://stackoverflow.com/a/4391978
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
11
# returns a filtered dict of dicts that meet some Key-value pair.
12
# I.E. key="result" value="FAILURE"
13
def key_value_find(list_1, key, value):
14
    found = list()
15
    for test in list_1:
16
        if test[key] == value:
17
            found.append(test)
18
    return found
19

20
21

#Were we intrept test logs into test dicts
22
def test_parser(string, current):
23
    test_list = {
Vincent Stehlé's avatar
Vincent Stehlé committed
24
      "name": string[2], #FIXME:Sometimes, SCT has name and Description,
25
      "result": string[1],
26
      **current,
27
      "guid": string[0], #FIXME:GUID's overlap
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
28
      #"comment": string[-1], #FIXME:need to hash this out, sometime there is no comments
29
      "log": ' '.join(string[3:])
30
    }
31
    return test_list
Vincent Stehlé's avatar
Vincent Stehlé committed
32

33
#Parse the ekl file, and create a map of the tests
34
35
def ekl_parser (file):
    #create our "database" dict
36
    temp_list = list()
37
    #All tests are grouped by the "HEAD" line the procedes them.
38
39
40
41
42
43
    current = {
        'group': "N/A",
        'test set': "N/A",
        'sub set': "N/A",
        'set guid': "N/A",
    }
44
45

    for line in file:
Vincent Stehlé's avatar
Vincent Stehlé committed
46
47
48
49
50
51
52
        # Strip the line from trailing whitespaces
        line = line.rstrip()

        # Skip empty line
        if line == '':
            continue

53
54
55
        #strip the line of | & || used for sepration
        split_line = [string for string in line.split('|') if string != ""]

Vincent Stehlé's avatar
Vincent Stehlé committed
56
57
        # Skip TERM
        if split_line[0] == "TERM":
58
59
60
61
62
            continue

        #The "HEAD" tag is the only indcation we are on a new test set
        if split_line[0]=="HEAD":
            #split the header into test group and test set.
63
            try:
64
                group, Set = split_line[8].split('\\')
65
            except:
66
67
68
69
70
71
                group, Set = '', split_line[8]
            current = {
                'group': group,
                'test set': Set,
                'sub set': split_line[6],
                'set guid': split_line[4],
72
73
74
75
76
77
                'iteration': split_line[1],
                'start date': split_line[2],
                'start time': split_line[3],
                'revision': split_line[5],
                'descr': split_line[7],
                'device path': split_line[9],
78
            }
79

Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
80
        #FIXME:? EKL file has an inconsistent line structure,
81
82
83
        # sometime we see a line that consits ' dump of GOP->I\n'
        #easiest way to skip is check for blank space in the first char
        elif split_line[0][0] != " ":
84
85
86
87
            try:
                #deliminiate on ':' for tests
                split_test = [new_string for old_string in split_line for new_string in old_string.split(':')]
                #put the test into a dict, and then place that dict in another dict with GUID as key
88
                tmp_dict = test_parser(split_test, current)
89
90
91
92
93
                temp_list.append(tmp_dict)
            except:
                print("Line:",split_line)
                sys.exit("your log may be corrupted")
    return temp_list
94

Jeff Booher-Kaeding's avatar
Jeff Booher-Kaeding committed
95
#Parse Seq file, used to tell which tests should run.
96
def seq_parser(file):
97
    temp_dict = list()
98
    lines=file.readlines()
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
99
    magic=7 #a test in a seq file is 7 lines, if not mod7, something wrong..
100
101
    if len(lines)%magic != 0:
        sys.exit("seqfile cut short, should be mod7")
102
103
104
105
106
107
108
109
    #the utf-16 char makes this looping a bit harder, so we use x+(i) where i is next 0-6th
    for x in range(0,len(lines),magic): #loop ever "7 lines"
        #(x+0)[Test Case]
        #(x+1)Revision=0x10000
        #(x+2)Guid=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
        #(x+3)Name=InstallAcpiTableFunction
        #(x+4)Order=0xFFFFFFFF
        #(x+5)Iterations=0xFFFFFFFF
Vincent Stehlé's avatar
Vincent Stehlé committed
110
        #(x+6)(utf-16 char)
111
        #currently only add tests that are supposed to run, should add all?
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
112
        #0xFFFFFFFF in "Iterations" means the test is NOT supposed to run
113
114
115
116
117
118
119
120
        if not "0xFFFFFFFF" in lines[x+5]:
            seq_dict = {
                "name": lines[x+3][5:-1],#from after "Name=" to end (5char long)
                "guid": lines[x+2][5:-1],#from after"Guid=" to the end, (5char long)
                "Iteration": lines[x+5][11:-1],#from after "Iterations=" (11char long)
                "rev": lines[x+1][9:-1],#from after "Revision=" (9char long)
                "Order": lines[x+4][6:-1]#from after "Order=" (6char long)
            }
121
            temp_dict.append(seq_dict) #put in a dict based on guid
122

123
    return temp_dict
124

125
126
127
128
#group items by key, and print by key
#we slowly iterate through the list, group and print groups
def key_tree_2_md(input_list,file,key):
    #make a copy so we don't destroy the first list.
Vincent Stehlé's avatar
Vincent Stehlé committed
129
    temp_list = input_list.copy()
130
131
132
133
134
135
136
137
138
139
140
141
    while temp_list:
        test_dict = temp_list.pop()
        found, not_found = [test_dict],[]
        #go through whole list looking for key match
        while temp_list:
            next_dict = temp_list.pop()
            if next_dict[key] == test_dict[key]: #if match add to found
                found.append(next_dict)
            else: #else not found
                not_found.append(next_dict)
        temp_list = not_found #start over with found items removed
        file.write("### " + test_dict[key])
142
        dict_2_md(found,file)
Vincent Stehlé's avatar
Vincent Stehlé committed
143

144
145


146
147
148
#generic writer, takes a list of dicts and turns the dicts into an MD table.
def dict_2_md(input_list,file):
    if len(input_list) > 0:
Jeff Booher-Kaeding's avatar
Jeff Booher-Kaeding committed
149
        file.write("\n\n")
150
151
152
153
        #create header for MD table using dict keys
        temp_string1, temp_string2 = "|", "|"
        for x in (input_list[0].keys()):
            temp_string1 += (x + "|")
Jeff Booher-Kaeding's avatar
Jeff Booher-Kaeding committed
154
            temp_string2 += ("---|")
155
156
157
158
159
160
161
        file.write(temp_string1+"\n"+temp_string2+"\n")
        #print each item from the dict into the table
        for x in input_list:
            test_string = "|"
            for y in x.keys():
                test_string += (x[y] + "|")
            file.write(test_string+'\n')
Vincent Stehlé's avatar
Vincent Stehlé committed
162
    #seprate table from other items in MD
163
    file.write("\n\n")
164

Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
165

Vincent Stehlé's avatar
Vincent Stehlé committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Generate csv
def gen_csv(cross_check, filename):
    # Find keys
    keys = set()

    for x in cross_check:
        keys = keys.union(x.keys())

    # Write csv
    with open(filename, 'w', newline='') as csvfile:
        writer = csv.DictWriter(
            csvfile, fieldnames=sorted(keys), delimiter=';')
        writer.writeheader()
        writer.writerows(cross_check)


182
def main():
Vincent Stehlé's avatar
Vincent Stehlé committed
183
184
185
186
187
    parser = argparse.ArgumentParser(
        description='Process SCT results.'
                    ' This program takes the SCT summary and sequence files,'
                    ' and generates a nice report in mardown format.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
Vincent Stehlé's avatar
Vincent Stehlé committed
188
    parser.add_argument('--csv', help='Output .csv filename')
Vincent Stehlé's avatar
Vincent Stehlé committed
189
190
191
192
193
194
195
196
197
198
199
200
201
    parser.add_argument(
        '--md', help='Output .md filename', default='result.md')
    parser.add_argument(
        'log_file', nargs='?', default='sample.ekl',
        help='Input .ekl filename')
    parser.add_argument(
        'seq_file', nargs='?', default='sample.seq',
        help='Input .seq filename')
    parser.add_argument('find_key', nargs='?', help='Search key')
    parser.add_argument('find_value', nargs='?', help='Search value')
    args = parser.parse_args()

    #Command line argument 1, ekl file to open
202
    db1 = list() #"database 1" all tests.
Vincent Stehlé's avatar
Vincent Stehlé committed
203
    with open(args.log_file,"r",encoding="utf-16") as f: #files are encoded in utf-16
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
204
205
        db1 = ekl_parser(f.readlines())

Vincent Stehlé's avatar
Vincent Stehlé committed
206
    #Command line argument 2, seq file to open
207
    db2 = dict() #"database 2" all test sets that should run
Vincent Stehlé's avatar
Vincent Stehlé committed
208
    with open(args.seq_file,"r",encoding="utf-16") as f: #files are encoded in utf-16
209
        db2 = seq_parser(f)
Vincent Stehlé's avatar
Vincent Stehlé committed
210

Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
211
    #cross check is filled only with tests labled as "run" int the seq file
212
    cross_check = list()
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
213
    #combine a list of test sets that did not run for whatever reason.
214
    would_not_run = list()
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
215
    for x in db2: #for each "set guid" in db2
216
        temp_dict = key_value_find(db1,"set guid",x["guid"])#find tests in db1 with given set guid
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
217
        if bool(temp_dict): #if its not empty, apprend it to our dict
218
            cross_check = (cross_check +temp_dict)
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
219
        else: #if it is empty, this test set was not run.
220
            would_not_run.append(x)
221

Vincent Stehlé's avatar
Vincent Stehlé committed
222

Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
223
    #search for failures and warnings & passes,
Vincent Stehlé's avatar
Vincent Stehlé committed
224

225
226
227
    failures = key_value_find(cross_check,"result","FAILURE")
    warnings = key_value_find(cross_check,"result","WARNING")
    passes = key_value_find(cross_check,"result","PASS")
228
229

    # generate MD summary
Vincent Stehlé's avatar
Vincent Stehlé committed
230
    with open(args.md, 'w') as resultfile:
231
232
233
234
235
236
237
        resultfile.write("# SCT Summary \n\n")
        resultfile.write("|  |  |\n")
        resultfile.write("|--|--|\n")
        resultfile.write("|Dropped:|" + str(len(would_not_run)) + "|\n")
        resultfile.write("|Failures:|" + str(len(failures)) + "|\n")
        resultfile.write("|Warnings:|" + str(len(warnings)) + "|\n")
        resultfile.write("|Passes:|" + str(len(passes)) + "|\n")
238
239
        resultfile.write("\n\n")

Jeff Booher-Kaeding's avatar
Jeff Booher-Kaeding committed
240
        resultfile.write("## 1. Silently dropped or missing")
241
242
        dict_2_md(would_not_run,resultfile)

243
        resultfile.write("## 4. Failure by group")
Vincent Stehlé's avatar
Vincent Stehlé committed
244
        resultfile.write("\n\n")
245
        key_tree_2_md(failures,resultfile,"group")
Vincent Stehlé's avatar
Vincent Stehlé committed
246

247

248
249
        resultfile.write("## 3. Warnings by group")
        resultfile.write("\n\n")
250
        key_tree_2_md(warnings,resultfile,"group")
251

Vincent Stehlé's avatar
Vincent Stehlé committed
252
253
254
    # Generate csv if requested
    if args.csv is not None:
        gen_csv(cross_check, args.csv)
Vincent Stehlé's avatar
Vincent Stehlé committed
255

Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
256
257
    #command line argument 3&4, key are to support a key & value search.
    #these will be displayed in CLI
Vincent Stehlé's avatar
Vincent Stehlé committed
258
259
    if args.find_key is not None and args.find_value is not None:
        found = key_value_find(db1, args.find_key, args.find_value)
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
260
261
262
        #print the dict
        print("found:",len(found),"items with search constraints")
        for x in found:
Vincent Stehlé's avatar
Vincent Stehlé committed
263
264
265
266
            print(
                x["guid"], ":", x["name"], "with", args.find_key, ":",
                x[args.find_key])

267

268
main()