summaryrefslogtreecommitdiff
path: root/website/helpers.py
blob: c5aad51c2d26f2bc48309addeeb2c1c5fef2e174 (plain)
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
import os
import re
import time
import subprocess

from soc.settings import PROJECT_DIR
 
def scilab_run(code, token):
    #Remove all clear;
    code = code.replace('clear','')
    code = code.replace(r'clear.*all','')
    plot_exists = False

    #Finding the plot and appending xs2jpg function
    p = re.compile(r'.*plot.*\(.*,.*,*\).*\n')
    if p.search(code):
        plot_exists = True
        code = code + '\n'
        temp = code
        code = ''
        start, end, count = 0, 0, 0
        current_time = time.time()
        plot_path = []
        for (count, match) in enumerate(p.finditer(temp)):
            print "count==================",count
            plot_path.append(
                PROJECT_DIR + '/static/tmp/{0}.jpg'.format(int(current_time) + count)
            )
            end = match.end()
            code += temp[start:end]
            code += 'xs2jpg(gcf(), "{0}");\n'.format(plot_path[-1])
            start = end
        code += temp[end:]

    #Check whether to load scimax / maxima
    if 'syms' in code or 'Syms' in code:
        code = code.replace('syms', 'Syms')
        code = 'exec(\'/home/cheese/scimax/loader.sce\');\nmaxinit\n' + code

    file_path = PROJECT_DIR + '/static/tmp/' + token + '.sci'

    #thanks @prathamesh920 github
    #traps even syntax errors eg: endfunton
    f = open(file_path, "w")
    f.write('mode(2);\n')
    f.write(code)
    f.write('\nquit();')
    f.close()
    

    #this makes it possible to execute scilab without the problem of \
    #getting stuck in the prompt in case of error
    cmd = 'printf "lines(0)\nexec(\'{0}\',2);\nquit();"'.format(file_path)
    cmd += ' | /home/cheese/scilab-5.4.1/bin/scilab-adv-cli -nw'
    print cmd

    output = subprocess.Popen(
        cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
    ).communicate()[0]

    #os.remove(file_path)
    output = trim(output)
    return output

def trim(output):
    #for future use
    return output