-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter_result.py
361 lines (276 loc) · 9.63 KB
/
filter_result.py
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import json, copy, sys, subprocess
from json2html import *
''' Helping Methods '''
with open(sys.argv[1]) as f:
data = json.load(f)
with open(sys.argv[2]) as f:
data2 = json.load(f)
def remove_line_info(locs):
if type(locs) == list:
for i in range(len(locs)):
loc = locs[i].rsplit(":", 1)[0] + ")"
locs[i] = loc
elif type(locs) == str:
locs = locs.rsplit(":", 1)[0] + ")"
return locs
def remove_description(exception):
if exception.find(":"):
exception = exception.split(":", 1)
return exception[0]
else:
return exception
def check_topmost_stack(loc1, loc2):
method1 = loc1.split(" ", 1)[1].rsplit("(")[0]
line1 = loc1.split(":")[1].split(")")[0]
method2 = loc2.split(" ", 1)[1].rsplit("(")[0]
line2 = loc2.split(":")[1].split(")")[0]
dic1 = {'line': line1, 'method': method1}
dic2 = {'line': line2, 'method': method2}
# print("methods: ", method1, method2)
# print("list: ", changed_method_list)
if method1 == method2:
for method in changed_method_list:
if method1 in method:
print("methods: ", method1, method2)
print("list: ", changed_method_list)
else:
return False
''' Heuristics '''
# exception type(+description), full stack, + line number
def heuristic1(case):
equal = False
for i in data['descriptions']:
if(i['exception']==case['exception']):
loc1 = i['location']
loc2 = case['location']
if loc1 == loc2:
equal = True
break
return equal
# exception type(+description), top-most, + line number
def heuristic2(case):
equal = False
for i in data['descriptions']:
if(i['exception']==case['exception']):
loc1 = i['location'][0]
loc2 = case['location'][0]
# if check_topmost_stack(loc1, loc2, changed_method_list):
# equal = True
# else:
# break
if loc1 == loc2:
equal = True
break
return equal
# exception type(+description), full stack, - line number
def heuristic3(case):
equal = False
for i in data['descriptions']:
if(i['exception']==case['exception']):
loc1 = remove_line_info(i['location'])
loc2 = remove_line_info(case['location'])
if loc1 == loc2:
equal = True
break
return equal
# exception type(+description), top-most, -line number
def heuristic4(case):
equal = False
for i in data['descriptions']:
if(i['exception']==case['exception']):
loc1 = remove_line_info(i['location'][0])
loc2 = remove_line_info(case['location'][0])
if loc1 == loc2:
equal = True
break
return equal
# exception type(+description), no stack
def heuristic5(case):
equal = False
for i in data['descriptions']:
if(i['exception']==case['exception']):
equal = True
return equal
# exception type(-description), full stack, + line number
def heuristic6(case):
equal = False
for i in data['descriptions']:
exception1 = remove_description(i['exception'][0])
exception2 = remove_description(case['exception'][0])
if(exception1 == exception2):
loc1 = i['location']
loc2 = case['location']
if loc1 == loc2:
equal = True
break
return equal
# exception type(-description), top-most, + line number
def heuristic7(case):
equal = False
for i in data['descriptions']:
exception1 = remove_description(i['exception'][0])
exception2 = remove_description(case['exception'][0])
if(exception1 == exception2):
loc1 = i['location'][0]
loc2 = case['location'][0]
if loc1 == loc2:
equal = True
break
return equal
# exception type(-description), full stack, - line number
def heuristic8(case):
equal = False
for i in data['descriptions']:
exception1 = remove_description(i['exception'][0])
exception2 = remove_description(case['exception'][0])
if(exception1 == exception2):
loc1 = remove_line_info(i['location'])
loc2 = remove_line_info(case['location'])
if loc1 == loc2:
equal = True
break
return equal
# exception type(-description), top-most, -line number
def heuristic9(case):
equal = False
for i in data['descriptions']:
exception1 = remove_description(i['exception'][0])
exception2 = remove_description(case['exception'][0])
if(exception1 == exception2):
loc1 = remove_line_info(i['location'][0])
loc2 = remove_line_info(case['location'][0])
if loc1 == loc2:
equal = True
break
return equal
# exception type(-description), no stack
def heuristic10(case):
equal = False
for i in data['descriptions']:
exception1 = remove_description(i['exception'][0])
exception2 = remove_description(case['exception'][0])
if(exception1 == exception2):
equal = True
return equal
'''
Argument explaination
python3 filter_result.py arg1 arg2 arg3 arg4 arg5 arg6 arg7
arg1: prev version json file(correct)
arg2: current version json file(buggy)
arg3: heuristic number(1~10)
arg4: prev commit hash(correct)
arg5: current commit hash(buggy)
arg6: path to source code directory
arg7: gerrit server address
arg8: number of result that will be shown(optional)
'''
def init_git_diff():
buggy_hash = sys.argv[5]
correct_hash = sys.argv[4]
target_path = sys.argv[6] # source code directory
diff_command = subprocess.Popen(["git", "diff", buggy_hash, correct_hash, target_path], stdout=subprocess.PIPE)
lines, err = diff_command.communicate()
lines = lines.decode('utf-8')
diff = lines.split('\n')
print("diff: ", diff)
return
result = []
diff_dic = dict() # @@로 시작하는 부분
block_dic = dict()
block = []
prev_cnt = 0
current_cnt = 0
for line in diff:
if 'diff' in line:
if block_dic:
block_dic['prev_line_cnt'] = prev_cnt
block_dic['current_line_cnt'] = current_cnt
diff_dic['block_list'].append(block_dic)
if diff_dic:
result.append(diff_dic)
# diff_dic init
diff_dic = dict()
diff_dic['block_list'] = list()
block_dic = dict()
method = line.rsplit(' ', 1)[1]
method = method.split('/', 1)[1]
method = method.replace("src/main/java/", "")
method = method.rsplit(".", 1)[0]
method = method.replace("/", ".")
diff_dic['method'] = method
elif '@@' in line:
if block_dic:
block_dic['prev_line_cnt'] = prev_cnt
block_dic['current_line_cnt'] = current_cnt
diff_dic['block_list'].append(block_dic)
# block dict init
block_dic = dict()
prev_cnt = 0
current_cnt = 0
prev_line_num = line.split(",", 1)[0].split("-")[1]
current_line_num = line.split("+")[1].split(",")[0]
block_dic['prev_line_start'] = prev_line_num
block_dic['current_line_start'] = current_line_num
elif line:
if line[0] == '-' and line[1] == ' ':
prev_cnt += 1
elif line[0] == '+' and line[1] == ' ':
current_cnt += 1
block_dic['prev_line_cnt'] = prev_cnt
block_dic['current_line_cnt'] = current_cnt
diff_dic['block_list'].append(block_dic)
result.append(diff_dic)
return result
def outputting_result(data):
result_list = []
i = 0
for datum in data:
if i == 3:
break
id_str = datum['id']
exception_str = datum['exception'][0]
loc_list = datum['location']
result_str = id_str + '\n' + exception_str + '\n'
for loc in loc_list:
result_str += '\t' + loc +'\n'
result_list.append(result_str)
i += 1
return result_list
def main():
cnt = 0
data3 = []
heuristic_key = 'heuristic' + sys.argv[3]
heuristic_dict = {
'heuristic1': heuristic1,
'heuristic2': heuristic2,
'heuristic3': heuristic3,
'heuristic4': heuristic4,
'heuristic5': heuristic5,
'heuristic6': heuristic6,
'heuristic7': heuristic7,
'heuristic8': heuristic8,
'heuristic9': heuristic9,
'heuristic10': heuristic10
}
diff_result = init_git_diff()
for i in data2['descriptions']:
# if not heuristic_dict[heuristic_key](i, changed_method_list):
if not heuristic4(i): # best heuristic
cnt+=1
data3.append(copy.deepcopy(i))
gerrit_server_address = sys.argv[7]
num_of_output = sys.argv[8]
if num_of_output.isdigit():
num_of_output = int(num_of_output)
result = outputting_result(data3)
if num_of_output:
num_of_output_final = min(len(result), num_of_output)
else:
num_of_output_final = len(result)
for i in range(num_of_output_final):
command1 = "ssh -p 29418 " + gerrit_server_address + " gerrit review —code-review 0 -m "
command2 = " $(git rev-parse HEAD)"
command = command1 + "cat << EOF\n" + result[i] + "EOF" + command2
subprocess.call(command, shell=True)
if __name__ == "__main__":
main()