-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
178 lines (140 loc) · 3.89 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
import flask
import sys
import time
from flask import request, jsonify
from io import StringIO
from contextlib import redirect_stdout
from flask_cors import CORS, cross_origin
from bridge_utils import code_to_file
from flask import stream_with_context, request
import code_engine
import bridge_utils
import os
"""
Create, Remove, Read, Write, Rename File
Create, Remove, Rename Folder
"""
app = flask.Flask(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'
@app.route('/init', methods=['POST'])
@cross_origin()
def init():
body = request.get_json()
userid = str(body['id'])
user_path = f"tmp/{userid}"
if userid:
if not os.path.exists(user_path):
os.mkdir(user_path)
f = open(f"{user_path}/main.py", "w")
f.write("""print("Hello World!")""")
f.close()
output = "[200] Successfully Initialized A User."
else:
output = "[200] The User Has Already Been Initialized. Skipping..."
else:
output = "[ERR] Missing ID Param."
print({
'output': output
})
return {
'output': output
}
# await time.sleep(20)
return str(output)
@app.route('/files/getlist', methods=['GET']) # List of the files in the directory
@cross_origin()
def get_list_from_index():
body = request.get_json()
userid = str(body['id'])
user_path = f"tmp/{userid}"
testarr = []
if os.path.exists(user_path):
for root, directories, files in os.walk(user_path, topdown=False):
for name in files:
testarr.append(os.path.join(root, name))
for name in directories:
testarr.append(os.path.join(root, name))
print({'output': testarr})
return {'output': testarr}
# await time.sleep(20)
return str(testarr)
@app.route('/files/getcontent', methods=['POST'])
@cross_origin()
def getcontent():
body = request.get_json()
userid = str(body['id'])
file_path =
if userid:
if not os.path.exists(user_path):
os.mkdir(user_path)
f = open(f"{user_path}/main.py", "w")
f.write("""print("Hello World!")""")
f.close()
output = "[200] Successfully Initialized A User."
else:
output = "[200] The User Has Already Been Initialized. Skipping..."
else:
output = "[ERR] Missing ID Param."
print({
'output': output
})
return {
'output': output
}
# await time.sleep(20)
return str(output)
@app.route('/run', methods=['POST'])
@cross_origin()
def run():
body = request.get_json()
code = body['code']
language = body['language']
thread_id = str(body['id'])
input_arr = body['input']
# code_to_file(code, language, id)
output_path, input_path = bridge_utils.generate_files(code, language, thread_id)
needs_input, output = code_engine.run(thread_id, output_path, input_arr, language, code)
print({
'needs_input': needs_input,
'output': output
})
return {
'needs_input': needs_input,
'output': output
}
# await time.sleep(20)
return str(output)
app.run(host="0.0.0.0", port=8000)
@app.route('/folder/delete', methods=['GET'])
@cross_origin()
def delete_folder():
body = request.get_json()
userid = str(body['id'])
user_path = f"tmp/{userid}"
try:
os.rmdir(user_path)
except FileNotFoundError:
print('Couldn\'t find specified folder')
return None
@app.route('/folder/create', methods=['GET'])
@cross_origin()
def create_folder():
body = request.get_json()
userid = str(body['id'])
user_path = f"tmp/{userid}"
try:
os.mkdir(user_path)
except FileNotFoundError:
print('Couldn\'t find specified folder')
return None
@app.route('/folder/rename', methods=['GET'])
@cross_origin()
def rename_folder():
body = request.get_json()
userid = str(body['id'])
user_path = f"tmp/{userid}"
try:
os.rename(user_path)
except FileNotFoundError:
print('Couldn\'t specified folder')
return None