|
1 |
| -#importing dependencies - Flask for server and docker to communicate with docker API |
| 1 | +# importing dependencies - Flask for server and docker to communicate with docker API |
2 | 2 | from flask import Flask, jsonify, request
|
3 |
| -from flask_cors import CORS,cross_origin |
| 3 | +from flask_cors import CORS, cross_origin |
4 | 4 | import docker
|
5 | 5 | import os
|
6 | 6 | import tarfile
|
|
9 | 9 |
|
10 | 10 | cors = CORS(app)
|
11 | 11 |
|
12 |
| -app.config['CORS_HEADERS'] = 'Content-Type' |
| 12 | +app.config["CORS_HEADERS"] = "Content-Type" |
13 | 13 |
|
14 |
| -#Editing the file with code inside editor |
15 |
| -def edit_file(code): |
16 |
| - Fortran_file = open('./File.f90','w+') |
17 |
| - Fortran_file.write(code) |
| 14 | +# Starting container |
| 15 | +client = docker.from_env() |
| 16 | +container = client.containers.run("playground-small", tty=True, detach=True) |
18 | 17 |
|
| 18 | +# Editing the file with code inside editor |
| 19 | +def edit_file(code, input=""): |
| 20 | + Fortran_file = open("./File.f90", "w+") |
| 21 | + Fortran_file.write(code) |
| 22 | + program_input = open("./program_input.txt", "w+") |
| 23 | + program_input.write(input) |
19 | 24 |
|
20 | 25 |
|
21 |
| -#Copying file with fortran code to container |
| 26 | +# Copying file with fortran code to container |
22 | 27 | def copy_to(src, dst, container):
|
23 |
| - dst = dst |
24 |
| - container = container |
| 28 | + dst = dst |
| 29 | + container = container |
25 | 30 |
|
26 |
| - os.chdir(os.path.dirname(src)) |
27 |
| - srcname = os.path.basename(src) |
28 |
| - tar = tarfile.open(src + '.tar', mode='w') |
29 |
| - try: |
30 |
| - tar.add(srcname) |
31 |
| - finally: |
32 |
| - tar.close() |
| 31 | + os.chdir(os.path.dirname(src)) |
| 32 | + srcname = os.path.basename(src) |
| 33 | + tar = tarfile.open(src + ".tar", mode="w") |
| 34 | + try: |
| 35 | + tar.add(srcname) |
| 36 | + finally: |
| 37 | + tar.close() |
33 | 38 |
|
34 |
| - data = open(src + '.tar', 'rb').read() |
35 |
| - container.put_archive(os.path.dirname(dst), data) |
| 39 | + data = open(src + ".tar", "rb").read() |
| 40 | + container.put_archive(os.path.dirname(dst), data) |
36 | 41 |
|
37 | 42 |
|
38 |
| -#Executing code inside container and getting it's output |
| 43 | +# Executing code inside container and getting it's output |
39 | 44 | def execute_code_in_container():
|
40 |
| - client = docker.from_env() |
41 |
| - container = client.containers.run('playground-small', tty=True,detach=True) |
42 |
| - copy_to('./File.f90', '/fortran/File.f90',container) |
43 |
| - container.exec_run('gfortran File.f90 -o executed_file.o',demux=True) |
44 |
| - a = container.exec_run('./executed_file.o') |
45 |
| - container.stop() |
46 |
| - return a |
47 |
| - |
48 |
| - |
49 |
| -#API Endpoint to submit code |
50 |
| -@app.route('/run', methods=['POST','GET','OPTIONS']) |
| 45 | + copy_to("./File.f90", "/fortran/File.f90", container) |
| 46 | + copy_to("./program_input.txt", "/fortran/program_input.txt", container) |
| 47 | + executable = container.exec_run( |
| 48 | + "gfortran File.f90 -o executed_file.o", demux=True |
| 49 | + ) |
| 50 | + if executable.exit_code == 0: |
| 51 | + a = container.exec_run( |
| 52 | + 'sh -c "cat program_input.txt | ./executed_file.o"', demux=True |
| 53 | + ) |
| 54 | + else: |
| 55 | + a = executable |
| 56 | + return a |
| 57 | + |
| 58 | + |
| 59 | +# API Endpoint to submit code |
| 60 | +@app.route("/run", methods=["POST", "GET", "OPTIONS"]) |
51 | 61 | @cross_origin()
|
52 | 62 | def run_code():
|
53 |
| - data = request.get_json() |
54 |
| - edit_file(data["code"]) |
55 |
| - code_result = execute_code_in_container() |
56 |
| - output = jsonify({"executed" : code_result.output.decode()}) |
57 |
| - return output, 202 |
| 63 | + data = request.get_json() |
| 64 | + edit_file(data["code"], data["programInput"]) |
| 65 | + code_result = execute_code_in_container() |
| 66 | + if code_result.exit_code == 0: |
| 67 | + print(code_result.output[0].decode()) |
| 68 | + output = jsonify({"executed": code_result.output[0].decode()}) |
| 69 | + else: |
| 70 | + print(code_result.output[1].decode()) |
| 71 | + output = jsonify({"executed": code_result.output[1].decode()}) |
| 72 | + |
| 73 | + return output, 202 |
58 | 74 |
|
59 |
| -if __name__ == '__main__': |
60 |
| - app.run(debug = True) |
61 | 75 |
|
| 76 | +if __name__ == "__main__": |
| 77 | + app.run(debug=True) |
0 commit comments