Repository navigation
Expand file tree
/
Copy pathextract_grades.py
More file actions
172 lines (139 loc) · 6.28 KB
/
Copy pathextract_grades.py
File metadata and controls
172 lines (139 loc) · 6.28 KB
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
import os, sys, json, copy, math, pytz
from datetime import datetime, timezone, timedelta
from collections import defaultdict as ddict
with open("graderconfig.json") as f:
CONF = json.load(f)
COURSE = "b"
not_reviewed = {}
CR_URL = f'https://tyler.caraza-harter.com/cs320/{CONF["SEMESTER"]}/code_review.html?project_id=%s&student_email=%s@wisc.edu'
# https://stackoverflow.com/questions/4563272/convert-a-python-utc-datetime-to-a-local-datetime-using-only-python-standard-lib
def utc_to_local(utc_dt):
return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)
def try_read_json(path, default={}):
try:
with open(path) as f:
return json.load(f)
except Exception as e:
return copy.deepcopy(default)
class Grades:
def __init__(self, project):
self.project = project
if not os.path.exists("snapshot"):
raise Exception(f"need to run 'python3 s3interface.py -dp --prefix \"{COURSE}\"' first")
with open(f"snapshot/{COURSE}/config.json") as f:
deadlines = json.load(f)["deadlines"]
# end of day + 1 hour slack (add 25 hours)
deadline = datetime.strptime(deadlines[project], "%m/%d/%y")
deadline = pytz.timezone("US/Central").localize(deadline).astimezone(tz=timezone.utc)
self.deadline = deadline + timedelta(hours=25)
with open(f"snapshot/{COURSE}/roster.json") as f:
net_ids = {row['net_id'] for row in json.load(f) if row['enrolled']}
self.extensions = self.get_extensions()
self.grade_rows = [self.get_student_grade(nid) for nid in net_ids]
# returns dict
# key: submission file path
# val: days extended
def get_extensions(self):
extensions = ddict(int)
dirname = 'snapshot/%s/extensions/%s/' % (COURSE, self.project)
if not os.path.exists(dirname):
return extensions
for name in os.listdir(dirname):
if not name.endswith(".json"):
continue
path = os.path.join(dirname, name)
with open(path) as f:
days = json.load(f)["days"]
net_id = name.split("*at*")[0]
links = self.get_proj_links(net_id)
for link in links:
with open(link) as f:
sub = json.load(f)['symlink']
extensions[sub] = max(extensions[sub], days)
return extensions
def dump(self, path):
with open(path, "w") as f:
f.write('[\n')
for i, row in enumerate(self.grade_rows):
json.dump(row, f)
if i < len(self.grade_rows) - 1:
f.write(",")
f.write("\n")
f.write(']\n')
def get_proj_links(self, net_id):
dirname = 'snapshot/%s/projects/%s/%s*at*wisc.edu' % (COURSE, self.project, net_id)
link = None
if os.path.exists(dirname):
return [os.path.join(dirname, p) for p in os.listdir(dirname) if p.endswith('-link.json')]
return []
def get_student_grade(self, net_id):
bonus_days = 0
grade = None
links = sorted(self.get_proj_links(net_id))
for i, link in enumerate(links):
with open(link) as f:
submission_dir = json.load(f)['symlink']
grade2 = self.get_submission_grade(net_id, submission_dir)
grade2["latest_submission"] = (i == len(links) - 1)
# only take a later submission over a prior one if the
# score is better, because the earlier one will incur
# fewer late days
if grade == None or grade2["score"] > grade["score"]:
grade = grade2
# if they submit on time and get 50%, they get 3 extra days
if grade2["test_score"] >= 50 and grade2["late_days"] <= 0:
bonus_days = 3
else:
submission_dir = None
if grade == None:
grade = self.get_submission_grade(net_id, None)
grade["bonus_days"] = bonus_days
return grade
def get_submission_grade(self, net_id, submission_dir):
# we need to check four files:
# - tests
# - CR
# - submission (to determine lateness)
# - extensions (to adjust lateness)
late_days = 0
submitted = ""
test_score = 0
ready = False
if submission_dir:
sub = try_read_json(os.path.join("snapshot", COURSE, submission_dir, "submission.json"), {})
test = try_read_json(os.path.join("snapshot", COURSE, submission_dir, "test.json"), {})
test_score = test.get("score", 0)
cr = try_read_json(os.path.join("snapshot", COURSE, submission_dir, "cr.json"), {})
if sub != {}:
submitted = datetime.strptime(sub["submit_time_utc"], "%Y-%m-%d %H:%M:%S")
submitted = utc_to_local(submitted)
late_days = math.ceil((submitted - self.deadline) / timedelta(days=1))
late_days -= self.extensions[submission_dir]
late_days = max(late_days, 0)
submitted = datetime.strftime(submitted, "%Y-%m-%d %H:%M:%S")
if sub != {} and test != {} and (cr != {} or self.project in not_reviewed):
test_score = test.get("score", 0)
ta_deduction = cr.get("points_deducted", 0)
comment_count = 0
for comments in cr.get("highlights", {}).values():
comment_count += len(comments)
ready = True
if not ready:
ta_deduction = 0
comment_count = 0
score = 0
else:
score = max(test_score - ta_deduction, 0)
cr_url = CR_URL % (self.project, net_id)
return {"project": self.project, "net_id": net_id, "test_score": test_score,
"ta_deduction": ta_deduction, "score": score, "submitted": submitted, "late_days": late_days,
"comment_count": comment_count, "ready": ready, "code_review_url": cr_url}
def main():
if len(sys.argv) < 2:
print("Usage: python3 extract_grades.py p1 [p2 ...]")
if not os.path.exists("grades"):
os.mkdir("grades")
for project in sys.argv[1:]:
Grades(project).dump(os.path.join('grades', project+'.json'))
if __name__ == '__main__':
main()