Skip to content

Commit 7519293

Browse files
authored
Merge pull request #16 from amit-62/dev1
Edited main.py and test.py
2 parents 895b2ca + 2d08262 commit 7519293

3 files changed

Lines changed: 146 additions & 114 deletions

File tree

fri/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
The Control-Core File Receiving Interface (FRI) is built with is Python-3.10. It is the core component that makes the distributed executions a reality in the Control-Core framework.
44

5+
# Install Dependencies
6+
7+
Install Jupyter lab
8+
````
9+
$ pip install jupyterlab
10+
````
511

612
# Building FRI Container
713

fri/server/main.py

Lines changed: 72 additions & 68 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"
@@ -14,7 +17,7 @@
1417
@app.route('/upload/<dir>', methods=['POST'])
1518
def upload(dir):
1619
apikey = request.args.get('apikey')
17-
dirname = dir + "_" + apikey
20+
dirname = secure_filename(dir) + "_" + apikey
1821

1922
if 'files[]' not in request.files:
2023
resp = jsonify({'message': 'No file in the request'})
@@ -26,13 +29,16 @@ def upload(dir):
2629
errors = {}
2730
success = False
2831

29-
if not os.path.exists(secure_filename(dirname)):
30-
os.makedirs(secure_filename(dirname))
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+
3137

3238
for file in files:
3339
if file:
3440
filename = secure_filename(file.filename)
35-
file.save(secure_filename(dirname)+"/"+filename)
41+
file.save(directory_name+"/"+filename)
3642
success = True
3743

3844
if success and errors:
@@ -49,58 +55,17 @@ def upload(dir):
4955
resp.status_code = 500
5056
return resp
5157

52-
# To execute any python file. For example, /execute/test?apikey=xyz
53-
@app.route('/execute/<dir>', methods=['POST'])
54-
def execute(dir):
55-
apikey = request.args.get('apikey')
56-
dirname = dir + "_" + apikey
57-
58-
if 'file' not in request.files:
59-
resp = jsonify({'message': 'No file in the request'})
60-
resp.status_code = 400
61-
return resp
62-
63-
file = request.files['file']
64-
65-
if file.filename == '':
66-
resp = jsonify({'message': 'No file selected for Executing'})
67-
resp.status_code = 400
68-
return resp
69-
70-
errors = {}
71-
success = False
72-
73-
if not os.path.exists(secure_filename(dirname)):
74-
os.makedirs(secure_filename(dirname))
7558

76-
if file:
77-
filename = secure_filename(file.filename)
78-
file.save(secure_filename(dirname)+"/"+filename)
79-
output_filename = filename + ".out"
80-
file_path = secure_filename(dirname) + "/"+filename
81-
outputfile_path = secure_filename(dirname)+"/"+output_filename
82-
f = open(outputfile_path, "w")
83-
call(["nohup", "python3", file_path], stdout=f)
84-
success = True
85-
86-
if success:
87-
resp = jsonify({'message': 'Files successfully executed'})
88-
resp.status_code = 201
89-
return resp
90-
else:
91-
resp = jsonify(errors)
92-
resp.status_code = 500
93-
return resp
9459

95-
# 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
9661
@app.route('/build/<dir>', methods=['POST'])
9762
def build(dir):
98-
graphml_file = request.args.get('fetch')
99-
makestudy_dir = dir+ "/" + graphml_file #for makestudy
100-
cur_path = os.getcwd()
101-
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
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
10267
dir_path = os.path.abspath(os.path.join(concore_path, graphml_file)) #path for ./build
103-
if not os.path.exists(secure_filename(dir_path)):
68+
if not os.path.exists(dir_path):
10469
proc = call(["./makestudy", makestudy_dir], cwd=concore_path)
10570
if(proc == 0):
10671
resp = jsonify({'message': 'Directory successfully created'})
@@ -114,33 +79,75 @@ def build(dir):
11479

11580
@app.route('/debug/<dir>', methods=['POST'])
11681
def debug(dir):
117-
cur_path = os.getcwd()
118-
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
82+
dir = secure_filename(dir)
11983
dir_path = os.path.abspath(os.path.join(concore_path, dir))
12084
proc = call(["./debug"], cwd=dir_path)
12185
if(proc == 0):
122-
resp = jsonify({'message': 'Close the pop window after obtaing result'})
86+
resp = jsonify({'message': 'Close the pop window after obtaining result'})
12387
resp.status_code = 201
12488
return resp
12589
else:
12690
resp = jsonify({'message': 'There is an Error'})
12791
resp.status_code = 500
12892
return resp
12993

130-
# to download /download/<dir>?fetch=<downloadfile>. For example, /download/test?fetch=example.py.out&apikey=xyz
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
131139
@app.route('/download/<dir>', methods=['POST', 'GET'])
132140
def download(dir):
133141
download_file = request.args.get('fetch')
134-
apikey = request.args.get('apikey')
135-
dirname = dir + "_" + apikey
136-
137-
if not os.path.exists(secure_filename(dirname)):
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):
138146
resp = jsonify({'message': 'Directory not found'})
139147
resp.status_code = 400
140148
return resp
141-
142149
try:
143-
return send_from_directory(secure_filename(dirname), download_file, as_attachment=True)
150+
return send_from_directory(directory_name, download_file, as_attachment=True)
144151
except:
145152
resp = jsonify({'message': 'file not found'})
146153
resp.status_code = 400
@@ -149,8 +156,7 @@ def download(dir):
149156

150157
@app.route('/destroy/<dir>', methods=['DELETE'])
151158
def destroy(dir):
152-
cur_path = os.getcwd()
153-
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
159+
dir = secure_filename(dir)
154160
proc = call(["./destroy", dir], cwd=concore_path)
155161
if(proc == 0):
156162
resp = jsonify({'message': 'Successfuly deleted Dirctory'})
@@ -163,9 +169,9 @@ def destroy(dir):
163169

164170
@app.route('/getFilesList/<dir>', methods=['POST'])
165171
def getFilesList(dir):
166-
cur_path = os.getcwd()
167-
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
168-
dir_path = os.path.abspath(os.path.join(concore_path, 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))
169175
res = []
170176
res = os.listdir(dir_path)
171177
res = json.dumps(res)
@@ -174,8 +180,6 @@ def getFilesList(dir):
174180

175181
@app.route('/openJupyter/', methods=['POST'])
176182
def openJupyter():
177-
cur_path = os.getcwd()
178-
concore_path = os.path.abspath(os.path.join(cur_path, '../../'))
179183
proc = subprocess.Popen(['jupyter', 'lab'], shell=False, stdout=subprocess.PIPE, cwd=concore_path)
180184
if proc.poll() is None:
181185
resp = jsonify({'message': 'Successfuly opened Jupyter'})

fri/test.py

Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,59 @@
1+
from cgi import test
12
import requests
23
import os
34
import urllib.request
5+
import time
46

57
# function to test upload() method.
6-
def upload():
7-
url = "http://127.0.0.1:5000/upload/test?apikey=xyz"
8-
9-
path = os.path.abspath("example.py")
108

9+
def upload(files):
10+
url = "http://127.0.0.1:5000/upload/test?apikey=xyz"
1111
payload={}
12-
files=[
13-
('files[]',('example.py',open(path,'rb'),'application/octet-stream'))
14-
]
1512
headers = {}
16-
1713
response = requests.request("POST", url, headers=headers, data=payload, files=files)
18-
1914
print(response.text)
2015

2116

2217
# # *******
2318

24-
# function to test execute() method.
25-
def execute():
26-
url = "http://127.0.0.1:5000/execute/test?apikey=xyz"
27-
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-
19+
# function to check build
20+
def build(dir, graphml, apikey):
21+
url = "http://127.0.0.1:5000/build/"+dir+"?"+"fetch="+graphml+"&"+"apikey="+apikey
22+
response = requests.request("POST", url)
3823
print(response.text)
3924

40-
# function to test download() method.
41-
def download():
42-
url = "http://127.0.0.1:5000/download/test?fetch=f1.txt&apikey=xyz"
43-
urllib.request.urlretrieve(url, "f1.txt")
25+
# function to debug
26+
def debug(graphml):
27+
url = "http://127.0.0.1:5000/debug/"+graphml
28+
response = requests.request("POST", url)
29+
print(response.text)
4430

31+
# function to test run() method.
32+
def run(graphml):
33+
url = "http://127.0.0.1:5000/run/"+graphml
34+
response = requests.request("POST", url)
35+
print(response.text)
4536

46-
# function to check build
47-
def build():
48-
url = "http://127.0.0.1:5000/build/test?fetch=sample1"
37+
def clear(graphml):
38+
url = "http://127.0.0.1:5000/clear/"+graphml
4939
response = requests.request("POST", url)
5040
print(response.text)
5141

52-
# function to debug
53-
def debug():
54-
url = "http://127.0.0.1:5000/debug/sample1"
42+
def stop(graphml):
43+
url = "http://127.0.0.1:5000/stop/"+graphml
5544
response = requests.request("POST", url)
56-
print(response.text)
45+
print(response.text)
5746

5847

5948
#function to destroy dir.
60-
def destroy():
61-
url = "http://127.0.0.1:5000/destroy/sample1"
49+
def destroy(dir):
50+
url = "http://127.0.0.1:5000/destroy/" + dir
6251
response = requests.request("DELETE", url)
6352

6453
print(response.text)
6554

66-
def getFilesList():
67-
url = "http://127.0.0.1:5000/getFilesList/test"
55+
def getFilesList(dir, sub_dir = ""):
56+
url = "http://127.0.0.1:5000/getFilesList/" + dir + "?"+"fetch="+sub_dir
6857
response = requests.request("POST", url)
6958
print(response.text)
7059

@@ -73,13 +62,46 @@ def openJupyter():
7362
response = requests.request("POST", url)
7463
print(response.text)
7564

65+
# function to test download() method.
66+
def download(dir, subDir, fileName ):
67+
url = "http://127.0.0.1:5000/download/"+dir+"?"+"fetchDir="+subDir+"&"+"fetch="+ fileName
68+
urllib.request.urlretrieve(url, fileName)
69+
70+
# file list to be uploaded
71+
cur_path = os.path.dirname(os.path.abspath(__file__))
72+
demo_path = os.path.abspath(os.path.join(cur_path, '../demo'))
73+
file_name1 = "controller.py"
74+
file_name2 = "pm.py"
75+
file_name3 = "sample1.graphml"
76+
path_file1 = demo_path + "/" +file_name1
77+
path_file2 = demo_path + "/" +file_name2
78+
path_file3 = demo_path + "/" +file_name3
79+
files=[
80+
#('files[]',(file_name,open(file_path,'rb'),'application/octet-stream'))
81+
('files[]',(file_name1,open(path_file1,'rb'),'application/octet-stream')),
82+
('files[]',(file_name2,open(path_file2,'rb'),'application/octet-stream')),
83+
('files[]',(file_name3,open(path_file3,'rb'),'application/octet-stream')),
84+
]
85+
86+
87+
upload(files)
88+
time.sleep(2)
89+
build("test", "sample1", "xyz")
90+
time.sleep(6)
91+
method = input("methods - 1 for debug, 0 for run :")
92+
if method == 1:
93+
debug("sample1")
94+
else:
95+
run("sample1")
96+
time.sleep(2)
97+
stop("sample1")
98+
time.sleep(2)
99+
getFilesList("sample1", "cu")
100+
getFilesList("sample1", "pym")
101+
time.sleep(5)
102+
download("sample1", "cu", "u")
103+
clear("sample1")
104+
destroy("sample1")
105+
openJupyter()
76106

77-
# upload()
78-
# execute()
79-
# download()
80-
# build()
81-
# debug()
82-
# destroy()
83-
getFilesList()
84-
# openJupyter()
85107

0 commit comments

Comments
 (0)