summaryrefslogtreecommitdiff
path: root/instances.py
blob: dd909f783797bb677535607ccf11a0c29c107757 (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
# importing the global modules
import pexpect
import os
import re
import time
import sys
import psutil
import requests
import json
import urllib.request
import base64


from datetime import datetime
from django.template.loader import render_to_string, get_template
from django.core.mail import EmailMultiAlternatives
# importing the local variables
from R_on_Cloud.settings import PROJECT_DIR
from R_on_Cloud.config import (BIN, API_URL)


def execute_code(code, session_id):
    #session_id = self.request.session['session_id']
    # Check for system commands
    system_commands = re.compile(
        r'unix\(.*\)|unix_g\(.*\)|unix_w\(.*\)|'
        r'unix_x\(.*\)|unix_s\(.*\)|host|newfun'
        r'|execstr|ascii|mputl|dir\(\)'
    )
    if system_commands.search(code):
        return {
            'output': 'System Commands not allowed',
        }

    code = re.sub(r"View\(", "print(", code)

    body = {
            'code': code,
            'session_id': session_id,
    }
    req = urllib.request.Request(API_URL)
    req.add_header('Content-Type', 'application/json; charset=utf-8')
    jsondata = json.dumps(body)
    jsondataasbytes = jsondata.encode('utf-8')
    req.add_header('Content-Length', len(jsondataasbytes))
    print(jsondataasbytes)
    result = urllib.request.urlopen(req, jsondataasbytes)
    result = result.read().decode("utf8")
    output = json.loads(result)["output"]
    data = {
        'output': output
    }
    return data


def trim(output):
    output = [line for line in output.split('\n') if line.strip() != '']
    output = '\n'.join(output)
    return output