#!/usr/bin/env python3

import subprocess
from bottle import get, post, request, run, static_file

@get("/")
def serve_index():
    return static_file("web.html", root="static")

@get("/<path:path>")
def serve_static(path):
    return static_file(path, root="static")

@post("/evaluate.json")
def evaluate():
    version = request.json["version"]
    if version not in ("master",):
        return {"error": "invalid version"}
    print(request.json)
    with subprocess.Popen(["playpen",
                          "root-" + version,
                           "--user=rust",
                           "--timeout=5",
                           "--syscalls-file=whitelist",
                           "--devices=c:1:9",
                           "--memory-limit=128M",
                           "--",
                           "/usr/local/bin/evaluate.sh",
                           request.json["code"]],
                           stdout=subprocess.PIPE,
                           stderr=subprocess.STDOUT) as p:
        return {"result": p.stdout.read().decode()}

@post("/format.json")
def format():
    version = request.json["version"]
    if version not in ("master",):
        return {"error": "invalid version"}
    print(request.json)
    with subprocess.Popen(["playpen",
                           "root-" + version,
                           "--user=rust",
                           "--timeout=5",
                           "--syscalls-file=whitelist",
                           "--devices=c:1:9",
                           "--memory-limit=128M",
                           "--",
                           "/usr/local/bin/format.sh",
                           request.json["code"]],
                           stdout=subprocess.PIPE,
                           stderr=subprocess.STDOUT) as p:
        output = p.communicate()[0][:-1].decode()
        if p.returncode:
            return {"error": output}
        else:
            return {"result": output}

run(host='0.0.0.0', port=8000, server='cherrypy')

@@ -266,125 +266,49 @@
 %22)%0A%0A
-@post(%22/evaluate.json%22)%0Adef evaluate():%0A    version = request.json%5B%22version%22%5D%0A    if version not in (%22master%22,
+def playpen(version, command, argument
 ):%0A
-    
     
@@ -318,70 +318,8 @@
 urn 
-%7B%22error%22: %22invalid version%22%7D%0A    print(request.json)%0A    with 
 subp
@@ -335,32 +335,35 @@
 pen(%5B%22playpen%22,%0A
+   
                 
@@ -383,32 +383,79 @@
 ot-%22 + version,%0A
+                             %22--mount-proc%22,%0A  
                 
@@ -472,32 +472,34 @@
  %22--user=rust%22,%0A
+  
                 
@@ -543,32 +543,34 @@
                 
+ 
+ 
 %22--syscalls-file
@@ -601,32 +601,34 @@
                 
+  
 %22--devices=c:1:9
@@ -618,32 +618,43 @@
  %22--devices=
-c:1:9
+/dev/urandom:r
 %22,%0A
+  
             
@@ -679,36 +679,37 @@
 memory-limit=128
-M
 %22,%0A
+  
                 
@@ -756,36 +756,17 @@
     
-%22/usr/local/bin/evaluate.sh%22
+  command
 ,%0A  
@@ -790,39 +790,31 @@
         
-request.json%5B%22code%22%5D
+  argument
 %5D,%0A
+  
         
@@ -875,32 +875,34 @@
                 
+  
 stderr=subproces
@@ -914,66 +914,8 @@
 OUT)
- as p:%0A        return %7B%22result%22: p.stdout.read().decode()%7D
 %0A%0A@p
@@ -920,22 +920,24 @@
 @post(%22/
-form
+evalu
 at
+e
 .json%22)%0A
@@ -940,22 +940,24 @@
 n%22)%0Adef 
-form
+evalu
 at
+e
 ():%0A    
@@ -1106,559 +1106,387 @@
 ith 
-subprocess.Popen(%5B%22playpen%22,%0A                           %22root-%22 + version,%0A                           %22--user=rust%22,%0A                           %22--timeout=5%22,%0A                           %22--syscalls-file=whitelist%22,%0A                           %22--devices=c:1:9%22,%0A                           %22--memory-limit=128M%22,%0A                           %22--%22,%0A                           %22/usr/local/bin/format.sh%22,%0A                           request.json%5B%22code%22%5D%5D,%0A                           stdout=subprocess.PIPE,%0A                           stderr=subprocess.STDOUT
+playpen(version, %22/usr/local/bin/evaluate.sh%22, request.json%5B%22code%22%5D) as p:%0A        return %7B%22result%22: p.stdout.read().decode()%7D%0A%0A@post(%22/format.json%22)%0Adef format():%0A    version = request.json%5B%22version%22%5D%0A    if version not in (%22master%22,):%0A        return %7B%22error%22: %22invalid version%22%7D%0A    print(request.json)%0A    with playpen(version, %22/usr/local/bin/format.sh%22, request.json%5B%22code%22%5D
 ) as

update for playpen changes