blob: 6bfe0d60807149f89909f5484e55548ed5ff19aa (
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
|
from xmlrpclib import ServerProxy
import time
import random
import socket
import json
import urllib
from settings import SERVER_PORTS, SERVER_POOL_PORT
class ConnectionError(Exception):
pass
###############################################################################
# `CodeServerProxy` class.
###############################################################################
class CodeServerProxy(object):
"""A class that manages accesing the farm of Python servers and making
calls to them such that no one XMLRPC server is overloaded.
"""
def __init__(self):
pool_url = 'http://localhost:%d' % (SERVER_POOL_PORT)
self.pool_url = pool_url
def run_code(self, language, test_case_type, json_data, user_dir):
"""Tests given code (`answer`) with the `test_code` supplied. If the
optional `in_dir` keyword argument is supplied it changes the directory
to that directory (it does not change it back to the original when
done). The parameter language specifies which language to use for the
tests.
Parameters
----------
json_data contains;
user_answer : str
The user's answer for the question.
test_code : str
The test code to check the user code with.
language : str
The programming language to use.
user_dir : str (directory)
The directory to run the tests inside.
Returns
-------
A json string of a dict: {success: success, err: error message}.
"""
try:
server = self._get_server()
result = server.check_code(language, test_case_type, json_data, user_dir)
except ConnectionError:
result = json.dumps({'success': False, 'error': 'Unable to connect to any code servers!'})
return result
def _get_server(self):
port = json.loads(urllib.urlopen(self.pool_url).read())
proxy = ServerProxy('http://localhost:%d' % port)
return proxy
# views.py calls this Python server which forwards the request to one
# of the running servers.
code_server = CodeServerProxy()
|