-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathref_pull_grader.py
86 lines (71 loc) · 2.95 KB
/
ref_pull_grader.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
import time
import logging
import json
import urllib2
import xqueue_util as util
import settings
import urlparse
import project_urls
log = logging.getLogger(__name__)
QUEUE_NAME = settings.QUEUE_NAME
def each_cycle():
print('[*]Logging in to xqueue')
session = util.xqueue_login()
success_length, queue_length = get_queue_length(QUEUE_NAME, session)
if success_length and queue_length > 0:
success_get, queue_item = get_from_queue(QUEUE_NAME, session)
print(queue_item)
success_parse, content = util.parse_xobject(queue_item, QUEUE_NAME)
if success_get and success_parse:
grade(content)
content_header = json.loads(content['xqueue_header'])
content_body = json.loads(content['xqueue_body'])
xqueue_header, xqueue_body = util.create_xqueue_header_and_body(content_header['submission_id'], content_header['submission_key'], True, 1, '<p><emph>Good Job!</emph></p>', 'reference_dummy_grader')
(success, msg) = util.post_results_to_xqueue(session, json.dumps(xqueue_header), json.dumps(xqueue_body),)
if success:
print("successfully posted result back to xqueue")
def grade(content):
print(content)
body = json.loads(content['xqueue_body'])
student_info = json.loads(body.get('student_info', '{}'))
email = student_info.get('student_email', '')
print("submitted by email: " + email)
files = json.loads(content['xqueue_files'])
for (filename, fileurl) in files.iteritems():
response = urllib2.urlopen(fileurl)
with open(filename, 'w') as f:
f.write(response.read())
f.close()
response.close()
def get_from_queue(queue_name,xqueue_session):
"""
Get a single submission from xqueue
"""
try:
success, response = util._http_get(xqueue_session,
urlparse.urljoin(settings.XQUEUE_INTERFACE['url'], project_urls.XqueueURLs.get_submission),
{'queue_name': queue_name})
except Exception as err:
return False, "Error getting response: {0}".format(err)
return success, response
def get_queue_length(queue_name,xqueue_session):
"""
Returns the length of the queue
"""
try:
success, response = util._http_get(xqueue_session,
urlparse.urljoin(settings.XQUEUE_INTERFACE['url'], project_urls.XqueueURLs.get_queuelen),
{'queue_name': queue_name})
if not success:
return False,"Invalid return code in reply"
except Exception as e:
log.critical("Unable to get queue length: {0}".format(e))
return False, "Unable to get queue length."
return True, response
try:
logging.basicConfig()
while True:
each_cycle()
time.sleep(2)
except KeyboardInterrupt:
print '^C received, shutting down'