-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_ast.py
62 lines (58 loc) · 2.82 KB
/
create_ast.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
from pymongo import MongoClient
from tqdm import tqdm
import ast
from bson.objectid import ObjectId
import math
print("Connecting to db...")
client = MongoClient('da1.eecs.utk.edu')
db = client['fdac19-Stackbot']
col = db['SOdata']
docs = list(col.find({}))
for doc in tqdm(docs):
all_blocks = doc['code_and_ast']
for i, each in enumerate(all_blocks):
#####################################
if not isinstance(each, dict):
col.remove({"_id": doc['_id']})
break
#####################################
cmp_str = 'code_and_ast.'+str(i)+'.compilation_message'
err_str = 'code_and_ast.'+str(i)+'.error_type'
ast_str = 'code_and_ast.'+str(i)+'.ast'
if doc['language'] == 'python-3.x' and (not isinstance(each['compilation_message'], str) or not isinstance(each['error_type'], str) or not isinstance(each['ast'], str)):
code = each['code']
# try to compile the code
message = ''
try:
tree = ast.parse(code)
tree_dump = ast.dump(tree)
except Exception as e:
message = e
err = str(type(message)).replace("<class '", '')
err = err.replace("'>", '')
#print(err)
if message == '':
#print("\nCompilation successful!")
#print("#_______________________________________________________________________________#")
col.update({'_id': ObjectId(doc['_id']), 'code_and_ast.code': code}, {"$set": {cmp_str: 'successful'}})
col.update({'_id': ObjectId(doc['_id']), 'code_and_ast.code': code}, {"$set": {err_str: 'none'}})
col.update({'_id': ObjectId(doc['_id']), 'code_and_ast.code': code}, {"$set": {ast_str: tree_dump}})
else:
if err == "ModuleNotFoundError":
fw = open('NEEDED_LIBRARIES', 'a')
fr = open('NEEDED_LIBRARIES', 'r')
lib = str(message).replace("No module named ", '')
lib = lib[1:]
lib = lib[:len(lib)-1]
if lib not in fr.read():
fw.write(lib+'\n')
fw.close()
fr.close()
continue
#print("\nCompilation failed!")
#print("#_______________________________________________________________________________#")
col.update({'_id': ObjectId(doc['_id']), 'code_and_ast.code': code}, {"$set": {cmp_str: str(message)}})
col.update({'_id': ObjectId(doc['_id']), 'code_and_ast.code': code}, {"$set": {err_str: err}})
col.update({'_id': ObjectId(doc['_id']), 'code_and_ast.code': code}, {"$set": {ast_str : 'none'}})
#print()
#print()