-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautograder.py
945 lines (816 loc) · 31.6 KB
/
autograder.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
"""
Student autograder utilities.
This file provides a common interface for the CS 61A project
student-side autograder. Students do not need to read or understand
the contents of this file.
Usage:
This file is intended to run as the main script. Test cases should
be defined in the faile 'tests.pkl'
This file supports the following primary options:
* Unlocking tests (-u): Students will receive test cases in locked
form. Test cases can be unlocked by using the "-u" flag. Once a
test is unlocked, students will be able to run the autograder with
those cases (see below)
* Testing individual questions (-q): Students can test an individual
question using the "-q" flag. Omitting the "-q" flag will test
all unlocked test cases. By default, the autograder will stop once
the first error is encountered (see below)
* Testing all questions regardless of errors (-a): When specified, the
autograder continues running even if it encounters errors. The
"-a" flag works even if the "-q" is used
* Interactive debugging (-i): by default, when the autograder
encounters an error, it prints the stack trace and terminates. The
"-i" will instead print the stack trace, then open an interactive
console to allow students to inspect the environment.
* Adjusting the timeout limit (-t): specify the number of seconds
before the autograder exits due to a timeout.
"""
import argparse
from code import InteractiveConsole, InteractiveInterpreter, compile_command
import re
import traceback
import os
import sys
import hmac
import pickle
import rlcompleter
import urllib.request
import pdb
######################
# PRINTING UTILITIES #
######################
def make_output_fns():
"""Returns functions related to printing output."""
devnull = open(os.devnull, 'w')
stdout = sys.stdout
on = True
def toggle_output(toggle_on=None):
"""Toggles output between stdout and /dev/null.
PARAMTERS:
toggle_on -- bool or None; if None, switch the output
destination; if True, switch output to stdout;
if False, switch output to /dev/null
"""
nonlocal on
if toggle_on is not None:
on = not toggle_on
if on:
sys.stdout = devnull
else:
sys.stdout = stdout
on = not on
return toggle_output
toggle_output = make_output_fns()
def split(src, join=False):
"""Splits a (possibly multiline) string of Python input into
a list, adjusting for common indents.
PARAMETERS:
src -- str; (possibly) multiline string of Python input
join -- bool; if True, join output into one string
DESCRIPTION:
Indentation adjustment is determined by the first nonempty
line. The characters of indentation for that line will be
removed from the front of each subsequent line.
RETURNS:
list of strings; lines of Python input
str; all lines combined into one string if join is True
"""
src = src.lstrip('\n').rstrip()
match = re.match('\s+', src)
length = len(match.group(0)) if match else 0
result = [line[length:] for line in src.split('\n')]
if join:
result = '\n'.join(result)
return result
def underline(line, under='='):
"""Prints an underlined version of the given line with the
specified underline style.
PARAMETERS:
line -- str
under -- str; a one-character string that specifies the underline
style
"""
print(line + '\n' + under * len(line))
def display_prompt(line, prompt='>>> '):
"""Formats and prints a given line as if it had been typed in an
interactive interpreter.
PARAMETERS:
line -- object; represents a line of Python code. If not a
string, line will be converted using repr. Otherwise,
expected to contain no newlines for aesthetic reasons
prompt -- str; prompt symbol. If a space is desired between the
symbol and input, prompt must contain the space itself
"""
if type(line) != str:
line = repr(line)
print(prompt + line)
PS1 = '>>> '
PS2 = '... '
#####################
# TIMEOUT MECHANISM #
#####################
class TimeoutError(Exception):
"""Exception for timeouts."""
_message = 'Evaluation timed out!'
def __init__(self, timeout):
"""Constructor.
PARAMTERS:
timeout -- int; number of seconds before timeout error occurred
"""
super().__init__(self)
self.timeout = timeout
TIMEOUT = 10
def timed(fn, args=(), kargs={}, timeout=TIMEOUT):
"""Evaluates expr in the given frame.
PARAMETERS:
fn -- function; Python function to be evaluated
args -- tuple; positional arguments for fn
kargs -- dict; keyword arguments for fn
timeout -- int; number of seconds before timer interrupt
RETURN:
Result of calling fn(*args, **kargs).
RAISES:
TimeoutError -- if thread takes longer than timemout to execute
Error -- if calling fn raises an error, raise it
"""
from threading import Thread
class ReturningThread(Thread):
"""Creates a daemon Thread with a result variable."""
def __init__(self):
Thread.__init__(self)
self.daemon = True
self.result = None
self.error = None
def run(self):
try:
self.result = fn(*args, **kargs)
except Exception as e:
e._message = traceback.format_exc(limit=2)
self.error = e
submission = ReturningThread()
submission.start()
submission.join(timeout)
if submission.is_alive():
raise TimeoutError(timeout)
if submission.error is not None:
raise submission.error
return submission.result
#####################
# Testing Mechanism #
#####################
class TestError(Exception):
"""Custom exception for autograder."""
PREAMBLE = -1
def __init__(self, case=None, frame=None):
"""Constructor.
PARAMETERS:
case -- int; specifies the index of the case in the suite that
caused the error. If case == TestError.PREAMBLe,
denotes the preamble of the suite that cased the error
frame -- dict; the global frame right before the error occurred
"""
super().__init__()
self.case = case
self.frame = frame
self.super_preamble = None
def get(self, test, suite):
"""Gets the code that caused the error.
PARAMTERS:
test -- dict; the test in which the error occurred
suite -- int; the index of the suite in which the error
occurred
RETURNS:
str; the code that caused the error.
"""
preamble = self.super_preamble + \
test.get('preamble', {}).get('all', '') + '\n' + \
test.get('preamble', {}).get(suite, '')
preamble = split(preamble, join=True)
if self.case == self.PREAMBLE:
return preamble, ''
assert 0 <= suite < len(test['suites']), 'Test {} does not have Suite {}'.format(get_name(test), suite)
assert 0 <= self.case < len(test['suites'][suite]), 'Suite {} does not have Case {}'.format(suite, self.case)
code, outputs, status = test['suites'][suite][self.case]
code = split(code, join=True)
return preamble + '\n' + code, outputs
def get_name(test):
"""Gets the name of a test.
PARAMETERS:
test -- dict; test cases for a question. Expected to contain a key
'name', which either maps to a string or a iterable of
strings (in which case the first string will be used)
RETURNS:
str; the name of the test
"""
if type(test['name']) == str:
return test['name']
return test['name'][0]
def get_test(tests, question):
"""Retrieves a test for the specified question in the given list
of tests.
PARAMETERS:
tests -- list of dicts; list of tests
question -- str; name of test
RETURNS:
dict; the test corresponding to question. If no such test is found,
return None
"""
for test in tests:
if 'name' not in test:
continue
names = test['name']
if type(names) == str:
names = (names,)
if question in names:
return test
def run(test, global_frame=None, interactive=False, super_preamble=''):
"""Runs all test suites for this class.
PARAMETERS:
test -- dict; test cases for a single question
global_frame -- dict; bindings for the global frame
interactive -- bool; True if interactive mode is enabled
super_preamble -- str; preamble that is executed for every test
DESCRIPTION:
Test suites should be correspond to the key 'suites' in test.
If no such key exists, run as if zero suites are
defined. Use the first value corresponding to the key 'name' in
test as the name of the test.
RETURNS:
bool; True if all suites passed.
"""
name = get_name(test)
underline('Test ' + name)
if global_frame is None:
global_frame = {}
if 'note' in test:
print(split(test['note'], join=True))
if 'suites' not in test:
test['suites'] = []
if 'cache' in test:
try:
cache = compile(split(test['cache'], join=True),
'{} cache'.format(name), 'exec')
timed(exec, (cache, global_frame))
except Exception as e:
# TODO
pass
preamble = super_preamble
if 'preamble' in test and 'all' in test['preamble']:
preamble += test['preamble']['all']
postamble = ''
if 'postamble' in test and 'all' in test['postamble']:
postamble = test['postamble']['all']
passed = 0
for counter, suite in enumerate(test['suites']):
# Preamble and Postamble
label = '{} suite {}'.format(name, counter)
new_preamble = preamble
if 'preamble' in test:
new_preamble += test['preamble'].get(counter, '')
new_preamble = compile(split(new_preamble, join=True),
'{} preamble'.format(label), 'exec')
new_postamble = postamble
if 'postamble' in test:
new_postamble += test['postamble'].get(counter, '')
new_postamble = compile(split(new_postamble, join=True),
'{} postamble'.format(label), 'exec')
# TODO
# Run tests
toggle_output(False)
try:
frame = run_suite(new_preamble, suite, new_postamble, global_frame)
except TestError as e:
exec(new_postamble, e.frame)
e.super_preamble = super_preamble
toggle_output(True)
frame = handle_failure(e, test, counter,
global_frame.copy(), interactive)
exec(new_postamble, frame)
break
else:
passed += 1
toggle_output(True)
unlocked_cases = sum(1 if case[2] == 'unlocked' else 0
for suite in test['suites'] for case in suite)
total_cases = sum(len(suite) for suite in test['suites'])
if passed == len(test['suites']):
print('All unlocked tests passed!')
if unlocked_cases < total_cases:
print('-- NOTE: {} still has {} locked cases! --'.format(name,
total_cases - unlocked_cases))
print()
return passed == len(test['suites'])
def test_call(fn, args=(), kargs={}, case=-1, frame={}, exception=None):
"""Attempts to call fn with args and kargs. If a timeout or error
occurs in the process, raise a TestError.
PARAMTERS:
fn -- function
args -- tuple; positional arguments to fn
kargs -- dict; keyword arguments to fn
case -- int; index of case to which the function call belongs
frame -- dict; current state of the global frame
RETURNS:
result of calling fn
RAISES:
TestError; if calling fn causes an Exception or Timeout
"""
try:
result = timed(fn, args, kargs)
except Exception as e:
if type(exception)==type and issubclass(exception, BaseException) and isinstance(e, exception):
return exception
raise TestError(case, frame)
else:
return result
def run_suite(preamble, suite, postamble, global_frame):
"""Runs tests for a single suite.
PARAMETERS:
preamble -- str; the preamble that should be run before
every test
suite -- list; each element is a test case, represented
as a 2-tuple or 3-tuple
postamble -- str; the postamble that should be run after every
test case
global_frame -- dict; global frame
DESCRIPTION:
Each test case in the parameter suite is represented as a
2-tuple
(input, outputs)
where:
input -- str; a (possibly multiline) string of Python
source code
outputs -- iterable or string; if string, outputs is the
sole expected output. If iterable, each element
in outputs should correspond to an input slot
in input (delimited by '$ ').
For each test, a new frame is created and houses all bindings
made by the test. The preamble will run first (if it exists)
before the test input. The postamble will be run after the test.
Expected output and actual output are tested on shallow equality
(==). If a test fails, a TestError will be raised that
contains information about the test.
RETURNS:
dict; the global frame to signal a success
RAISES:
TestError; contains information about the test that failed.
"""
for case_num, (case, outputs, status) in enumerate(suite):
if status == 'locked':
continue
frame = global_frame.copy()
test_call(exec, (preamble, frame),
case=TestError.PREAMBLE, frame=frame)
if type(outputs) == str:
outputs = (outputs,)
out_iter = iter(outputs)
current, prompts = '', 0
lines = split(case) + ['']
for i, line in enumerate(lines):
if line.startswith(' ') or compile_command(current.replace('$ ', '')) is None:
current += line + '\n'
continue
if current.startswith('$ ') or \
(i == len(lines) - 1 and prompts == 0):
expect = test_call(eval, (next(out_iter), frame.copy()),
case=case_num, frame=frame)
actual = test_call(eval, (current.replace('$ ', ''), frame),
case=case_num, frame=frame,
exception=expect)
if expect != actual:
raise TestError(case_num, frame)
else:
test_call(exec, (current, frame), case=case_num,
frame=frame)
current = ''
if line.startswith('$ '):
prompts += 1
current += line + '\n'
exec(postamble, frame)
return global_frame
def handle_failure(error, test, suite_number, global_frame, interactive):
"""Handles a test failure.
PARAMETERS:
error -- TestError; contains information about the
failed test
test -- dict; contains information about the test
suite_number -- int; suite number (for informational purposes)
global_frame -- dict; global frame
interactive -- bool; True if interactive mode is enabled
DESCRIPTION:
Expected output and actual output are checked with shallow
equality (==).
RETURNS:
bool; True if error actually occurs, which should always be
the case -- handle_failure should only be called if a test
fails.
"""
code_source, outputs = error.get(test, suite_number)
underline('Test case failed:', under='-')
try:
compile(code_source.replace('$ ', ''),
'Test {} suite {} case {}'.format(get_name(test), suite_number, error.case),
'exec')
except SyntaxError as e:
print('SyntaxError:', e)
return global_frame
console = InteractiveConsole(locals=global_frame)
if type(outputs) == str:
outputs = (outputs,)
out_iter = iter(outputs)
current, prompts = '', 0
lines = split(code_source) + ['']
for i, line in enumerate(lines):
if line.startswith(' ') or compile_command(current.replace('$ ', '')) is None:
current += line + '\n'
display_prompt(line.replace('$ ', ''), PS2)
continue
if current.startswith('$ ') or \
(i == len(lines) - 1 and prompts == 0):
try:
expect = handle_test(eval, (next(out_iter), global_frame.copy()),
console=console, current=current,
interactive=interactive)
actual = handle_test(eval, (current.replace('$ ', ''), global_frame),
console=console, current=current,
interactive=interactive,
expect=expect)
except TestError:
return global_frame
display_prompt(actual, '')
if expect != actual:
print('# Error: expected', repr(expect), 'got', repr(actual))
if interactive:
interact(console)
print()
return global_frame
else:
try:
handle_test(exec, (current, global_frame),
console=console, current=current,
interactive=interactive)
except TestError:
return global_frame
current = ''
if line.startswith('$ '):
prompts += 1
current += line + '\n'
display_prompt(line.replace('$ ', ''), PS1)
print()
return global_frame
def handle_test(fn, args=(), kargs={}, console=None, current='',
interactive=False, expect=None):
"""Handles a function call and possibly starts an interactive
console.
PARAMTERS:
fn -- function
args -- tuple; positional arguments to fn
kargs -- dict; keyword arguments to fn
console -- InteractiveConsole
line -- str; line that contained call to fn
interactive -- bool; if True, interactive console will start upon
error
RETURNS:
result of calling fn
RAISES:
TestError if error occurs.
"""
assert isinstance(console, InteractiveConsole), 'Missing interactive console'
try:
result = timed(fn, args, kargs)
except RuntimeError:
print('# Error: maximum recursion depth exceeded')
if interactive:
interact(console)
print()
raise TestError()
except TimeoutError as e:
print('# Error: evaluation exceeded {} seconds'.format(e.timeout))
if interactive:
interact(console)
print()
raise TestError()
except Exception as e:
if type(expect) == type and issubclass(expect, BaseException) and isinstance(e, expect):
return expect
stacktrace = traceback.format_exc()
token = '<module>\n'
index = stacktrace.rfind(token) + len(token)
print('Traceback (most recent call last):')
print(stacktrace[index:])
print('# Error: expected', repr(expect), "got", e.__class__.__name__)
if interactive:
interact(console)
print()
raise TestError()
else:
return result
def interact(console):
"""Starts an interactive console.
PARAMTERS:
console -- InteractiveConsole
"""
console.resetbuffer()
console.interact('# Interactive console\n'
'# Type exit() to quit')
#######################
# UNLOCKING MECHANISM #
#######################
def run_preamble(preamble, frame):
"""Displays the specified preamble.
PARAMETERS:
preamble -- str
frame -- dict; global frame
"""
console = InteractiveConsole(frame)
incomplete = False
for line in split(preamble):
if not incomplete and not line:
incomplete = False
continue
prompt = PS2 if incomplete else PS1
display_prompt(line, prompt)
incomplete = console.push(line)
def unlock(question, tests):
"""Unlocks a question, given locked_tests and unlocked_tests.
PARAMETERS:
question -- str; the name of the test
tests -- module; contains a list of locked tests
DESCRIPTION:
This function incrementally unlocks all cases in a specified
question. Students must answer in the order that test cases are
written. Once a test case is unlocked, it will remain unlocked.
Persistant state is stored by rewriting the contents of
tests.pkl. Students should NOT manually change these files.
"""
hash_key = tests['project_info']['hash_key']
imports = tests['project_info']['imports']
test = get_test(tests['tests'], question)
if test is None:
print('Tests do not exist for "{}"'.format(question))
print('Try one of the following:')
print(map(get_name, tests['tests']))
return
name = get_name(test)
prompt = '?'
underline('Unlocking tests for {}'.format(name))
print('At each "{}", type in what you would expect the output to be if you had implemented {}'.format(prompt, name))
print('Type exit() to quit')
print()
global_frame = {}
for line in imports:
exec(line, global_frame)
has_preamble = 'preamble' in test
if has_preamble and 'all' in test['preamble']:
run_preamble(test['preamble']['all'], global_frame)
def hash_fn(x):
return hmac.new(hash_key.encode('utf-8'),
x.encode('utf-8')).digest()
if 'suites' not in test:
return
for suite_num, suite in enumerate(test['suites']):
if not suite:
continue
if has_preamble and suite_num in test['preamble']:
run_preamble(test['preamble'][suite_num],
global_frame.copy())
for case in suite:
if case[2] == 'unlocked':
continue
lines = split(case[0])
answers = []
outputs = case[1]
if type(outputs) not in (list, tuple):
outputs = [outputs]
output_num = 0
for line in lines:
if len(lines) > 1 and not line.startswith('$'):
if line.startswith(' '): # indented
print(PS2 + line)
else:
print(PS1 + line)
continue
line = line.replace('$ ', '')
print(PS1 + line)
correct = False
while not correct:
try:
student_input = input(prompt + ' ')
except (KeyboardInterrupt, EOFError):
try:
print('\nExiting unlocker...')
# When you use Ctrl+C in Windows, it throws
# two exceptions, so you need to catch both of
# them.... aka Windows is terrible.
except (KeyboardInterrupt, EOFError):
pass
return
if student_input in ('exit()', 'quit()'):
print('\nExiting unlocker...')
return
correct = hash_fn(student_input) == outputs[output_num]
if not correct:
print("Not quite...try again!")
answers.append(student_input)
output_num += 1
case[1] = answers
case[2] = 'unlocked'
print("Congratulations, you have unlocked this case!")
print()
print("You have unlocked all of the tests for this question!")
#########################
# AUTO-UPDATE MECHANISM #
#########################
def check_for_updates(tests):
"""Checks a remote url for changes to the project tests, and
applies changes depending on user input.
PARAMETERS:
tests -- contents of tests.pkl
RETURNS:
bool; True if new changes were made, False if no changes made or
error occurred.
"""
version = tests['project_info']['version']
remote = tests['project_info']['remote']
print('You are running version', version, 'of the autograder')
try:
url = os.path.join(remote, 'CHANGES')
data = timed(urllib.request.urlopen, (url,), timeout=5)
changelog = data.read().decode('utf-8')
except (urllib.error.URLError, urllib.error.HTTPError):
print("Couldn't check remote autograder")
return False
except TimeoutError:
print("Checking for updates timed out.")
return False
match = re.match('VERSION ([0-9.]+)', changelog)
if match and match.group(1) != version:
print('Version', match.group(1), 'is available.')
prompt = input('Do you want to automatically download changes? [y/n]: ')
if 'y' in prompt.lower():
success = parse_changelog(tests, changelog, remote)
return success
else:
print('Changes not made.')
return False
def parse_changelog(tests, changelog, remote):
"""Parses a changelog and updates the tests with the specified
changes.
PARAMTERS:
tests -- dict; contents tests.pkl
changelog -- str
remote -- str; url of remote files
RETURNS:
bool; True if updates successful
"""
current_version = tests['project_info']['version']
parts = changelog.partition('VERSION ' + current_version)
if len(parts) == 3: # If Version found
changelog = parts[0]
changes = re.split('VERSION ([0-9.]+)', changelog)
changes = list(reversed(changes)) # most recent changes last
changes.pop() # split will find empty string before first version
assert len(changes) % 2 == 0
num_changes = len(changes) // 2
for i in range(num_changes):
version = changes[2*i+1]
change_header, change_contents = '', ''
for line in changes[2*i].strip('\n').split('\n'):
if line.startswith(' ') or line == '':
change_contents += line[4:] + '\n'
continue
if change_header != '':
try:
apply_change(change_header, change_contents, tests,
remote)
except AssertionError as e:
print("Update error:", e)
return False
change_header = line
change_contents = ''
# Apply last change
try:
terminate = apply_change(change_header, change_contents, tests, remote)
except AssertionError as e:
print("Update error:", e)
return False
if terminate:
break
tests['project_info']['version'] = version
toggle_output(True)
tests['project_info']['version'] = version
print("Updated to VERSION " + version)
if version != changes[-1]:
print("Please re-run the autograder to check for further "
"updates")
else:
print("Applied changelog:\n")
print(changelog)
toggle_output(False)
return True
CHANGE = 'CHANGE'
APPEND = 'APPEND'
REMOVE = 'REMOVE'
GRADER = 'GRADER'
def apply_change(header, contents, tests, remote):
"""Subroutine that applies the changes described by the header
and contents.
PARAMTERS:
header -- str; a line describing the type of change
contents -- str; contents of change, if applicable
tests -- dict; contents of tests.pkl
RAISES:
AssertionError; if any invalid changes are attempted.
RETURNS:
bool; True if GRADER was updated, in which case update should
exit immediately
"""
error_msg = 'invalid change "{}"'.format(header)
if header.strip() == GRADER:
try:
url = os.path.join(remote, 'autograder.py')
data = timed(urllib.request.urlopen, (url,), timeout=5)
new_autograder = data.read().decode('utf-8')
except (urllib.error.URLError, urllib.error.HTTPError):
raise AssertionError("Couldn't retrive remote update for autograder.py")
except TimeoutError:
raise AssertionError("Checking for updates timed out.")
with open('autograder.py', 'w') as f:
f.write(new_autograder)
return True
header = header.split('::')
assert len(header) == 3, error_msg
change_type = header[0].strip()
if 'test' in header[1]:
test_name = header[1].replace('test', '').strip()
test = get_test(tests['tests'], test_name)
target = "test"
else:
target = "tests['" + header[1].strip() + "']"
target += header[2].strip()
assert target is not None, 'Invalid test to update: {}'.format(test_name)
if change_type == CHANGE:
update = "{} = {}".format(target, contents)
elif change_type == APPEND:
update = "{}.append({})".format(target, contents)
elif change_type == REMOVE:
assert contents == '', "Tried " + REMOVE + " with nonempty contents: " + contents
update = "del {}".format(target)
else:
raise(error_msg)
try:
exec(update)
except Exception as e:
raise AssertionError(e.__class__.__name__ + ": " + str(e) + ": " + update)
##########################
# COMMAND-LINE INTERFACE #
##########################
def run_all_tests():
"""Runs a command line interface for the autograder."""
parser = argparse.ArgumentParser(description='CS61A autograder')
parser.add_argument('-u', '--unlock', type=str,
help='Unlocks the specified question')
parser.add_argument('-q', '--question', type=str,
help='Run tests for the specified question')
parser.add_argument('-a', '--all', action='store_true',
help='Runs all tests, regardless of failures')
parser.add_argument('-i', '--interactive', action='store_true',
help='Enables interactive mode upon failure')
parser.add_argument('-t', '--timeout', type=int,
help='Change timeout length')
args = parser.parse_args()
with open('tests.pkl', 'rb') as f:
all_tests = pickle.load(f)
new = check_for_updates(all_tests)
if new:
with open('tests.pkl', 'wb') as f:
pickle.dump(all_tests, f, pickle.DEFAULT_PROTOCOL)
exit(0)
print()
if args.unlock:
unlock(args.unlock, all_tests)
with open('tests.pkl', 'wb') as f:
pickle.dump(all_tests, f, pickle.DEFAULT_PROTOCOL)
else:
if args.question:
tests = get_test(all_tests['tests'], args.question)
if not tests:
print('Test {} does not exist'.format(args.question))
exit(1)
tests = [tests]
else:
tests = all_tests['tests']
if args.timeout:
global TIMEOUT
TIMEOUT = args.timeout
global_frame = {}
for line in all_tests['project_info']['imports']:
exec(line, global_frame)
exec(split(all_tests.get('cache', ''), join=True), global_frame)
for test in tests:
passed = run(test, global_frame, args.interactive, all_tests['preamble'])
if not args.all and not passed:
return
underline('Note:', under='-')
print("""Remember that the tests in this autograder are not exhaustive, so try your own tests in the interpreter!""")
if __name__ == '__main__':
run_all_tests()