-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUtil.py
400 lines (305 loc) · 12.1 KB
/
Util.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
'''
Created on Nov. 23, 2017
@author Andrew Habib
'''
import json
import os
from collections import OrderedDict, namedtuple
from xml.etree import cElementTree as ET
class DataReader(object):
def __init__(self, data_paths):
self.data_paths = data_paths
def __iter__(self):
for data_path in self.data_paths:
name = os.path.split(data_path)[1]
with open(data_path, 'r') as file:
content = file.readlines()
yield name, content
class XmlReader(object):
def __init__(self, data_paths):
self.data_paths = data_paths
def __iter__(self):
for data_path in self.data_paths:
name = os.path.split(data_path)[1]
with open(data_path, 'r') as file:
yield name.replace('.xml', ''), ET.iterparse(file)
class JsonReader(object):
def __init__(self, data_path):
self.data_path = data_path
def __iter__(self):
with open(self.data_path, 'r') as file:
entries = json.load(file)
for entry in entries:
yield entry
class JsonDataReader(object):
def __init__(self, data_paths):
self.data_paths = data_paths
def __iter__(self):
for data_path in self.data_paths:
name = os.path.split(data_path)[1]
if os.path.getsize(data_path) < 1:
yield name, None
else:
with open(data_path, 'r') as file:
entries = json.load(file)
for entry in entries:
yield name, entry
def load_json_list(json_file):
json_list = []
for entry in JsonReader(json_file):
json_list.append(entry)
return json_list
def get_list_of_uniq_jsons(lst):
uniq = []
for new in lst:
found = False
for old in uniq:
if new == old:
found = True
break
if not found:
uniq.append(new)
return uniq
class PrettyDict(dict):
def __str__(self):
return "{" + ", ".join("%r: %r\n" % (key, self[key]) for key in sorted(self)) + "}"
__repr__ = __str__
class ErrorproneMsg(object):
keys = [' Proj',
'Class',
' Type',
' Cat',
' Msg',
' Code',
' Mark',
' Line']
def __init__(self, proj, cls, typ, cat, msg, code, mark, line):
self.proj = proj
self.cls = cls
self.typ = typ
self.cat = cat
self.msg = msg
self.code = code
self.mark = mark
self.line = int(line)
self.values = [self.proj, self.cls, self.typ, self.cat,
self.msg, self.code, self.mark, self.line]
def __str__(self):
return("\n" + "\n".join(k + ": " + str(v) for (k, v) in zip(ErrorproneMsg.keys, self.values)) + "\n")
__repr__ = __str__
class SpotbugsMsg(object):
keys = [' Proj',
' Class',
' Cat',
' Abbrev',
' Type',
'Priority',
' Rank',
' Msg',
' Method',
' Field',
' Lines']
def __init__(self, proj, cls, cat, abbrev, typ, prio, rank, msg, mth, field, lines):
self.proj = proj
self.cls = cls
self.cat = cat
self.abbrev = abbrev
self.typ = typ
self.prio = prio
self.rank = rank
self.msg = msg
self.mth = mth
self.field = field
# lines could be list of tuples during serialization
# or list of lists during deserialization
# so construct namedtuples here instead of passing it from outside
# so that it works during deserialization also.
self.lines = []
for l in lines:
self.lines.append(SpotbugsSrcline(int(l[0]), int(l[1]), l[2]))
self.values = [self.proj, self.cls, self.cat, self.abbrev, self.typ, self.prio,
self.rank, self.msg, self.mth, self.field, self.lines]
def __str__(self):
return("\n" + "\n".join(k + ": " + str(v) for (k, v) in zip(SpotbugsMsg.keys, self.values)) + "\n")
__repr__ = __str__
def unrollLines(self):
lines = []
for l in self.lines:
lines.extend(range(l.start, l.end + 1))
return list(set(lines))
SpotbugsSrcline = namedtuple('SpotbugsSrcline', ['start', 'end', 'role'])
'''
InferIssue and InferBugTrace are slightly modified to cope
with the new json format in Infer 0.15.0
'''
class InferIssue(object):
# keys = ['bug_class', 'kind', 'bug_type', 'qualifier', 'severity', 'visibility', 'line',
# 'column', 'procedure', 'procedure_id', 'procedure_start_line', 'file', 'bug_trace',
# 'key', 'qualifier_tags', 'hash', 'bug_type_hum']
keys = ['bug_class', 'kind', 'bug_type', 'qualifier', 'severity', 'visibility', 'line',
'column', 'procedure', 'procedure_id', 'procedure_start_line', 'file', 'bug_trace',
'key', 'node_key', 'hash', 'bug_type_hum']
def __init__(self, bug_class, kind, bug_type, qualifier, severity, visibility,
line, column, procedure, procedure_id, procedure_start_line,
file, bug_trace, key, qualifier_tags, hashh, bug_type_hum):
self.bug_class = bug_class
self.kind = kind
self.bug_type = bug_type
self.qualifier = qualifier
self.severity = severity
self.visibility = visibility
self.line = line
self.column = column
self.procedure = procedure
self.procedure_id = procedure_id
self.procedure_start_line = procedure_start_line
self.file = file
self.bug_trace = []
for t in bug_trace:
self.bug_trace.append(InferBugTrace(*list(t[k] for k in InferBugTrace.keys)))
self.key = key
# self.qualifier_tags = qualifier_tags
self.hashh = hashh
self.bug_type_hum = bug_type_hum
self.values = [self.bug_class, self.kind, self.bug_type, self.qualifier,
self.severity, self.visibility, self.line, self.column,
self.procedure, self.procedure_id, self.procedure_start_line,
self.file, self.bug_trace, self.key,
# self.qualifier_tags,
self.hashh, self.bug_type_hum]
def __str__(self):
return("\n" + "\n".join(k + ": " + str(v) for (k, v) in zip(InferIssue.keys, self.values)) + "\n")
__repr__ = __str__
class InferBugTrace(object):
# keys = ['level', 'filename', 'line_number', 'column_number', 'description', 'node_tags']
keys = ['level', 'filename', 'line_number', 'column_number', 'description']
# def __init__(self, level, filename, line, column, desc, tags):
def __init__(self, level, filename, line, column, desc):
self.level = level
self.filename = filename
self.line = line
self.column = column
self.desc = desc
# self.tags = tags
# self.values = [self.level, self.filename, self.line, self.column, self.desc, self.tags]
self.values = [self.level, self.filename, self.line, self.column, self.desc]
def __str__(self):
return("\n" + "\n".join(k + ": " + str(v) for (k, v) in zip(InferBugTrace.keys, self.values)) + "\n")
__repr__ = __str__
class InferMsg(object):
keys = [' Proj',
' Class',
' Bug_Class',
' Kind',
' Bug_Type',
' Msg',
' Severity',
'Visibility',
' Lines',
' Procedure']
def __init__(self, proj, cls, bug_class, kind, bug_type, msg,
severity, visibility, lines, procedure):
self.proj = proj
self.cls = cls
self.bug_class = bug_class
self.kind = kind
self.bug_type = bug_type
self.msg = msg
self.severity = severity
self.visibility = visibility
self.lines = lines
self.procedure = procedure
self.values = [self.proj, self.cls, self.bug_class, self.kind, self.bug_type, self.msg,
self.severity, self.visibility, self.lines, self.procedure]
def __str__(self):
return("\n" + "\n".join(k + ": " + str(v) for (k, v) in zip(InferMsg.keys, self.values)))
__repr__ = __str__
class FileDiff(object):
keys = ['Project: ',
' Class: ',
' Lines: ']
def __init__(self, proj, cls, lines):
self.proj = proj
self.cls = cls
self.lines = set(int(i) for i in lines)
self.values = [self.proj, self.cls, self.lines]
def __str__(self):
return("\n" + "\n".join(k + str(v) for (k, v) in zip(FileDiff.keys, self.values)) + "\n")
__repr__ = __str__
class CustomEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, ErrorproneMsg):
return OrderedDict(zip(ErrorproneMsg.keys, o.values))
elif isinstance(o, InferIssue):
return OrderedDict(zip(InferIssue.keys, o.values))
elif isinstance(o, InferMsg):
return OrderedDict(zip(InferMsg.keys, o.values))
elif isinstance(o, SpotbugsMsg):
return OrderedDict(zip(SpotbugsMsg.keys, o.values))
elif isinstance(o, FileDiff):
return OrderedDict(zip(FileDiff.keys, o.values))
elif isinstance(o, set):
return list(o)
else:
json.JSONEncoder.default(self, o)
def load_parsed_diffs(diffs_file):
diffs_ = []
for diff in JsonReader(diffs_file):
inst = FileDiff(*list(diff[k] for k in FileDiff.keys))
diffs_.append(inst)
return diffs_
def load_parsed_ep(ep_file):
ep_res_ = []
for msg in JsonReader(ep_file):
inst = ErrorproneMsg(*list(msg[k] for k in ErrorproneMsg.keys))
ep_res_.append(inst)
return ep_res_
def load_parsed_sb(sb_file):
sb_res_ = []
for msg in JsonReader(sb_file):
inst = SpotbugsMsg(*list(msg[k] for k in SpotbugsMsg.keys))
sb_res_.append(inst)
return sb_res_
def load_parsed_inf(inf_file):
inf_res_ = []
for msg in JsonReader(inf_file):
inst = InferMsg(*list(msg[k] for k in InferMsg.keys))
inf_res_.append(inst)
return inf_res_
def find_msg_by_proj_and_cls(proj, cls, msgs):
found_messages = []
for m in msgs:
if m.proj == proj and m.cls == cls:
found_messages.append(m)
return found_messages
LineMatchesToMessages = namedtuple('LineMatchesToMessages', ['lines', 'messages'])
def get_cls_name_from_file_path(cls_path):
cls = None
if '/com/' in cls_path:
cls = 'com.' + cls_path.split('/com/')[1].replace('/', '.').replace('.java', '')
elif '/org/' in cls_path:
cls = 'org.' + cls_path.split('/org/')[1].replace('/', '.').replace('.java', '')
return cls
def prepare_tool(path, proj):
proj_dir = os.path.join(path, proj)
with open(os.path.join(proj_dir, 'prop-source-dir')) as file:
proj_src = file.read()
proj_src = os.path.join(proj_dir, proj_src)
with open(os.path.join(proj_dir, 'prop-compile-path')) as file:
proj_cp = file.read()
with open(os.path.join(proj_dir, 'prop-buggy-classes')) as file:
proj_buggy_classes = file.read().splitlines()
try:
with open(os.path.join(proj_dir, 'prop-exclude-classes')) as file:
proj_exclude_classes = file.read().splitlines()
except IOError:
proj_exclude_classes = []
proj_buggy_classes = set(proj_buggy_classes) - set(proj_exclude_classes)
proj_buggy_files = map(lambda f: os.path.join(proj_src, f.replace('.', '/') + '.java'), proj_buggy_classes)
try:
with open(os.path.join(proj_dir, 'prop-javac-options')) as file:
proj_javac_opts = file.read()
except IOError:
proj_javac_opts = ""
return proj_src, proj_cp, proj_javac_opts, proj_buggy_files, proj_buggy_classes
NO_WARNING = "NO_WARNING"