-
Notifications
You must be signed in to change notification settings - Fork 1
/
flask_app.py
91 lines (72 loc) · 2.81 KB
/
flask_app.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
from flask import Flask, render_template, request
from flask_cors import CORS
import multiprocessing, secrets
import PyGrok
import git
app = Flask(__name__)
CORS(app)
import os, sys, shutil
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) + "/.."
sys.path.insert(1, THIS_FOLDER)
shutil.rmtree("sessions", ignore_errors=True)
os.system("mkdir sessions")
sessions = {}
terminated = set()
@app.route('/', methods=['POST','GET'])
def index():
session = secrets.token_hex(64)
sessions[session] = None
return render_template('main.html', session=session)
@app.route("/execute", methods=['POST'])
def execute():
flags = request.form['flags']
code = request.form['code'].replace("\r", "")
input_list = request.form["inputs"].replace("\r", "")
session = request.form["session"]
if session not in sessions:
return {"stdout": "", "stderr": "The session was invalid! You may need to reload your tab."}
shutil.rmtree(f"sessions/{session}", ignore_errors=True)
os.mkdir(f"sessions/{session}")
with open(f"sessions/{session}/.stdin", "w", encoding="utf-8") as f:
f.write(input_list)
with open(f"sessions/{session}/.stdin", "r", encoding="utf-8") as x:
with open(f"sessions/{session}/.stdout", "w", encoding="utf-8") as y:
with open(f"sessions/{session}/.stderr", "w", encoding="utf-8") as z:
manager = multiprocessing.Manager()
ret = manager.dict()
if "f" in flags:
time = 10
elif "F" in flags:
time = 15
elif "b" in flags:
time = 30
elif "T" in flags:
time = 60
elif "B" in flags:
time = 120
else:
time = 5
ret[1] = ""
ret[2] = ""
sessions[session] = multiprocessing.Process(target=PyGrok.execute, args=(code, flags, input_list, ret))
sessions[session].start()
sessions[session].join(time)
if sessions[session].is_alive():
sessions[session].terminate()
if 2 in ret:
ret[2] += "\n" + f"Code timed out after {time} seconds"
y.write(ret[1])
z.write(ret[2])
with open(f"sessions/{session}/.stdout", "r", encoding="utf-8") as x:
with open(f"sessions/{session}/.stderr", "r", encoding="utf-8") as y:
val = {"stdout": x.read(), "stderr": y.read()}
shutil.rmtree(f"sessions/{session}", ignore_errors=True)
return val
@app.route('/commit', methods=['POST'])
def webhook():
if request.method in ["POST"]:
repo = git.Repo('/home/Grok/grok_online')
origin = repo.remotes.origin
origin.pull()
return 'Updated PythonAnywhere successfully', 200
return 'Wrong event type', 400