forked from mfejzer/tracking_buggy_files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_import_graph_features.py
83 lines (63 loc) · 2.09 KB
/
project_import_graph_features.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
import collections as col
import networkx as nx
import pandas as pd
import sys
def subfinder(iset, fset, fname, iname):
'''Fast matching of package names in files paths'''
if len(fset.intersection(iset)) == len(iset):
if iname in fname:
return True
return False
def get_graph(filename):
# Get correct import names
imports_dict = {}
files_dict = {}
imports = col.defaultdict(list)
# read imports
with open(filename) as fd:
for line in fd:
sline = line.strip().split(":")
if len(sline) < 3: continue # empty line
node_name = sline[0].replace(".java","").replace("/",".")
# handle "import static" case
if "import static" in sline[2]:
value = '.'.join(sline[2].replace("import static ","").replace(";","").split(".")[0:-1])
else:
value = sline[2].replace("import ","").replace(";","")
imports_dict[value] = set(value.split("."))
files_dict[node_name] = set(node_name.split("."))
imports[node_name].append(value)
# remap filenames to imports
imports_fixed = col.defaultdict(list)
for kf, vf in files_dict.items():
for ki, vi in imports_dict.items():
if subfinder(vi, vf, kf, ki):
imports_fixed[ki] = imports[kf]
break
else:
imports_fixed[kf] = imports[kf]
G = nx.from_dict_of_lists(imports_fixed, nx.DiGraph())
return G
def process_filename(filename):
G = get_graph(filename)
return process_graph(G)
def process_graph(G):
n = {}
# Out nodes
n["out"] = dict(G.out_degree())
# In nodes
n["in"] = dict(G.in_degree())
# Pagerank
n["pr"] = nx.pagerank(G, alpha=0.9)
# Hubs and auths
n["h"], n["a"] = nx.hits(G, max_iter = 1000)
a = pd.DataFrame.from_dict(n)
return a
def main(filename):
a = process_filename(filename)
print(a.to_csv())
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage %s filename"%(argv[0]))
exit()
main(sys.argv[1])