-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_to_db.py
More file actions
68 lines (59 loc) · 3.31 KB
/
json_to_db.py
File metadata and controls
68 lines (59 loc) · 3.31 KB
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
from db import DataBase
import os
import json
def convert_json_to_db(project_names: str):
"""
Converts the json files to a database
:param project_names: list of selected projects in /build/class_parser to convert to database
:return:
"""
for project_name in os.listdir("./build/class_parser"):
if project_name not in project_names:
continue
# create new database for every project for faster querying and avoiding conflicts between projects
db = DataBase()
db.reset()
db.create_tables()
# insert project
db.insert_project(str(project_name))
n_classes = len(os.listdir("./build/class_parser/" + project_name))
curr_class = 1
# loop over all classes
for class_name in os.listdir("./build/class_parser/" + project_name):
if os.path.exists("./build/class_parser/" + project_name + "/" + class_name + "/class.json") \
and os.path.exists("./build/class_parser/" + project_name + "/" + class_name + "/methods.json"):
class_file = open("./build/class_parser/" + project_name + "/" + class_name + "/class.json", "r")
method_file = open("./build/class_parser/" + project_name + "/" + class_name + "/methods.json", "r")
with class_file:
class_list = json.load(class_file)
with method_file:
method_list = json.load(method_file)
for class_dict in class_list:
# write to database
db.insert_class(
project_name,
class_dict.get("filepath"),
class_dict.get("class_identifier"),
class_dict.get("class_body")
)
for method_dict in method_list:
# write methods of class to database
db.insert_method(method_dict["method_identifier"],
class_dict["class_identifier"],
method_dict["method_full_text"])
methodId = db.get_method_id(method_dict["method_identifier"], class_dict["class_identifier"])
curr_class = 1
# create intra-project relations
for class_name in os.listdir("./build/class_parser/" + project_name):
if os.path.exists("./build/class_parser/" + project_name + "/" + class_name + "/class.json") \
and os.path.exists("./build/class_parser/" + project_name + "/" + class_name + "/methods.json"):
class_file = open("./build/class_parser/" + project_name + "/" + class_name + "/class.json", "r")
method_file = open("./build/class_parser/" + project_name + "/" + class_name + "/methods.json", "r")
with class_file:
class_list = json.load(class_file)
with method_file:
method_list = json.load(method_file)
for class_dict in class_list:
for method_dict in method_list:
source_method_id = db.get_method_id(method_dict["method_identifier"],
class_dict["class_identifier"])