-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmain.py
More file actions
207 lines (175 loc) · 6.83 KB
/
Copy pathmain.py
File metadata and controls
207 lines (175 loc) · 6.83 KB
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
203
204
205
206
207
from flask import Flask, request, jsonify, send_file, send_from_directory
from werkzeug.utils import secure_filename
import os
from subprocess import call
from pathlib import Path
import json
import subprocess
from flask_cors import CORS, cross_origin
cur_path = os.path.dirname(os.path.abspath(__file__))
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
app = Flask(__name__)
app.secret_key = "secret key"
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
# To upload multiple file. For example, /upload/test?apikey=xyz
@app.route('/upload/<dir>', methods=['POST'])
def upload(dir):
apikey = request.args.get('apikey')
dirname = secure_filename(dir) + "_" + apikey
if 'files[]' not in request.files:
resp = jsonify({'message': 'No file in the request'})
resp.status_code = 400
return resp
files = request.files.getlist('files[]')
errors = {}
success = False
directory_name = os.path.abspath(os.path.join(concore_path, secure_filename(dirname)))
if not os.path.isdir(directory_name):
os.mkdir(directory_name)
for file in files:
if file:
filename = secure_filename(file.filename)
file.save(directory_name+"/"+filename)
success = True
if success and errors:
errors['message'] = 'File(s) successfully uploaded'
resp = jsonify(errors)
resp.status_code = 500
return resp
if success:
resp = jsonify({'message': 'Files successfully uploaded'})
resp.status_code = 201
return resp
else:
resp = jsonify(errors)
resp.status_code = 500
return resp
# to download /build/<dir>?fetch=<graphml>. For example, /build/test?fetch=sample1&apikey=xyz
@app.route('/build/<dir>', methods=['POST'])
def build(dir):
graphml_file = request.args.get('fetch')
apikey = request.args.get('apikey')
dirname = secure_filename(dir) + "_" + apikey
makestudy_dir = dirname + "/" + graphml_file #for makestudy
dir_path = os.path.abspath(os.path.join(concore_path, graphml_file)) #path for ./build
if not os.path.exists(dir_path):
proc = call(["makestudy", makestudy_dir],shell=True, cwd=concore_path)
if(proc == 0):
resp = jsonify({'message': 'Directory successfully created'})
resp.status_code = 201
else:
resp = jsonify({'message': 'There is an Error'})
resp.status_code = 500
else:
resp= jsonify({"message":"Success"})
resp.status_code=200
call(["build"],shell=True, cwd=dir_path)
return resp
# Give the directory of the build files that are build using ./build
@app.route('/debug/<dir>', methods=['POST'])
def debug(dir):
dir = secure_filename(dir)
dir_path = os.path.abspath(os.path.join(concore_path, dir))
proc = call(["debug"],shell=True , cwd=dir_path)
if(proc == 0):
resp = jsonify({'message': 'Close the pop window after obtaining result'})
resp.status_code = 201
return resp
else:
resp = jsonify({'message': 'There is an Error'})
resp.status_code = 500
return resp
# Give the directory of the build files that are build using ./build
@app.route('/run/<dir>', methods=['POST'])
def run(dir):
dir = secure_filename(dir)
dir_path = os.path.abspath(os.path.join(concore_path, dir))
print(dir_path)
proc = call(["run"],shell=True, cwd=dir_path)
if(proc == 0):
resp = jsonify({'message': 'result prepared'})
resp.status_code = 201
return resp
else:
resp = jsonify({'message': 'There is an Error'})
resp.status_code = 500
return resp
# Give the directory of the build files that are build using ./build
@app.route('/stop/<dir>', methods=['POST'])
def stop(dir):
dir = secure_filename(dir)
dir_path = os.path.abspath(os.path.join(concore_path, dir))
proc = call(["stop"],shell=True, cwd=dir_path)
if(proc == 0):
resp = jsonify({'message': 'resources cleaned'})
resp.status_code = 201
return resp
else:
resp = jsonify({'message': 'There is an Error'})
resp.status_code = 500
return resp
@app.route('/clear/<dir>', methods=['POST'])
def clear(dir):
dir = secure_filename(dir)
dir_path = os.path.abspath(os.path.join(concore_path, dir))
proc = call(["clear"],shell=True, cwd=dir_path)
if(proc == 0):
resp = jsonify({'message': 'result deleted'})
resp.status_code = 201
return resp
else:
resp = jsonify({'message': 'There is an Error'})
resp.status_code = 500
return resp
# to download /download/<dir>?fetch=<downloadfile>. For example, /download/test?fetchDir=xyz&fetch=u
@app.route('/download/<dir>', methods=['POST', 'GET'])
def download(dir):
download_file = request.args.get('fetch')
sub_folder = request.args.get('fetchDir')
dirname = secure_filename(dir) + "/" + secure_filename(sub_folder)
directory_name = os.path.abspath(os.path.join(concore_path, dirname))
if not os.path.exists(directory_name):
resp = jsonify({'message': 'Directory not found'})
resp.status_code = 400
return resp
try:
return send_from_directory(directory_name, download_file, as_attachment=True)
except:
resp = jsonify({'message': 'file not found'})
resp.status_code = 400
return resp
@app.route('/destroy/<dir>', methods=['DELETE'])
def destroy(dir):
dir = secure_filename(dir)
proc = call(["destroy", dir],shell=True, cwd=concore_path)
if(proc == 0):
resp = jsonify({'message': 'Successfuly deleted Dirctory'})
resp.status_code = 201
return resp
else:
resp = jsonify({'message': 'There is an Error'})
resp.status_code = 500
return resp
@app.route('/getFilesList/<dir>', methods=['POST'])
def getFilesList(dir):
sub_dir = request.args.get('fetch')
dirname = secure_filename(dir) + "/" + secure_filename(sub_dir)
dir_path = os.path.abspath(os.path.join(concore_path, dirname))
res = []
res = os.listdir(dir_path)
res = json.dumps(res)
return res
@app.route('/openJupyter', methods=['POST'])
def openJupyter():
proc = subprocess.Popen(['jupyter', 'lab'], shell=False, stdout=subprocess.PIPE, cwd=concore_path)
if proc.poll() is None:
resp = jsonify({'message': 'Successfuly opened Jupyter'})
resp.status_code = 308
return resp
else:
resp = jsonify({'message': 'There is an Error'})
resp.status_code = 500
return resp
if __name__ == "__main__":
app.run(host="0.0.0.0")