-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexemple_exec.py
79 lines (60 loc) · 1.77 KB
/
exemple_exec.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
import time
import json
from bottle import route, run, template, response, get, post, request, Bottle
app = Bottle()
@app.get("/text")
def text_form():
return """
Votre code:
<form action="/text" method="post">
<textarea name="code" rows="50" cols="100">
def mafonction(n):
#votre code ici
return None
</textarea>
<input value="Ajouter" type="submit" />
</form>
"""
from importlib import import_module
from itertools import count
template_name = "tcode_%s"
counter = count()
dockerfile_template = """
FROM python:3.7-alpine
RUN python3 -m pip install bottle
COPY . /app
WORKDIR /app
#RUN service nginx start
EXPOSE {port}
CMD ["python","applauncher.py", "{port}"]
"""
@app.post("/text") # or @route('/login', method='POST')
def process_text():
code = request.forms.get("code") or request.json.get("kind")
number = next(counter)
port = number
dirname = f"{template_name}" % number
import shutil
os.mkdir(dirname)
shutil.copyfile("applauncher.py", f"{dirname}/applauncher.py")
os.chdir(dirname)
# module_name = f"{template_name}" % number
with open(f"app.py", "w") as f:
f.write(code)
port = str(8000 + number)
with open(f"Dockerfile", "w") as f:
f.write(dockerfile_template.format(**{"port": port}))
import subprocess
import shlex
for x in str(
subprocess.check_output(shlex.split(f"docker build -t demo_{number} .")).decode(
"utf-8"
)
).split("\n"):
yield "%s<br/>" % x
os.chdir("..")
subprocess.call(shlex.split(f"docker run -p {port}:{port} -d demo_{number}"))
if __name__ == "__main__":
import os
os.system("rm -r tcode_*")
app.run(host="localhost", port=8080, debug=True)