forked from DavidLeoni/softpython-it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexam.py
executable file
·322 lines (253 loc) · 11 KB
/
exam.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
#!/usr/bin/python3
# David Leoni Sept 2017
# This script allows initialization and management of exams.
import conf
import sys
import os
import shutil
import datetime
import glob
import re
from arghandler import *
def fatal(msg, ex=None):
if ex == None:
exMsg = ""
else:
exMsg = " \n " + repr(ex)
info("\n\n ERROR! " + str(msg) + exMsg + "\n\n")
exit(1)
def info(msg=""):
print(" " + str(msg))
def warn(msg):
print("\n\n WARNING: " + str(msg))
def expand_JM(source, target, exam_date):
d = conf.parse_date(exam_date)
sourcef = open(source, "r")
s = sourcef.read()
s = s.replace('_JM_(exam.date)', exam_date )
s = s.replace('_JM_(exam.date_human)', d.strftime('%A %d, %B %Y') )
for k in conf.__dict__:
s = s.replace('_JM_(conf.' + k + ')', str(conf.__dict__[k]))
p = re.compile('_JM_\([a-zA-Z][\w\.]*\)')
if p.search(s):
warn("FOUND _JM_ macros which couldn't be expanded!")
print(" file: " + source)
print("\n ".join(p.findall(s)))
print("")
destf = open(target, 'w')
destf.write(s)
cur_dir_names = os.listdir('.')
if 'exam.py' not in cur_dir_names:
fatal('You must execute exam.py from within the directory it is contained!')
def arg_date(parser, args):
parser.add_argument('date', help="date in format 'yyyy-mm-dd'" )
return conf.parse_date_str(vars(parser.parse_args(args))['date'])
@subcmd(help="Initializes a new exam")
def init(parser,context,args):
ld = arg_date(parser, args)
eld = "private/" + ld
pubeld = "past-exams/" + ld
exam_ipynb = eld + '/exam-' + ld + '.ipynb'
if os.path.exists(eld):
fatal("PRIVATE EXAM ALREADY EXISTS: " + eld)
if os.path.exists(pubeld):
fatal("PUBLIC EXAM ALREADY EXISTS: " + pubeld)
if os.path.exists(exam_ipynb):
fatal("PUBLIC EXAM ALREADY EXISTS: " + exam_ipynb)
shutil.copytree("jm-templates/exam", eld)
expand_JM('jm-templates/exam/exam-yyyy-mm-dd.ipynb', exam_ipynb, ld)
os.rename(eld + "/" + "jupman-yyyy-mm-dd-grades.ods", eld + "/" + conf.filename + "-" + ld + "-grades.ods")
info("Following material is now ready to edit: ")
print("")
info(' Python exercises and tests : ' + eld + "/exercises")
info(' Python solutions : ' + eld + "/solutions" )
info(' Exam notebook : ' + exam_ipynb)
@subcmd(help='Zips a builded exam, making it ready for deploy on the exam server')
def package(parser,context,args):
ld = arg_date(parser, args)
eld = "private/" + ld
source_student = eld + "/exercises"
target_student = eld + "/server/" + conf.filename + "-" + ld + "/FIRSTNAME-LASTNAME-ID"
target_student_zip = eld +"/server/" + conf.filename + "-" + ld
built_site_dir = "_build/"
if not os.path.exists(built_site_dir):
fatal(built_site_dir + " WAS NOT BUILT !")
if not os.path.exists(source_student):
fatal("MISSING SOURCE STUDENT EXERCISES: " + source_student)
if os.path.exists(target_student):
fatal("TARGET STUDENT EXERCISES DIRECTORY ALREADY EXISTS: " + target_student)
try:
dir_names = os.listdir(built_site_dir)
except Exception as e:
fatal("ERROR WITH DIR " + built_site_dir, ex=e)
if len(dir_names) == 0:
fatal("SITE DIRECTORY AT " + built_site_dir + " WAS NOT BUILT !")
server_jupman = eld + "/server/" + conf.filename
if os.path.exists(server_jupman):
info("Cleaning " + server_jupman + " ...")
delete_tree(server_jupman, "server/" + conf.filename)
info("Copying built website ...")
shutil.copytree(built_site_dir, server_jupman)
info("Copying exercises to " + str(target_student))
shutil.copytree(source_student, target_student)
info("Creating student exercises zip: " + target_student_zip + ".zip" )
shutil.make_archive(target_student_zip, 'zip', target_student_zip)
target_server_zip = eld +"/" + conf.filename + "-" + ld + "-server" # without '.zip'
info("Creating server zip: " + target_server_zip + ".zip")
shutil.make_archive(target_server_zip, 'zip', eld + "/server")
print("")
info("You can now browse the website at: " + os.path.abspath(eld + "/server/" + conf.filename + "/html/index.html"))
print("")
@subcmd(help='Set up grading for the provided exam')
def grade(parser,context,args):
ld = arg_date(parser, args)
eld = "private/" + ld
shipped = eld + "/shipped"
graded = eld + "/graded"
if not os.path.exists(shipped):
fatal("Couldn't find directory: " + shipped)
try:
dir_names = next(os.walk(shipped))[1]
except Exception as e:
info("\n\n ERROR! " + repr(e) + "\n\n")
exit(1)
if len(dir_names) == 0:
fatal("NOTHING TO GRADE IN " + shipped)
for dn in dir_names:
target = graded + "/" + dn
if (os.path.exists(target + "/shipped")):
fatal("DIRECTORY ALREADY EXISTS: " + target + "/shipped\n\n")
if (os.path.exists(target + "/corrected")):
fatal("DIRECTORY ALREADY EXISTS: " + target + "/corrected\n\n")
info("Copying Python files to execute and eventually correct in " + target + "/corrected")
shutil.copytree(eld + "/shipped/" + dn , target + "/shipped")
info("Copying original shipped files (don't touch them!) in " + target + "/shipped")
shutil.copytree(eld + "/shipped/" + dn , target + "/corrected")
@subcmd('zip-grades', help='Creates a separate zip for each student containing his graded sheet and code')
def zip_grades(parser,context,args):
ld = arg_date(parser, args)
eld = "private/" + ld
shipped = eld + "/shipped"
graded = eld + "/graded"
try:
dir_names = next(os.walk(shipped))[1]
except Exception as e:
info("\n\n ERROR! " + repr(e) + "\n\n")
exit(1)
if len(dir_names) == 0:
info("\n\n ERROR! NOTHING TO ZIP!\n\n")
for dn in dir_names:
target = eld + "/graded/" + dn
shutil.make_archive(target, 'zip', target)
print("")
info("You can now find zips to send to students in " + eld + "/graded")
print("")
@subcmd('publish', help='Copies exam python files from private/ to exam/ (both exercises and solutions), and zips them')
def publish(parser,context,args):
ld = arg_date(parser, args)
source = "private/" + ld + "/"
source_exercises = source + 'exercises'
source_solutions = source + 'solutions'
source_ipynb = source + 'exam-' + ld + '.ipynb'
if not os.path.isdir(source):
fatal("SOURCE PRIVATE EXAM FOLDER " + source + " DOES NOT EXISTS !")
if not os.path.isdir(source_exercises):
fatal("SOURCE PRIVATE EXAM FOLDER " + source_exercises + " DOES NOT EXISTS !")
if not os.path.isdir(source_solutions):
fatal("SOURCE PRIVATE EXAM FOLDER " + source_solutions + " DOES NOT EXISTS !")
dest = "past-exams/" + ld + "/"
dest_zip = "past-exams/" + ld
dest_exercises = dest + "exercises"
dest_solutions = dest + "solutions"
dest_ipynb = dest + 'exam-' + ld + '.ipynb'
if os.path.exists(dest):
fatal("TARGET PUBLIC EXAM FOLDER " + dest + " ALREADY EXISTS !")
if os.path.exists(dest_zip):
fatal("TARGET PUBLIC EXAM ZIP " + dest_zip + ".zip ALREADY EXISTS !")
if os.path.exists(dest_exercises):
fatal("TARGET PUBLIC EXAM FOLDER " + dest_exercises + " ALREADY EXISTS !")
if os.path.exists(dest_solutions):
fatal("TARGET PUBLIC EXAM FOLDER " + dest_solutions + " ALREADY EXISTS !")
info("Copying exercises to " + str(dest_exercises))
shutil.copytree(source_exercises, dest_exercises)
info("Copying solutions to " + str(dest_solutions))
shutil.copytree(source_solutions, dest_solutions)
info('Copying notebook to ' + str(dest_ipynb))
shutil.copy(source_ipynb, dest_ipynb)
info("Creating zip " + dest_zip + '.zip')
shutil.make_archive(dest_zip, 'zip', dest)
info()
info("Exam python files copied.")
info()
info("You can now manually run the following git instructions to publish the exam,")
info("ReadTheDocs will automatically build the website.")
info()
info(" git status # just to check everything is ok")
info(" git add .")
info(" git commit -m 'published " + ld + " exam'")
info(" git push")
info()
def check_paths(path, path_check):
if not isinstance(path, str):
raise Exception("Path to delete must be a string! Found instead: " + str(type(path)))
if len(path.strip()) == 0:
raise Exception("Provided an empty path !")
if not isinstance(path_check, str):
raise Exception("Path check to delete must be a string! Found instead: " + str(type(path_check)))
if len(path_check.strip()) == 0:
raise Exception("Provided an empty path check!")
def delete_file(path, path_check):
""" Deletes a file, checking you are deleting what you really want
path: the path to delete as a string
path_check: the end of the path to delete, as a string
"""
check_paths(path, path_check)
if path.endswith(path_check):
os.remove(path)
else:
fatal("FAILED SAFETY CHECK FOR DELETING DIRECTORY " + path + " ! \n REASON: PATH DOES NOT END IN " + path_check)
def delete_tree(path, path_check):
""" Deletes a directory, checking you are deleting what you really want
path: the path to delete as a string
path_check: the end of the path to delete, as a string
"""
check_paths(path, path_check)
if path.endswith(path_check):
shutil.rmtree(path)
else:
fatal("FAILED SAFETY CHECK FOR DELETING DIRECTORY " + path + " ! \n REASON: PATH DOES NOT END IN " + path_check)
@subcmd('delete', help="Deletes an existing exam")
def delete_exam(parser,context,args):
ld = arg_date(parser, args)
eld = "private/" + ld
pubeld = "past-exams/" + ld
pubeldzip = pubeld + ".zip"
deleted = []
ans = ''
while ans != 'Y' and ans != 'n':
print ("DO YOU *REALLY* WANT TO DELETE EXAM " + ld + " (NOTE: CANNOT BE UNDONE) [Y/n]? "),
ans = input()
if ans != 'Y':
print("")
info("User cancelled, no data was deleted.")
return
print("")
if os.path.exists(eld):
info("Deleting " + eld + " ...")
delete_tree(eld, "private/" + ld)
deleted.append(eld)
if os.path.exists(pubeld):
info("Deleting " + pubeld + " ...")
delete_tree(pubeld, "past-exams/" + ld)
deleted.append(pubeld)
if os.path.exists(pubeldzip):
info("Deleting " + pubeldzip + " ...")
delete_file(pubeldzip, "past-exams/" + ld + ".zip")
deleted.append(pubeldzip)
if len(deleted) == 0:
fatal("COULDN'T FIND ANY EXAM FILE TO DELETE FOR DATE: " + ld)
handler = ArgumentHandler(description='Manages ' + conf.filename + ' exams.',
use_subcommand_help=True)
handler.run()
print("")
info("DONE.\n")