-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
73 lines (56 loc) · 2.22 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
from flask import Flask, render_template, request
from flask_cors import CORS
import multiprocessing, secrets
import brainbox
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():
code = request.form['code'].replace("\r", "")
input_list = request.form["inputs"].replace("\r", "")
session = request.form["session"]
if session not in sessions:
return {"stdout": "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:
manager = multiprocessing.Manager()
ret = manager.dict()
time = 15
ret[1] = ""
ret[2] = ""
sessions[session] = multiprocessing.Process(target=brainbox.execute, args=(code, input_list, ret))
sessions[session].start()
sessions[session].join(time)
if sessions[session].is_alive():
sessions[session].terminate()
ret[1] += "\n\n\n" + f"Code timed out after {time} seconds"
y.write(ret[1])
with open(f"sessions/{session}/.stdout", "r", encoding="utf-8") as x:
val = {"stdout": x.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/brainbox/mysite')
origin = repo.remotes.origin
origin.pull()
return 'Updated PythonAnywhere successfully', 200
return 'Wrong event type', 400