-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalyzer.py
653 lines (593 loc) · 28.8 KB
/
analyzer.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
"""
Written by Debojit Kaushik (1st April 2018)
Script to analyze patch files and analyze what changes were made.
"""
import os, sys, traceback, time, re, json
try:
import requests
except ImportError:
print("Missing Dependency! Installing requests..")
os.system('pip3 install requests')
import requests
try:
import numpy as np
except ImportError:
print("Package 'numpy' missing..\nInstalling system-wide.")
os.system('sudo pip3 install numpy')
try:
import pygal
except Exception:
os.system('sudo pip3 install pygal')
import pygal
try:
from progress.bar import ChargingBar
except ImportError:
os.system('sudo pip3 install progress')
import ChargingBar
except Exception:
print(traceback.format_exc())
'''
List of keywords to check for in patches.
'''
KEYWORDS = [
'for',
'if',
'else:',
'while',
'try:',
'except',
'class',
'def',
'elif',
'import',
'raise',
'finally',
'del',
'assert',
'break'
]
pos_keys, neg_keys, extract= {}, {}, {}
PATCH_DUMP = []
CHANGE_TEMPLATE = {
'value': None,
'action': None,
'keywords': None,
'condition': None,
'loop_entity': None,
'exception_type': None,
'raise_condition': None
}
#Initiate the dictionaries.
for item in KEYWORDS:
pos_keys[item], neg_keys[item] = 0, 0
#Function to plot the values.
def plot_it(x, y, repo_name='no_name'):
# x: Added keys, y: Removed keys.
try:
bar_chart = pygal.Bar()
bar_chart.title = "Repository patch analysis"
bar_chart.x_labels = list(x.keys())
bar_chart.add("Additions", list(x.values()))
bar_chart.add("Deletions", list(y.values()))
file_name = './' + repo_name + '_analysis.svg'
bar_chart.render_to_file(file_name)
return file_name
except Exception:
print(traceback.format_exc())
patterns = ["if no_LHS_change (operator) no_RHS_change:",
"if no_LHS_change (operator) changed_RHS:",
"if no_LHS_change (operator_change) no_RHS_change:",
"if no_LHS_change (operator_change) changed_RHS:",
"if changed_LHS (operator) no_RHS_change:",
"if changed_LHS (operator) changed_RHS:",
"if changed_LHS (operator_change) no_RHS_change:",
"if changed_LHS (operator_change) changed_RHS:"]
def plot_repo_analysis(result_dict,repo):
try:
bar_chart = pygal.Bar()
bar_chart.title = "If pattern analysis"
bar_chart.x_labels = patterns
bar_chart.add("If Patterns", list(result_dict.values()))
bar_chart.render_to_file('./if_analysis_'+repo+'.svg')
except Exception:
print(traceback.format_exc())
class File:
'''
Class which captures file and establishes a File -> Changes heirarchy.
File <> Change relationship.
File (FKey) into Change.
** This is repository independent.
Constructor params:
file_name: name of the file. Effectively represents the file entity.
changes: change objects which represents culmination of channges wrt of a file.
'''
def __init__(self, **kwargs):
self.file_name = kwargs['file_name']
self.changes = kwargs['changes']
class Changes:
"""
Class which captures changes with respect to every keyword.
Serialize this class.
"""
def __init__(self, args):
self.action = args['action']
self.keyword = args['keyword']
self.conditions = args['condition']
self.value = args['value']
self.loop_entity = args['loop_entity']
self.exception_type = args['exception_type']
self.raise_condition = args['raise_condition']
class CodeAnalysis:
"""
Public class with different methods to analyze patch files.
Contains different methods to deal with analysis of patch files.
Class is specific to Python patch files only.
- frequency_analyzer():
Naive Method to analyze occurrance of different language
keywords over patches. Indicates Patches contain what changes from a very high level.
- change_analyzer():
Naive method to analyze changes in code and their propertie which
were added/removed. Generates a analysis sugar out of the patch files.
"""
patterns = ["if no_LHS_change (operator) no_RHS_change:",
"if no_LHS_change (operator) changed_RHS:",
"if no_LHS_change (operator_change) no_RHS_change:",
"if no_LHS_change (operator_change) changed_RHS:",
"if changed_LHS (operator) no_RHS_change:",
"if changed_LHS (operator) changed_RHS:",
"if changed_LHS (operator_change) no_RHS_change:",
"if changed_LHS (operator_change) changed_RHS:"]
pattern_look_up = {'LopR':"if no_LHS_change (operator) no_RHS_change:",
'Lop!R':"if no_LHS_change (operator) changed_RHS:",
'L!opR':"if no_LHS_change (operator_change) no_RHS_change:",
'L!op!R':"if no_LHS_change (operator_change) changed_RHS:",
'!LopR':"if changed_LHS (operator) no_RHS_change:",
'!Lop!R':"if changed_LHS (operator) changed_RHS:",
'!L!opR':"if changed_LHS (operator_change) no_RHS_change:",
'!L!op!R':"if changed_LHS (operator_change) changed_RHS:"
}
result_dict = {'LopR':0,'Lop!R':0,'L!opR':0,'L!op!R':0,'!LopR':0,'!Lop!R':0,'!L!opR':0,'!L!op!R':0}
def __str__(self):
print("<CodeAnalysis Class>")
@staticmethod
def frequency_analyzer(pat_lines):
"""
Method to perform frequency analysis on keywords occurring
over input patch file.
Input:
List of code-patch lines.
[
-,
+,
+,
-,
.
.
.
]
Returns:
None
Performs changes to global variable, pos_keys & neg_keys.
"""
try:
for item in pat_lines:
for item2 in item.strip().split()[1:]:
if item2 and item2 in KEYWORDS:
#Check for keyword in pre-defined list of keywords.
#Select non-empty lines based on
#first character being '+' or '-'.
if item.strip().split()[0] == "+":
pos_keys[item2] += 1
elif item.strip().split()[0] == "-":
neg_keys[item2] += 1
else:
pass
except Exception:
print(traceback.format_exc())
@staticmethod
def diff_extract(diff_lines):
try:
"""
Construct workable data structure.
Params:
diff_extract: list of lines from a patch file.
[
'+ line 1',
'+ line 2',
'- line 3'
.
.
.
'+/- line n'
]
Returns:
Dictionary of diff blocks of lines where every key is
a diff block line and value is a list of diff lines
{
'diff --git a/file b/file':[line1, line2, line3..line_n]
}
"""
key = 0
state = False
diff_blocks = {}
for item in diff_lines:
'''
Selects only file with .py extensions. Ignores readmes, Gitignores
and other none code artifacts.
For every time 'diff' is encountered, collect the successive lines
starting with "+/-" until next 'diff' is encountered, then redo the process
as the next diff block of lines.
State is flag variable taking care of the first diff encountered or not.
'''
if item.split(' ')[0] == 'diff' and re.match(r'.*.py', item.split(' ')[2]):
key = item
state = True
diff_blocks[key] = []
elif item.split(' ')[0] == 'diff' and re.match(r'.*.py', item.split(' ')[2]):
state = False
elif item.split(' ')[0] != 'diff' and state:
if item.split(' ')[0] in ['+', '-']:
diff_blocks[key].append(item[:1] + ' ' + item[1:].strip())
else:
pass
return diff_blocks
except Exception:
print(traceback.format_exc())
@staticmethod
def DumpGenerator(file_name, change_dict):
try:
'''
CHANGE_TEMPLATE: Change stub dictionnary.
Generates JSON for every change detected WRT to every
type of change.
Params:
file_name: File name of every change detected.
change_dict: Dictionary of change recorded
'''
global CHANGE_TEMPLATE
CHANGE_TEMPLATE = CHANGE_TEMPLATE.fromkeys(list(CHANGE_TEMPLATE.keys()), None)
for item in change_dict:
CHANGE_TEMPLATE[item] = change_dict[item]
ch = Changes(CHANGE_TEMPLATE)
return ch.__dict__
except Exception:
print(traceback.format_exc())
@staticmethod
def change_analyzer(diff_extract = []):
'''
Method to extract changes and generate a JSON.
Writes JSON dump into a file ('extract.json')
Returns:
None
'''
try:
global PATCH_DUMP
assert diff_extract
'''For every file.'''
for _file in diff_extract:
dump = []
'''Looping every file object in diff_extract'''
for diff in diff_extract[_file]:
if len(diff_extract[_file][diff]):
for line in diff_extract[_file][diff]:
if len(line.split()[1:]) > 1 :
'''
Conditional Statements to check for every case.
Checking Keywords:
if, elif, raise, except, for, while, import
Detects every keywords here for each line and extracts
properties using ReGex or string analysis.
'''
keyword = line.split()[1]
if keyword == 'if' or keyword == 'elif':
match_object = re.match(r'([elif]+)\s*(.*)]:', line[1:].strip())
if match_object:
# temp is a stub dictionary to generate Change().__dict__
#Generate Changes() and serialize it.
groups = match_object.groups()
temp = {}
#First element of every string denontes if the line was added or removed.
if line.split()[0] == '+':
temp['action'] = 'added'
elif line.split()[0] == '-':
temp['action'] = 'removed'
temp['keyword'] = groups[0]
temp['condition'] = groups[1]
dump.append(CodeAnalysis.DumpGenerator(_file ,temp))
elif keyword == 'for':
match_object = re.match(r'for\s*(.*)\s*in\s*(.*):', line[1:].strip())
if match_object:
#Generate Changes() and serialize it.
groups = match_object.groups()
temp = {}
if line.split()[0] == '+':
temp['action'] = 'added'
elif line.split()[0] == '-':
temp['action'] = 'removed'
temp['keyword'] = 'for'
temp['loop_entity'] = groups[1]
temp['value'] = groups[0]
dump.append(CodeAnalysis.DumpGenerator(_file ,temp))
elif keyword == 'while':
match_object = re.match(r'while\s*(.*):', line[1:].strip())
if match_object:
#Generate Changes() and serialize it.
groups = match_object.groups()
temp = {}
if line.split()[0] == '+':
temp['action'] = 'added'
elif line.split()[0] == '-':
temp['action'] = 'removed'
temp['keyword'] = 'while'
temp['condition'] = groups[0]
dump.append(CodeAnalysis.DumpGenerator(_file ,temp))
elif keyword == 'except':
match_object = re.match(r'except\s*([a-zA-Z0-9,.\s]+)\s*.*:', line[1:].strip())
if len(line[1:].strip().replace(':', '').split()) == 1:
exception_conditions = 'general'
else:
exception_conditions = line[1:].strip().replace(':', '').split()[1]
if match_object:
groups = match_object.groups()
temp = {}
if line.split()[0] == '+':
temp['action'] = 'added'
elif line.split()[0] == '-':
temp['action'] = 'removed'
temp['keyword'] = 'except'
temp['exception_type'] = exception_conditions
dump.append(CodeAnalysis.DumpGenerator(_file ,temp))
elif keyword == 'raise':
raise_conditions = line[1:].strip().replace(':', '').split()[1]
temp = {}
#Genreate Dictionary temp to make Changes() instance.
#Same process for every keyword detected.
if line.split()[0] == '+':
temp['action'] = 'added'
elif line.split()[0] == '-':
temp['action'] = 'removed'
temp['keyword'] = 'raise'
temp['raise_condition'] = raise_conditions
dump.append(CodeAnalysis.DumpGenerator(_file ,temp))
elif keyword == 'import':
import_statement = line[1:].strip().replace(':', '').split()[1]
temp = {}
if line.split()[0] == '+':
temp['action'] = 'added'
elif line.split()[0] == '-':
temp['action'] = 'removed'
temp['keyword'] = 'import'
temp['value'] = import_statement
dump.append(CodeAnalysis.DumpGenerator(_file ,temp))
else:
pass
if dump:
#If dump is a non-empty list, create File() class and append to
#Global dictionary of PATCH_DUMP with changes of the file instance as dump.
fl = File(file_name = _file, changes = dump)
PATCH_DUMP.append(fl.__dict__)
else:
dump = []
pass
#Dump JSON into file. If file exists then open and dump,
#If file doesn't exist create file and dump JSON after opening it.
try:
f = open('extract.json', 'w')
except Exception:
os.system('touch extract.json')
f = open('extract.json', 'w')
try:
f.write(json.dumps(PATCH_DUMP))
print("\033[1;32m...JSON dumped!\033[1;m")
except Exception:
print(traceback.format_exc())
except AssertionError:
print('\nBad Parameters. Please check parameters.')
except Exception:
print(traceback.format_exc())
@staticmethod
def analyze_if(patlines):
try:
'''
Result Dict is used to store one of the 9 possible patterns in if conditions between + and - lines
Result lookup dict is used to identify the pattern and increment the appropriate pattern in result dict
'''
result_lookup_dict = {'111':'LopR','110':'Lop!R','101':'L!opR','100':'L!op!R','011':'!LopR','010':'!Lop!R','001':'!L!opR','000':'!L!op!R'}
pat_lines = []
'''
Remove all the " and ' from the lines so that the detection of
pattern of change is not due to a change in the type of quote
'''
for line in patlines:
line = line.replace('\'','').replace('\"','')
pat_lines.append(line)
patlines = pat_lines
'''
We need to check if we have enough + and - lines containing a pattern --> if .*(operator).*:,
only then proceed to detect the particular change.
First we'll check the line for the pattern with regex and
then check if we have enough lines to analyze. If not we'll return
'''
new_patlines = []
if_regex = re.compile('if.*(>|<|<=|>=|!=|==).*:')
num_lines_with_if = 0
for line in patlines:
nline = line.replace('+','').replace('-','')
if(if_regex.match(nline.strip())):
new_patlines.append(line.strip())
num_lines_with_if = num_lines_with_if + 1
if(num_lines_with_if <=1):
# print("not enough lines with if")
return
lineDict = {}
# Creating a dictionary of lines and their corresponding state( addition -> + or removal -> -)
newLines = []
for line in new_patlines:
newline = line[1:]
lineDict[newline.strip()] = {'sign':line[0]}
newLines.append(newline.strip())
patlines = newLines
if(patlines == []):
# print("0 lines with if")
return
'''
Checking for the comparision operator in the of condition and recording the
respective operator and its location. Location of the operator helps in
detecting if LHS of the operator is different while comparing with another line
'''
comps = ['==','>=','<=','!=','>','<',]
for line in lineDict.keys():
for op in comps:
if(line.find(op) != -1):
lineDict[line]['op_loc'] = line.find(op)
lineDict[line]['op'] = op
break
'''
We now store lines with if pattern and +, and lines with if pattern and - in
respective lists. If any one of them have less than one, we don't have enough
data to compare between + and - lines. So we return. Else, we'll proceed.
'''
pLines = []
nLines = []
for line in lineDict.keys():
if(lineDict[line]['sign'] == '+'):
pLines.append(line)
else:
nLines.append(line)
if(len(pLines) >= 1 and len(nLines) >=1 ):
pass
else:
# print("not enough data to proceed")
return
'''
We'll take each addition line containing if pattern and check how different it is from each of the removal line
and see how different the lines are and which pattern it belongs to.
'''
for pLine in pLines:
for nLine in nLines:
res_pat = ''
# print(lineDict[pLine])
if(pLine[:lineDict[pLine]['op_loc']] == nLine[:lineDict[nLine]['op_loc']]):
res_pat = res_pat + '1'
# print("\n*******************************************************************\n")
# print(pLine + " and " + nLine + " has same LHS variables")
if(lineDict[pLine]['op'] == lineDict[nLine]['op']):
res_pat = res_pat + '1'
# print(pLine + " and " + nLine + " has same comparision operators")
else:
res_pat = res_pat + '0'
# print(pLine + " and " + nLine + " has different comparision operators")
p_op_loc = lineDict[pLine]['op_loc']
p_op_len = len(lineDict[pLine]['op'])
n_op_loc = lineDict[nLine]['op_loc']
n_op_len = len(lineDict[nLine]['op'])
if(pLine[p_op_loc+p_op_len:] == nLine[n_op_loc+n_op_len:]):
res_pat = res_pat + '1'
# print(pLine + " and " + nLine + " has same RHS evaluating parameter")
else:
res_pat = res_pat + '0'
# print(pLine + " and " + nLine + " has different RHS evaluating parameter")
else:
# print("\n*******************************************************************\n")
res_pat = res_pat + '0'
# print(pLine + " and " + nLine + " has different LHS variables")
if(lineDict[pLine]['op'] == lineDict[nLine]['op']):
res_pat = res_pat + '1'
# print(pLine + " and " + nLine + " has same comparision operators")
else:
res_pat = res_pat + '0'
# print(pLine + " and " + nLine + " has different comparision operators")
p_op_loc = lineDict[pLine]['op_loc']
p_op_len = len(lineDict[pLine]['op'])
n_op_loc = lineDict[nLine]['op_loc']
n_op_len = len(lineDict[nLine]['op'])
if(pLine[p_op_loc+p_op_len:] == nLine[n_op_loc+n_op_len:]):
res_pat = res_pat + '1'
# print(pLine + " and " + nLine + " has same RHS evaluating parameter")
else:
res_pat = res_pat + '0'
# print(pLine + " and " + nLine + " has different RHS evaluating parameter")
pat_cond = result_lookup_dict[res_pat]
# print(CodeAnalysis.result_dict)
CodeAnalysis.result_dict[pat_cond] = CodeAnalysis.result_dict[pat_cond] + 1
except Exception:
print(traceback.format_exc())
'''Routine entry point.'''
if __name__ == "__main__":
try:
dirs = os.listdir()
if 'Repos' not in dirs:
print('Please download patch files for repositories')
exit()
# Get the list of all the repositories so that we can iterate over patch files of the repositories
# and then analyze them.
repo_list = os.listdir('./Repos')
if repo_list == []:
print('Please download the patch files and run the script again')
exit()
metric_dict = {}
final_result = {}
for repo in repo_list:
if '.DS_Store' in repo:
continue
print('Analysing Repo: '+repo+'\n')
patch_files = os.listdir('./Repos/'+repo)
bar = ChargingBar("\033[1;33mProgress\033[1;m", max = len(patch_files))
curr_time = time.time()
for patch in patch_files:
patch_file = open('./Repos/'+repo+'/'+patch)
blob = patch_file.read().split('\n')
patlines = []
for line in blob:
if line.split(' ')[0] in ['+', '-']:
patlines.append(line)
bar.next()
CodeAnalysis.analyze_if(patlines)
bar.finish()
plot_repo_analysis(CodeAnalysis.result_dict,repo)
CodeAnalysis.result_dict = {'LopR':0,'Lop!R':0,'L!opR':0,'L!op!R':0,'!LopR':0,'!Lop!R':0,'!L!opR':0,'!L!op!R':0}
final_result[repo] = CodeAnalysis.result_dict
bar = ChargingBar("\033[1;33mProgress\033[1;m", max = len(patch_files))
curr_time = time.time()
print('\033[1;32m-------------------------------------------------------------------------\033[1;m', end = '\n')
print("\n\033[1;33mInitializing Change Analysis\033[1;m")
for item in patch_files:
patch_file = './Repos/'+repo+'/'+item
f = open(patch_file)
blob = f.read().split('\n')
ext = CodeAnalysis.diff_extract(blob)
if ext and len(list(ext.values())):
extract[item] = ext
else:
pass
bar.next()
bar.finish()
#Initiate Change Analyzer routine.
CodeAnalysis.change_analyzer(extract)
print('\n\033[1;32m-------------------------------------------------------------------------\033[1;m', end = '\n')
bar = ChargingBar("\033[1;33mProgress\033[1;m", max = len(patch_files))
print("\n\033[1;33mInitializing Frequency Analysis\033[1;m")
'''Frequency analysis.'''
for item in patch_files:
patch_file = './Repos/'+repo+'/'+item
f = open(patch_file)
blob = f.read().split('\n')
patlines = []
for item2 in blob:
if item2.split(' ')[0] in ['+', '-']:
patlines.append(item2)
CodeAnalysis.frequency_analyzer(patlines)
bar.next()
bar.finish()
print("\n\033[1;32mTime Elapsed:\033[1;m", (time.time() - curr_time)/60, 'mins')
print("\n\033[1;36mDo you want to plot the result of your analysis? [Y/N]\033[1;m", end = ' ')
plot = input()
if plot and plot.strip().lower() == 'y' or plot.strip().lower() == 'yes':
file_name = plot_it(pos_keys, neg_keys, repo)
print('\n\033[1;32mGenerated %s file. Please open with a suitable party.\033[1;m' %(file_name))
else:
print('\033[1;33mByypassing plotting.\033[1;m')
print("\n\033[1;35mPostive Keys:\033[1;m\n", pos_keys)
print("\n\033[1;35mNegative Keys:\033[1;m\n", neg_keys)
except Exception:
print(traceback.format_exc())