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

4

5
6
import sys

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

17
18

#Were we intrept test logs into test dicts
19
def test_parser(string,current_group,current_test_set,current_set_guid,current_sub_set):
20
    test_list = {
Vincent Stehlé's avatar
Vincent Stehlé committed
21
      "name": string[2], #FIXME:Sometimes, SCT has name and Description,
22
23
      "result": string[1],
      "group": current_group,
Vincent Stehlé's avatar
Vincent Stehlé committed
24
      "test set": current_test_set,
25
26
      "sub set": current_sub_set,
      "set guid": current_set_guid,
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)
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
    current_group = "N/A"
    current_set = "N/A"
40
41
    current_set_guid = "N/A"
    current_sub_set = "N/A"
42
43
44
45
46

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

Vincent Stehlé's avatar
Vincent Stehlé committed
47
        #TODO:I can skip TERM, but I feel like "\n" should be able to be handled in the above list comprehension
48
49
50
51
52
53
        if split_line[0]=="TERM" or split_line[0]=="\n":
            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.
54
55
56
57
            try:
                current_group, current_set = split_line[8].split('\\')
            except:
                current_group, current_set =split_line[8],split_line
58
59
            current_set_guid = split_line[4]
            current_sub_set = split_line[6]
60

Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
61
        #FIXME:? EKL file has an inconsistent line structure,
62
63
64
        # 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] != " ":
65
66
67
68
            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
Jeff Booher-Kaeding's avatar
Jeff Booher-Kaeding committed
69
                tmp_dict = test_parser(split_test,current_group,current_set,current_set_guid,current_sub_set)
70
71
72
73
74
                temp_list.append(tmp_dict)
            except:
                print("Line:",split_line)
                sys.exit("your log may be corrupted")
    return temp_list
75

Jeff Booher-Kaeding's avatar
Jeff Booher-Kaeding committed
76
#Parse Seq file, used to tell which tests should run.
77
def seq_parser(file):
78
    temp_dict = list()
79
    lines=file.readlines()
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
80
    magic=7 #a test in a seq file is 7 lines, if not mod7, something wrong..
81
82
    if len(lines)%magic != 0:
        sys.exit("seqfile cut short, should be mod7")
83
84
85
86
87
88
89
90
    #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
91
        #(x+6)(utf-16 char)
92
        #currently only add tests that are supposed to run, should add all?
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
93
        #0xFFFFFFFF in "Iterations" means the test is NOT supposed to run
94
95
96
97
98
99
100
101
        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)
            }
102
            temp_dict.append(seq_dict) #put in a dict based on guid
103

104
    return temp_dict
105

106
107
108
109
#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
110
    temp_list = input_list.copy()
111
112
113
114
115
116
117
118
119
120
121
122
    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])
123
        dict_2_md(found,file)
Vincent Stehlé's avatar
Vincent Stehlé committed
124

125
126


127
128
129
#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
130
        file.write("\n\n")
131
132
133
134
        #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
135
            temp_string2 += ("---|")
136
137
138
139
140
141
142
        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
143
    #seprate table from other items in MD
144
    file.write("\n\n")
145

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

147
def main():
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
148
    #Command line argument 1, ekl file to open, else open sample
149
    log_file = sys.argv[1] if len(sys.argv) >= 2 else "sample.ekl"
150
    db1 = list() #"database 1" all tests.
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
151
152
153
154
    with open(log_file,"r",encoding="utf-16") as f: #files are encoded in utf-16
        db1 = ekl_parser(f.readlines())

    #Command line argument 2, seq file to open, else open sample
155
    seq_file = sys.argv[2] if len(sys.argv) >= 3 else "sample.seq"
156
    db2 = dict() #"database 2" all test sets that should run
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
157
    with open(seq_file,"r",encoding="utf-16") as f: #files are encoded in utf-16
158
        db2 = seq_parser(f)
Vincent Stehlé's avatar
Vincent Stehlé committed
159

Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
160
    #cross check is filled only with tests labled as "run" int the seq file
161
    cross_check = list()
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
162
    #combine a list of test sets that did not run for whatever reason.
163
    would_not_run = list()
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
164
    for x in db2: #for each "set guid" in db2
165
        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
166
        if bool(temp_dict): #if its not empty, apprend it to our dict
167
            cross_check = (cross_check +temp_dict)
Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
168
        else: #if it is empty, this test set was not run.
169
            would_not_run.append(x)
170

Vincent Stehlé's avatar
Vincent Stehlé committed
171

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

174
175
176
    failures = key_value_find(cross_check,"result","FAILURE")
    warnings = key_value_find(cross_check,"result","WARNING")
    passes = key_value_find(cross_check,"result","PASS")
177
178
179
180
181


    # generate MD summary
    with open('result.md', 'w') as resultfile:
        resultfile.write("# SCT Summary \n")
182
183
184
185
186
187
        resultfile.write("### 1. Dropped: "+str(len(would_not_run))+"\n")
        resultfile.write("### 2. Failures: "+str(len(failures))+"\n")
        resultfile.write("### 3. Warnings: "+str(len(warnings))+"\n")
        resultfile.write("### 4. Passes: "+str(len(passes))+"\n")
        resultfile.write("\n\n")

Jeff Booher-Kaeding's avatar
Jeff Booher-Kaeding committed
188
        resultfile.write("## 1. Silently dropped or missing")
189
190
        dict_2_md(would_not_run,resultfile)

191
        resultfile.write("## 4. Failure by group")
Vincent Stehlé's avatar
Vincent Stehlé committed
192
        resultfile.write("\n\n")
193
        key_tree_2_md(failures,resultfile,"group")
Vincent Stehlé's avatar
Vincent Stehlé committed
194

195

196
197
        resultfile.write("## 3. Warnings by group")
        resultfile.write("\n\n")
198
        key_tree_2_md(warnings,resultfile,"group")
199

Vincent Stehlé's avatar
Vincent Stehlé committed
200

Jeff Booher-Kaeding's avatar
V1.0?    
Jeff Booher-Kaeding committed
201
202
203
204
205
206
207
208
209
    #command line argument 3&4, key are to support a key & value search.
    #these will be displayed in CLI
    if len(sys.argv) >= 5:
        find_key = sys.argv[3]
        find_value = sys.argv[4]
        found = key_value_find(db1,find_key,find_value)
        #print the dict
        print("found:",len(found),"items with search constraints")
        for x in found:
210
            print(x["guid"],":",x["name"],"with",find_key,":",x[find_key])
211

212
main()