forked from abreen/socrates.py
/
grader.py
178 lines (128 loc) · 5.2 KB
/
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
177
178
import sys
import os
import io
import datetime
from util import *
from prompt import prompt
def grade(criteria, submissions, filename):
found = []
num_missing = 0
total = criteria.total_points
for f in criteria.files:
crit_dir, crit_name = os.path.split(f.path)
for s in submissions:
sub_dir, sub_name = os.path.split(s)
if crit_name == sub_name:
found.append(f)
break
else:
# in the case that the file is missing, we should prompt
# the grader to pick the incorrect file name or declare
# the file missing
sprint(COLOR_YELLOW + "could not find "
"file '{}'".format(f.path) + COLOR_RESET)
if len(submissions) < 1:
continue
# find the submission directory (it could be the
# current working directory, but maybe not)
submission_dir, _ = os.path.split(submissions[0])
if not submission_dir:
submission_dir = os.path.abspath(os.curdir)
choices = [f for f in os.listdir(submission_dir)
if os.path.isfile(os.path.join(submission_dir, f))]
choices.append("skip grading this submission now")
choices.append("mark the file as missing")
sprint("this student may have named the file incorrectly...")
# we prompt the grader for zero or one choice
got = prompt(choices, '1')
got = got[0]
if got == len(choices) - 1:
# declare the file missing
num_missing += 1
continue
elif got == len(choices) - 2:
sprint("skipping this submission; no grade file written")
sys.exit(EXIT_WITH_DEFER)
else:
# get absolute path to the old and new files
sname = choices[got]
opath = os.path.join(submission_dir, sname)
npath = os.path.join(submission_dir, crit_name)
try:
os.rename(opath, npath)
except:
import traceback
sprint("error renaming incorrectly named file", error=True)
traceback.print_exc()
sys.exit(ERR_GRADING_MISC)
found.append(f)
out = io.StringIO()
try:
for f in criteria.files:
out.write(heading("{} [{} points]".format(f, f.point_value),
level=2))
if f not in found:
total -= f.point_value
out.write("-{}\tnot submitted\n".format(f.point_value))
continue
sprint("running tests for " + str(f))
points_taken = 0
points_taken += _write_results(out, f.run_tests())
file_stat = os.stat(f.path)
mtime = datetime.datetime.fromtimestamp(file_stat.st_mtime)
if mtime > criteria.due:
if mtime > criteria.due + datetime.timedelta(hours=24):
sprint(COLOR_YELLOW + "taking all points for >24-hour "
"late-submitted {}".format(f) + COLOR_RESET)
late_penalty = f.point_value
out.write("-{}\tsubmitted >24 hours "
"late\n".format(late_penalty))
else:
sprint(COLOR_YELLOW + "taking 10% penalty for "
"late-submitted {}".format(f) + COLOR_RESET)
late_penalty = f.point_value * 0.10
out.write("-{}\tsubmitted late\n".format(late_penalty))
points_taken += late_penalty
total -= min(f.point_value, points_taken)
out.write("\n")
out.write("\nTotal: {}\n".format(total))
except KeyboardInterrupt:
sprint("\nstopping (received interrupt)")
out.close()
sys.exit(ERR_INTERRUPTED)
except:
import traceback
err = sys.exc_info()
sprint("encountered an error while "
"grading: {}".format(err[0].__name__), error=True)
traceback.print_exc()
sprint("you might have to grade the old-fashioned "
"way (sorry)")
out.close()
sys.exit(ERR_GRADING_MISC)
with open(filename, 'w') as f:
out.seek(0)
f.write(out.read())
sprint("grading completed")
return num_missing
def _write_results(f, results, indent='\t'):
if not results:
return 0
total = 0
for r in results:
desc = r['description']
if 'deduction' in r:
ded = r['deduction']
total += ded
f.write("-{}{}{}\n".format(ded, indent, desc))
else:
f.write("{}{}\n".format(indent, desc))
if 'notes' in r and r['notes']:
for n in r['notes']:
f.write(indent + '\t' + n + '\n')
if 'subresults' in r and r['subresults']:
for subr in r['subresults']:
total += _write_results(f, [subr], '\t' + indent)
return total
if __name__ == '__main__':
print("To start a grading session, run the 'socrates' module.")