66import json
77import 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
1013app = Flask (__name__ )
1114app .secret_key = "secret key"
1417@app .route ('/upload/<dir>' , methods = ['POST' ])
1518def 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' ])
9762def 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' ])
11681def 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' ])
132140def 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' ])
151158def 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' ])
165171def 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' ])
176182def 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' })
0 commit comments