-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIary.py
More file actions
240 lines (170 loc) · 8.83 KB
/
Copy pathAPIary.py
File metadata and controls
240 lines (170 loc) · 8.83 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
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
from MethodTargeter import get_current_method_changes, get_API_churn_per_commit
from FileFollower import extract_methods
from Scoper import scope, add_root_path
from Commentator import get_commit_comments, get_method_tag_comments
from TargetMethod import TargetMethod
from CommonChangeTracker import get_affected_commits
from Linker import get_links
from Suggester import get_suggestion
from MethodGrabber import get_all_current_methods
import os
import json
import csv
def order_objects_by_file(object_list):
method_object_dict = {}
for method_object in object_list:
if not (method_object.repo_path in method_object_dict):
method_object_dict[method_object.repo_path] = [method_object]
else:
method_object_dict[method_object.repo_path].append(method_object)
return method_object_dict
def turn_dict_to_list(object_dict):
object_list = []
for path in object_dict:
for o in object_dict[path]:
object_list.append(o)
return object_list
def get_changed_methods(repo_dict, s_tag, e_tag):
if os.path.isfile('changed_methods'+e_tag+'.json'):
with open('changed_methods'+e_tag+'.json', 'r') as fp:
changes = json.load(fp)
else:
changes = get_current_method_changes(repo_dict, s_tag, e_tag)
with open('changed_methods'+e_tag+'.json', 'w') as fp:
json.dump(changes, fp)
return changes
def get_class_commits(repo_directory, s_tag, e_tag, method_objs):
if os.path.isfile('class_commits'+e_tag+'.json'):
with open('class_commits'+e_tag+'.json', 'r') as fp:
commits = json.load(fp)
else:
commits = extract_methods(repo_directory, s_tag, e_tag, method_objs)
with open('class_commits'+e_tag+'.json', 'w') as fp:
json.dump(commits, fp)
return commits
def get_method_tag_coms(repository_directory, start_tag, end_tag, rooted_method_objects):
if os.path.isfile('method_comments_at_tags'+end_tag+'.json'):
method_comments = []
with open('method_comments_at_tags'+end_tag+'.json', 'r') as fp:
json_methods = json.load(fp)
for method in json_methods:
item = TargetMethod('temp')
item.__dict__ = method
method_comments.append(item)
else:
method_comments = get_method_tag_comments(rooted_method_objects, repository_directory, start_tag, end_tag)
with open('method_comments_at_tags'+end_tag+'.json', 'w') as fp:
json.dump([o.__dict__ for o in method_comments], fp)
return method_comments
def save_affected_commits(repository_directory, end_tag, method_objects, class_commits):
output_file = 'method_objects'+end_tag+'.json'
if os.path.isfile(output_file):
method_list = []
with open(output_file,'r') as fp:
json_methods = json.load(fp)
for method in json_methods:
item = TargetMethod('temp')
item.__dict__ = method
method_list.append(item)
else:
method_object_dict = order_objects_by_file(method_objects)
method_results = get_affected_commits(method_object_dict, repository_directory, class_commits)
method_list = turn_dict_to_list(method_results)
with open(output_file, 'w') as fp:
json.dump([o.__dict__ for o in method_list], fp)
return method_list
def get_API_churn_per_com(repository_directory, start_tag, end_tag, class_commits_w_comments):
if os.path.isfile('Current_Commits'+end_tag+'.json'):
with open('Current_Commits'+end_tag+'.json', 'r') as fp:
commits = json.load(fp)
else:
commits = get_API_churn_per_commit(class_commits_w_comments, repository_directory)
with open('Current_Commits'+end_tag+'.json', 'w') as fp:
json.dump(commits, fp)
return commits
def get_final_results(end_tag, method_objects, class_commits, all_API_methods):
if os.path.isfile('apiary_results'+end_tag+'.json'):
with open('apiary_results'+end_tag+'.json', 'r') as fp:
results = json.load(fp)
else:
results = get_links(method_objects, class_commits, all_API_methods)
with open('apiary_results'+end_tag+'.json', 'w') as fp:
json.dump(results, fp)
return results
def get_all_methods(repo_directory, s_tag, e_tag):
if os.path.isfile("all_methods"+e_tag+".json"):
method_list = []
with open("all_methods" + e_tag + ".json", 'r') as fp:
json_methods = json.load(fp)
for method in json_methods:
item = TargetMethod('temp')
item.__dict__ = method
method_list.append(item)
else:
method_list = get_all_current_methods(repo_directory, s_tag, e_tag)
with open("all_methods"+e_tag+".json", 'w') as fp:
item = []
for o in method_list:
temp = o.__dict__
item.append(temp)
json.dump(item, fp)
return method_list
def save_suggestions(final_results, end_tag):
suggestions = get_suggestion(final_results)
with open("suggestions"+end_tag+".csv", 'w') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(["ID", "Kind", "Top Suggestion", "2nd", "3rd", "4th", "5th", "Links"])
for key, value in suggestions.items():
kind = suggestions[key].pop('Kind', None)
links = suggestions[key].pop('Links', None)
likely = suggestions[key].pop('Likely', None)
# making the list of likely have only unique entries
likely = list(set(likely))
sorted_list = []
remove_keys = []
for item in suggestions[key]:
if len(suggestions[key][item]) == 0:
remove_keys.append(item)
for item in remove_keys:
del suggestions[key][item]
for item in sorted(suggestions[key], key=lambda k: suggestions[key][k][-1]):
sorted_list.append(suggestions[key][item])
to_write = [key, kind]
# padding the list to make it the right length
sorted_list = (sorted_list + ['NA'] + ['NA'] + ['NA'] + ['NA'])[:4]
to_write.append(likely)
to_write.extend(sorted_list)
to_write.append(links)
writer.writerow(to_write)
def save_final_results(final_results, end_tag):
with open("final_results"+end_tag+".csv", 'w') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(["ID", "Kind", "Original_Doc_mention", "Final_Doc_mention", "Change in Doc", "Originally_called", "Finally_called:", "Commit_Link", "Commit_Message_Mention", "Implementation_changed_together"])
for item, value in final_results["Methods"].items():
writer.writerow([item,value["Kind"], value["Original_Doc_mention"], value["Final_Doc_mention"], value["Doc_diff"], value["Originally_called"], value["Finally_called:"], value["Commit_Link"], value["Commit_Message_Mention"], value["Implementation_changed_together"]])
def main():
repository_directory = '/Users/max/Desktop/Release7'
remote_repo_dir = '/home/release7/platform_frameworks_base'
if os.path.exists(remote_repo_dir):
repository_directory = remote_repo_dir
start_tag_number = '5.1.1' # input("Enter Start Tag Version number (with dots): ")
end_tag_number = '6.0.0' # input("Enter End Tag Version number (with dots): ")
end_tag = 'android-'+str(end_tag_number)+'_r1'
start_tag = 'android-'+str(start_tag_number)+'_r1'
print("Processing from: "+start_tag+" to " + end_tag)
method_changes = get_changed_methods(repository_directory, start_tag, end_tag)
all_API_methods = get_all_methods(repository_directory, start_tag, end_tag)
method_objects = scope(method_changes)
class_commits = get_class_commits(repository_directory, start_tag, end_tag, method_objects)
class_commits_w_comments = get_commit_comments(class_commits, repository_directory)
rooted_method_objects = add_root_path(method_objects, class_commits_w_comments)
method_objects_w_coms = get_method_tag_coms(repository_directory, start_tag, end_tag, rooted_method_objects)
churned_class_commits = get_API_churn_per_com(repository_directory, start_tag, end_tag, class_commits_w_comments)
print("Now getting final results: this part is the longest, saving/reload everyting in case of crash")
saved_method_objects = get_method_tag_coms(repository_directory, start_tag, end_tag, rooted_method_objects)
saved_churn = get_API_churn_per_com(repository_directory, start_tag, end_tag, class_commits_w_comments)
commited_method_objects = save_affected_commits(repository_directory, end_tag, saved_method_objects, saved_churn)
final_results = get_final_results(end_tag, commited_method_objects, churned_class_commits, all_API_methods)
save_suggestions(final_results, end_tag)
save_final_results(final_results, end_tag)
main()