-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPythonExecutor.py
82 lines (72 loc) · 2.63 KB
/
PythonExecutor.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
import traceback
from flask import flash
from os import getcwd, path
from time import sleep
ALLOWED_EXTENSIONS = {'py'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def formatTraceback(tb):
traces = tb.split('\n')
traces.pop(1)
traces.pop(1)
trace = '\n'.join(traces)
directory = getcwd() + '/userscripts/UserScript.py' #linux
#directory = getcwd() + '\\userscripts\\UserScript.py' #windows
trace = trace.replace(directory, "User_Submitted_Code")
return trace
def swapTabSpace(file):
with open(file) as f:
lines = f.readlines()
for i in range(0, len(lines)):
lines[i] = lines[i].replace(" ", " ")
with open(file, "w+") as f:
f.writelines(lines)
def runUserScript(tb):
try:
with open("userscripts/printlog.txt", "w+") as file:
file.write("---SCRIPT STARTING---\n")
swapTabSpace("userscripts/UserScript.py")
import userscripts.UserScript
#code.main(eh)
with open("userscripts/printlog.txt", "a") as f:
f.write("---SCRIPT FINISHED---\n")
except ImportError:
trace = traceback.format_exc()
traces = trace.split('\n')
if traces[-2] != "ModuleNotFoundError: No module named 'userscripts.UserScript'":
with open("userscripts/printlog.txt", "a") as f:
f.write("---ERROR---\n")
tb.append(formatTraceback(trace))
print("Error in user script:")
print(tb[0])
else:
print("Error. Demo script not found.")
tb.append("Error. No script found. Try redownloading, or this might be an internal server issue.")
except (Exception,):
with open("userscripts/printlog.txt", "a") as f:
f.write("---ERROR---\n")
print("Error in user script:")
trace = traceback.format_exc()
tb.append(formatTraceback(trace))
print(tb[0])
sleep(0.1)
import motoroff
motoroff.none()
def upload_file(app, request):
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return "No file"
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
flash('No selected file')
return "No file"
if file and allowed_file(file.filename):
file.save(path.join(app.config['UPLOAD_FOLDER'], "UserScript.py"))
return "200 OK"
print(file.filename)
print(file.stream.readlines())
return "Invalid filename"