-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormat.py
211 lines (185 loc) · 9.62 KB
/
Format.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
import argparse
import csv
import json
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--tool", type=str, required=True, choices=['enre', 'understand', 'sourcetrail', 'depends', 'code2graph'],
help="choose the tool you used, eg: understand, enre, depends...")
parser.add_argument("-e", "--entityInput", type=str, required=True, help="please input the input entity file path")
parser.add_argument("-d", "--dependencyInput", type=str, required=True, help="please input the input dependency file path")
parser.add_argument("-p", "--projectname", type=str, required=True, help="please input the project name")
parser.add_argument("-o", "--output", type=str, required=True, help="please input the output file path")
args = parser.parse_args()
return args.tool, args.entityInput, args.dependencyInput, args.projectname, args.output
def output_file(cell: dict, json_path: str, projectname:str, type:str):
info = dict()
info["schemaVersion"] = 1.0
info[type] = cell
info['projectName'] = projectname
entity_str = json.dumps(info, indent=4)
with open(json_path, 'w') as json_file:
json_file.write(entity_str)
def Entity(entityID, entityName, entityType, entityFile = None, startLine = -1, startColumn = -1, endLine = -1, endColumn = -1):
entity = dict()
entity['entityID'] = entityID
entity['entityName'] = entityName
entity['entityType'] = entityType
entity['entityFile'] = entityFile
entity['startLine'] = startLine
entity['startColumn'] = startColumn
entity['endLine'] = endLine
entity['endColumn'] = endColumn
return entity
def Dependency(dependencyType, dependencySrcID, dependencydestID, startLine = -1, startColumn = -1, endLine = -1, endColumn = -1):
dependency = dict()
dependency['dependencyType'] = dependencyType
dependency['dependencySrcID'] = dependencySrcID
dependency['dependencyDestID'] = dependencydestID
dependency['startLine'] = startLine
dependency['startColumn'] = startColumn
dependency['endLine'] = endLine
dependency['endColumn'] = endColumn
return dependency
def enre_format(path: str, projectname:str, output:str):
with open(path, 'r', encoding='utf-8') as understand_file:
enre_result = json.load(understand_file)
nodes = enre_result['variables']
edges = enre_result['cells']
node_count = list()
edge_count = list()
for node in nodes:
if node['external'] == True:
break
node_count.append(Entity(node['id'], node['qualifiedName'], node['category']))
for edge in edges:
values = edge['values']
for value in values.keys():
type = value
edge_count.append(Dependency(type, edge['src'], edge['dest']))
output_file(node_count, output+"/enre_"+projectname+"_entity.json", projectname, "entity")
output_file(edge_count, output+"/enre_"+projectname+"_dependency.json", projectname, "dependency")
def understand_format(entity_path: str, dependency_path:str, projectname:str, output:str):
with open(dependency_path, 'r', encoding='utf-8') as understand_file:
understand_edge = json.load(understand_file)
edges = understand_edge['cells']
edge_count = list()
for edge in edges:
details = edge['details']
for detail in details:
src = detail['src']
dest = detail['dest']
edge_count.append(Dependency(detail['type'], src['object'], dest['object']))
output_file(edge_count, output + "/understand_" + projectname + "_dependency.json", projectname, "dependency")
def sourcetrail_format(entity_path: str, dependency_path:str, projectname:str, output:str):
entity_dict = {"1": "symbol", "4": "built-in", "16": "namespace", "32": "package",
"128": "public class", "256": "interface", "512": "Annotation",
"1024": "global variable", "2048": "field", "4096": "function",
"8192": "method", "16384": "enum", "32768": "enumerator", "65536": "typedef",
"131072": "class", "262144": "file", "524144": "macro", "1048576": "union"}
dependency_dict = {"1": "scope resolve", "2": "type use", "4": "use", "8": "call",
"16": "extend", "32": "override", "64": "type argument",
"256": "include", "512": "import", "2048": "macro use",
"4096": "annotation use"}
csvFile = open(entity_path, "r")
dict_reader = csv.DictReader(csvFile)
node_list = list()
for row in dict_reader:
if row['serialized_name'].__contains__(" s p"):
row['serialized_name'] = row['serialized_name'].replace(" s p", "")
if row['serialized_name'].__contains__("/ m"):
row['serialized_name'] = row['serialized_name'].replace("/ m", "")
if row['serialized_name'].__contains__(":: m.:main:."):
row['serialized_name'] = row['serialized_name'].replace(":: m.:main:.", "")
if row['serialized_name'].__contains__(":: m"):
row['serialized_name'] = row['serialized_name'].replace(":: m", "")
if row['serialized_name'].__contains__("\tn"):
row['serialized_name'] = row['serialized_name'].replace("\tn", ".")
if row['serialized_name'].__contains__("\tm"):
row['serialized_name'] = row['serialized_name'].replace("\tm", "")
if row['serialized_name'].__contains__("\ts"):
row['serialized_name'] = row['serialized_name'].replace("\ts", "")
if row['serialized_name'].__contains__("\tp"):
row['serialized_name'] = row['serialized_name'].replace("\tp", "")
if row['type'] in entity_dict.keys():
type = entity_dict[row['type']]
else:
type = None
print(row['type'])
print(row['serialized_name'])
node_list.append(Entity(int(row['id']), row['serialized_name'], type))
output_file(node_list, output + "/sourcetrail_" + projectname + "_entity.json", projectname, "entity")
csvFile = open(dependency_path, "r")
dict_reader = csv.DictReader(csvFile)
edge_list = list()
for row in dict_reader:
if row['type'] in dependency_dict.keys():
type = dependency_dict[row['type']]
else:
type = None
print(row['type'])
print(row)
edge_list.append(Dependency(type, int(row['source_node_id']), int(row['target_node_id'])))
output_file(edge_list, output + "/sourcetrail_" + projectname + "_dependency.json", projectname, "dependency")
def depends_format(entity_path: str, dependency_path:str, projectname:str, output:str):
with open(entity_path, 'r', encoding='utf-8') as txtfile:
nodes = txtfile.read()
node_list = nodes.split("\n")
node_count = list()
for node in node_list:
node_info = node.split("/")
if len(node_info) >= 3:
type = node_info[2].split(".")[-1]
type = type.replace("Entity", "")
node_count.append(Entity(int(node_info[0]), node_info[1], type))
output_file(node_count, output + "/depends_" + projectname + "_entity.json", projectname, "entity")
with open(dependency_path, 'r', encoding='utf-8') as depends_file:
depends_edge = json.load(depends_file)
edge_count = list()
for cell in depends_edge['cells']:
values = cell['values']
for value in values.keys():
edge_count.append(Dependency(value, cell['src'], cell['dest']))
output_file(edge_count, output + "/depends_" + projectname + "_dependency.json", projectname, "dependency")
def code2graph_format(entity_path: str, dependency_path:str, projectname:str, output:str):
f = open(entity_path, encoding="utf-8")
file_text = f.read()
f.close()
lines = file_text.split("\n")
entityList = list()
dependencyList = list()
for line in lines:
if len(line) > 11:
if (line.__contains__("->")) & ("id=" not in line):
srcID = int(line[2:line.find("-") - 1])
destID = int(line[line.find(">") + 2:line.find("[") - 1])
indexes = line.split("\"")
type = None
for i in range(len(indexes)):
if indexes[i].__contains__("type="):
type = indexes[i + 1]
dependencyList.append(Dependency(type, srcID, destID))
else:
indexes = line.split("\"")
entityName = None
for i in range(len(indexes)):
if indexes[i].endswith("id="):
entityId = int(indexes[i + 1])
if indexes[i].__eq__(" uri="):
entityName = indexes[i + 1]
if indexes[i].__eq__(" type="):
entityType = indexes[i + 1]
entityList.append(Entity(entityId, entityName, entityType))
output_file(entityList, output + "/code2graph_" + projectname + "_entity.json", projectname, "entity")
output_file(dependencyList, output + "/code2graph_" + projectname + "_dependency.json", projectname, "dependency")
if __name__ == "__main__":
tool, entityInput, dependencyInput, projectname, output = parse_args()
if tool == "enre":
enre_format(entityInput, projectname, output)
if tool == "understand":
understand_format(entityInput, dependencyInput, projectname, output)
if tool == "sourcetrail":
sourcetrail_format(entityInput, dependencyInput, projectname, output)
if tool == "depends":
depends_format(entityInput, dependencyInput, projectname, output)
if tool == "code2graph":
code2graph_format(entityInput, dependencyInput, projectname, output)