forked from instructure/straitjacket
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
executable file
·119 lines (98 loc) · 3.87 KB
/
server.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
#!/usr/bin/env python
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of StraitJacket.
#
# StraitJacket is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# StraitJacket is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
import web
import json
import os
import logging
from lib import straitjacket
import straitjacket_settings
LOGGER = logging.getLogger('server')
ROOT_DIRECTORY = os.path.realpath(os.path.dirname(__file__))
DEFAULT_CONFIG_DIR = os.path.join(ROOT_DIRECTORY, "config")
def _setup_logging():
root = logging.getLogger()
root.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler()
formatter = logging.Formatter('[%(levelname)-8s] %(process)-5d %(asctime)s %(name)s: %(message)s')
stream_handler.setFormatter(formatter)
root.addHandler(stream_handler)
root.info("Set up logging")
_setup_logging()
class JSONWrapper(object):
def __init__(self, my_json):
self.json = my_json
def __getattr__(self, name):
try:
return self.json[name]
except KeyError:
raise AttributeError
def _get_file_content(file_path):
with open(file_path, 'r') as f:
return f.read()
def webapp(wrapper=None, config_dir=DEFAULT_CONFIG_DIR, skip_language_checks=False):
if not wrapper:
wrapper = straitjacket.StraitJacket(skip_language_checks=skip_language_checks)
class index: # pylint: disable=W0612
def GET(self):
return _get_file_content(os.path.join(ROOT_DIRECTORY, 'static/html/index.html'))
class execute: # pylint: disable=W0612
def POST(self):
web.header('Content-Type', 'text/json')
data = web.data()
try:
data = JSONWrapper(json.loads(data))
except ValueError:
data = web.input()
timelimit = getattr(data, 'timelimit', straitjacket_settings.MAX_RUNTIME)
timelimit = float(timelimit) if timelimit else None
if hasattr(data, 'stdin'):
stdin = [data.stdin] if not type(data.stdin) == list else data.stdin
else:
stdin = [None]
try:
results = wrapper.run(data.language, data.source, stdin, timelimit=timelimit)
return json.dumps(results)
except straitjacket.InputError as ex:
LOGGER.error("Input error: {0}".format(ex))
raise web.badrequest()
except AttributeError as ex:
LOGGER.error("Attribute error: {0}".format(ex))
raise web.badrequest()
class info: # pylint: disable=W0612
def GET(self):
web.header('Content-Type', 'text/json')
language_info = {'languages': {}}
for language in wrapper.languages.values():
try:
language_info['languages'][language.name] = {
'visible_name' : language.visible_name,
'version' : language.version
}
except OSError as ex:
LOGGER.error("Unable to get language info for %s: %s", language.name, ex)
return json.dumps(language_info, sort_keys=True)
app = web.application((
'/', 'index',
'/execute', 'execute',
'/info', 'info',
), locals())
return app
if __name__ == "__main__":
webapp().run()
application = webapp().wsgifunc()