|
| 1 | +from flask import Flask, request, jsonify, send_file, send_from_directory |
| 2 | +from werkzeug.utils import secure_filename |
| 3 | +import os |
| 4 | +from subprocess import call |
| 5 | +from pathlib import Path |
| 6 | +import json |
| 7 | +import subprocess |
| 8 | + |
| 9 | +cur_path = os.path.dirname(os.path.abspath(__file__)) |
| 10 | +concore_path = os.path.abspath(os.path.join(cur_path, '../../')) |
| 11 | + |
| 12 | + |
| 13 | +app = Flask(__name__) |
| 14 | +app.secret_key = "secret key" |
| 15 | + |
| 16 | +# To upload multiple file. For example, /upload/test?apikey=xyz |
| 17 | +@app.route('/upload/<dir>', methods=['POST']) |
| 18 | +def upload(dir): |
| 19 | + apikey = request.args.get('apikey') |
| 20 | + dirname = secure_filename(dir) + "_" + apikey |
| 21 | + |
| 22 | + if 'files[]' not in request.files: |
| 23 | + resp = jsonify({'message': 'No file in the request'}) |
| 24 | + resp.status_code = 400 |
| 25 | + return resp |
| 26 | + |
| 27 | + files = request.files.getlist('files[]') |
| 28 | + |
| 29 | + errors = {} |
| 30 | + success = False |
| 31 | + |
| 32 | + directory_name = os.path.abspath(os.path.join(concore_path, secure_filename(dirname))) |
| 33 | + |
| 34 | + if not os.path.isdir(directory_name): |
| 35 | + os.mkdir(directory_name) |
| 36 | + |
| 37 | + |
| 38 | + for file in files: |
| 39 | + if file: |
| 40 | + filename = secure_filename(file.filename) |
| 41 | + file.save(directory_name+"/"+filename) |
| 42 | + success = True |
| 43 | + |
| 44 | + if success and errors: |
| 45 | + errors['message'] = 'File(s) successfully uploaded' |
| 46 | + resp = jsonify(errors) |
| 47 | + resp.status_code = 500 |
| 48 | + return resp |
| 49 | + if success: |
| 50 | + resp = jsonify({'message': 'Files successfully uploaded'}) |
| 51 | + resp.status_code = 201 |
| 52 | + return resp |
| 53 | + else: |
| 54 | + resp = jsonify(errors) |
| 55 | + resp.status_code = 500 |
| 56 | + return resp |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +# to download /build/<dir>?fetch=<graphml>. For example, /build/test?fetch=sample1&apikey=xyz |
| 61 | +@app.route('/build/<dir>', methods=['POST']) |
| 62 | +def build(dir): |
| 63 | + graphml_file = request.args.get('fetch') |
| 64 | + apikey = request.args.get('apikey') |
| 65 | + dirname = secure_filename(dir) + "_" + apikey |
| 66 | + makestudy_dir = dirname + "/" + graphml_file #for makestudy |
| 67 | + dir_path = os.path.abspath(os.path.join(concore_path, graphml_file)) #path for ./build |
| 68 | + if not os.path.exists(dir_path): |
| 69 | + proc = call(["./makestudy", makestudy_dir], cwd=concore_path) |
| 70 | + if(proc == 0): |
| 71 | + resp = jsonify({'message': 'Directory successfully created'}) |
| 72 | + resp.status_code = 201 |
| 73 | + else: |
| 74 | + resp = jsonify({'message': 'There is an Error'}) |
| 75 | + resp.status_code = 500 |
| 76 | + call(["./build"], cwd=dir_path) |
| 77 | + return resp |
| 78 | + |
| 79 | + |
| 80 | +@app.route('/debug/<dir>', methods=['POST']) |
| 81 | +def debug(dir): |
| 82 | + dir = secure_filename(dir) |
| 83 | + dir_path = os.path.abspath(os.path.join(concore_path, dir)) |
| 84 | + proc = call(["./debug"], cwd=dir_path) |
| 85 | + if(proc == 0): |
| 86 | + resp = jsonify({'message': 'Close the pop window after obtaining result'}) |
| 87 | + resp.status_code = 201 |
| 88 | + return resp |
| 89 | + else: |
| 90 | + resp = jsonify({'message': 'There is an Error'}) |
| 91 | + resp.status_code = 500 |
| 92 | + return resp |
| 93 | + |
| 94 | + |
| 95 | +@app.route('/run/<dir>', methods=['POST']) |
| 96 | +def run(dir): |
| 97 | + dir = secure_filename(dir) |
| 98 | + dir_path = os.path.abspath(os.path.join(concore_path, dir)) |
| 99 | + proc = call(["./run"], cwd=dir_path) |
| 100 | + if(proc == 0): |
| 101 | + resp = jsonify({'message': 'result prepared'}) |
| 102 | + resp.status_code = 201 |
| 103 | + return resp |
| 104 | + else: |
| 105 | + resp = jsonify({'message': 'There is an Error'}) |
| 106 | + resp.status_code = 500 |
| 107 | + return resp |
| 108 | + |
| 109 | +@app.route('/stop/<dir>', methods=['POST']) |
| 110 | +def stop(dir): |
| 111 | + dir = secure_filename(dir) |
| 112 | + dir_path = os.path.abspath(os.path.join(concore_path, dir)) |
| 113 | + proc = call(["./stop"], cwd=dir_path) |
| 114 | + if(proc == 0): |
| 115 | + resp = jsonify({'message': 'resources cleaned'}) |
| 116 | + resp.status_code = 201 |
| 117 | + return resp |
| 118 | + else: |
| 119 | + resp = jsonify({'message': 'There is an Error'}) |
| 120 | + resp.status_code = 500 |
| 121 | + return resp |
| 122 | + |
| 123 | + |
| 124 | +@app.route('/clear/<dir>', methods=['POST']) |
| 125 | +def clear(dir): |
| 126 | + dir = secure_filename(dir) |
| 127 | + dir_path = os.path.abspath(os.path.join(concore_path, dir)) |
| 128 | + proc = call(["./clear"], cwd=dir_path) |
| 129 | + if(proc == 0): |
| 130 | + resp = jsonify({'message': 'result deleted'}) |
| 131 | + resp.status_code = 201 |
| 132 | + return resp |
| 133 | + else: |
| 134 | + resp = jsonify({'message': 'There is an Error'}) |
| 135 | + resp.status_code = 500 |
| 136 | + return resp |
| 137 | + |
| 138 | +# to download /download/<dir>?fetch=<downloadfile>. For example, /download/test?fetchDir=xyz&fetch=u |
| 139 | +@app.route('/download/<dir>', methods=['POST', 'GET']) |
| 140 | +def download(dir): |
| 141 | + download_file = request.args.get('fetch') |
| 142 | + sub_folder = request.args.get('fetchDir') |
| 143 | + dirname = secure_filename(dir) + "/" + secure_filename(sub_folder) |
| 144 | + directory_name = os.path.abspath(os.path.join(concore_path, dirname)) |
| 145 | + if not os.path.exists(directory_name): |
| 146 | + resp = jsonify({'message': 'Directory not found'}) |
| 147 | + resp.status_code = 400 |
| 148 | + return resp |
| 149 | + try: |
| 150 | + return send_from_directory(directory_name, download_file, as_attachment=True) |
| 151 | + except: |
| 152 | + resp = jsonify({'message': 'file not found'}) |
| 153 | + resp.status_code = 400 |
| 154 | + return resp |
| 155 | + |
| 156 | + |
| 157 | +@app.route('/destroy/<dir>', methods=['DELETE']) |
| 158 | +def destroy(dir): |
| 159 | + dir = secure_filename(dir) |
| 160 | + proc = call(["./destroy", dir], cwd=concore_path) |
| 161 | + if(proc == 0): |
| 162 | + resp = jsonify({'message': 'Successfuly deleted Dirctory'}) |
| 163 | + resp.status_code = 201 |
| 164 | + return resp |
| 165 | + else: |
| 166 | + resp = jsonify({'message': 'There is an Error'}) |
| 167 | + resp.status_code = 500 |
| 168 | + return resp |
| 169 | + |
| 170 | +@app.route('/getFilesList/<dir>', methods=['POST']) |
| 171 | +def getFilesList(dir): |
| 172 | + sub_dir = request.args.get('fetch') |
| 173 | + dirname = secure_filename(dir) + "/" + secure_filename(sub_dir) |
| 174 | + dir_path = os.path.abspath(os.path.join(concore_path, dirname)) |
| 175 | + res = [] |
| 176 | + res = os.listdir(dir_path) |
| 177 | + res = json.dumps(res) |
| 178 | + return res |
| 179 | + |
| 180 | + |
| 181 | +@app.route('/openJupyter/', methods=['POST']) |
| 182 | +def openJupyter(): |
| 183 | + proc = subprocess.Popen(['jupyter', 'lab'], shell=False, stdout=subprocess.PIPE, cwd=concore_path) |
| 184 | + if proc.poll() is None: |
| 185 | + resp = jsonify({'message': 'Successfuly opened Jupyter'}) |
| 186 | + resp.status_code = 308 |
| 187 | + return resp |
| 188 | + else: |
| 189 | + resp = jsonify({'message': 'There is an Error'}) |
| 190 | + resp.status_code = 500 |
| 191 | + return resp |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | +if __name__ == "__main__": |
| 196 | + app.run(host="0.0.0.0") |
0 commit comments