diff --git a/fri/server/main.py b/fri/server/main.py index 51f8f97e..a548d341 100644 --- a/fri/server/main.py +++ b/fri/server/main.py @@ -1,10 +1,11 @@ from flask import Flask, request, jsonify, send_file, send_from_directory from werkzeug.utils import secure_filename import os +import subprocess from subprocess import call from pathlib import Path import json -import subprocess +import platform from flask_cors import CORS, cross_origin cur_path = os.path.dirname(os.path.abspath(__file__)) @@ -21,8 +22,10 @@ @app.route('/upload/', methods=['POST']) def upload(dir): apikey = request.args.get('apikey') - dirname = secure_filename(dir) + "_" + apikey - + if(apikey == None): + dirname = secure_filename(dir) + else: + dirname = secure_filename(dir) + "_" + apikey if 'files[]' not in request.files: resp = jsonify({'message': 'No file in the request'}) resp.status_code = 400 @@ -38,7 +41,6 @@ def upload(dir): if not os.path.isdir(directory_name): os.mkdir(directory_name) - for file in files: if file: filename = secure_filename(file.filename) @@ -60,32 +62,42 @@ def upload(dir): return resp - # to download /build/?fetch=. For example, /build/test?fetch=sample1&apikey=xyz @app.route('/build/', methods=['POST']) def build(dir): graphml_file = request.args.get('fetch') apikey = request.args.get('apikey') - dirname = secure_filename(dir) + "_" + apikey + if(apikey == None): + dirname = secure_filename(dir) + else: + 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], cwd=concore_path) + if(platform.uname()[0]=='Windows'): + proc= call(["makestudy", makestudy_dir], shell=True, cwd=concore_path) + else: + proc = call(["./makestudy", makestudy_dir], 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 - call(["./build"], cwd=dir_path) + resp.status_code = 500 + if(platform.uname()[0]=='Windows'): + call(["build"], cwd=dir_path, shell=True) + else: + call(["./build"], cwd=dir_path) return resp - @app.route('/debug/', methods=['POST']) def debug(dir): - dir = secure_filename(dir) - dir_path = os.path.abspath(os.path.join(concore_path, dir)) - proc = call(["./debug"], cwd=dir_path) + dir_name = secure_filename(dir) + dir_path = os.path.abspath(os.path.join(concore_path, dir_name)) + if(platform.uname()[0]=='Windows'): + proc=call(["debug"],shell=True, cwd=dir_path) + else: + proc = call(["./debug"], cwd=dir_path) if(proc == 0): resp = jsonify({'message': 'Close the pop window after obtaining result'}) resp.status_code = 201 @@ -95,12 +107,14 @@ def debug(dir): resp.status_code = 500 return resp - @app.route('/run/', methods=['POST']) def run(dir): - dir = secure_filename(dir) - dir_path = os.path.abspath(os.path.join(concore_path, dir)) - proc = call(["./run"], cwd=dir_path) + dir_name = secure_filename(dir) + dir_path = os.path.abspath(os.path.join(concore_path, dir_name)) + if(platform.uname()[0]=='Windows'): + proc=call(["run"],shell=True, cwd=dir_path) + else: + proc = call(["./run"], cwd=dir_path) if(proc == 0): resp = jsonify({'message': 'result prepared'}) resp.status_code = 201 @@ -112,9 +126,12 @@ def run(dir): @app.route('/stop/', methods=['POST']) def stop(dir): - dir = secure_filename(dir) - dir_path = os.path.abspath(os.path.join(concore_path, dir)) - proc = call(["./stop"], cwd=dir_path) + dir_name = secure_filename(dir) + dir_path = os.path.abspath(os.path.join(concore_path, dir_name)) + if(platform.uname()[0]=='Windows'): + proc=call(["stop"],shell=True, cwd=dir_path) + else: + proc = call(["./stop"], cwd=dir_path) if(proc == 0): resp = jsonify({'message': 'resources cleaned'}) resp.status_code = 201 @@ -127,9 +144,12 @@ def stop(dir): @app.route('/clear/', methods=['POST']) def clear(dir): - dir = secure_filename(dir) - dir_path = os.path.abspath(os.path.join(concore_path, dir)) - proc = call(["./clear"], cwd=dir_path) + dir_name = secure_filename(dir) + dir_path = os.path.abspath(os.path.join(concore_path, dir_name)) + if(platform.uname()[0]=='Windows'): + proc=call(["clear"],shell=True, cwd=dir_path) + else: + proc = call(["./clear"], cwd=dir_path) if(proc == 0): resp = jsonify({'message': 'result deleted'}) resp.status_code = 201 @@ -157,11 +177,13 @@ def download(dir): resp.status_code = 400 return resp - @app.route('/destroy/', methods=['DELETE']) def destroy(dir): dir = secure_filename(dir) - proc = call(["./destroy", dir], cwd=concore_path) + if(platform.uname()[0]=='Windows'): + proc=call(["destroy"],shell=True, cwd=concore_path) + else: + proc = call(["./destroy"], cwd=concore_path) if(proc == 0): resp = jsonify({'message': 'Successfuly deleted Dirctory'}) resp.status_code = 201 @@ -181,7 +203,6 @@ def getFilesList(dir): 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) @@ -195,6 +216,5 @@ def openJupyter(): return resp - if __name__ == "__main__": app.run(host="0.0.0.0", port=5000) diff --git a/fri/test.py b/fri/test.py index 684c6eff..8204c687 100644 --- a/fri/test.py +++ b/fri/test.py @@ -13,7 +13,6 @@ def upload(files): response = requests.request("POST", url, headers=headers, data=payload, files=files) print(response.text) - # # ******* # function to check build @@ -23,37 +22,36 @@ def build(dir, graphml, apikey): print(response.text) # function to debug -def debug(graphml): - url = "http://127.0.0.1:5000/debug/"+graphml +def debug(graphml, apikey): + url = "http://127.0.0.1:5000/debug/"+graphml+"?"+"apikey="+apikey response = requests.request("POST", url) print(response.text) # function to test run() method. -def run(graphml): - url = "http://127.0.0.1:5000/run/"+graphml +def run(graphml, apikey): + url = "http://127.0.0.1:5000/run/"+graphml+"?"+"apikey="+apikey response = requests.request("POST", url) print(response.text) -def clear(graphml): - url = "http://127.0.0.1:5000/clear/"+graphml +def clear(graphml, apikey): + url = "http://127.0.0.1:5000/clear/"+graphml+"?"+"apikey="+apikey response = requests.request("POST", url) print(response.text) -def stop(graphml): - url = "http://127.0.0.1:5000/stop/"+graphml +def stop(graphml, apikey): + url = "http://127.0.0.1:5000/stop/"+graphml+"?"+"apikey="+apikey response = requests.request("POST", url) print(response.text) - #function to destroy dir. -def destroy(dir): - url = "http://127.0.0.1:5000/destroy/" + dir +def destroy(dir, apikey): + url = "http://127.0.0.1:5000/destroy/" + dir+"?"+"apikey="+apikey response = requests.request("DELETE", url) print(response.text) -def getFilesList(dir, sub_dir = ""): - url = "http://127.0.0.1:5000/getFilesList/" + dir + "?"+"fetch="+sub_dir +def getFilesList(apikey, dir, sub_dir = ""): + url = "http://127.0.0.1:5000/getFilesList/" + dir + "?"+"fetch="+sub_dir+"&"+"apikey="+apikey response = requests.request("POST", url) print(response.text) @@ -63,8 +61,8 @@ def openJupyter(): print(response.text) # function to test download() method. -def download(dir, subDir, fileName ): - url = "http://127.0.0.1:5000/download/"+dir+"?"+"fetchDir="+subDir+"&"+"fetch="+ fileName +def download(dir, subDir, fileName , apikey ): + url = "http://127.0.0.1:5000/download/"+dir+"?"+"fetchDir="+subDir+"&"+"fetch="+ fileName+"&"+"apikey="+apikey urllib.request.urlretrieve(url, fileName) # file list to be uploaded @@ -83,25 +81,22 @@ def download(dir, subDir, fileName ): ('files[]',(file_name3,open(path_file3,'rb'),'application/octet-stream')), ] - upload(files) time.sleep(2) build("test", "sample", "xyz") time.sleep(6) method = input("methods - 1 for debug, 0 for run :") if method == "1": - debug("sample") + debug("sample", "xyz") else: - run("sample") + run("sample", "xyz") time.sleep(2) -stop("sample") +stop("sample", "xyz") time.sleep(2) -getFilesList("sample", "cu") -getFilesList("sample", "pym") +getFilesList("xyz", "sample", "CU") +getFilesList("xyz","sample", "PYM") time.sleep(5) -download("sample", "cu", "u") -clear("sample") -destroy("sample") +download("sample", "CU", "u", "xyz") +clear("sample", "xyz") +destroy("sample", "xyz") openJupyter() - -