-
Notifications
You must be signed in to change notification settings - Fork 21
/
ASTBuilder.py
83 lines (75 loc) · 2.36 KB
/
ASTBuilder.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
import _ast
from ASTAnalyser import ASTAnalyser
from ASTFunctionVisitor import ASTFunctionVisitor
import sys
import os
import json
import multiprocessing as mp
class ASTBuilder:
def __init__(self, src):
self.src = src
def build_AST(self):
dfgraph=None
astTree = None
try:
astTree = compile(self.src, "<string>", "exec", _ast.PyCF_ONLY_AST)
except:
return
try:
functionVisitor = ASTFunctionVisitor()
functionVisitor.visit(astTree)
astVisitor = ASTAnalyser(func_list=functionVisitor.func_list)
astVisitor.visit(astTree)
dfgraph=astVisitor.df_graph
except:
print "Unexpected error:", sys.exc_info()[0]
return dfgraph
"""
def worker(folder):
filename = 'repoData/' + folder + '/allPythonContent.py'
fullfile = open(filename).read()
file_splits = fullfile.split('########NEW FILE########')
df_graphs={
'folder':folder,
'files':[]}
for piece in file_splits:
piece = piece.strip()
piece_name = piece.split('\n')[0].strip()
if len(piece_name.split())==3:
file_name = piece_name.split()[2]
try:
print "Foldername:"+folder, "Filename:"+file_name
df_graph = ASTBuilder(piece).build_AST().serialize()
if df_graph is not None:
if int(df_graph['count'])>1:
prog_info={
'file':file_name,
'graph':df_graph}
df_graphs['files'].append(prog_info)
except:
print "Unexpected error in worker:", sys.exc_info()[0]
f_test=open('srcfiles/test.py', 'w')
f_test.write(piece)
f_test.close()
proc_name=str(os.getpid())
if df_graphs['files']:
f=open('graphs/graph-'+proc_name+'.txt','a')
f.write(json.dumps(df_graphs))
f.write('\n'+'-' * 20 + '\n')
f.close()
def main():
pool = mp.Pool(mp.cpu_count())
jobs = []
for proj in os.listdir('repoData'):
job = pool.apply_async(worker, (proj,))
jobs.append(job)
for job in jobs:
try:
job.get()
except:
continue
pool.close()
pool.join()
if __name__=="__main__":
main()
"""