-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref_pull_grader.py
176 lines (149 loc) · 6.15 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import time
import logging
import json
import urllib2
import xqueue_util as util
import settings
import urlparse
import project_urls
import requests
import boto3
import auth
import traceback
log = logging.getLogger(__name__)
QUEUE_NAME = auth.XQUEUE_INTERFACE['queue_name']
LAMBDA_URL = project_urls.LAMBDA_URL
def each_cycle():
print('\n[*]Logging in to xqueue: ' + QUEUE_NAME)
try:
session = util.xqueue_login()
except Exception as ex:
print('Error: Unable to log into XQueue:', ex)
return
success_length, queue_length = get_queue_length(QUEUE_NAME, session)
print('Get queue successfully: ' + str(success_length) + ', number of queues: ' + str(queue_length))
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:
try:
correct, score, comment = grade(content)
except Exception as ex:
print(ex)
traceback.print_exc()
correct, score, comment = False, 0, '<p>SERVER UNEXPECTED ERROR</p>'
print('correct: ', correct,'score: ', score, 'comment: ', comment)
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'], correct, score, comment, '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")
print(msg)
def grade(content):
body = json.loads(content['xqueue_body'])
student_info = json.loads(body.get('student_info', '{}'))
email = student_info.get('student_email', '')
files = json.loads(content['xqueue_files'])
grader_payload = json.loads(body.get('grader_payload', '{}'))
question = grader_payload.get('question', '')
score = 0
count = 0
comment = ''
lambda_client = boto3.client('lambda', region_name='us-east-1')
for (filename, fileurl) in files.iteritems():
try:
result = lambda_client.invoke(
FunctionName=LAMBDA_URL,
InvocationType='RequestResponse',
LogType = 'None',
Payload = json.dumps({ "file" : fileurl, "question" : question, "info": student_info})
)
except Exception as ex:
comment += '<p><emph>File: ' + filename + ': error</emph></p>'
comment += '<p>Comment: Grading Error</p>'
count += 1
print('Grading error:',ex)
continue
response = json.loads(result['Payload'].read())
if response['correct']:
comment += '<p>File: ' + filename + ': correct</p>'
comment += '<p>Comment: ' + "<br />".join(response['comment'].split("\n")) + '</p>'
else:
comment += '<p><emph>File: ' + filename + ': error</emph></p>'
comment += '<p>Comment: ' + "<br />".join(response['comment'].split("\n")) + '</p>'
if not score:
score = response['score']
else:
score += response['score']
count += 1
score = score / count
if score > 0:
success = True
else:
success = False
return success, score, comment
# auth = AWSRequestsAuth(aws_access_key= KEY,
# aws_secret_access_key= SEC,
# aws_host= settings.IAM['aws_host'],
# aws_region= settings.IAM['aws_region'],
# aws_service= settings.IAM['aws_service'])
# score = None
# count = 0
# comment = ''
# for (filename, fileurl) in files.iteritems():
# r = requests.post(url = LAMBDA_URL, data = json.dumps({ "file" : fileurl, "question" : question}), auth=auth)
# # r = requests.post(url = LAMBDA_URL, data = json.dumps({ "file" : fileurl}))
# response = r.json()
# print(response)
# if response['correct']:
# comment += '<p>File: ' + filename + ': correct</p>'
# comment += '<p>Comment: ' + "<br />".join(response['comment'].split("\n")) + '</p>'
# else:
# comment += '<p><emph>File: ' + filename + ': error</emph></p>'
# comment += '<p>Comment: ' + "<br />".join(response['comment'].split("\n")) + '</p>'
# if not score:
# score = response['score']
# else:
# score += response['score']
# count += 1
# r.close()
# score /= count
# if score > 0:
# success = True
# else:
# success = False
# return success, score, comment
def get_from_queue(queue_name,xqueue_session):
"""
Get a single submission from xqueue
"""
try:
success, response = util._http_get(xqueue_session,
urlparse.urljoin(auth.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(auth.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')