-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer.py
129 lines (112 loc) · 3.61 KB
/
tracer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from flask import Flask, request, Response, make_response
from flask.json import jsonify
import json
import subprocess
from runestone.codelens.pg_logger import exec_script_str_local
# at some point maybe worry about Access-Control-Allow-Origin: *
# but for now this isn't an issue for command line usage.
app = Flask(__name__)
@app.route("/")
def hello_world():
return "Hello Class!"
# docker run -m 512M --rm --user=netuser --net=none --cap-drop all pgbovine/cokapi-java:v1 /tmp/run-java-backend.sh '{"usercode": "public class Test { public static void main(String[] args) { int x=42; x+=1; x+=1; x+=1;} }", "options": {}, "args": [], "stdin": ""}'
@app.route("/tracejava", methods=["POST"])
def tracejava():
#code = request.args["src"]
code = request.form.get("src")
stdin = request.form.get("stdin")
if stdin == "null" or stdin is None:
stdin = ""
docker_args = [
"docker",
"run",
"-m",
"512M",
"--rm",
"--user=netuser",
"--net=none",
"--cap-drop",
"all",
"pgbovine/cokapi-java:v1",
"/tmp/run-java-backend.sh",
]
runspec = {}
runspec["usercode"] = code
runspec["options"] = {}
runspec["args"] = []
runspec["stdin"] = stdin
docker_args.append(json.dumps(runspec))
res = subprocess.run(docker_args, capture_output=True)
print(res)
resp = make_response(res.stdout)
resp.headers['Content-type'] = 'application/json'
return resp
# docker run -t -i --rm --user=netuser --net=none --cap-drop all pgbovine/opt-cpp-backend:v1 python /tmp/opt-cpp-backend/run_cpp_backend.py "int main() {int x=12345;}" c
@app.route("/tracec", methods=["POST"])
def tracec():
code = request.form.get("src")
docker_args = [
"docker",
"run",
# "-t", Removed these two as they caused failure and I don't know that they are needed
# "-i",
"--rm",
"--user=netuser",
"--net=none",
"--cap-drop",
"all",
"pgbovine/opt-cpp-backend:v1",
"python",
"/tmp/opt-cpp-backend/run_cpp_backend.py",
code,
"c"
]
res = subprocess.run(docker_args, capture_output=True)
resp = make_response(res.stdout)
resp.headers['Content-type'] = 'application/json'
return resp
@app.route("/tracecpp", methods=["POST"])
def tracecpp():
code = request.form.get("src")
docker_args = [
"docker",
"run",
"--rm",
"--user=netuser",
"--net=none",
"--cap-drop",
"all",
"pgbovine/opt-cpp-backend:v1",
"python",
"/tmp/opt-cpp-backend/run_cpp_backend.py",
code,
"cpp"
]
done = False
tries = 5
while not done and tries > 0:
res = subprocess.run(docker_args, capture_output=True)
if len(res.stderr) != 0:
print(f"Error: {res.stderr}")
tries -= 1
if len(res.stdout) == 0:
print("No results from docker")
tries -= 1
if len(res.stdout) > 0:
done = True
resp = make_response(res.stdout)
resp.headers['Content-type'] = 'application/json'
return resp
def js_var_finalizer(input_code, output_trace):
ret = dict(code=input_code, trace=output_trace)
json_output = json.dumps(ret, indent=None)
return json_output
@app.route("/tracepy", methods=["POST"])
def tracepy():
code = request.form.get("src")
tracedata = exec_script_str_local(
code, None, False, None, js_var_finalizer
)
resp = make_response(tracedata)
resp.headers['Content-type'] = 'application/json'
return resp