-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsafeExecuteCode.py
119 lines (103 loc) · 3.65 KB
/
safeExecuteCode.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
#!/usr/bin/env python3
# Link between python API/code to a Camisole server running in a VM, accessible by "http://localhost:42920/"
# Author: Lilian BESSON
# Email: lilian DOT besson AT crans D O T org
# Version: 1
# Date: 19-02-2021
# Web: https://github.com/Naereen/Peut-on-coder-avec-OCaml-Python-et-C-par-SMS.git
#
from pprint import pprint
# See https://www.geeksforgeeks.org/get-post-requests-using-python/ for help on this HTTP client using a POST request
import json
import requests
from camisole_limited_ressources_conf import camisole_limited_ressources_conf
assert camisole_limited_ressources_conf
print(f"\nDEBUG: using this camisole_limited_ressources_conf for all requests:")
pprint(camisole_limited_ressources_conf)
# DONE: when importing the module, connect to VM, and list languages
SUPPORTED_LANGUAGES = [
"python",
"ocaml",
"c",
]
PROTOCOL = "http"
ADDRESS = "localhost"
PORT = 42920
URL = f"{PROTOCOL}://{ADDRESS}:{PORT}"
def post_request_to_camisole(data,
protocol=PROTOCOL,
port=PORT,
address=ADDRESS,
endpoint="run",
url=None,
use_json=False,
):
""" Post data as a JSON object to the URL and returns JSON response."""
if not url:
if endpoint:
endpoint = f"/{endpoint}"
url = f"{protocol}://{address}:{port}{endpoint}"
# TODO: use a real logger? Flemme!
print(f"DEBUG: using url = {url}")
try:
print(f"DEBUG: reading data = {data}")
json_data = data
if use_json:
json_data = json.dumps(data)
print(f"DEBUG: forcing data to be JSON: json_data = '{json_data}'")
print(f"DEBUG: making this request...")
print(f"DEBUG: requests.post(url='{url}', json='{json_data}')...")
print(f"DEBUG: types: url='{type(url)}', json='{type(json_data)}')...")
result = requests.post(url=url, json=json_data)
print(f"DEBUG: got a result from this request...")
print(f" DEBUG: result = {result}")
print(f" DEBUG: result.status_code = {result.status_code}")
print(f" DEBUG: result.text = {result.text}")
try:
result_data = result.json()
except json.JSONDecodeError as e:
print(f"Error:\n{e}")
result_data = result.text
pprint(result_data)
print(f"DEBUG: result_data of length {len(result_data)}")
return result_data
except Exception as e:
print(f"Error:\n{e}")
raise e
return {
"success": False,
}
# DONE try to discover the list of supported languages
try:
print(f"DEBUG: querying the camisole backend to get list of languages...")
data = ""
result = post_request_to_camisole(data, use_json=False, endpoint="languages")
json_languages = result["languages"]
languages = list(json_languages.keys())
SUPPORTED_LANGUAGES = languages
print(f"DEBUG: List of languages:\n{SUPPORTED_LANGUAGES}")
except Exception as e:
print(f"Error:\n{e}")
raise e
def safe_execute_code(inputcode,
language="python",
protocol=PROTOCOL,
port=PORT,
address=ADDRESS,
url=None
):
""" Ask Camisole to execute the <inputcode> written in <language>, and returns the JSON result from Camisole."""
data = {
"lang": str(language),
"source": str(inputcode),
}
data.update(camisole_limited_ressources_conf)
# TODO: remove this when debugging is done?
print(f"\nDEBUG: using this data after adding camisole_limited_ressources_conf:")
pprint(data)
return post_request_to_camisole(data,
protocol=protocol,
port=port,
address=address,
url=url,
)