This repository has been archived by the owner on Sep 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
202 lines (160 loc) · 5.1 KB
/
main.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
# uvicorn main:app --port 8000 // Run locally
import asyncio
import os
import platform
import subprocess
import uuid
from concurrent.futures.process import ProcessPoolExecutor
from enum import Enum
from subprocess import TimeoutExpired
from typing import Optional
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
app = FastAPI()
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# From Stack Overflow
# Enables concurrent processing
async def run_in_process(fn, *args):
loop = asyncio.get_event_loop()
return await loop.run_in_executor(
app.state.executor, fn, *args
) # wait and return result
# From Stack Overflow
@app.on_event("startup")
async def on_startup():
app.state.executor = ProcessPoolExecutor()
# From Stack Overflow
@app.on_event("shutdown")
async def on_shutdown():
app.state.executor.shutdown()
class TintSubmission(BaseModel):
program: str
tests: str
verbose: Optional[bool]
class Config:
schema_extra = {
"example": {
"program": "start: start \naccept: accept \nreject: reject \n\ntransitions:\n- [start, a, q1, _, R]\n- [start, _, reject, _, R]\n- [q1, a, accept, _, R]\n- [q1, _, reject, _, R]",
"tests": "a b\n",
"verbose": True,
}
}
class TintOutput(BaseModel):
status: int
output: str
class Config:
schema_extra = {
"example": {
"status": 200,
"output": 'Simulating with "a b".\nstart: a b _\n ^\nq1: _ b _\n ^\naccept: _ _ _\n ^\nAccepted.\n\n1 accepted.\n0 rejected.\n0 errors.\n',
}
}
class MachineType(Enum):
DFA = "dfa"
ONE_WAY_TM = "one-way-tm"
TWO_WAY_TM = "two-way-tm"
def run_machine(ts: TintSubmission, machine: MachineType) -> TintOutput:
"""Run the given machine type with the given tint code."""
# create tint file
tint_file_name = f"./{str(uuid.uuid4().hex)}.txt"
tint_file = open(tint_file_name, "w")
tint_file.write(ts.program)
tint_file.close()
# create test file
test_file_name = f"./{str(uuid.uuid4().hex)}.txt"
test_file = open(test_file_name, "w")
test_file.write(ts.tests)
test_file.close()
opSys = platform.platform().lower() # current operating system
try:
# match binary to current operating system
if opSys.find("linux") >= 0:
binary_file = "./tint-linux"
elif opSys.find("macos") >= 0:
binary_file = "./tint-mac"
else:
raise Exception(opSys)
# build command
if ts.verbose:
tint_args = [
binary_file,
"-m",
machine.value,
"-v",
tint_file_name,
test_file_name,
]
else:
tint_args = [
binary_file,
"-m",
machine.value,
tint_file_name,
test_file_name,
]
# run process
process = subprocess.run(tint_args, capture_output=True, timeout=3)
output = process.stdout
status = 200
except TimeoutExpired:
output = "Error: Program took too long or encountered an infinite loop"
status = 400
except Exception:
output = "Error: Something went wrong"
status = 400
finally:
# clean up
os.remove(tint_file_name)
os.remove(test_file_name)
return {"status": status, "output": output}
@app.post(
"/api/one-way-tm",
description="Runs a one-way turing machine program on the given tests.",
response_model=TintOutput,
)
async def run_one_way_turing_machine(tm: TintSubmission) -> TintOutput:
"""Endpoint for running a one-way turing machine in Tint."""
res = await run_in_process(run_machine, tm, MachineType.ONE_WAY_TM)
return res
@app.post(
"/api/two-way-tm",
description="Runs a two-way turing machine program on the given tests.",
response_model=TintOutput,
)
async def run_two_way_turing_machine(tm: TintSubmission) -> TintOutput:
"""Endpoint for running a two-way turing machine in Tint."""
res = await run_in_process(run_machine, tm, MachineType.TWO_WAY_TM)
return res
@app.post(
"/api/dfa",
description="Runs a dfa program on the given tests.",
response_model=TintOutput,
)
async def run_dfa(tm: TintSubmission) -> TintOutput:
"""Endpoint for running a deterministic finite automata in Tint."""
res = await run_in_process(run_machine, tm, MachineType.DFA)
return res
class StartOutput(BaseModel):
status: int
class Config:
schema_extra = {
"example": {
"status": 200,
}
}
@app.get(
"/api/start",
description="Starts the API and confirms it is awake.",
response_model=StartOutput,
)
async def confirmAwake() -> StartOutput:
"""Endpoint for confirming the API is responding."""
return {"status": 200}