-
Notifications
You must be signed in to change notification settings - Fork 1
/
batchmark.py
executable file
·474 lines (418 loc) · 18.5 KB
/
batchmark.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
#!/usr/bin/env python2
# vim: et:ts=4:textwidth=80
# Automark
#
# David Llewellyn-Jones
# Liverpool John Moores University
# 18/12/2014
# Released under the GPL v.3. See the LICENSE file for more details.
"""
Check multiple programs against task requirements
This script allows multiple programs to be checked either locally or
using the ideone api.
David Llewellyn-Jones, Posco Tso
Liverpool John Moores University
18/12/2014
Released under the GPL v.3. See the LICENSE file for more details.
"""
import os
import automark
import automarktask1
import automarktask2
import automarktask3
import automarktask4
from argparse import ArgumentParser
from docx import Document
from time import time
from xlrd import open_workbook
from zipfile import ZipFile
__all__ = ('BatchMark')
# Task number
# Folder containing students' folders
# Marker's initials (optional)
# Temp build folder (optional)
# Feedback template (optional)
# Student details list (optional)
# Summary output (optional)
class BatchMark(object):
"""
Mark multiple code submissions and generate feedback sheets.
Initialise with the various attributes outlined below. Then call the
go() method to start the batch marking process.
Attributes:
task: Task number
marking_dir: Folder containing students' folders
marker_name: Marker's initials (optional)
build_dir: Temp build folder (optional)
feedback_doc_name: Feedback template (optional)
marking_sheet_name: Student details list (optional)
summary-out: Summary output (optional)
"""
def __init__(
self, task, marking_dir, marker_name, build_dir,
feedback_doc_name, marking_sheet_name, summary_out):
"""
Initialise the BatchMark class with the attributes needed for it
to run, and print out those attributes.
"""
print ('Task: ' + str(task))
print ("Folder to check in: " + marking_dir)
print ('Marker name: ' + marker_name)
print ("Build output folder: " + build_dir)
print ("Feedback template: " + feedback_doc_name)
print ("Marking sheet: " + marking_sheet_name)
print ("Summary output: " + summary_out)
self._task = task
self._marking_dir = marking_dir
self._marker_name = marker_name
self._buid_dir = build_dir
self._feedback_doc_name = feedback_doc_name
self._marking_sheet_name = marking_sheet_name
self._summary_out = summary_out
# Select the appropriate task
tasks = [automark, automarktask1, automarktask2, automarktask3,
automarktask4]
if (self._task < len(tasks)) and (self._task >= 0):
self._task_specific = tasks[self._task]
else:
self._task_specific = tasks[0]
print 'Task number out of bounds. Applying general checks.'
#load student feedback form as a template
self._feedback_document = Document(feedback_doc_name)
#load my marking sheet 'PT' from workbook
self._marking_sheet = open_workbook(
marking_sheet_name).sheet_by_name(marker_name)
#do things
def go(self):
"""
Start the marking process based on the initialisation parameters.
"""
#username to firstname lastname map/dictionary
self._name_map = {}
self._construct_name_map()
# Output the headers to the summary output csv file
with open(self._summary_out, 'w') as self._summary:
self._output_csv_co(
['Username', 'Name'])
self._output_csv_co(
self._task_specific.Automark.get_scores_structure())
self._output_csv_co(
self._task_specific.Automark.get_internal_stats_structure())
self._output_csv_nl(
self._task_specific.Automark.get_output_checks_structure())
self._create_new_feedback_document()
def _unzip_submission(self, student_dir):
"""
Internal method, cycle through the folders in the given path and
extract the contents of any zip archives found.
"""
archive_file = ''
for check_dir, _, file in os.walk(student_dir):
# Ensure we don't search through metadata folders
if '__MACOSX' not in check_dir.split('/'):
# This is a folder to check
for file_name in file:
if file_name.endswith('.zip'):
# This is a zip archive, so record it
archive_file = os.path.join(check_dir, file_name)
if archive_file != '':
with ZipFile(archive_file ,'r') as archive:
# Extract everything from the archive
archive.extractall(student_dir)
archive.close()
else:
print ('No zip archive')
def _output_csv(self, items):
"""
Internal method, output the item list to a csv file. Items are
separated by commas, but there's no comma at the start or end.
"""
count = 0
for item in items:
self._summary.write('\"')
self._summary.write(str(item).replace('"', "'"))
self._summary.write('\"')
if count < (len(items) - 1):
# Don't put a comma after the last item
self._summary.write(', ')
count += 1
def _output_csv_co(self, items):
"""
Internal method, output the item list to a csv file. Items are
separated by commas and a comma is acced to the end for
continuation.
"""
self._output_csv(items)
self._summary.write(', ')
def _output_csv_nl(self, items):
"""
Internal method, output the item list to a csv file. Items are
separated by commas and a newline is output at the end.
"""
self._output_csv(items)
self._summary.write('\n')
def _mark (self, directory):
"""
Internal method, return the last java file found in the
directory.
"""
java_file = ''
for check_dir, _, file in os.walk(directory):
# Ensure we don't search through metadata folders
if '__MACOSX' not in check_dir.split('/'):
# This is a folder to check
for file_name in file:
if file_name.endswith('.java'):
# This is a Java file, so record it
java_file = os.path.join(check_dir, file_name)
return java_file
def _create_new_feedback_document(self):
"""
Internal method, cycle through the folders, unzip any archives,
compile and execute the java programs, test the results, output
a completed feedback file, write out the results to the csv
summary file.
"""
print
# Cycle through each of the student directories
for student_dir, _, file in os.walk(self._marking_dir):
student_dir_name = os.path.relpath(student_dir, self._marking_dir)
# Other than the base folder, check the folder name against
# the list of usernames in the Excel sheet
if (student_dir_name is not '.') and (
student_dir_name in self._name_map):
print 'Student: {}'.format(student_dir_name)
student_name = self._name_map[student_dir_name][0] \
+ ' ' + self._name_map[student_dir_name][1]
self._unzip_submission(student_dir)
java_path = self._mark(student_dir)
if java_path == '':
print 'No java file'
self._write_student_name_to_document(
student_dir, student_dir_name, student_name)
else:
#print 'file: {}'.format(java_path)
# Actually perform the automarking process
marks = self._task_specific.Automark(
java_path, 'credentials.txt', self._buid_dir)
# Output the results to the summary csv file
self._write_details_to_document(
student_dir, student_dir_name, student_name, marks)
self._write_comments_to_document(
student_dir, student_dir_name, marks)
self._output_csv_co(
[student_dir_name, student_name])
self._output_csv_co(
marks.get_scores())
self._output_csv_co(
marks.get_internal_stats())
self._output_csv_nl(
marks.get_output_checks())
def _write_details_to_document(
self, student_dir, student_dir_name, student_name, marks):
"""
Internal method, write out student details and the results of the
marking to a feedback document.
"""
#default cell for student's firstname lastname
filename = self._feedback_doc_name.replace(
'username', student_dir_name)
self._feedback_document.paragraphs[0].text = \
self._feedback_document.paragraphs[0].text.replace(
'<task>', str(self._task))
self._feedback_document.tables[0].cell(1,0).text = student_name
self._feedback_document.tables[0].cell(1,1).text = student_dir_name
# There is no marker now!
#self._feedback_document.tables[0].cell(1,2).text = self._marker_name
self._feedback_document.tables[0].cell(1,2).text = 'auto'
#print student_dir+'/'+filename
# Clear tables
for row in range(1,16):
self._feedback_document.tables[2].cell(row, 2).text = ''
# Get the marks to provide as feedback
execution_score = marks.get_execution_score()
indentation_score = marks.get_indentation_score()
variables_score = marks.get_variables_score()
comment_score = marks.get_comment_score()
total_score = marks.get_total_score()
# Split the execution score into requirements and efficiency
efficient_score = 0
if execution_score > 4:
efficient_score = 1
execution_score -= 1
# Output the info to the marks table in the feedback sheet
self._feedback_document.tables[2].cell(
(2 + int(execution_score)), 2).text = '{:g}'.format(
execution_score)
self._feedback_document.tables[2].cell(
9, 2).text = str(indentation_score)
self._feedback_document.tables[2].cell(
10, 2).text = str(variables_score)
self._feedback_document.tables[2].cell(
11, 2).text = str(efficient_score)
self._feedback_document.tables[2].cell(
13 + int(comment_score), 2).text = str(comment_score)
# Output the total score to the marks table in the feedback sheet
self._feedback_document.tables[2].cell(
16, 2).text = '{:g}'.format(total_score)
# Save the resulting feedback sheet
self._feedback_document.save(student_dir+'/../'+filename)
def _write_student_name_to_document(
self, student_dir, student_dir_name, student_name):
"""
Internal method, write out student details to the feedback
document.
"""
#default cell for student's firstname lastname
filename = self._feedback_doc_name.replace(
'username', student_dir_name)
self._feedback_document.tables[0].cell(1,0).text = student_name
self._feedback_document.tables[0].cell(1,1).text = student_dir_name
self._feedback_document.tables[0].cell(1,2).text = self._marker_name
self._feedback_document.save(student_dir+'/../'+filename)
#print student_dir+'/'+filename
def _write_comments_to_document(
self, student_dir, student_dir_name, marks):
"""
Internal method, write out marking comments to the feedback
document.
"""
filename = os.path.basename(
self._feedback_doc_name).replace('username', student_dir_name)
feedback_document = Document(student_dir+'/../'+filename)
# Add the headings
feedback_document.add_heading('Program Comments', 2)
feedback_document.add_heading('Program input', 3)
inputs = marks.get_input().splitlines()
# Output the inputs past to the program
for line in inputs:
feedback_document.add_paragraph(line, style='CodeChunk')
# Output any extra inputs passed to the program if there are any
extra_program_inputs = marks.get_extra_program_inputs()
for extra in extra_program_inputs:
feedback_document.add_heading(extra[0], 3)
extra_lines = extra[1].splitlines()
for line in extra_lines:
feedback_document.add_paragraph(line, style='CodeChunk')
# Output the outputs generated by the program
feedback_document.add_heading('Program output', 3)
output = marks.get_output().splitlines()
for line in output:
feedback_document.add_paragraph(line, style='CodeChunk')
# Output any extra outputs generated by the program if there are any
extra_program_outputs = marks.get_extra_program_outputs()
for extra in extra_program_outputs:
feedback_document.add_heading(extra[0], 3)
extra_lines = extra[1].splitlines()
for line in extra_lines:
feedback_document.add_paragraph(line, style='CodeChunk')
# Output any comments concerning the execution and output correctness
feedback_document.add_heading('Execution comments', 3)
comments = marks.get_execution_comments().splitlines()
for line in comments:
feedback_document.add_paragraph(line, style='CodeChunkComment')
# Output an annotated copy of the code
feedback_document.add_heading('Your code', 3)
program = marks.get_full_program().splitlines()
comments = marks.get_error_list()
line_num = 1
for line in program:
line_encoded = line.decode('ascii', 'replace')
highlight = False
# Chech whether there are any comments associated with this line
# of code
for comment in comments:
if comment[0] == line_num:
highlight = True
if highlight:
# Add the comments straight after the relevant line of code
feedback_document.add_paragraph(
str(line_num) + '\t: ' + \
line_encoded, style='CodeChunkHighlight')
for comment in comments:
if comment[0] == line_num:
feedback_document.add_paragraph(
'\t ' + comment[1], style='CodeChunkComment')
else:
# Add in just the line of code
feedback_document.add_paragraph(
str(line_num) + '\t: ' + line_encoded, style='CodeChunk')
line_num += 1
for comment in comments:
# Output any comments that relate to the code, but not to any
# particular line (e.g. if there are no comments at all in the
# code).
if comment[0] == 0:
feedback_document.add_paragraph(
'\t ' + comment[1], style='CodeChunkComment')
# Save the resulting feedback file out
feedback_document.save(student_dir+'/../'+filename)
def _construct_name_map(self):
"""
Internal method, collect the usernames and student names from a
provided Excel document. The details are used to populate the
feedback document with student details.
"""
username_index = 0
is_constructing_name_map = False
# Create a list of usernames and the real names they're associated
# with. The details are extracted from the input Excel file provided
for i in range(self._marking_sheet.nrows):
if is_constructing_name_map:
username = self._marking_sheet.row_values(i)[username_index]
firstname = self._marking_sheet.row_values(i)[username_index-1]
lastname = self._marking_sheet.row_values(i)[username_index-2]
self._name_map[username]=[firstname, lastname]
elif self._marking_sheet.row_values(i).count('Username') is 1:
username_index = self._marking_sheet.row_values(i).index(
'Username')
is_constructing_name_map = True
# Task number
# Folder containing students' folders
# Marker's initials (optional)
# Temp build folder (optional)
# Feedback template (optional)
# Student details list (optional)
# Summary output (optional)
# Run as a script, but skip this bit if the code is being imported
if __name__ == "__main__":
start = time()
parser = ArgumentParser(
description='Batch marker for 4001COMP Java programming.')
# Required parameters
parser.add_argument(
'task', metavar='TASK', type=int, help='Task number (e.g. 1)')
parser.add_argument(
'workdir', metavar='WORK', type=str,
help='Folder containing students\' folders (e.g. ./DLJ)')
# Optional parameters
parser.add_argument(
'-i', '--initials', metavar='INITIALS', type=str,
help='Marker\'s initials (default Master)', default='Master')
parser.add_argument(
'-b', '--builddir', metavar='BUILD', type=str,
help='Folder to output build files to (default ./build)',
default='./build')
parser.add_argument(
'-t', '--template', metavar='TEMPLATE', type=str,
help='Word feedback sheeet template (default '
'./feedback_username.docx)',
default='./feedback_username.docx')
parser.add_argument(
'-d', '--details', metavar='DETAILS', type=str,
help='Excel student list (default ./4001COMP Marking 2014-15.xlsx)',
default='./4001COMP Marking 2014-15.xlsx')
parser.add_argument(
'-s', '--summary', metavar='SUMMARY', type=str,
help='Summary of marks as a CSV file (default ./summary.csv)',
default='./summary.csv')
# Apply these arguments
args = parser.parse_args()
batchmark = BatchMark(
args.task, args.workdir, args.initials, args.builddir, args.template,
args.details, args.summary)
batchmark.go()
# Output the time taken for perforance testing
end = time()
duration = end - start
print 'Time taken: {}'.format(duration)