-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmueffler.py
executable file
·103 lines (82 loc) · 3.08 KB
/
mueffler.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
#!/usr/bin/python3
import json
import http.server
import subprocess
def translate_arguments(arguments, argument_map):
arg_list = []
for arg in arguments.keys():
if not arg in argument_map:
continue
arg_list.extend(argument_map[arg](arguments[arg]))
return arg_list
def single(name):
return lambda x: ['{}={}'.format(name, x)]
def multi(name):
return lambda xs: ['{}={}'.format(name, x) for x in xs]
def flag(name):
return lambda cond: [name] if cond else []
class MuefflerHandler(http.server.BaseHTTPRequestHandler):
def echo(**args):
return args
def mueval(**args):
argument_map = {
'expression': single('--expression'),
'timeLimit': single('--time-limit'),
'loadFile': single('--load-file'),
'modules': multi('--module'),
'extensions': flag('--Extensions'),
'inferredType': flag('--inferred-type'),
'typeOnly': flag('--type-only'),
'resourceLimits': flag('--resource-limits'),
'packageTrust': flag('--package-trust'),
'trust': multi('--trust')
# Not included: --password and --help
}
completed_proc = subprocess.run(
[ "mueval" ] + translate_arguments(args, argument_map),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
return {
"returncode": completed_proc.returncode,
"output": completed_proc.stdout.decode('utf-8')
}
procedures = {
'echo': echo,
'mueval': mueval
}
# http stuff (ugly)
def do_POST(self):
if self.path != '/rpc':
self.send_error(400, explain = "invalid endpoint (only /rpc is allowed)")
return
if self.headers.get('content-type') != 'application/json':
self.send_error(400, explain = 'invalid content type (only application/json is allowed')
return
try:
content_length = int(self.headers.get('content-length'))
except:
self.send_error(400, explain = "invalid content-length")
return
body = json.loads(self.rfile.read(content_length))
if not 'proc' in body or not body['proc'] in self.procedures:
self.send_error(400, explain = "missing or invalid proc, use {}".format("|".join(self.procedures.keys())))
return
if not 'args' in body:
self.send_error(400, explain = "missing args")
return
try:
result = self.procedures[body['proc']](**body['args'])
except Exception as err:
self.send_error(500, explain = str(err))
return
print(body)
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(result).encode('utf-8'))
if __name__ == '__main__':
hostname = '0.0.0.0'
port = 8080
server = http.server.HTTPServer((hostname, port), MuefflerHandler)
server.serve_forever()