-
Notifications
You must be signed in to change notification settings - Fork 4
/
script_AnalyzeRepos.py
313 lines (255 loc) · 10.2 KB
/
script_AnalyzeRepos.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/python3
import os
import subprocess
import sys
import re
import time
from collections import Counter
import json
from os import path, scandir
from Code.TypeErrors.TypeAnnotationCounter import count_type_annotations
import config
repos_base_dir = config.ROOT_DIR + "/GitHub/"
results_base_dir = config.ROOT_DIR + "/Resources/Output_typeErrors/"
def find_all_projects():
projects = []
for e in scandir(repos_base_dir):
if e.is_dir():
projects.append(e.name)
return projects
projects = find_all_projects()
def invoke_cmd(cmd, cwd):
global out
try:
# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before exec() to run the shell.
proc = subprocess.Popen(cmd.split(" "), cwd=cwd, stdout=subprocess.PIPE, preexec_fn=os.setsid)
pid = proc.pid
out = proc.communicate(timeout=180)[0]
out = out.decode(sys.stdout.encoding)
except subprocess.TimeoutExpired as e:
pass
# except subprocess.CalledProcessError as e:
# # handle exit codes, e.g., when type checker reports warnings
# out = e.output.decode(sys.stdout.encoding)
return out
def init_pyre(repo_dir):
if not path.isfile(repo_dir+"/.pyre_configuration"):
print(f"Creating pyre configuration for {repo_dir}")
subprocess.run(
f"cp data/.pyre_configuration {repo_dir}".split(" "))
else:
print(f"Reusing existing pyre configuration for {repo_dir}")
def invoke_cmd(cmd, cwd):
try:
out = subprocess.check_output(
cmd.split(" "), cwd=cwd)
out = out.decode(sys.stdout.encoding)
except subprocess.CalledProcessError as e:
# handle exit codes, e.g., when type checker reports warnings
out = e.output.decode(sys.stdout.encoding)
return out
def check_commit(repo_dir, commit):
print("\n===========================================")
print(f"Checking commit {commit} of {repo_dir}")
# go to commit
subprocess.run(f"git checkout {commit}".split(" "), cwd=repo_dir)
# get date of commit
out = subprocess.check_output(
f"git show -s --format=%ci {commit}".split(" "), cwd=repo_dir)
commit_date = out.decode(sys.stdout.encoding).rstrip()
# count lines of code
print("--- Counting lines of code")
out = invoke_cmd(f"sloccount .", repo_dir)
loc = 0
for l in out.split("\n"):
l_search = re.search(r"python:\s*(\d+) .*", l)
if l_search is not None:
loc = int(l_search.group(1))
# count Python files
out = invoke_cmd(f"find . -name *.py", repo_dir)
nb_python_files = len(out.split("\n")) - 1 # last line is empty
# type check
print("--- Type checking")
out = invoke_cmd("cp ../.pyre_configuration .", repo_dir)
out = invoke_cmd("pyre check", repo_dir)
warnings = out.split("\n")
warnings = warnings[:-1] # last line is empty
print(f"Got {len(warnings)} warnings")
# analyze warnings
kind_to_nb = Counter()
for w in warnings:
w_search = re.search(r".*:\d+:\d+ (.*\[\d+\]):.*", w)
if w_search is None:
raise Exception(f"Warning: Could not parse warning -- {w}")
warning_kind = w_search.group(1)
kind_to_nb[warning_kind] += 1
# count type annotations
param_types, return_types, variable_types, _, _, _ = count_type_annotations(
repo_dir)
result = {
"commit": commit,
"commit_date": commit_date,
"loc": loc, # number line of code
"nb_python_files": nb_python_files,
"nb_param_types": param_types,
"nb_return_types": return_types,
"nb_variable_types": variable_types,
"nb_warnings": len(warnings),
"kind_to_nb": kind_to_nb
}
return result
def nb_types(r):
return r["nb_param_types"] + r["nb_variable_types"] + r["nb_return_types"]
def get_all_commits(repo_dir):
out = invoke_cmd("git log --all --oneline", repo_dir)
lines = out.split("\n")
lines = lines[:-1] # last line is empty
commits = [l.split(" ")[0] for l in lines]
print(f"Found {len(commits)} commits")
return commits
def write_results(name, results):
with open(results_base_dir+name+".json", "w") as fp:
fp.write(json.dumps(results, indent=2))
def sample_commits(all_commits, max_commits_per_project):
if len(all_commits) <= max_commits_per_project:
commits = all_commits
else:
stride = int(round(len(all_commits) /
float(max_commits_per_project - 1)))
commits = [all_commits[i]
for i in range(0, len(all_commits), stride)]
return commits
def analyze_histories(projects, max_commits_per_project):
for p in projects:
try:
repo_dir = repos_base_dir+p
init_pyre(repo_dir)
all_commits = get_all_commits(repo_dir)
commits = sample_commits(all_commits, max_commits_per_project)
#commits = all_commits
project_results = []
for c in commits:
r = check_commit(repo_dir, c)
project_results.append(r)
write_results("history_"+p, project_results)
except Exception as e:
print(f"WARNING: Some problem with {p} -- skipping this project")
print(e)
def analyze_latest_commit(projects):
results = []
for p in projects:
repo_dir = repos_base_dir+p
init_pyre(repo_dir)
all_commits = get_all_commits(repo_dir)
latest_commit = all_commits[0]
r = check_commit(repo_dir, latest_commit)
r["project"] = p
results.append(r)
write_results("latest", results)
def get_parent_commit(repo_dir, commit):
cmd = f"git log --pretty=%P -n 1 {commit}"
out = invoke_cmd(cmd, repo_dir)
return out.split("\n")[0]
def is_add_only_commit(c):
return (c["added_per_commit_percentage"] == "100.0 %" and
int(c["typeannotation_line_inserted"]) > 0 and
int(c["typeannotation_line_removed"]) == 0 and
int(c["typeannotation_line_changed"]) == 0)
def is_remove_only_commit(c):
return (c["removed_per_commit_percentage"] == "100.0 %" and
int(c["typeannotation_line_inserted"]) == 0 and
int(c["typeannotation_line_removed"]) > 0 and
int(c["typeannotation_line_changed"]) == 0)
def is_changed_only_commit(c):
return (c["changed_per_commit_percentage"] == "100.0 %" and
int(c["typeannotation_line_inserted"]) == 0 and
int(c["typeannotation_line_removed"]) == 0 and
int(c["typeannotation_line_changed"]) > 0)
def analyze_specific_commits(commits_file):
with open(commits_file) as fp:
commit_stats = json.load(fp)
result = {
"add_only_commits": {
"same_nb_errors": 0,
"more_errors": 0,
"fewer_errors": 0,
},
"remove_only_commits": {
"same_nb_errors": 0,
"more_errors": 0,
"fewer_errors": 0,
},
"change_only_commits": {
"same_nb_errors": 0,
"more_errors": 0,
"fewer_errors": 0,
},
}
commit_url_regexp = r"https.*github\.com\/(.*)\/(.*)\/commit\/(.*)"
skipped = 0
used = 0
index = -1
for c in commit_stats:
try:
commit_url = c["url"]
match = re.match(commit_url_regexp, commit_url)
project = match.group(1) + '-' + match.group(2)
commit = match.group(3)
if commit == 'b2b9e3e343c8860acde1ec1d50932f1a41b25ccc':
print("Skipping commit")
add_only = is_add_only_commit(c)
remove_only = is_remove_only_commit(c)
change_only = is_changed_only_commit(c)
if change_only:
commit_url = c["url"]
match = re.match(commit_url_regexp, commit_url)
project = match.group(1) + '-' + match.group(2)
commit = match.group(3)
if commit == 'fb96392e73d0d12b845dabea185cc9e2ffa4652a':
continue
repo_dir = repos_base_dir+project
parent_commit = get_parent_commit(repo_dir, commit)
pre_change = check_commit(repo_dir, parent_commit)
post_change = check_commit(repo_dir, commit)
if add_only and not (nb_types(pre_change) < nb_types(post_change)):
skipped += 1
continue
if remove_only and not (nb_types(pre_change) > nb_types(post_change)):
skipped += 1
continue
if change_only and not (nb_types(pre_change) == nb_types(post_change)):
skipped += 1
continue
# write result for this commit into overall stats
if add_only:
relevant_result = result["add_only_commits"]
elif remove_only:
relevant_result = result["remove_only_commits"]
else: # change-only
relevant_result = result["change_only_commits"]
warnings_diff = post_change["nb_warnings"] - \
pre_change["nb_warnings"]
if warnings_diff == 0:
relevant_result["same_nb_errors"] += 1
elif warnings_diff > 0:
relevant_result["more_errors"] += 1
elif warnings_diff < 0:
relevant_result["fewer_errors"] += 1
used += 1
if used % 10 == 0:
print(f"\nUsed {used}, skipped {skipped}")
print(f"{result}")
except Exception as e:
print(str(e))
write_results("pure_commits_vs_errors.json", result)
#if __name__ == "__main__":
#analyze_histories(projects, max_commits_per_project=11)
# analyze_latest_commit(projects) # TODO: still needed?
start = time.time()
analyze_histories(projects, max_commits_per_project=10)
analyze_specific_commits(config.ROOT_DIR + "/Resources/Output/typeAnnotationCommitStatistics.json")
end = time.time()
hours, rem = divmod(end - start, 3600)
minutes, seconds = divmod(rem, 60)
print("{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), seconds))