-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep5.py
301 lines (253 loc) · 9.85 KB
/
step5.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
# see https://gist.github.com/jpivarski/001867b9da51a47b93913a0b9809db3a
import concurrent.futures
import time
import glob
import tarfile
import json
import ast
import re
import gzip
from collections import Counter
import jupytext
import pycparser
c_parser = pycparser.c_parser.CParser()
c_directive = re.compile("\s*#.*")
c_include = re.compile("\s*#include [<\"](.*)[>\"]")
cuda_bracket = re.compile("<<<.*>>>")
cpp_suffixes = set(["c", "h", "c++", "cxx", "hxx", "cpp", "hpp", "hp", "cu", "cuh", "cp", "hh", "cc"])
others = {
"pyx": "Cython",
"pxi": "Cython",
"f": "Fortran",
"for": "Fortran",
"f90": "Fortran",
"jl": "Julia",
"rs": "Rust",
"r": "R",
"rdata": "R",
"rmd": "R",
"abap": "ABAP",
"mat": "MATLAB",
"asv": "MATLAB",
"m": "Mathematica",
"wl": "Mathematica",
"nb": "Mathematica",
"go": "Go",
"ada": "Ada",
"java": "Java",
"scala": "Scala",
"groovy": "Groovy",
"kt": "Kotlin",
"kts": "Kotlin",
"cs": "C#",
"fs": "F#",
"swift": "Swift",
"perl": "Perl",
"pl": "Perl",
"pm": "Perl",
"rb": "Ruby",
"hs": "Haskell",
"lhs": "Haskell",
"cbl": "COBOL",
"cob": "COBOL",
}
jit_functions = set(
["numba.jit", "numba.njit", "numba.generated_jit", "numba.vectorize", "numba.guvectorize", "numba.cfunc"]
)
class APIVisitor(ast.NodeVisitor):
def __init__(self, numba_imports):
self.numba_imports = numba_imports
self.all_imports = Counter()
self.all_numba_references = []
def visit_Import(self, node):
for subnode in node.names:
self.all_imports[subnode.name.split(".")[0]] += 1
def visit_ImportFrom(self, node):
if node.level == 0:
self.all_imports[node.module.split(".")[0]] += 1
def visit_FunctionDef(self, node):
for x in node.decorator_list:
n = len(self.all_numba_references)
self.visit(x)
if n != len(self.all_numba_references):
self.all_numba_references[-1] = "@" + self.all_numba_references[-1]
self.visit(node.args)
if node.returns is not None:
self.visit(node.returns)
for x in node.body:
self.visit(x)
def visit_Call(self, node):
n = len(self.all_numba_references)
self.visit(node.func)
if n != len(self.all_numba_references):
if self.all_numba_references[-1] in jit_functions:
unparsed = ast.unparse(node)
self.all_numba_references[-1] = self.all_numba_references[-1] + unparsed[unparsed.index("("):]
for x in node.args:
self.visit(x)
for x in node.keywords:
self.visit(x)
def visit_Attribute(self, node):
if isinstance(node.ctx, ast.Load):
name = ()
while isinstance(node, ast.Attribute):
name = (node.attr,) + name
node = node.value
if isinstance(node, ast.Name):
name = (node.id,) + name
self._check(".".join(name))
def visit_Name(self, node):
if isinstance(node.ctx, ast.Load):
self._check(node.id)
def _check(self, name):
for real, alias in self.numba_imports:
if name == alias or name.startswith(alias + "."):
fixed = real + name[len(alias):]
self.all_numba_references.append(fixed)
break
def analyze_python(syntax_tree):
assert isinstance(syntax_tree, ast.Module)
top_imports = Counter()
numba_imports = []
for node in syntax_tree.body:
if isinstance(node, ast.Import):
for subnode in node.names:
name = subnode.name.split(".")[0]
top_imports[name] += 1
if name == "numba":
asname = subnode.name if subnode.asname is None else subnode.asname
numba_imports.append((subnode.name, asname))
elif isinstance(node, ast.ImportFrom) and node.level == 0:
name = node.module.split(".")[0]
top_imports[name] += 1
if name == "numba":
for subname in node.names:
asname = subname.name if subname.asname is None else subname.asname
numba_imports.append((node.module + "." + subname.name, asname))
visitor = APIVisitor(numba_imports)
try:
visitor.visit(syntax_tree)
except RecursionError:
return None
nested_imports = {k: v - top_imports.get(k, 0) for k, v in visitor.all_imports.items()}
nested_imports = {k: v for k, v in nested_imports.items() if v != 0}
all_numba_references = Counter()
for x in visitor.all_numba_references:
all_numba_references[x] += 1
return {"top": dict(top_imports), "nested": nested_imports, "numba": dict(all_numba_references)}
def analyze_repo(filename):
reponame = filename[49:-4]
print(reponame)
repodata = {"name": reponame, "python": [], "c": [], "other_language": Counter()}
with tarfile.open(filename) as file:
subfilenames = file.getnames()
repodata["num_files"] = len(subfilenames)
names_in_repo = set(x.split("/")[-1] for x in subfilenames)
for subfilename in subfilenames:
assert subfilename[5 : 5 + len(reponame)] == reponame
pieces = subfilename.lower().rsplit(".", 1)
if len(pieces) != 2 or pieces[0] == "":
continue
if pieces[1] in ("py", "pyi"):
try:
subfile = file.extractfile(subfilename)
except (KeyError, RecursionError):
continue
if subfile is None:
continue
try:
syntax_tree = ast.parse(subfile.read())
parsable = True
except:
parsable = False
repodata["python"].append({
"name": subfilename[5 + len(reponame) + 1:],
"suffix": pieces[1],
"data": analyze_python(syntax_tree) if parsable else None,
})
elif pieces[1] == "ipynb":
try:
subfile = file.extractfile(subfilename)
except (KeyError, RecursionError):
continue
if subfile is None:
continue
try:
notebook = jupytext.reads(
subfile.read().decode("utf-8", errors="surrogateescape")
)
text = jupytext.writes(notebook, fmt="py:percent")
syntax_tree = ast.parse(text)
parsable = True
except:
parsable = False
repodata["python"].append({
"name": subfilename[5 + len(reponame) + 1:],
"suffix": pieces[1],
"data": analyze_python(syntax_tree) if parsable else None,
})
elif pieces[1] in cpp_suffixes:
try:
subfile = file.extractfile(subfilename)
except (KeyError, RecursionError):
continue
if subfile is None:
continue
text = subfile.read().decode("utf-8", errors="surrogateescape")
global_include = Counter()
local_include = Counter()
for include in c_include.finditer(text):
if include.group(1).split("/")[-1] in names_in_repo:
local_include[include.group(1)] += 1
else:
global_include[include.group(1)] += 1
try:
c_parser.parse(c_directive.sub("", text), subfilename)
is_c = True
except pycparser.plyparser.ParseError:
is_c = False
num_cuda_brackets = len(cuda_bracket.findall(text))
repodata["c"].append({
"name": subfilename[5 + len(reponame) + 1:],
"suffix": pieces[1],
"data": {
"global": dict(global_include),
"local": dict(local_include),
"is_c": is_c,
"num_cuda": num_cuda_brackets,
},
})
else:
language = others.get(pieces[1])
if language is not None:
repodata["other_language"][language] += 1
repodata["other_language"] = dict(repodata["other_language"])
print("DONE", reponame)
return filename + "\n", json.dumps(repodata, ensure_ascii=True, allow_nan=False, separators=(",", ":")) + "\n"
filenames = glob.glob("GitHub-numba-user-nonfork-raw-data-1Mcut-imports/*/*.tgz")
try:
with open("input-skip.txt") as names:
for name in names:
filenames.remove(name.rstrip("\n"))
except FileNotFoundError:
pass
print(filenames)
raise Exception
with open("output-names.txt", "w") as names, open("output-errors.txt", "w") as errors, open("output-results.jsons", "w") as results:
with concurrent.futures.ProcessPoolExecutor() as pool:
start = time.time()
for i, (name, result) in enumerate(pool.map(analyze_repo, filenames, chunksize=1)):
# for i, (name, result) in enumerate(analyze_repo(x) for x in filenames):
if name is None:
errors.write(result)
errors.flush()
else:
results.write(result)
results.flush()
names.write(name)
names.flush()
now = int(time.time() - start)
print(
f"{now // 3600}:{(now % 3600) // 60:02d}:{now % 60:02d}: {i+1}/{len(filenames)} = {(i+1)/len(filenames)}",
flush=True,
)