-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest5.py
152 lines (118 loc) · 3.76 KB
/
test5.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
f5 = open("callgraph.json","r")
f6 = open("callgraph_distinct.txt","w")
f7 = open("callgraph_full.txt","w")
f8 = open("callgraph_full2.txt","w")
lines5 = f5.readlines()
y = set(())
for i in range(0,len(lines5)):
x = lines5[i].replace(" ","").replace(" ","").replace(": [",",").replace('",',"").replace('"','')
if x not in ["}\n","{\n","]\n"]:
f7.write(x)
if x not in ["],\n","}\n","{\n","]\n"]:
y.add(x)
for i in y:
f6.write(i)
f6.write("\n")
f7.close()
f7 = open("callgraph_full.txt","r")
lines7 = f7.readlines()
for i in range(0,len(lines7)-1):
if lines7[i] != lines7[i+1]:
f8.write(lines7[i])
f8.write(lines7[len(lines7)-1])
f6.close()
def add(sample_dict, key, list_of_values):
if key not in sample_dict:
sample_dict[key] = list()
else:
sample_dict[key].extend(list_of_values)
return sample_dict
f1 = open("callgraph_distinct.txt","r")
f2 = open("callgraph_full2.txt","r")
f3 = open("callgraph_final.txt",'w')
lines1 = f1.readlines()
lines2 = f2.readlines()
l3 = list(lines1)
n1 = len(lines1)
n2 = len(lines2)
lines2 = reversed(lines2)
l2 = list(lines2)
sample_dict = {'*\n':[1]}
sample_dict2 = {'*\n':[1]}
for i in range(0,n2-1):
a=l2[i]
b=[l2[i+1]]
sample_dict = add(sample_dict, a,b)
ok = 1
for key in sample_dict:
sample_dict2[key] = list(set(sample_dict[key]))
if key not in ["*\n","],\n"]:
f3.write('name: "')
f3.write(key.replace("\n",""))
f3.write('",\n')
f3.write("children: [")
f3.write("\n")
x = str(sample_dict2[key]).replace("',",'", value: 11 },\n').replace("\\n","").replace("]","").replace("'\n'","")
y = x.replace("\n",'\n{ name: "').replace("['","").replace(" '","").replace("'","")
f3.write('{ name: "')
f3.write(y)
f3.write('", value: 11 }\n],')
f3.write("\n")
lst1 = []
for key in sample_dict:
sample_dict2[key] = list(set(sample_dict[key]))
for i in sample_dict2[key]:
x = str(key).replace("\n","")
y = str(i).replace("\n","")
tup1 = tuple([x,y])
lst1.append(tup1)
x = 0
lst = []
for i in range(1,65):
lst.append(lst1[i])
from collections import defaultdict
sample_dict3 = defaultdict()
for i in sample_dict2.keys():
sample_dict3[i.strip()]= list(filter(lambda y: y != "],", map(lambda x: str(x).strip(), sample_dict2[i])))
del sample_dict3["*"]
del sample_dict3["],"]
roots = ["MASTER"]
_str_end = ""
def traverse(_dict, root, depth, lastChild = False):
global _str_end
_str_end += "{\n\"name\" : \"" + root + "\",\n"
_str_end += "\"value\" : 0,\n"
_str_end += "\"path\" : \"\","
_str_end += "\"depth\" : " + str(depth) + ",\n"
_str_end += "\"children\" : [\n"
depth += 1
for index, node in enumerate(_dict[root]):
traverse(_dict, node, depth, index == len(_dict) - 1)
depth -= 1
_str_end += "]\n},\n"
import re
def clean_json(string):
string = re.sub(",[ \t\r\n]+}", "}", string)
string = re.sub(",[ \t\r\n]+\]", "]", string)
return string
traverse(sample_dict3, "MASTER", 0)
with open("result.json", "w+") as result:
result.write(clean_json(_str_end)[:-2])
f1.close()
f2.close()
f3.close()
f10 = open("callgraph_final.txt",'r')
f11 = open("callgraph.txt",'w') #here is the final txt file
lines10 = f10.readlines()
#must delete the files created and used to obtain the final one
for i in lines10:
if i not in ['{ name: ",", value: 11 },\n','{ name: "[", value: 11 },\n', '{ name: "[", value: 11 }\n', '{ name: ",", value: 11 }\n']:
f11.write(str(i))
f10.close()
f11.close()
#must delete the files created and used to obtain the final one
import os
os.remove("callgraph_final.txt")
os.remove("callgraph_distinct.txt")
os.remove("callgraph_full2.txt")
os.remove("callgraph_full.txt")