11from flask import Flask , request , jsonify , send_file , send_from_directory
22from werkzeug .utils import secure_filename
3+ import xml .etree .ElementTree as ET
34import os
45import subprocess
56from subprocess import call ,check_output
1819cors = CORS (app )
1920app .config ['CORS_HEADERS' ] = 'Content-Type'
2021
22+ def check_node_labels (graphml_file ):
23+ tree = ET .parse (graphml_file )
24+ root = tree .getroot ()
25+ namespace = {'y' : 'http://www.yworks.com/xml/graphml' }
26+ node_labels = root .findall ('.//y:NodeLabel' , namespace )
27+ for node_label in node_labels :
28+ label = node_label .text
29+ if label .endswith ('.m' ):
30+ return True
31+ return False
32+
2133# To upload multiple file. For example, /upload/test?apikey=xyz
2234@app .route ('/upload/<dir>' , methods = ['POST' ])
2335def upload (dir ):
@@ -65,9 +77,14 @@ def upload(dir):
6577# to download /build/<dir>?fetch=<graphml>. For example, /build/test?fetch=sample1&apikey=xyz
6678@app .route ('/build/<dir>' , methods = ['POST' ])
6779def build (dir ):
68- graphml_file = request .args .get ('fetch' )
80+ graphml_file = request .args .get ('fetch' )
81+ params = request .args .get ('params' )
82+ docker = request .args .get ('docker' )
83+ octave = request .args .get ('octave' )
84+ maxtime = request .args .get ('maxtime' )
6985 apikey = request .args .get ('apikey' )
7086 out_dir = request .args .get ('outdir' )
87+ output_str = ""
7188 if (apikey == None ):
7289 dirname = secure_filename (dir )
7390 else :
@@ -77,35 +94,132 @@ def build(dir):
7794 dir_path = os .path .abspath (os .path .join (concore_path , graphml_file )) #path for ./build
7895 else :
7996 dir_path = os .path .abspath (os .path .join (concore_path , out_dir )) #path for ./build
97+
98+ dotMCheck = check_node_labels (os .path .abspath (os .path .join (concore_path , makestudy_dir )) + '.graphml' )
99+ if ((dotMCheck == False or octave == 'false' ) and os .path .isfile (os .path .abspath (os .path .join (concore_path , 'concore.octave' )))):
100+ if (platform .uname ()[0 ]!= 'Windows' ):
101+ proc = call (["rm" , "concore.octave" ], cwd = concore_path )
102+ else :
103+ proc = call (["del" , "concore.octave" ], shell = True , cwd = concore_path )
104+
105+ if (octave == 'true' and dotMCheck ):
106+ if (platform .uname ()[0 ]!= 'Windows' ):
107+ proc = call (["touch" , "concore.octave" ], cwd = concore_path )
108+ else :
109+ proc = open (os .path .abspath (os .path .join (concore_path , 'concore.octave' )), 'x' )
110+
111+
80112 if not os .path .exists (dir_path ):
81113 if (platform .uname ()[0 ]== 'Windows' ):
82114 if (out_dir == None or out_dir == "" ):
83- proc = call (["makestudy" , makestudy_dir ], shell = True , cwd = concore_path )
115+ if (docker == 'true' ):
116+ try :
117+ output_bytes = subprocess .check_output (["makedocker" , makestudy_dir ], cwd = concore_path , shell = True )
118+ output_str = output_bytes .decode ("utf-8" )
119+ proc = 0
120+ except subprocess .CalledProcessError as e :
121+ output_str = f"Docker study creation failed with return code { e .returncode } (check duplicate directory)"
122+ proc = 1
123+ else :
124+ try :
125+ output_bytes = subprocess .check_output (["makestudy" , makestudy_dir ], cwd = concore_path , shell = True )
126+ output_str = output_bytes .decode ("utf-8" )
127+ proc = 0
128+ except subprocess .CalledProcessError as e :
129+ output_str = f"Study creation failed with return code { e .returncode } (check duplicate directory)"
130+ proc = 1
84131 else :
85- proc = call (["makestudy" , makestudy_dir , out_dir ], shell = True , cwd = concore_path )
132+ if (docker == 'true' ):
133+ try :
134+ output_bytes = subprocess .check_output (["makedocker" , makestudy_dir , out_dir ], cwd = concore_path , shell = True )
135+ output_str = output_bytes .decode ("utf-8" )
136+ proc = 0
137+ except subprocess .CalledProcessError as e :
138+ output_str = f"Docker study creation failed with return code { e .returncode } (check duplicate directory)"
139+ proc = 1
140+ else :
141+ try :
142+ output_bytes = subprocess .check_output (["makestudy" , makestudy_dir , out_dir ], cwd = concore_path , shell = True )
143+ output_str = output_bytes .decode ("utf-8" )
144+ proc = 0
145+ except subprocess .CalledProcessError as e :
146+ output_str = f"Study creation failed with return code { e .returncode } (check duplicate directory)"
147+ proc = 1
86148 else :
87149 if (out_dir == None or out_dir == "" ):
88- proc = call (["./makestudy" , makestudy_dir ], cwd = concore_path )
150+ if (docker == 'true' ):
151+ try :
152+ output_bytes = subprocess .check_output (["./makedocker" , makestudy_dir ], cwd = concore_path )
153+ output_str = output_bytes .decode ("utf-8" )
154+ proc = 0
155+ except subprocess .CalledProcessError as e :
156+ output_str = f"Docker study creation failed with return code { e .returncode } (check duplicate directory)"
157+ proc = 1
158+ else :
159+ try :
160+ output_bytes = subprocess .check_output (["./makestudy" , makestudy_dir ], cwd = concore_path )
161+ output_str = output_bytes .decode ("utf-8" )
162+ proc = 0
163+ except subprocess .CalledProcessError as e :
164+ output_str = f"Study creation failed with return code { e .returncode } (check duplicate directory)"
165+ proc = 1
89166 else :
90- proc = call (["./makestudy" , makestudy_dir , out_dir ], cwd = concore_path )
167+ if (docker == 'true' ):
168+ try :
169+ output_bytes = subprocess .check_output (["./makedocker" , makestudy_dir , out_dir ], cwd = concore_path )
170+ output_str = output_bytes .decode ("utf-8" )
171+ proc = 0
172+ except subprocess .CalledProcessError as e :
173+ output_str = f"Docker study creation failed with return code { e .returncode } (check duplicate directory)"
174+ proc = 1
175+ else :
176+ try :
177+ output_bytes = subprocess .check_output (["./makestudy" , makestudy_dir , out_dir ], cwd = concore_path )
178+ output_str = output_bytes .decode ("utf-8" )
179+ proc = 0
180+ except subprocess .CalledProcessError as e :
181+ output_str = f"Study creation failed with return code { e .returncode } (check duplicate directory)"
182+ proc = 1
91183 if (proc == 0 ):
92184 resp = jsonify ({'message' : 'Directory successfully created' })
93185 resp .status_code = 201
94186 else :
95187 resp = jsonify ({'message' : 'There is an Error' })
96- resp .status_code = 500
188+ resp .status_code = 500
97189 if (platform .uname ()[0 ]== 'Windows' ):
98- call (["build" ], cwd = dir_path , shell = True )
190+ try :
191+ output_bytes = subprocess .check_output ("build" , cwd = dir_path , shell = True )
192+ output_str = output_str + output_bytes .decode ("utf-8" )
193+ resp = jsonify ({'message' : 'Directory successfully created' , 'output' : output_str })
194+ except subprocess .CalledProcessError as e :
195+ output_str = f"Build failed with return code { e .returncode } "
196+ resp = jsonify ({'message' : 'Build Failed' , 'output' : output_str })
197+ resp .status_code = 500
198+ if (maxtime != None and maxtime != '' ):
199+ proc = call (["maxtime" , maxtime ],shell = True , cwd = dir_path )
200+ if (params != None and params != '' ):
201+ proc = call (["params" , params ],shell = True , cwd = dir_path )
99202 else :
100- call (["./build" ], cwd = dir_path )
203+ try :
204+ output_bytes = subprocess .check_output ("./build" , cwd = dir_path )
205+ output_str = output_str + output_bytes .decode ("utf-8" )
206+ resp = jsonify ({'message' : 'Directory successfully created' , 'output' : output_str })
207+ except subprocess .CalledProcessError as e :
208+ output_str = f"Build failed with return code { e .returncode } "
209+ resp = jsonify ({'message' : 'Build Failed' , 'output' : output_str })
210+ resp .status_code = 500
211+ if (maxtime != None and maxtime != '' ):
212+ proc = call (["./maxtime" , maxtime ], cwd = dir_path )
213+ if (params != None and params != '' ):
214+ proc = call (["./params" , params ], cwd = dir_path )
101215 return resp
102216
103217@app .route ('/debug/<dir>' , methods = ['POST' ])
104218def debug (dir ):
105219 dir_name = secure_filename (dir )
106220 dir_path = os .path .abspath (os .path .join (concore_path , dir_name ))
107221 if (platform .uname ()[0 ]== 'Windows' ):
108- proc = call (["debug" ],shell = True , cwd = dir_path )
222+ proc = call (["debug" ],shell = True , cwd = dir_path )
109223 else :
110224 proc = call (["./debug" ], cwd = dir_path )
111225 if (proc == 0 ):
@@ -122,7 +236,7 @@ def run(dir):
122236 dir_name = secure_filename (dir )
123237 dir_path = os .path .abspath (os .path .join (concore_path , dir_name ))
124238 if (platform .uname ()[0 ]== 'Windows' ):
125- proc = call (["run" ],shell = True , cwd = dir_path )
239+ proc = call (["run" ],shell = True , cwd = dir_path )
126240 else :
127241 proc = call (["./run" ], cwd = dir_path )
128242 if (proc == 0 ):
@@ -139,7 +253,7 @@ def stop(dir):
139253 dir_name = secure_filename (dir )
140254 dir_path = os .path .abspath (os .path .join (concore_path , dir_name ))
141255 if (platform .uname ()[0 ]== 'Windows' ):
142- proc = call (["stop" ],shell = True , cwd = dir_path )
256+ proc = call (["stop" ],shell = True , cwd = dir_path )
143257 else :
144258 proc = call (["./stop" ], cwd = dir_path )
145259 if (proc == 0 ):
@@ -154,12 +268,23 @@ def stop(dir):
154268
155269@app .route ('/clear/<dir>' , methods = ['POST' ])
156270def clear (dir ):
271+ unlock = request .args .get ('unlock' )
272+ params = request .args .get ('params' )
273+ maxtime = request .args .get ('maxtime' )
157274 dir_name = secure_filename (dir )
158275 dir_path = os .path .abspath (os .path .join (concore_path , dir_name ))
159276 if (platform .uname ()[0 ]== 'Windows' ):
160- proc = call (["clear" ],shell = True , cwd = dir_path )
277+ proc = call (["clear" ],shell = True , cwd = dir_path )
278+ if (maxtime != None and maxtime != '' ):
279+ proc = call (["maxtime" , maxtime ],shell = True , cwd = dir_path )
280+ if (params != None and params != '' ):
281+ proc = call (["params" , params ],shell = True , cwd = dir_path )
161282 else :
162283 proc = call (["./clear" ], cwd = dir_path )
284+ if (maxtime != None and maxtime != '' ):
285+ proc = call (["./maxtime" , maxtime ], cwd = dir_path )
286+ if (params != None and params != '' ):
287+ proc = call (["./params" , params ], cwd = dir_path )
163288 if (proc == 0 ):
164289 resp = jsonify ({'message' : 'result deleted' })
165290 resp .status_code = 201
@@ -222,7 +347,7 @@ def download(dir):
222347def destroy (dir ):
223348 dir = secure_filename (dir )
224349 if (platform .uname ()[0 ]== 'Windows' ):
225- proc = call (["destroy" , dir ],shell = True , cwd = concore_path )
350+ proc = call (["destroy" , dir ],shell = True , cwd = concore_path )
226351 else :
227352 proc = call (["./destroy" , dir ], cwd = concore_path )
228353 if (proc == 0 ):
@@ -232,7 +357,31 @@ def destroy(dir):
232357 else :
233358 resp = jsonify ({'message' : 'There is an Error' })
234359 resp .status_code = 500
235- return resp
360+ return resp
361+
362+ @app .route ('/library/<dir>' , methods = ['POST' ])
363+ def library (dir ):
364+ dir_name = secure_filename (dir )
365+ dir_path = os .path .abspath (os .path .join (concore_path , dir_name ))
366+ filename = request .args .get ('filename' )
367+ library_path = request .args .get ('path' )
368+ proc = 0
369+ if (library_path == None or library_path == '' ):
370+ library_path = "../tools"
371+ if (platform .uname ()[0 ]== 'Windows' ):
372+ proc = subprocess .check_output (["..\library" , library_path , filename ],shell = True , cwd = dir_path )
373+ else :
374+ proc = subprocess .check_output (["../library" , library_path , filename ], cwd = dir_path )
375+ if (proc != 0 ):
376+ resp = jsonify ({'message' : proc .decode ("utf-8" )})
377+ resp .status_code = 201
378+ return resp
379+ else :
380+ resp = jsonify ({'message' : 'There is an Error' })
381+ resp .status_code = 500
382+ return resp
383+
384+
236385
237386@app .route ('/getFilesList/<dir>' , methods = ['POST' ])
238387def getFilesList (dir ):
0 commit comments