Skip to content

Commit 788ffbe

Browse files
committed
Added backend for fpm library processing
1 parent e0db90e commit 788ffbe

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

Backend/Pipfile

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ flask = "*"
88
docker = "*"
99
flask-cors = "*"
1010
pyyaml = "*"
11+
tomlkit = "*"
1112

1213
[dev-packages]

Backend/app.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import tarfile
77
import yaml
88
import json
9+
import tomlkit
10+
911
app = Flask(__name__)
1012

1113
cors = CORS(app)
@@ -14,7 +16,7 @@
1416

1517
# Starting container
1618
client = docker.from_env()
17-
container = client.containers.run("playground-fpm", tty=True, detach=True, network_disabled=True, mem_limit="16g")
19+
container = client.containers.run("playground-f", tty=True, detach=True, network_disabled=True, mem_limit="16g")
1820

1921
#Converting tutorial YAML
2022
with open('tutorial.yml', 'r') as file:
@@ -25,13 +27,22 @@
2527

2628

2729
# Editing the file with code inside editor
28-
def edit_file(code, input=""):
29-
Fortran_file = open("./main.f90", "w+")
30+
def edit_file(code, input, libs):
31+
Fortran_file = open("./main.f90", "w+") #main source code from editor
3032
Fortran_file.write(code)
3133
Fortran_file.close()
32-
program_input = open("./program_input.txt", "w+")
34+
program_input = open("./program_input.txt", "w+") #user input
3335
program_input.write(input)
3436
program_input.close()
37+
#Generating fpm.toml for fpm processing
38+
with open("fpm.toml", mode="rt", encoding="utf-8") as fp:
39+
fpm = tomlkit.load(fp)
40+
if "stdlib" in libs:
41+
fpm["dependencies"] = {'stdlib' : {'path' : "libraries/stdlib"}}
42+
else:
43+
fpm["dependencies"] = {}
44+
with open("fpm.toml", mode="wt", encoding="utf-8") as fp:
45+
tomlkit.dump(fpm, fp)
3546

3647
# Copying file with fortran code to container
3748
def copy_to(src, dst, container):
@@ -54,9 +65,9 @@ def copy_to(src, dst, container):
5465
def execute_code_in_container():
5566
copy_to('./main.f90', '/fortran/playground/app/main.f90', container)
5667
copy_to('./program_input.txt', '/fortran/playground/program_input.txt', container)
57-
58-
container.exec_run('sh -c "/fortran/fpm-0.6.0-linux-x86_64 build"')
59-
a = container.exec_run('sh -c "cat program_input.txt | timeout 15s /fortran/fpm-0.6.0-linux-x86_64 run"',demux=True)
68+
copy_to('./fpm.toml','/fortran/playground/fpm.toml', container)
69+
container.exec_run('sh -c "/fortran/fpm build"')
70+
a = container.exec_run('sh -c "cat program_input.txt | timeout 15s /fortran/fpm run"',demux=True)
6071

6172
return a
6273

@@ -66,12 +77,13 @@ def execute_code_in_container():
6677
@cross_origin()
6778
def run_code():
6879
data = request.get_json()
69-
edit_file(data["code"], data["programInput"])
80+
edit_file(data["code"], data["programInput"], data["libs"])
7081
code_result = execute_code_in_container()
7182
if code_result.output[0] == None:
7283
output = jsonify({"executed": ""})
7384
return output, 202
7485
output = jsonify({"executed": code_result.output[0].decode()})
86+
print(code_result.output)
7587

7688

7789
return output, 202

Backend/fpm.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name = "playground"
2+
author = "Ashirwad Mishra"
3+
maintainer = "[email protected]"
4+
5+
[build]
6+
auto-executables = true
7+
auto-tests = true
8+
auto-examples = true
9+
10+
[install]
11+
library = false
12+
13+
[dependencies]

0 commit comments

Comments
 (0)