-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit.py
459 lines (354 loc) · 13.6 KB
/
submit.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
import json
import time
import os
from collections import namedtuple
# Python 2/3 compatibility
# Python 2:
try:
from urlparse import urlparse
from urllib import urlencode
from urllib2 import urlopen, Request, HTTPError
except:
pass
# Python 3:
try:
from time import process_time
from urllib.parse import urlparse, urlencode
from urllib.request import urlopen, Request
from urllib.error import HTTPError
except:
pass
import sys
# Python 2:
if sys.version_info < (3, 0):
def process_time():
return time.clock()
def input(str):
return raw_input(str)
# Python 3, backward compatibility with unicode test
if sys.version_info >= (3, 0):
unicode = type(str)
version = '1.0.0'
submitt_url = \
'https://www.coursera.org/api/onDemandProgrammingScriptSubmissions.v1'
Metadata = namedtuple("Metadata", ['assignment_key', 'name', 'part_data'])
Part = namedtuple("Part", ['id', 'input_file', 'solver_file', 'name'])
def load_metadata(metadata_file_name='_coursera'):
'''
Parses an assignment metadata file
Args:
metadata_file_name (str): location of the metadata file
Returns:
metadata as a named tuple structure
'''
if not os.path.exists(metadata_file_name):
print('metadata file "%s" not found' % metadata_file_name)
quit()
try:
with open(metadata_file_name, 'r') as metadata_file:
url = metadata_file.readline().strip()
name = metadata_file.readline().strip()
part_data = []
for line in metadata_file.readlines():
if ',' in line:
line_parts = line.split(',')
line_parts = [x.strip() for x in line_parts]
assert(len(line_parts) == 4)
part_data.append(Part(*line_parts))
if len(url) <= 0:
print('Empty url in _coursera file: %s' % metadata_file_name)
quit()
if len(name) <= 0:
print('Empty assignment name in _coursera file: %s' % metadata_file_name)
quit()
except Exception as e:
print('problem parsing assignment metadata file')
print('exception message:')
print(e)
quit()
return Metadata(url, name, part_data)
def part_prompt(problems):
'''
Prompts the user for which parts of the assignment they would like to
submit.
Args:
problems: a list of assignment problems
Returns:
the selected subset of problems
'''
count = 1
print('Hello! These are the assignment parts that you can submit:')
for i, problem in enumerate(problems):
print(str(count) + ') ' + problem.name)
count += 1
print('0) All')
part_text = input('Please enter which part(s) you want to submit (0-%d): ' % (count-1))
selected_problems = []
selected_models = []
for item in part_text.split(','):
try:
i = int(item)
except:
print('Skipping input "' + item + '". It is not an integer.')
continue
if i >= count or i < 0:
print('Skipping input "' + item + '". It is out of the valid range (0-%d).' % (count-1))
continue
if i == 0:
selected_problems.extend(problems)
continue
if i <= len(problems):
selected_problems.append(problems[i-1])
if len(selected_problems) <= 0:
print('No valid assignment parts identified. Please try again. \n')
return part_prompt(problems)
else:
return selected_problems
def compute(metadata, solver_file_override=None):
'''
Determines which assignment parts the student would like to submit.
Then computes his/her answers to those assignment parts
Args:
metadata: the assignment metadata
solver_file_override: an optional model file to override the metadata
default
Returns:
a dictionary of results in the format Coursera expects
'''
if solver_file_override is not None:
print('Overriding solver file with: '+solver_file_override)
selected_problems = part_prompt(metadata.part_data)
results = {}
#submission needs empty dict for every assignment part
results.update({prob_data.id : {} for prob_data in metadata.part_data})
for problem in selected_problems:
if solver_file_override != None:
solver_file = solver_file_override
else:
solver_file = problem.solver_file
if not os.path.isfile(solver_file):
print('Unable to locate assignment file "%s" in the current working directory.' % solver_file)
continue
# if a relative path is given, add that patth to system path so import will work
if os.path.sep in solver_file:
split = solver_file.rfind(os.path.sep)
path = solver_file[0:split]
file_name = solver_file[split+1:]
sys.path.insert(0, path)
solver_file = file_name
submission = output(problem.input_file, solver_file)
if submission != None:
results[problem.id] = {'output':submission}
print('\n== Computations Complete ...')
return results
def load_input_data(file_location):
with open(file_location, 'r') as input_data_file:
input_data = ''.join(input_data_file.readlines())
return input_data
def output(input_file, solver_file):
'''
Attempts to execute solve_it locally on a given input file.
Args:
input_file: the assignment problem data of interest
solver_file: a python file containing the solve_it function
Returns:
the submission string in a format that the grader expects
'''
try:
pkg = __import__(solver_file[:-3]) # remove '.py' extension
if not hasattr(pkg, 'solve_it'):
print('the solve_it() function was not found in %s' % solver_file)
quit()
except ImportError:
print('import error with python file "%s".' % solver_file)
quit()
solution = ''
start = process_time()
try:
solution = pkg.solve_it(load_input_data(input_file))
except Exception as e:
print('the solve_it(input_data) method from solver.py raised an exception')
print('try testing it with python ./solver.py before running this submission script')
print('exception message:')
print(str(e))
print('')
return 'Local Exception =('
end = process_time()
if not (isinstance(solution, str) or isinstance(solution, unicode)):
print('Warning: the solver did not return a string. The given object will be converted with the str() method.')
solution = str(solution)
print('Submitting: ')
print(solution)
return solution.strip() + '\n' + str(end - start)
def login_dialog(assignment_key, results, credentials_file_location = '_credentials'):
'''
Requests Coursera login credentials from the student and submits the
student's solutions for grading
Args:
assignment_key: Coursera's assignment key
results: a dictionary of results in Cousera's format
credentials_file_location: a file location where login credentials can
be found
'''
success = False
tries = 0
while not success:
# stops infinate loop when credentials file is incorrect
if tries <= 0:
login, token = login_prompt(credentials_file_location)
else:
login, token = login_prompt('')
code, response = submit_solution(assignment_key, login, token, results)
print('\n== Coursera Response ...')
#print(code)
print(response)
if code != 401:
success = True
else:
print('\ntry logging in again')
tries += 1
def login_prompt(credentials_file_location):
'''
Attempts to load credentials from a file, if that fails asks the user.
Returns:
the user's login and token
'''
if os.path.isfile(credentials_file_location):
try:
with open(credentials_file_location, 'r') as metadata_file:
login = metadata_file.readline().strip()
token = metadata_file.readline().strip()
metadata_file.close()
except:
login, token = basic_prompt()
else:
login, token = basic_prompt()
return login, token
def basic_prompt():
'''
Prompt the user for login credentials.
Returns:
the user's login and token
'''
login = input('User Name (e-mail address): ')
token = input('Submission Token (from the assignment page): ')
return login, token
def submit_solution(assignment_key, email_address, token, results):
'''
Sends the student's submission to Coursera for grading via the submission
API.
Args:
assignment_key: Coursera's assignment key
email_address: the student's email
token: the student's assignment token
results: a dictionary of results in Cousera's format
Returns:
the https response code and a feedback message
'''
print('\n== Connecting to Coursera ...')
print('Submitting %d of %d parts' %
(sum(['output' in v for k,v in results.items()]), len(results)))
# build json datastructure
parts = {}
submission = {
'assignmentKey': assignment_key,
'submitterEmail': email_address,
'secret': token,
'parts': results
}
# send submission
req = Request(submitt_url)
req.add_header('Cache-Control', 'no-cache')
req.add_header('Content-type', 'application/json')
try:
res = urlopen(req, json.dumps(submission).encode('utf8'))
except HTTPError as e:
response = json.loads(e.read().decode('utf8'))
if 'details' in response and response['details'] != None and \
'learnerMessage' in response['details']:
return e.code, response['details']['learnerMessage']
else:
return e.code, 'Unexpected response code, please contact the ' \
'course staff.\nDetails: ' + response['message']
code = res.code
response = json.loads(res.read().decode('utf8'))
if code >= 200 and code <= 299:
return code, 'Your submission has been accepted and will be ' \
'graded shortly.'
return code, 'Unexpected response code, please contact the '\
'course staff.\nDetails: ' + response
def main(args):
'''
1) Reads a metadata file to customize the submission process to
a particular assignment.
2) The compute the student's answers to the assignment parts.
3) Submits the student's answers for grading.
Provides the an option for saving the submissions locally. This is very
helpful when testing the assignment graders.
Args:
args: CLI arguments from an argparse parser
'''
# needed so that output can import from the cwd
sys.path.append(os.getcwd())
if args.metadata is None:
metadata = load_metadata()
else:
print('Overriding metadata file with: '+args.metadata)
metadata = load_metadata(args.metadata)
print('==\n== '+metadata.name+' Solution Submission \n==')
# compute dialog
results = compute(metadata, args.override)
if sum(['output' in v for k,v in results.items()]) <= 0:
return
# store submissions if requested
if args.record_submission == True:
print('Recording submission as files')
for sid, submission in results.items():
if 'output' in submission:
directory = '_'+sid
if not os.path.exists(directory):
os.makedirs(directory)
submission_file_name = directory+'/submission.sub'
print(' writting submission file: '+submission_file_name)
with open(submission_file_name,'w') as submission_file:
submission_file.write(submission['output'])
submission_file.close()
return
# submit dialog
if args.credentials is None:
login_dialog(metadata.assignment_key, results)
else:
print('Overriding credentials file with: '+args.credentials)
login_dialog(metadata.assignment_key, results, args.credentials)
import argparse
def build_parser():
'''
Builds an argument parser for the CLI
Returns:
parser: an argparse parser
'''
parser = argparse.ArgumentParser(
description='''The submission script for Discrete Optimization
assignments on the Coursera Platform.''',
epilog='''Please file bugs on github at:
https://github.com/discreteoptimization/assignment/issues. If you
would like to contribute to this tool's development, check it out at:
https://github.com/discreteoptimization/assignment'''
)
parser.add_argument('-v', '--version', action='version',
version='%(prog)s '+version)
parser.add_argument('-o', '--override',
help='overrides the python source file specified in the \'_coursera\' file')
parser.add_argument('-m', '--metadata',
help='overrides the \'_coursera\' metadata file')
parser.add_argument('-c', '--credentials',
help='overrides the \'_credentials\' credentials file')
parser.add_argument('-rs', '--record_submission',
help='records the submission(s) as files', action='store_true')
return parser
if __name__ == '__main__':
parser = build_parser()
main(parser.parse_args())