-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcompiler_backend.py
executable file
·394 lines (347 loc) · 15 KB
/
compiler_backend.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env python
import subprocess
import os
import json
import urllib2
import time
import datetime
import traceback
import shutil
import errno
import sys
import random
import os.path
import hashlib
import SimpleHTTPServer
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer
ALLOW_CACHE = True
PORT = 11111
TEST_SERVERS = False
TMP_DIR = "tmp/"
LANGS = {
"java": { # uses in-memory compilation
"jsonReplCmd": "java -cp target/classes:lib/* fastjavacompile.App",
"testCode": '''
class Program {
public static void main(String[] args) {
System.out.println("hello world!");
}
}
''',
"mainFn": "Program.java",
"stdlibFn": "one.java",
"cmd": "javac Program.java one.java && java Program",
"versionCmd": "javac -version; java -version",
},
"typescript": { # uses in-memory compilation
"jsonReplCmd": "node jsonrepl.js",
"testCode": "console.log('hello world!');",
"cmd": "tsc index.ts node_modules/one/index.ts > /dev/null; node index.js",
"mainFn": "index.ts",
"stdlibFn": "node_modules/one/index.ts",
"versionCmd": "echo Node: `node -v`; echo TSC: `tsc -v`",
},
"javascript": { # uses in-memory compilation
"jsonReplCmd": "node jsonrepl.js",
"jsonReplDir": "TypeScript",
"testCode": "console.log('hello world!');",
"cmd": "node index.js",
"mainFn": "index.js",
"stdlibFn": "node_modules/one/index.js",
"versionCmd": "echo Node: `node -v`",
},
"python": { # uses in-memory compilation
"jsonReplCmd": "python -u jsonrepl.py",
"testCode": "print 'hello world!'",
"mainFn": "main.py",
"stdlibFn": "one.py",
"cmd": "python main.py",
"versionCmd": "python --version",
},
"ruby": { # uses in-memory compilation
"jsonReplCmd": "ruby jsonrepl.rb",
"testCode": "puts 'hello world!'",
"mainFn": "main.rb",
"stdlibFn": "one.rb",
"cmd": "ruby -I. main.rb",
"versionCmd": "ruby -v",
},
"csharp": { # uses in-memory compilation
"jsonReplCmd": "dotnet run --no-build",
"testCode": """
using System;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
""",
"cmd": "csc Program.cs StdLib.cs > /dev/null && ./Program.exe",
"compileCmd": "csc -recurse:src/*.cs -out:bin/Program.exe",
"runCmd": "mono Program.exe",
"mainFn": "Program.cs",
"stdlibFn": "StdLib.cs",
"versionCmd": "dotnet --info; echo CSC version: `csc -version`",
},
"php": { # uses in-memory compilation
#"jsonReplCmd": "php jsonrepl.php", # require_once causes fatal error which stops PHP execution
#"serverCmd": "php -S 127.0.0.1:{port} server.php",
"port": 8003,
"testCode": "print 'hello world!';",
"mainFn": "main.php",
"stdlibFn": "one.php",
"cmd": "php main.php",
"versionCmd": "php -v",
},
"cpp": {
"ext": "cpp",
"mainFn": "main.cpp",
"stdlibFn": "one.hpp",
"cmd": "g++ -std=c++17 main.cpp one_packages/*/*.cpp -I. -Ione_packages -o binary && ./binary",
"versionCmd": "g++ -v",
},
"go": {
"ext": "go",
"mainFn": "main.go",
"stdlibFn": "src/one/one.go",
"cmd": "GOPATH=$PWD go run main.go",
"versionCmd": "go version",
},
"perl": {
"ext": "pl",
"mainFn": "main.pl",
"stdlibFn": "one.pm",
"cmd": "perl -I. main.pl",
"versionCmd": "perl -v",
},
"swift": {
"ext": "swift",
"mainFn": "main.swift",
"stdlibFn": "one.swift",
"cmd": "cat main.swift | swift -",
"versionCmd": "swift --version",
}
}
def log(text):
print "[CompilerBackend] %s" % text
class JsonReplClient:
def __init__(self, cmd, cwd):
self.p = subprocess.Popen(cmd.split(" "), stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=1, universal_newlines=True, cwd=cwd)
def request(self, request):
self.p.stdin.write(json.dumps(request) + "\n")
return json.loads(self.p.stdout.readline())
def compile(self, code, stdlib):
return self.request({"cmd": "compile", "code": code, "stdlibCode": stdlib, "className": "TestClass", "methodName": "testMethod" })
def postRequest(url, request):
return urllib2.urlopen(urllib2.Request(url, request, headers={"Origin": "http://127.0.0.1:8000"})).read()
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def providePath(fileName):
mkdir_p(os.path.dirname(fileName))
return fileName
allowRemote = not "--localOnly" in sys.argv
requireToken = allowRemote or "--requireToken" in sys.argv
version_cache = None
mkdir_p(TMP_DIR)
if requireToken:
token = ""
if os.path.isfile(".secret_token"):
with open(".secret_token", "r") as f: token = f.read()
if len(token) < 32:
token = "%16x" % random.SystemRandom().getrandbits(128)
with open(".secret_token", "w") as f: f.write(token)
if not "--noInMemoryCompilation" in sys.argv:
for langName in LANGS:
try:
lang = LANGS[langName]
cwd = "%s/InMemoryCompilers/%s" % (os.getcwd(), lang.get("jsonReplDir", langName))
if "jsonReplCmd" in lang:
log("Starting %s JSON-REPL..." % langName)
lang["jsonRepl"] = JsonReplClient(lang["jsonReplCmd"], cwd)
elif "serverCmd" in lang:
log("Starting %s HTTP server..." % langName)
args = lang["serverCmd"].replace("{port}", str(lang["port"])).split(" ")
lang["server"] = subprocess.Popen(args, cwd=cwd, stdin=subprocess.PIPE)
except Exception as e:
print "Failed to start compiler %s: %r" % (langName, e)
if TEST_SERVERS: # TODO
testText = "Works!"
requestJson = json.dumps(lang["testRequest"], indent=4).replace("{testText}", testText)
maxTries = 10
for i in xrange(maxTries):
try:
time.sleep(0.1 * (i + 1))
log(" Checking %s compiler's status (%d / %d)..." % (langName, i + 1, maxTries))
responseJson = postRequest("http://127.0.0.1:%d/compile" % lang["port"], requestJson)
break
except:
pass
response = json.loads(responseJson)
log(" %s compiler's test response: %s" % (langName, response))
if response["result"] != testText:
log("Invalid response. Compiler will be disabled.")
else:
log("%s compiler is ready!" % langName)
class HTTPHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def log_message(self, format, *args):
pass
def resp(self, statusCode, result):
result["controllerVersion"] = "one:v2:20200218"
responseBody = json.dumps(result)
self.send_response(statusCode)
self.send_header("Content-Length", "%d" % len(responseBody))
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
self.end_headers()
self.wfile.write(responseBody)
self.wfile.close()
def end_headers(self):
self.send_header("Cache-Control", "no-cache, no-store")
SimpleHTTPServer.SimpleHTTPRequestHandler.end_headers(self)
def api_compile(self):
requestJson = self.rfile.read(int(self.headers.getheader('content-length')))
useCache = self.queryParams.get("useCache")
if ALLOW_CACHE:
requestHash = hashlib.sha256(requestJson).hexdigest()[0:12]
cacheFn = "%s/compilecache_%s_resp.json" % (TMP_DIR, requestHash)
if useCache and os.path.exists(cacheFn):
with open(cacheFn, "rt") as f:
response = json.loads(f.read())
response["fromCache"] = True
response["cacheId"] = requestHash
self.resp(200, response)
return
request = json.loads(requestJson)
request["cmd"] = "compile"
langName = request["lang"]
mode = request["mode"] if "mode" in request else "auto"
lang = LANGS[langName]
start = time.time()
if mode == "jsonRepl" or (mode == "auto" and "jsonRepl" in lang):
response = lang["jsonRepl"].request(request)
elif mode == "server" or (mode == "auto" and "server" in lang):
responseJson = postRequest("http://127.0.0.1:%d" % lang["port"], requestJson)
response = json.loads(responseJson)
elif mode == "native" or mode == "auto":
dateStr = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
dirName = "%s_%s" % (dateStr, langName)
if "name" in request:
dirName += "_" + request["name"]
outDir = "%s%s/" % (TMP_DIR, dirName)
binDir = outDir+"/bin/"
mkdir_p(binDir)
response = { "tmpDir": dirName, "success": False }
for (filename, code) in request["files"].items():
with open(providePath(outDir + "src/" + filename), "wt") as f: f.write(code)
#with open(providePath(outDir + lang["mainFn"]), "wt") as f: f.write(request["code"])
#if "stdlibCode" in request:
# with open(providePath(outDir + lang["stdlibFn"]), "wt") as f: f.write(request["stdlibCode"])
#for pkgSrc in request["packageSources"]:
# with open(providePath(outDir + "one_packages/" + pkgSrc["packageName"] + "/" + pkgSrc["fileName"]), "wt") as f: f.write(pkgSrc["code"])
pipes = subprocess.Popen(lang["compileCmd"], shell=True, cwd=outDir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = pipes.communicate()
success = pipes.returncode == 0 and stderr == ""
response["compilation"] = { "stdout": stdout, "stderr": stderr, "exitCode": pipes.returncode, "success": success }
if not success:
response["error"] = "Program run failed with exitCode %d\nStderr:\n%s\nStdout:\n%s" % (pipes.returncode, stderr, stdout)
else:
pipes = subprocess.Popen(lang["runCmd"], shell=True, cwd=binDir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = pipes.communicate()
success = pipes.returncode == 0 and stderr == ""
response["run"] = { "stdout": stdout, "stderr": stderr, "exitCode": pipes.returncode, "success": success }
if not success:
response["error"] = "Program run failed with exitCode %d\nStderr:\n%s\nStdout:\n%s" % (pipes.returncode, stderr, stdout)
else:
response["success"] = True
shutil.rmtree(outDir)
else:
response = { "error": "Unknown mode '%s'" % mode, "success": False }
# cache should be overwritten even when useCache is False, otherwise a broken version stays there forever
if ALLOW_CACHE:
with open(cacheFn, "wt") as f: f.write(json.dumps(response))
response["elapsedMs"] = int((time.time() - start) * 1000)
response["fromCache"] = False
self.resp(200, response)
def api_compiler_versions(self):
# TODO: thread-safety
global version_cache
if not version_cache:
version_cache = {}
for lang in LANGS:
try:
version_cache[lang] = subprocess.check_output(LANGS[lang]["versionCmd"], shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
version_cache[lang] = e.output
self.resp(200, version_cache)
def api_status(self):
self.resp(200, { "status": "ok" })
def do_GET(self):
return self.resp(403, { "exceptionText": "GET method is not allowed", "errorCode": "method_not_allowed" })
def originCheck(self):
origin = self.headers.getheader('origin') or "<null>"
if origin != "https://ide.onelang.io" and origin != "http://ide.onelang.io" and not origin.startswith("http://127.0.0.1:") and not origin.startswith("http://localhost:"):
self.resp(403, { "exceptionText": "Origin is not allowed: " + origin, "errorCode": "origin_not_allowed" })
return False
return True
def do_OPTIONS(self):
if not self.originCheck(): return
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", self.headers.getheader('origin'))
self.send_header("Access-Control-Allow-Methods", "POST, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "authentication")
self.end_headers()
def do_POST(self):
try:
if not self.originCheck(): return
global requireToken, token
if requireToken and self.headers.getheader('authentication') != "Token %s" % token:
return self.resp(403, { "exceptionText": "Authentication token is invalid", "errorCode": "invalid_token" })
pathParts = self.path.split('?', 1)
self.path = pathParts[0]
self.qs = pathParts[1] if len(pathParts) > 1 else ""
self.queryParams = {}
for keyValue in self.qs.split('&'):
keyValueParts = keyValue.split("=", 1)
self.queryParams[keyValueParts[0]] = keyValueParts[1] if len(keyValueParts) > 1 else True
if self.path == '/compiler_versions':
self.api_compiler_versions()
elif self.path == '/compile':
self.api_compile()
elif self.path == '/status':
self.api_status()
else:
self.resp(403, { "exceptionText": "API endpoint was not found: " + self.path, "errorCode": "endpoint_not_found" })
except Exception as e:
log(repr(e))
self.resp(400, { 'exceptionText': traceback.format_exc() })
log("Starting onelang.io CompilerBackend on port %d..." % PORT)
if requireToken:
log("Please use this token for authentication:")
log(" ===> %s <===" % token)
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
daemon_threads = True
"""Handle requests in a separate thread."""
log("Press Ctrl+C to exit.")
try:
ThreadedHTTPServer(("0.0.0.0" if allowRemote else "127.0.0.1", PORT), HTTPHandler).serve_forever()
except KeyboardInterrupt:
pass
for langName in LANGS:
lang = LANGS[langName]
if not "subp" in lang: continue
log("Send stop signal to %s compiler" % langName)
lang["subp"].communicate("\n")
log("Waiting for %s compiler to stop..." % langName)
lang["subp"].wait()
log("Exiting...")