forked from BeeStation/dmcompile-listener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.py
executable file
·200 lines (170 loc) · 5.83 KB
/
listener.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import platform
import random
import re
import os
from flask import Flask, jsonify, request, abort
import shutil
import string
import subprocess
from pathlib import Path
import docker
app = Flask(__name__)
client = docker.from_env()
CODE_FILE = Path.cwd().joinpath("templates/code.dm")
HOST = "127.0.0.1"
PORT = 5000
HOST_OS = platform.system()
MAIN_PROC = "proc/main()"
TEST_DME = Path.cwd().joinpath("templates/test.dme")
template = None
test_killed = False
@app.route("/compile", methods=["POST"])
def startCompile():
if request.method == "POST":
posted_data = request.get_json()
if "code_to_compile" in posted_data:
return jsonify(
compileTest(
posted_data["code_to_compile"], posted_data["byond_version"]
)
)
else:
abort(400)
def loadTemplate(line: str, includeProc=True):
with open(CODE_FILE) as filein:
template = string.Template(filein.read())
if includeProc:
line = "\n\t".join(line.splitlines())
d = {"proc": MAIN_PROC, "code": f"{line}\n"}
else:
d = {"proc": line, "code": ""}
return template.substitute(d)
def randomString(stringLength=24):
letters = string.ascii_lowercase
return "".join(random.choice(letters) for i in range(stringLength))
def checkVersions(version: str):
try:
image_list = client.images.list(name="test")
except IndexError:
return False
for image in image_list:
if f"test:{version}" in image.tags:
return True
return False
def buildVersion(version: str):
# Check if the version is already built
if checkVersions(version=version):
return
else:
try:
print(f"Attempting to build version: {version}")
client.images.build(
path=f"https://github.com/BeeStation/byond-docker.git",
platform="linux/386",
rm=True,
pull=True,
tag=f"beestation/byond:{version}",
buildargs={
"buildtime_BYOND_MAJOR": version.split(".")[0],
"buildtime_BYOND_MINOR": version.split(".")[1],
},
)
return client.images.build(
path=f"{Path.cwd()}",
dockerfile="Dockerfile",
rm=True,
tag=f"test:{version}",
buildargs={"BYOND_VERSION": version},
)
except docker.errors.BuildError:
raise
def normalizeCode(codeText: str):
indent_step = float("inf")
lines = codeText.split("\n")
for line in lines:
indent_level = len(line) - len(line.lstrip())
if indent_level > 0:
indent_step = min(indent_step, indent_level)
if indent_step == float("inf"):
return codeText
result_lines = []
for line in lines:
stripped = line.lstrip()
indent_level = len(line) - len(stripped)
result_lines.append("\t" * (indent_level // indent_step) + stripped)
return "\n".join(result_lines)
def compileTest(codeText: str, version: str):
try:
buildVersion(version=version)
except docker.errors.BuildError as e:
results = {"build_error": True, "exception": str(e)}
return results
codeText = normalizeCode(codeText)
randomDir = Path.cwd().joinpath(randomString())
randomDir.mkdir()
shutil.copyfile(TEST_DME, randomDir.joinpath("test.dme"))
with open(randomDir.joinpath("code.dm"), "a") as fc:
if MAIN_PROC not in codeText:
fc.write(loadTemplate(codeText))
else:
fc.write(loadTemplate(codeText, False))
docker_path = None
if HOST_OS == "Windows":
docker_path = "docker"
else:
docker_path = f"{Path.home()}/bin/docker"
if not os.path.isfile(docker_path):
docker_path = "/usr/bin/docker"
proc = subprocess.Popen(
[
docker_path,
"run",
"--name",
f"{randomDir.name}",
"--platform",
"linux/386",
"--rm",
"--network",
"none",
"-v",
f"{randomDir}:/app/code:ro",
f"test:{version}",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
try:
compile_log, run_log = proc.communicate(
timeout=30
) # A bit hacky, but provides exceptionally clean results. The main output will be captured as the compile_log while the "error" output is captured as run_log
test_killed = False
except subprocess.TimeoutExpired:
proc.kill()
subprocess.run([docker_path, "stop", f"{randomDir.name}"], capture_output=True)
compile_log, run_log = proc.communicate()
test_killed = True
compile_log = compile_log.decode("utf-8")
run_log = run_log.decode("utf-8")
run_log = re.sub(
r"The BYOND hub reports that port \d* is not reachable.", "", run_log
) # remove the network error message
run_log = re.sub(r"\n---------------\n", "\n", run_log) # remove the dashes
run_log = re.sub(r"World opened on network port \d*\.\n", "", run_log)
compile_log = re.sub(r"loading test.dme\n", "", compile_log)
compile_log = re.sub(r"saving test.dmb\n", "", compile_log)
compile_log = (
(compile_log[:1200] + "...") if len(compile_log) > 1200 else compile_log
)
run_log = (run_log[:1200] + "...") if len(run_log) > 1200 else run_log
shutil.rmtree(randomDir)
if f"Unable to find image 'test:{version}' locally" in run_log:
results = {"build_error": True, "exception": run_log}
else:
results = {
"compile_log": compile_log,
"run_log": run_log,
"timeout": test_killed,
}
return results
if __name__ == "__main__":
app.run(host=HOST, port=PORT)