forked from FOSSEE/xcos_on_cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendLog.py
1278 lines (1121 loc) · 49.3 KB
/
SendLog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
from xml.dom import minidom
from xml.dom.minidom import parse
import gevent
import fileinput
import time
import shutil
import os
from threading import Timer
from PIL import Image
from datetime import date
from datetime import time
from datetime import datetime
import threading
import urllib2
from gevent import monkey
import fileinput
import bs4
from gevent.pywsgi import WSGIServer
from flask import Flask, flash, request, redirect, url_for, Response, render_template, send_from_directory ,send_file
# Added send_file to ease download
from werkzeug import secure_filename
from os.path import exists
import re
monkey.patch_all(aggressive=False)
import subprocess32 as subprocess
app = Flask(__name__, static_folder='webapp/')
# This is the path to the upload directory and values directory
app.config['UPLOAD_FOLDER'] = 'uploads/'
app.config['VALUES_FOLDER'] = 'values/'
# Make the upload directory and values directory if not available
subprocess.Popen('mkdir -p ' + app.config['UPLOAD_FOLDER'], shell = True)
subprocess.Popen('mkdir -p ' + app.config['VALUES_FOLDER'], shell = True)
# These are the extension that we are accepting to be uploaded
app.config['ALLOWED_EXTENSIONS'] = set(['zcos', 'xcos', 'txt'])
# Delay time to look for new line (in s)
LOOK_DELAY = 0.1
# States of the line
INITIALIZATION = 0
ENDING = 1
DATA = 2
NOLINE = -1
BLOCK_IDENTIFICATION = -2
# Scilab dir, can't run absolute paths
SCI = "../scilab_for_xcos_on_cloud/"
# List to store figure IDs
figure_list = []
# List to store filenames of files
xcos_file_list = []
# List to identify whether to save to workspace ,or load from workspce or neither
workspace_list = []
# Dictionary to find variable to load or save from workspace
workspace_dict = {}
#workspace_counter = 0
log_dir = ''
log_name = ''
filename = ''
file_image = ''
flag_sci = False
ts_image = 0
counter = 1
# For Affich_m
workspace_variable_list = []
# For keeping count of affich_m blocks fro replacing with TOWS_c in diagram and also for assigning variable name for TOWS_c workspace variable
affich_count = 0
#path = os.getcwd() + '/scifunc_files/'
#filename = secure_filename(file.filename)
class line_and_state:
# Class to store the line and its state
line = None
state = NOLINE
def __init__(self, line, state):
self.line = line
self.state = state
def set(self, line_state):
self.line = line_state[0]
self.state = line_state[1]
return False
def get_line(self):
return self.line
def get_state(self):
return self.state
def parse_line(line):
# Function to parse the line
# Returns tuple of figure ID and state
# state = INITIALIZATION if new figure is created
# ENDING if current fig end
# DATA otherwise
line_words = line.split(' ')
#The below condition determines the block ID
if line_words[2] == "Block":
block_id=int(line_words[4])
return (block_id, BLOCK_IDENTIFICATION)
if line_words[2] == "Initialization":
# New figure created
# Get fig id
figure_id = int(line_words[-1])
return (figure_id, INITIALIZATION)
elif line_words[2] == "Ending":
# Current figure end
# Get fig id
figure_id = int(line_words[-1])
return (figure_id, ENDING)
else:
# Current figure coordinates
figure_id = int(line_words[3])
return (figure_id, DATA)
def get_line_and_state_modified(file):
# Function to get a new line from file
# This also parses the line and appends new figures to figure List
global figure_list
line = file.readline()
if not line:
return (None, NOLINE)
parse_result = parse_line(line)
figure_id = parse_result[0]
state = parse_result[1]
if state == INITIALIZATION:
# New figure created
# Add figure ID to list
figure_list.append(figure_id)
return (None, INITIALIZATION)
# Check for block identification
elif state == BLOCK_IDENTIFICATION:
return (line,BLOCK_IDENTIFICATION)
elif state == ENDING:
# End of figure
# Remove figure ID from list
figure_list.remove(figure_id)
return (None, ENDING)
return (line, DATA)
def get_line_and_state(file, count):
# Return the line from the log filebased on the line count
# Function to get a new line from file
# This also parses the line and appends new figures to figure List
global figure_list
line = file.readlines()
# If required line is not present
if not line[count]:
return (None, NOLINE)
parse_result = parse_line(line[count])
figure_id = parse_result[0]
state = parse_result[1]
if state == INITIALIZATION:
# New figure created
# Add figure ID to list
figure_list.append(figure_id)
return (None, INITIALIZATION)
# Check for block identification
elif state == BLOCK_IDENTIFICATION:
return (str(figure_id),BLOCK_IDENTIFICATION)
elif state == ENDING:
# End of figure
# Remove figure ID from list
figure_list.remove(figure_id)
return (None, ENDING)
return (line[count], DATA)
app.config['UPLOAD_FOLDER'] = 'scifunc_files/'
@app.route('/uploadsci', methods=['POST'])
def uploadsci():
file = request.files['file']
if file and request.method == 'POST':
global flag_sci
global filename
ts = datetime.now()
filename = Details.uid + str(ts) + secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flag_sci = True
path = os.getcwd() + '/scifunc_files/'
read = open(os.path.join(path, filename), "r")
command = ["./"+SCI+"bin/scilab-adv-cli", "-nogui", "-noatomsautoload", "-nb", "-nw", "-e","loadXcosLibs();exec('"+path + filename+"'),mode(2);quit()"]
output_com = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False,bufsize=1, universal_newlines=True)
out = output_com.communicate()[0]
system_commands = re.compile('unix\(.*\)|unix_g\(.*\)|unix_w\(.*\)|''unix_x\(.*\)|unix_s\(.*\)|host|newfun''|execstr|ascii|mputl|dir\(\)')
match = re.findall(system_commands, open(os.path.join(path, filename), 'r').read())
if('!--error' in out):
error_index = out.index('!')
msg = out[error_index:-9]
return msg
elif(match):
msg = "System calls are not allowed in .sci file!\n Please upload another .sci file!!"
return msg
else:
msg = "File is uploaded successfully!!"
return msg
@app.route('/requestfilename', methods=['POST'])
def sendfile():
global file_image
global flag_sci
if(flag_sci == True):
file_image = filename
file_image = file_image[:-4]
else:
file_image = ""
flag_sci = False
return file_image
def event_stream(xcos_file_id):
global figure_list
global kill_scilab
global filename
global ts_image
global file_image
global counter
# If no id is sent, return
if(len(xcos_file_id)==0):
return
xcos_file_id = int(xcos_file_id)
xcos_file_dir = os.getcwd() + '/uploads/'
xcos_affich_function_file_dir = os.getcwd() + '/'
path = os.getcwd() + '/scifunc_files/'
#filename = 'Scifunc2.sci'
#print(xcos_affich_function_file_dir)
xcos_file_name = xcos_file_list[xcos_file_id]
# Get previously running scilab process IDs
proc = subprocess.Popen("pgrep scilab", stdout=subprocess.PIPE, shell=True)
# out will contain output of command, the list of process IDs of scilab
(out, err) = proc.communicate()
_l = len(out)
# Initialise pid
pid = 0
# id to identify each session for saving workspace
session=Details.uid
ts = datetime.now()
ts_fmt = ts.strftime('%Y-%m-%d %H:%M:%S.%f')
ts_image = ts_fmt[:-3]
# name of worspace file the session
workspace="workspace"+session+".dat"
#ts_image = datetime.now()
#filename = Details.uid + ts + filename
#print(workspace)
#print(len(xcos_file_id))
workspace_counter=workspace_list[xcos_file_id]
#For affich_m block
variablename=""; # Stores all workspace variable name in format (var1,var2,var3)
if (workspace_counter==4): # To check affich_m block presence
for i in range(len(workspace_variable_list)): # to get count of affich replace with tows_c block and to use to iterate their name
variable_name=workspace_variable_list[i] # workspace_variable_list contains name of workspace variable
if(i==(len(workspace_variable_list)-1)): # if last element in list then it should not be concatenate with ,
variablename=variablename+variable_name+""
else:
variablename=variablename+variable_name+","
#print("...."+variablename)
############################################################################################################
# commands for ruuning of scilab based on existence of TOWS_c and FROMWSB
# 3 means both exists,2 FROMWSB exists,1 TOWS_c exists,0 none exists meaning normal set of commands
#print(path)
#print(filename)
if (workspace_counter ==3 and exists(workspace)):
append=workspace_dict[xcos_file_id]
command = ["./"+SCI+"bin/scilab-adv-cli", "-nogui", "-noatomsautoload", "-nb", "-nw", "-e","load('"+workspace+"');loadXcosLibs();importXcosDiagram('" + xcos_file_dir + xcos_file_name + "');xcos_simulate(scs_m,4);xs2jpg(gcf(),'webapp/res_imgs/img_test.jpg'),mode(2);deletefile('"+workspace+"');save('"+workspace+"') ;quit()"]
elif (workspace_counter ==1 or workspace_counter==3):
append=workspace_dict[xcos_file_id]
command = ["./"+SCI+"bin/scilab-adv-cli", "-nogui", "-noatomsautoload", "-nb", "-nw", "-e","loadXcosLibs();importXcosDiagram('" + xcos_file_dir + xcos_file_name + "');xcos_simulate(scs_m,4);xs2jpg(gcf(),'webapp/res_imgs/img_test.jpg'),mode(2);deletefile('"+workspace+"');save('"+workspace+"') ;quit()"]
elif (workspace_counter ==4): # added for affich_m
workspace_variable_list[:] = []
command = ["./"+SCI+"bin/scilab-adv-cli", "-nogui", "-noatomsautoload", "-nb", "-nw", "-e","loadXcosLibs();importXcosDiagram('" + xcos_file_dir + xcos_file_name + "');xcos_simulate(scs_m,4);xs2jpg(gcf(),'webapp/res_imgs/img_test.jpg'),mode(2);exec('" + xcos_affich_function_file_dir + "affichm.sci"+"');affichm("+variablename+");deletefile('"+workspace+"');save('"+workspace+"') ;quit()"]
#print(xcos_affich_function_file_dir)
elif (workspace_counter ==2 and exists(workspace)):
command = ["./"+SCI+"bin/scilab-adv-cli", "-nogui", "-noatomsautoload", "-nb", "-nw", "-e", "load('"+workspace+"');loadXcosLibs();importXcosDiagram('" + xcos_file_dir + xcos_file_name + "');xcos_simulate(scs_m,4);xs2jpg(gcf(),'webapp/res_imgs/img_test.jpg'),mode(2);deletefile('"+workspace+"') ;quit()"]
elif (workspace_counter == 7):
command = ["./"+SCI+"bin/scilab-adv-cli", "-nogui", "-noatomsautoload", "-nb", "-nw", "-e","loadXcosLibs();exec('" + path + filename +"');importXcosDiagram('" + xcos_file_dir + xcos_file_name + "');xcos_simulate(scs_m,4);xs2jpg(gcf(),'webapp/res_imgs/img_test"+file_image+".jpg'),mode(2);quit()"]
counter = counter + 1
t = Timer(15.0, delete_image)
t.start()
t1 = Timer(10.0, delete_scifile)
t1.start()
else:
command = ["./"+SCI+"bin/scilab-adv-cli", "-nogui", "-noatomsautoload", "-nb", "-nw", "-e", "loadXcosLibs();importXcosDiagram('" + xcos_file_dir + xcos_file_name + "');xcos_simulate(scs_m,4);xs2jpg(gcf(),'webapp/res_imgs/img_test.jpg'),mode(2);quit()"]
scilab_proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False);
# Wait till xcos is launched
while len(out) == _l:
# If length of out equals _l,
# it means scilab hasn't launched yet
# Wait
gevent.sleep(LOOK_DELAY)
# Get process IDs of scilab instances
proc = subprocess.Popen("pgrep scilab", stdout=subprocess.PIPE, shell=True)
# out will contain output of command, the list of process IDs of scilab
(out, err) = proc.communicate()
# out will contain output of command, the list of process IDs of scilab
# Get the latest process ID of scilab
pid = out.split()[-1]
# Define function to kill scilab(if still running) and remove files
def kill_scilab():
# Kill scilab by it's pid
subprocess.Popen(["kill", "-9", pid])
# Remove log file
subprocess.Popen(["rm", "-f", log_dir+log_name], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
# Remove xcos file
subprocess.Popen(["rm", "-f", xcos_file_dir+xcos_file_name], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
scilab_proc.kill()
line_count = 0
# Log file directory
# As the scilab process is spawned by this script,
# the log directory is same as that of this script
log_dir = ""
# Log file name
log_name = "scilab-log-"+pid+".txt"
# Initialise output and error variables for subprocess
scilab_out = ""
scilab_err = ""
line_count = 0
line = line_and_state(None, NOLINE)
# Checks if such a file exists
while not (os.path.isfile(log_name)):
pass
# This variable is for running the sleep command
# Start sending log
put_delay = False
line_id = -1
delay_length = -1
if(Details.tk_is_present):
try:
# For processes taking less than 10 seconds
scilab_out, scilab_err = scilab_proc.communicate(timeout=4)
print(scilab_out)
# Check for errors in Scilab
if "Empty diagram" in scilab_out:
yield "event: ERROR\ndata: Empty diagram\n\n"
kill_scilab()
return
# For processes taking more than 10 seconds
except subprocess.TimeoutExpired:
# Check for errors in Scilab
if "Empty diagram" in scilab_out:
yield "event: ERROR\ndata: Empty diagram\n\n"
kill_scilab()
return
# Open the log file
log_file = open(log_dir + log_name, "r")
# Start sending log
line = line_and_state(None, NOLINE)
while (True):
# The chart must be updated for all the various lines it has in 0.1 seconds.
# Hence dividing the sleep time 0.1 by the number of lines the chart contains
if put_delay:
gevent.sleep(0.1 / delay_length)
if not (os.path.isfile(log_name)):
break
log_file = open(log_dir + log_name, "r+")
if not ( line.set(get_line_and_state(log_file,line_count)) or line.get_state() != ENDING or len(figure_list) > 0 ):
break
if line.get_state()== BLOCK_IDENTIFICATION:
# Split the line obtained from log file and the 8th element is line ID
logLine = line.get_line()
line_contents = logLine.split(' ')
# Checking if the current line ID is same as first line ID and is the first occurence of matching
if(line_id == line_contents[7] and not put_delay):
delay_length = line_count - 1
# The count of total number of lines in the chart
put_delay = True
# The first line ID
if(line_count == 1):
line_id = line_contents[7]
yield "event: block\ndata: "+logLine+"\n\n"
elif line.get_state() != DATA:
gevent.sleep(LOOK_DELAY)
else:
# Split the line obtained from log file and the 8th element is line ID
logLine = line.get_line()
line_contents = logLine.split(' ')
# Checking if the current line ID is same as first line ID and is the first occurence of matching
if(line_id == line_contents[7] and not put_delay):
# Take the count of total number of lines in the chart
delay_length = line_count - 1
put_delay = True
# The first line ID
if(line_count == 1):
line_id = line_contents[7]
yield "event: log\ndata: "+logLine+ "\n\n"
# Reset line, so server won't send same line twice
line = line_and_state(None, NOLINE)
line_count = line_count + 1
log_file.close()
# Finished Sending Log
kill_scilab()
# Notify Client
yield "event: DONE\ndata: None\n\n"
else:
# Open the log file
if not (os.path.isfile(log_name)):
return
log_file = open(log_dir + log_name, "r")
# Start sending log
line = line_and_state(None, NOLINE)
while (line.set(get_line_and_state_modified(log_file)) or len(figure_list) > 0):
# Get the line and loop until the state is ENDING and figure_list empty
# Determine if we get block id and give it to chart.js
if line.get_state()== BLOCK_IDENTIFICATION:
yield "event: block\ndata: "+line.get_line()+"\n\n"
elif line.get_state() != DATA:
gevent.sleep(LOOK_DELAY)
else:
yield "event: log\ndata: "+line.get_line()+"\n\n"
# Reset line, so server won't send same line twice
line = line_and_state(None, NOLINE)
# Finished Sending Log
kill_scilab()
# Notify Client
yield "event: DONE\ndata: None\n\n"
# class used to get the user_id and the boolean value is to make run a thread
class Details:
import uuid
tk_is_present = False
my_id = uuid.uuid1()
uid = str(my_id)
# user_id
tkbool = True
# boolean value to run the thread acc. to it
print("user_id:"+uid)
names = {}
def delete_image():
global file_image
file_uid = file_image[0:36]
if(file_uid == Details.uid):
image_path = os.getcwd() + '/webapp/res_imgs/img_test' + file_image + '.jpg'
os.remove(image_path)
def delete_scifile():
global filename
scifile_uid = filename[0:36]
if(scifile_uid == Details.uid):
sci_path = os.getcwd() + '/scifunc_files/' + filename
os.remove(sci_path)
# function which will check and make initialization of every required files.
def findFile():
r = open("values/"+Details.uid+"_val.txt","r")
line = r.readline()
# at first the val.txt contains "Start" indicating the starting of the process
r.close()
if line == "Start":
Details.tkbool = True
w = open("values/"+Details.uid+"_tktime.txt","w")
# tktime.txt is the file where we will update the time basing on the parameter ("Period" of CLOCK_c) for each tkscale
w.write("0\n0\n0\n0\n0\n0\n0\n0\n0\n0")
# initialize all tktime values to 0
w.close()
for i in range(10):
open("values/"+Details.uid+"_tk"+str(i+1)+".txt","w").close();
# create empty tk text files
return 0
elif line=="Stop":
# at last the val.txt contains "Stop" indicating the ending process
return 1
return 2
# function which changes flaoting to req scientific format
def changeFormat(n):
n = float(n)
check = 1
exp = 0
if n<0:
n = n*-1
check=-1
while not(0<=n and n<1):
n = n/10
exp=exp+1
n=n*check
n = "%.3f" % n
exp = "%02d" % (exp,)
formated = n+"E+"+exp
return formated
# function which appends the 'updated' ('new') value to the file
def AppendtoTKfile(fname,tlist,i):
tl = tlist.split(' ')
# converting string to list which stores the Period parameter at 0 index, data to be appended at index 1
if(tl[1]==''):
# if data is empty, do not append it
return
with open("values/"+Details.uid+"_tktime.txt", 'r') as file:
# read the tktime folder to get tkfile time at respective index (tk1.txt at 0 index)
tktime = file.readlines()
# update the time
time = float(tktime[i])+float(tl[0])
tktime[i]=str(time)+'\n'
with open("values/"+Details.uid+"_tktime.txt", 'w') as file:
# write the updated the time to tktime.txt
file.writelines( tktime )
data = tl[1]
time = changeFormat(time)
line = time+" "+data+"\n"
# append data to the tk.txt
w= open(fname,'a')
w.write(line)
w.close()
# function which take values from val.txt send the data to append them in their respective 'tk' files.
def getDetails():
r=open("values/"+Details.uid+"_val.txt","r")
line=r.readline()
tklist=line.split(',')
for i in range(len(tklist)):
tl = tklist[i].split(' ')
if len(tklist)==1 or len(tl)==1:
break
else:
fname="values/"+Details.uid+"_tk"+str(i+1)+".txt"
AppendtoTKfile(fname,tklist[i],i)
r.close()
# function which makes the initialisation of thread
def getDetailsThread():
if Details.tkbool:
getDetails()
import threading
from threading import Timer
# calls the same function adter 0.1 second
threading.Timer(0.1,getDetailsThread).start()
def stopDetailsThread():
Details.tkbool = False # stops the thread
import os, glob
for filename in glob.glob("values/"+Details.uid+"*"):
# deletes all files created under the 'uid' name
os.remove(filename)
# Route that will process the file upload
@app.route('/upload', methods=['POST'])
def upload():
# Get the file
file = request.files['file']
# flags to check if both TOWS_c and FROMWSB are present
flag1=0
flag2=0
list1=[]
list2=[]
# Check if the file is not null
if file:
# Make the filename safe, remove unsupported chars
client_id = len(xcos_file_list)
# Save the file in xml extension and using it for further modification by using xml parser
temp_file_xml_name = str(client_id)+".xml"
file.save(os.path.join(temp_file_xml_name))
new_xml = minidom.parse(temp_file_xml_name)
# to identify if we have to load or save to workspace or neither #0 if neither TOWS_c or FROWSB found
workspace_counter=0
blocks = new_xml.getElementsByTagName("BasicBlock")
Details.tk_is_present = False
pattern = re.compile("<SplitBlock")
for i, line in enumerate(open(temp_file_xml_name)):
for match in re.finditer(pattern, line):
list1.append(i+1)
pattern1 = re.compile("<ControlPort")
for i, line in enumerate(open(temp_file_xml_name)):
for match in re.finditer(pattern1, line):
list2.append(i+1)
pattern2 = re.compile("<ImplicitInputPort")
count1=0
for i, line in enumerate(open(temp_file_xml_name)):
for match in re.finditer(pattern2, line):
count1+=1
if count1>=1:
splitline=[]
count=0;
for i in range(len(list1)):
for j in range(len(list2)):
if list2[j]==list1[i]+3:
count+=1
splitline.append(list1[i])
blocksplit = new_xml.getElementsByTagName("SplitBlock")
block_ids=[]#this stores the id of split blocks
for block in blocksplit:
if block.getAttribute("style") == "SPLIT_f":
block_ids.append(int(block.getAttribute("id")))
compsplit=[]
for i in range(len(splitline)):
for j in range(len(list1)):
if splitline[i]==list1[j]:
compsplit.append(j)
finalsplit=[]
for i in range(len(compsplit)):
finalsplit.append(block_ids[compsplit[i]])
blockcontrol = new_xml.getElementsByTagName("ControlPort")
for block in blockcontrol:
for i in range(len(finalsplit)):
if block.getAttribute("parent") == str(finalsplit[i]):#match the lines with the parent of our spliblocks which we need to change
block.setAttribute('id', '-1')
blockcommand = new_xml.getElementsByTagName("CommandPort")
for block in blockcommand:
for i in range(len(finalsplit)):
if block.getAttribute("parent") == str(finalsplit[i]):
block.setAttribute('id', '-1')
finalchangeid=[]#here we take the ids of command controllink which we will search and change
for i in range(len(finalsplit)):
finalchangeid.append(finalsplit[i]+4)
finalchangeid.append(finalsplit[i]+5)
with open(temp_file_xml_name,'w') as f:#here we save the contents
f.write(new_xml.toxml())
with open(temp_file_xml_name,"r") as f:
newline=[]
i=0
for word in f.readlines():
if "<CommandControlLink id=" in word:
temp_word=""
for i in range(len(finalchangeid)):
if "<CommandControlLink id=\""+str(finalchangeid[i])+"\"" in word:
temp_word=word.replace("<CommandControlLink id=\""+str(finalchangeid[i])+"\"","<ImplicitLink id=\""+str(finalchangeid[i])+"\"")
i=i+1
if temp_word!="":
newline.append(temp_word)
else:
newline.append(word)
else:
newline.append(word)
with open(temp_file_xml_name,"w") as f:
for line in newline:
f.writelines(line)
with open(temp_file_xml_name,"r") as in_file:
buf = in_file.readlines()
#length=len(finalsplit)
#return finalsplit
#print finalsplit1
with open(temp_file_xml_name,"w") as out_file:
for line in buf:
for i in range (len(finalsplit)):
if '<ControlPort connectable=\"0\" dataType=\"UNKNOW_TYPE\" id=\"-1\" ordering=\"1\" parent="'+str(finalsplit[i])+"\"" in line:
line=" <ImplicitInputPort connectable=\"0\" dataType=\"UNKNOW_TYPE\" id=\""+str(finalsplit[i]+1)+"\""+" ordering=\"1\" parent=\""+str(finalsplit[i])+"\""+" style=\"ImplicitInputPort\">\n <mxGeometry as=\"geometry\" height=\"10\" relative=\"1\" width=\"10\" y=\"0.5000\">\n </mxGeometry>\n </ImplicitInputPort>\n <ImplicitOutputPort connectable=\"0\" dataType=\"UNKNOW_TYPE\" id=\""+str(finalsplit[i]+2)+"\""+" ordering=\"1\" parent=\""+str(finalsplit[i])+"\""+" style=\"ImplicitOutputPort\">\n <mxGeometry as=\"geometry\" height=\"10\" relative=\"1\" width=\"10\" y=\"0.5000\">\n </mxGeometry>\n </ImplicitOutputPort>\n <ImplicitOutputPort connectable=\"0\" dataType=\"UNKNOW_TYPE\" id=\""+str(finalsplit[i]+3)+"\""+" ordering=\"1\" parent=\""+str(finalsplit[i])+"\""+" style=\"ImplicitOutputPort\">\n <mxGeometry as=\"geometry\" height=\"10\" relative=\"1\" width=\"10\" y=\"0.5000\">\n </mxGeometry>\n </ImplicitOutputPort>\n"+line
out_file.write(line)
list3=[]
implitdetect=[]
#return temp_file_xml_name
for i in range(len(finalsplit)):
implitdetect.append(finalsplit[i]+5)
implitdetect.append(finalsplit[i]+6)
for i in range(len(implitdetect)):
pattern3 = re.compile("<ImplicitLink id=\""+str(implitdetect[i])+"\"")
for i, line in enumerate(open(temp_file_xml_name)):
for match in re.finditer(pattern3, line):
list3.append(i-1)
with open(temp_file_xml_name,'r+') as f:
data = f.read().splitlines()
replace = list3
for i in replace:
data[i] = ' </ImplicitLink>'
f.seek(0)
f.write('\n'.join(data))
f.truncate()
base_filename = os.path.splitext(temp_file_xml_name)[0]
os.rename(temp_file_xml_name, base_filename + ".xcos")
source_folder = os.getcwd()
destination_folder = source_folder + "/uploads/"
folder_file_content = filter(os.path.isfile, os.listdir( os.curdir ) )
for file in folder_file_content:
if file.endswith(".xcos"):
shutil.copy(file, destination_folder)
os.remove(file)# After moving to the required folder deleting the xcos file.
temp_file_xml_name = base_filename + ".xcos"
xcos_file_list.append(temp_file_xml_name)
workspace_list.append(workspace_counter)
return str(client_id)
# List to contain all affich blocks
blockaffich = new_xml.getElementsByTagName("AfficheBlock")
block_ida = []
for block in blockaffich:
if block.getAttribute("interfaceFunctionName") == "AFFICH_m":
block_ida.append(block.getAttribute("id"))
block.setAttribute('id', '-1')
workspace_counter=4
flag1=1
# List to contain all the block IDs of tkscales so that we can create read blocks with these IDs
block_id = []
for block in blocks:
if block.getAttribute("interfaceFunctionName") == "TKSCALE":
block_id.append(block.getAttribute("id"))
block.setAttribute('id', '-1')
Details.tk_is_present = True
# Changed the ID of tkscales to -1 so that virtually the tkscale blocks get disconnected from diagram at the backend
# Taking workspace_counter 1 for TOWS_c and 2 for FROMWSB
if block.getAttribute("interfaceFunctionName")== "scifunc_block_m":
workspace_counter = 7
if block.getAttribute("interfaceFunctionName")== "TOWS_c":
workspace_counter=1
flag1=1
main_attributes=block.getElementsByTagName("ScilabString")
data= main_attributes[0].getElementsByTagName("data")
workspace_variable=data[1].getAttribute("value")
workspace_dict[client_id]=workspace_variable
if block.getAttribute("interfaceFunctionName")== "FROMWSB":
workspace_counter=2
flag2=1
if (flag1 and flag2):
# Both TOWS_c and FROMWSB are present
workspace_counter=3
# Hardcoded the real time scaling to 1.0 (i.e., no scaling of time occurs) only if tkscale is present
if(Details.tk_is_present):
diagram = new_xml.getElementsByTagName("XcosDiagram")
for dia in diagram:
dia.setAttribute('realTimeScaling', '1.0')
# Save the changes made by parser
tk_count = 0
with open(temp_file_xml_name,'w') as f:
f.write(new_xml.toxml())
# In front of block tkscale printing the block corresponding to read function and assigning corresponding values
for line in fileinput.input(temp_file_xml_name, inplace=1):
if 'interfaceFunctionName=\"TKSCALE\"' in line:
print("<BasicBlock blockType=\"d\" id=\"", end ='')
# change the block ID
print(block_id[tk_count], end = '')
print("\" interfaceFunctionName=\"RFILE_f\" parent=\"1\" simulationFunctionName=\"readf\" simulationFunctionType=\"DEFAULT\" style=\"RFILE_f\">")
print("<ScilabString as=\"exprs\" height=\"5\" width=\"1\">")
print("<data column=\"0\" line=\"0\" value=\"1\"/>")
# Value equal to 1 implies take readings from first column in the file
print("<data column=\"0\" line=\"1\" value=\"2\"/>")
print("<data column=\"0\" line=\"2\" value=\"", end = '')
path_current_directory = os.getcwd()
print(path_current_directory, end='')
print("/values/", end ='')
print(Details.uid, end ='')
print("_tk", end = '')
# Path to the file from which read block obtains the values
print(tk_count + 1, end = '')
print(".txt", end = '')
print("\"/>")
print("<data column=\"0\" line=\"3\" value=\"(2(e10.3,1x))\"/>")
# (2(e10.3,1x)) The format in which numbers are written
# Two columns with base 10 and 3 digits after decimal and 1x represents 1 unit space between two columns.
print("<data column=\"0\" line=\"4\" value=\"2\"/>")
print("</ScilabString>")
print("<ScilabDouble as=\"realParameters\" height=\"0\" width=\"0\"/>")
print("<ScilabDouble as=\"integerParameters\" height=\"105\" width=\"1\">")
tk_count = tk_count + 1
# The remaining part of the block is read from the Read_Content.txt file and written to the xml file
read_file = open("Read_Content.txt", "r")
for line_content in read_file:
print(line_content,end= '')
print(line,end = '')
aff_count = 0
for line in fileinput.input(temp_file_xml_name, inplace=1):# if the keyword argument inplace=1 is passed to fileinput.input() or to the FileInput constructor, the file is moved to a backup file and standard output is directed to the input file
if 'interfaceFunctionName=\"AFFICH_m\"' in line:
print("<BasicBlock blockType=\"d\" dependsOnU=\"1\" id=\"", end ='')
print(block_ida[aff_count], end = '')
print("\" interfaceFunctionName=\"TOWS_c\" parent=\"1\" simulationFunctionName=\"tows_c\" simulationFunctionType=\"C_OR_FORTRAN\" style=\"TOWS_c\">")
print("<ScilabString as=\"exprs\" height=\"3\" width=\"1\">")
print("<data column=\"0\" line=\"0\" value=\"1\"/>")
#For workspace variable name
aff_count = aff_count + 1
print("<data column=\"0\" line=\"1\" value=\"", end = '')
print("var"+str(aff_count), end ='')
workspace_variable_list.append("var"+str(aff_count))
print("\"/>")
print("<data column=\"0\" line=\"2\" value=\"0\"/>")
print("</ScilabString>")
affich_count = aff_count
read_file = open("Read_tows_c.txt", "r")
for line_content in read_file:
print(line_content,end= '')
print(line,end = '')
block_idint=[]
block_idmatblsk=[]
block_det=[]
block_diag=[]
block_div=[]
block_curl=[]
for block in blocks:
if block.getAttribute("style") == "INTMUL":
block_idint.append(int(block.getAttribute("id")))
if block.getAttribute("style") == "MATBKSL":
block_idmatblsk.append(int(block.getAttribute("id")))
if block.getAttribute("style") == "MATDET":
block_det.append(int(block.getAttribute("id")))
if block.getAttribute("style") == "MATDIAG":
block_diag.append(int(block.getAttribute("id")))
if block.getAttribute("style") == "MATDIV":
block_div.append(int(block.getAttribute("id")))
if block.getAttribute("style") == "CURV_f":
block_curl.append(int(block.getAttribute("id")))
if len(block_idint)>=1:
with open(temp_file_xml_name,"r") as f:
newline=[]
i=0
for word in f.readlines():
if "<ExplicitInputPort dataType=\"REAL_MATRIX\"" in word:
temp_word=""
for i in range(len(block_idint)):
if "<ExplicitInputPort dataType=\"REAL_MATRIX\" id=\""+str(block_idint[i]+2)+"\"" in word:
temp_word=word.replace("<ExplicitInputPort dataType=\"REAL_MATRIX\" id=\""+str(block_idint[i]+2)+"\"","<ExplicitInputPort dataColumns=\"-3\" dataLines=\"-2\" dataType=\"REAL_MATRIX\" id=\""+str(block_idint[i]+2)+"\"")
i=i+1
if temp_word!="":
newline.append(temp_word)
else:
newline.append(word)
else:
newline.append(word)
with open(temp_file_xml_name,"w") as f:
for line in newline:
f.writelines(line)
with open(temp_file_xml_name,"r") as f:
newline=[]
i=0
for word in f.readlines():
if "<ExplicitOutputPort dataType=\"REAL_MATRIX\"" in word:
temp_word=""
for i in range(len(block_idint)):
if "<ExplicitOutputPort dataType=\"REAL_MATRIX\" id=\""+str(block_idint[i]+3)+"\"" in word:
temp_word=word.replace("<ExplicitOutputPort dataType=\"REAL_MATRIX\" id=\""+str(block_idint[i]+3)+"\"","<ExplicitOutputPort dataColumns=\"-3\" dataType=\"REAL_MATRIX\" id=\""+str(block_idint[i]+3)+"\"")
i=i+1
if temp_word!="":
newline.append(temp_word)
else:
newline.append(word)
else:
newline.append(word)
with open(temp_file_xml_name,"w") as f:
for line in newline:
f.writelines(line)
if len(block_idmatblsk)>=1:
with open(temp_file_xml_name,"r") as f:
newline=[]
i=0
for word in f.readlines():
if "<ExplicitInputPort dataType=\"REAL_MATRIX\"" in word:
temp_word=""
for i in range(len(block_idmatblsk)):
if "<ExplicitInputPort dataType=\"REAL_MATRIX\" id=\""+str(block_idmatblsk[i]+2)+"\"" in word:
temp_word=word.replace("<ExplicitInputPort dataType=\"REAL_MATRIX\" id=\""+str(block_idmatblsk[i]+2)+"\"","<ExplicitInputPort dataColumns=\"-3\" dataType=\"REAL_MATRIX\" id=\""+str(block_idmatblsk[i]+2)+"\"")
i=i+1
if temp_word!="":
newline.append(temp_word)
else:
newline.append(word)
else:
newline.append(word)
with open(temp_file_xml_name,"w") as f:
for line in newline:
f.writelines(line)
with open(temp_file_xml_name,"r") as f:
newline=[]
i=0
for word in f.readlines():
if "<ExplicitOutputPort dataType=\"REAL_MATRIX\"" in word:
temp_word=""
for i in range(len(block_idmatblsk)):
if "<ExplicitOutputPort dataType=\"REAL_MATRIX\" id=\""+str(block_idmatblsk[i]+3)+"\"" in word:
temp_word=word.replace("<ExplicitOutputPort dataType=\"REAL_MATRIX\" id=\""+str(block_idmatblsk[i]+3)+"\"","<ExplicitOutputPort dataColumns=\"-3\" dataLines=\"-2\" dataType=\"REAL_MATRIX\" id=\""+str(block_idmatblsk[i]+3)+"\"")
i=i+1
if temp_word!="":
newline.append(temp_word)
else:
newline.append(word)
else:
newline.append(word)
with open(temp_file_xml_name,"w") as f:
for line in newline:
f.writelines(line)
if len(block_det)>=1:
with open(temp_file_xml_name,"r") as f:
newline=[]
i=0
for word in f.readlines():
if "<ExplicitInputPort dataType=\"REAL_MATRIX\"" in word:
temp_word=""
for i in range(len(block_det)):
if "<ExplicitInputPort dataType=\"REAL_MATRIX\" id=\""+str(block_det[i]+1)+"\"" in word:
temp_word=word.replace("<ExplicitInputPort dataType=\"REAL_MATRIX\" id=\""+str(block_det[i]+1)+"\"","<ExplicitInputPort dataColumns=\"-1\" dataType=\"REAL_MATRIX\" id=\""+str(block_det[i]+1)+"\"")
i=i+1
if temp_word!="":
newline.append(temp_word)
else:
newline.append(word)
else:
newline.append(word)
with open(temp_file_xml_name,"w") as f:
for line in newline:
f.writelines(line)
with open(temp_file_xml_name,"r") as f:
newline=[]
i=0
for word in f.readlines():
if "<ExplicitOutputPort dataType=\"REAL_MATRIX\"" in word:
temp_word=""
for i in range(len(block_det)):
if "<ExplicitOutputPort dataType=\"REAL_MATRIX\" id=\""+str(block_det[i]+2)+"\"" in word:
temp_word=word.replace("<ExplicitOutputPort dataType=\"REAL_MATRIX\" id=\""+str(block_det[i]+2)+"\"","<ExplicitOutputPort dataColumns=\"1\" dataLines=\"1\" dataType=\"REAL_MATRIX\" id=\""+str(block_det[i]+2)+"\"")
i=i+1
if temp_word!="":
newline.append(temp_word)
else:
newline.append(word)
else:
newline.append(word)
with open(temp_file_xml_name,"w") as f:
for line in newline:
f.writelines(line)
if len(block_curl)>=1:
with open(temp_file_xml_name,"r") as f:
newline=[]
i=0
for word in f.readlines():
if "<ExplicitOutputPort dataType=\"REAL_MATRIX\"" in word:
temp_word=""
for i in range(len(block_curl)):
if "<ExplicitOutputPort dataType=\"REAL_MATRIX\" id=\""+str(block_curl[i]+4)+"\"" in word:
temp_word=word.replace("<ExplicitOutputPort dataType=\"REAL_MATRIX\" id=\""+str(block_curl[i]+4)+"\"","<ExplicitOutputPort dataColumns=\"1\" dataLines=\"1\" dataType=\"REAL_MATRIX\" id=\""+str(block_curl[i]+4)+"\"")
i=i+1
if temp_word!="":
newline.append(temp_word)
else:
newline.append(word)