Skip to content

Commit 8e53f0a

Browse files
committed
added clear and stop method
1 parent 27442fe commit 8e53f0a

2 files changed

Lines changed: 75 additions & 82 deletions

File tree

fri/server/main.py

Lines changed: 45 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import json
77
import subprocess
88

9+
cur_path = os.path.dirname(os.path.abspath(__file__))
10+
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
11+
912

1013
app = Flask(__name__)
1114
app.secret_key = "secret key"
@@ -26,8 +29,6 @@ def upload(dir):
2629
errors = {}
2730
success = False
2831

29-
cur_path = os.path.dirname(os.path.abspath(__file__))
30-
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
3132
directory_name = os.path.abspath(os.path.join(concore_path, secure_filename(dirname)))
3233

3334
if not os.path.isdir(directory_name):
@@ -54,58 +55,15 @@ def upload(dir):
5455
resp.status_code = 500
5556
return resp
5657

57-
# To execute any python file. For example, /execute/test?apikey=xyz
58-
@app.route('/execute/<dir>', methods=['POST'])
59-
def execute(dir):
60-
apikey = request.args.get('apikey')
61-
dirname = dir + "_" + apikey
62-
63-
if 'file' not in request.files:
64-
resp = jsonify({'message': 'No file in the request'})
65-
resp.status_code = 400
66-
return resp
67-
68-
file = request.files['file']
69-
70-
if file.filename == '':
71-
resp = jsonify({'message': 'No file selected for Executing'})
72-
resp.status_code = 400
73-
return resp
74-
75-
errors = {}
76-
success = False
77-
78-
if not os.path.exists(secure_filename(dirname)):
79-
os.makedirs(secure_filename(dirname))
80-
81-
if file:
82-
filename = secure_filename(file.filename)
83-
file.save(secure_filename(dirname)+"/"+filename)
84-
output_filename = filename + ".out"
85-
file_path = secure_filename(dirname) + "/"+filename
86-
outputfile_path = secure_filename(dirname)+"/"+output_filename
87-
f = open(outputfile_path, "w")
88-
call(["nohup", "python3", file_path], stdout=f)
89-
success = True
9058

91-
if success:
92-
resp = jsonify({'message': 'Files successfully executed'})
93-
resp.status_code = 201
94-
return resp
95-
else:
96-
resp = jsonify(errors)
97-
resp.status_code = 500
98-
return resp
9959

100-
# to download /build/<dir>?fetch=<graphml>. For example, /build/test?fetch=sample1
60+
# to download /build/<dir>?fetch=<graphml>. For example, /build/test?fetch=sample1&apikey=xyz
10161
@app.route('/build/<dir>', methods=['POST'])
10262
def build(dir):
10363
graphml_file = request.args.get('fetch')
10464
apikey = request.args.get('apikey')
10565
dirname = dir + "_" + apikey
10666
makestudy_dir = dirname + "/" + graphml_file #for makestudy
107-
cur_path = os.path.dirname(os.path.abspath(__file__))
108-
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
10967
dir_path = os.path.abspath(os.path.join(concore_path, graphml_file)) #path for ./build
11068
if not os.path.exists(secure_filename(dir_path)):
11169
proc = call(["./makestudy", makestudy_dir], cwd=concore_path)
@@ -121,8 +79,6 @@ def build(dir):
12179

12280
@app.route('/debug/<dir>', methods=['POST'])
12381
def debug(dir):
124-
cur_path = os.path.dirname(os.path.abspath(__file__))
125-
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
12682
dir_path = os.path.abspath(os.path.join(concore_path, dir))
12783
proc = call(["./debug"], cwd=dir_path)
12884
if(proc == 0):
@@ -134,15 +90,53 @@ def debug(dir):
13490
resp.status_code = 500
13591
return resp
13692

93+
94+
@app.route('/run/<dir>', methods=['POST'])
95+
def run(dir):
96+
dir_path = os.path.abspath(os.path.join(concore_path, dir))
97+
proc = call(["./run"], cwd=dir_path)
98+
if(proc == 0):
99+
resp = jsonify({'message': 'result prepared'})
100+
resp.status_code = 201
101+
return resp
102+
else:
103+
resp = jsonify({'message': 'There is an Error'})
104+
resp.status_code = 500
105+
return resp
106+
107+
@app.route('/stop/<dir>', methods=['POST'])
108+
def stop(dir):
109+
dir_path = os.path.abspath(os.path.join(concore_path, dir))
110+
proc = call(["./stop"], cwd=dir_path)
111+
if(proc == 0):
112+
resp = jsonify({'message': 'resources cleaned'})
113+
resp.status_code = 201
114+
return resp
115+
else:
116+
resp = jsonify({'message': 'There is an Error'})
117+
resp.status_code = 500
118+
return resp
119+
120+
121+
@app.route('/clear/<dir>', methods=['POST'])
122+
def clear(dir):
123+
dir_path = os.path.abspath(os.path.join(concore_path, dir))
124+
proc = call(["./clear"], cwd=dir_path)
125+
if(proc == 0):
126+
resp = jsonify({'message': 'result deleted'})
127+
resp.status_code = 201
128+
return resp
129+
else:
130+
resp = jsonify({'message': 'There is an Error'})
131+
resp.status_code = 500
132+
return resp
133+
137134
# to download /download/<dir>?fetch=<downloadfile>. For example, /download/test?fetch=example.py.out&apikey=xyz
138135
@app.route('/download/<dir>', methods=['POST', 'GET'])
139136
def download(dir):
140137
download_file = request.args.get('fetch')
141138
apikey = request.args.get('apikey')
142139
dirname = dir + "_" + apikey
143-
144-
cur_path = os.path.dirname(os.path.abspath(__file__))
145-
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
146140
directory_name = os.path.abspath(os.path.join(concore_path, secure_filename(dirname)))
147141

148142

@@ -161,9 +155,6 @@ def download(dir):
161155

162156
@app.route('/destroy/<dir>', methods=['DELETE'])
163157
def destroy(dir):
164-
# cur_path = os.getcwd()
165-
cur_path = os.path.dirname(os.path.abspath(__file__))
166-
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
167158
proc = call(["./destroy", dir], cwd=concore_path)
168159
if(proc == 0):
169160
resp = jsonify({'message': 'Successfuly deleted Dirctory'})
@@ -176,9 +167,6 @@ def destroy(dir):
176167

177168
@app.route('/getFilesList/<dir>', methods=['POST'])
178169
def getFilesList(dir):
179-
# cur_path = os.getcwd()
180-
cur_path = os.path.dirname(os.path.abspath(__file__))
181-
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
182170
dir_path = os.path.abspath(os.path.join(concore_path, dir))
183171
res = []
184172
res = os.listdir(dir_path)
@@ -188,10 +176,6 @@ def getFilesList(dir):
188176

189177
@app.route('/openJupyter/', methods=['POST'])
190178
def openJupyter():
191-
# cur_path = os.getcwd()
192-
cur_path = os.path.dirname(os.path.abspath(__file__))
193-
print(cur_path)
194-
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
195179
proc = subprocess.Popen(['jupyter', 'lab'], shell=False, stdout=subprocess.PIPE, cwd=concore_path)
196180
if proc.poll() is None:
197181
resp = jsonify({'message': 'Successfuly opened Jupyter'})

fri/test.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import requests
33
import os
44
import urllib.request
5+
import time
56

67
# function to test upload() method.
78

@@ -21,21 +22,7 @@ def upload(files):
2122

2223
# # *******
2324

24-
# function to test execute() method.
25-
def execute():
26-
url = "http://127.0.0.1:5000/execute/test?apikey=xyz"
2725

28-
path = os.path.abspath("example.py")
29-
30-
payload={}
31-
files=[
32-
('file',('example.py',open(path,'rb'),'application/octet-stream'))
33-
]
34-
headers = {}
35-
36-
response = requests.request("POST", url, headers=headers, data=payload, files=files)
37-
38-
print(response.text)
3926

4027
# function to check build
4128
def build(dir, graphml, apikey):
@@ -49,6 +36,22 @@ def debug(graphml):
4936
response = requests.request("POST", url)
5037
print(response.text)
5138

39+
# function to test run() method.
40+
def run(graphml):
41+
url = "http://127.0.0.1:5000/run/"+graphml
42+
response = requests.request("POST", url)
43+
print(response.text)
44+
45+
def clear(graphml):
46+
url = "http://127.0.0.1:5000/clear/"+graphml
47+
response = requests.request("POST", url)
48+
print(response.text)
49+
50+
def stop(graphml):
51+
url = "http://127.0.0.1:5000/stop/"+graphml
52+
response = requests.request("POST", url)
53+
print(response.text)
54+
5255

5356
#function to destroy dir.
5457
def destroy(dir):
@@ -75,16 +78,22 @@ def download():
7578
# file list to be uploaded
7679
files=[
7780
#('files[]',(file_name,open(file_path,'rb'),'application/octet-stream'))
78-
81+
('files[]',('controller.py',open('/home/amit/Desktop/test_xyz/controller.py','rb'),'application/octet-stream')),
82+
('files[]',('pm.py',open('/home/amit/Desktop/test_xyz/pm.py','rb'),'application/octet-stream')),
83+
('files[]',('sample1.graphml',open('/home/amit/Desktop/test_xyz/sample1.graphml','rb'),'application/octet-stream')),
84+
# ('files[]',('example.py',open('/home/amit/Desktop/fri/example.py','rb'),'application/octet-stream'))
7985
]
8086

8187

82-
upload(files)
83-
execute()
84-
build("test", "sample1", "xyz")
85-
debug("sample1")
86-
destroy("sample1")
87-
# getFilesList("fri")
88+
# upload(files)
89+
# build("test", "sample1", "xyz")
90+
# time.sleep(6)
91+
# debug("sample1")
92+
# run("sample1")
93+
# clear("sample1")
94+
# stop("sample1")
95+
# getFilesList("sample1")
96+
# destroy("sample1")
8897
# openJupyter()
8998
# download()
9099

0 commit comments

Comments
 (0)