This repository was archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagscore.py
More file actions
476 lines (391 loc) · 13.5 KB
/
agscore.py
File metadata and controls
476 lines (391 loc) · 13.5 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
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
import argparse
import distutils.dir_util
import glob
import os
import shutil
import subprocess as sp
import sys
import webbrowser
import zipfile
import agsmsg as msg
import agsutil as util
def precheck():
""" Pre-check the assignment structure before starting grading. """
if not msg.ask_yn(f'Continue with {msg.underline(asmt_disp_name)}?'):
print('Bye:)')
exit(0)
link = util.get_link(f'{asmt_name}{asmt_num}')
if msg.ask_yn('Open link in browser?'):
msg.info(f'Opening {msg.underline(link)}...')
webbrowser.open_new_tab(link)
if not os.path.exists('submission'):
if len(glob.glob('CSC 116*.zip')) == 0:
return 1
msg.info('Extracting...')
g = glob.glob('CSC 116*.zip')
if len(g) == 1:
unzip(g[0])
return 0
def unzip(file):
with zipfile.ZipFile(file) as f:
f.extractall('submission')
def rename():
""" Rename all directories to [firstname lastname]. """
msg.info('Renaming...')
moodle_sub_specifier = '_assignsubmission_file_'
g = glob.glob(f'submission/*{moodle_sub_specifier}')
while len(g) == 0:
if msg.ask_yn('Directory names do not match, remove /submission and retry?',
msgtype='warn'):
shutil.rmtree('submission')
msg.info('Extracting...')
z = glob.glob('CSC 116*.zip')
if len(z) == 1:
unzip(z[0])
g = glob.glob(f'submission/*{moodle_sub_specifier}')
for entry in g:
print(msg.align_left(msg.name(entry.split('/')[1]), 80))
entry_new = entry.split('__')[0]
shutil.move(entry, entry_new)
print(f'\t-> {msg.name(entry_new.split("/")[1])}')
msg.info(f'Renamed to [lastname firstname]')
msg.press_continue()
def javac_all():
""" Compile all Java files in /src and /test, then copy to /bin. """
msg.info('Compiling...')
for student in glob.glob('* *'):
if not student.is_dir():
continue
os.chdir(student)
print(msg.name(student.name))
files = util.get_conf_asmt('files')
if files:
for file in files:
javac(file)
else:
for file in glob.glob('*.java', recursive=True):
javac(file)
os.chdir('..')
msg.press_continue()
def javac(file, lib='.:../../../../../lib/*'):
""" Compile a Java file to /bin. """
msg.info(f'Compiling {msg.underline(file)}...', '')
if not os.path.exists(file):
print()
msg.fail(f'{msg.underline(file)} does not exist', '')
input()
return -1
# src
cmd = f'javac {file}'
# test
if file.startswith('TS_') or file.endswith('Test.java'):
if args.junit or args.tstest:
cmd = f'javac -cp {lib} {file}'
proc = sp.Popen(cmd, shell=True,
stdout=sp.PIPE, stderr=sp.PIPE)
out, err = proc.communicate()
rc = proc.wait()
out = out.decode(encoding='utf-8')
err = err.decode(encoding='utf-8')
if not args.nostacktrace:
if len(out) > 0:
print()
msg.info(f'Output:\n{out}')
if len(err) > 0:
print()
msg.fail(f'Error:\n{err}')
if rc != 0:
print()
msg.fail(f'Failed to compile by {msg.underline(cmd)}')
sp.Popen([default_open, file])
if msg.ask_retry():
javac(file)
else:
print('done')
return rc
def java(file, arg='', arg2=''):
""" Run a compiled Java program. """
# TODO: Refactor
msg.info(f'Running {file}...')
cmd = f'java -cp ".:*" {arg} {file.replace(".java", "")} {arg2}'
if os.system(cmd) != 0:
msg.fail(f'Failed to run by {msg.underline(" ".join(cmd.split()))}')
if msg.ask_retry():
java(file)
def java_all():
""" Run all Java classes. """
msg.info('Running...')
for student in sorted(os.scandir('.'), key=lambda s: s.name):
if not student.is_dir():
continue
print(msg.name(student.name))
os.chdir(student.name)
cmds = util.get_conf_asmt('custom run')
files = util.get_conf_asmt('files')
if cmds:
run_custom(cmds)
elif files:
for f in files:
java(f)
else:
for f in glob.glob('*.java'):
java(f)
os.chdir('..')
def ts_test():
""" Run all teaching staff tests. """
msg.info('Running teaching staff tests...')
with zipfile.ZipFile(args.tstest) as zf:
msg.info(f'Extracting {args.tstest}...')
zf.extractall('ts')
ts_path = os.path.abspath('ts')
os.chdir('submission')
for student in sorted(glob.glob('* *')):
os.chdir(student)
print(msg.name(student))
msg.info('Copying TS files...')
distutils.dir_util.copy_tree(ts_path, '.')
src = util.get_conf_asmt('src')
for f in src:
msg.info(f'Opening {msg.underline(f)}...')
sp.Popen([default_open, f])
test = util.get_conf_asmt('test')
for item in util.get_conf_asmt('order'):
# Custom run is a dict: {'custom run' : [..., ...]}
if type(item) is dict:
_item = next(iter(item))
msg.info(f'Current grading: {msg.underline(_item)}')
cmds = item.get(_item)
run_custom(cmds)
continue
_item = item.lower()
msg.info(f'Current grading: {msg.underline(item)}')
if _item == 'wce':
ts_wce()
elif _item == 'tsbbt':
ts_tsbbt()
elif _item == 'tswbt':
ts_tswbt()
elif _item == 'bbt':
ts_bbt()
elif _item == 'wbt':
ts_wbt(test)
elif _item == 'style' or _item == 'checkstyle':
cs = checkstyle(src, test)
if cs == 0:
msg.info('No checkstyle error found')
else:
msg.textbar('Total')
print(msg.align_right(cs, 3))
msg.info('Testing done')
msg.press_continue()
os.chdir('..')
def ts_wce():
msg.info('Compiling files...')
msg.info(f'Current grading: {msg.underline("src")}')
arg2 = args.argument if args.argument else ''
for f in util.get_conf_asmt('src'):
javac(f, '.:*')
java(f, arg2=arg2)
msg.press_continue()
msg.info(f'Current grading: {msg.underline("test")}')
for f in util.get_conf_asmt('test'):
javac(f, '.:*')
java(f, arg='org.junit.runner.JUnitCore')
msg.press_continue()
def ts_tsbbt():
msg.info('Compiling TS_BBT...')
for f in sorted(glob.glob('TS_*_BB_Test.java')):
javac(f, '.:*')
java(f, arg='org.junit.runner.JUnitCore')
msg.press_continue()
def ts_tswbt():
msg.info('Compiling TS_WBT...')
ts = glob.glob('TS_*_WB_Runner.java')
javac(ts[0], '.:*')
java(ts[0])
msg.press_continue()
def ts_bbt():
pdf = glob.glob('*.pdf')
if len(pdf) != 1:
msg.fail('BBTP pdf file not found')
input()
return
msg.info(f'Opening {pdf[0]}...')
sp.Popen([util.get_conf_glob('open'), pdf[0]])
cmd = None
while msg.ask_yn('Continue running?'):
msg.info(f'Current command: {msg.underline(cmd)}')
if cmd is None:
msg.info('Enter a command to execute: ')
cmd = input()
else:
if msg.ask_yn('Change the current command?'):
cmd = input()
else:
msg.info('No change')
msg.info(f'Running {cmd}...')
os.system(cmd)
def ts_wbt(test):
for f in test:
javac(f)
java(f, 'org.junit.runner.JUnitCore')
def checkstyle(src, test, cs='~/cs-checkstyle/checkstyle'):
total = 0
for f in src:
msg.textbar(f)
cmd = f'{cs} {f} | grep -c ""'
proc = sp.run(cmd, shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
num = int(proc.stdout) - 4
total += num
print(msg.align_right(num, 3))
# Ignore magic numbers for test files
for f in test:
msg.textbar(f)
cmd = f'{cs} {f} | grep -v "is a magic number" -c'
proc = sp.run(cmd, shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
num = int(proc.stdout) - 4
total += num
print(msg.align_right(num, 3))
return total
def checkstyle_all():
for student in sorted(os.scandir('.'), key=lambda s: s.name):
if not student.is_dir():
continue
os.chdir(student.name)
print(msg.name(student.name, swap=True))
src = util.get_conf_asmt('src')
test = util.get_conf_asmt('test')
total = checkstyle(src, test)
print('-' * 30)
msg.textbar('Total')
print(msg.align_right(total, 3))
print()
os.chdir('..')
def hw():
msg.info('Checking homework...')
folders = glob.glob('* *')
for f in folders:
print(msg.name(f))
os.chdir(f)
files = glob.glob('*.*')
num = len(files)
if num == 0:
msg.fail('No submission')
elif num == 1:
pdf = files[0]
msg.info(f'Opening {msg.underline(pdf)}...')
sp.Popen(f'{default_open} \"{pdf}\"', shell=True)
else:
msg.warn(f'Multiple files found (total {num}):')
msg.index_list(files)
print('Select a file to open: ', end='')
i = msg.ask_index(0, num)
if 0 < i < num:
pdf = files[i]
msg.info(f'Opening {msg.underline(pdf)}...')
sp.Popen(f'{default_open} \"{pdf}\"', shell=True)
input()
os.chdir('..')
def run_custom(cmds):
for c in cmds:
msg.info(f'Running {msg.underline(c)}')
while sp.call(c, shell=True) != 0:
print()
msg.fail(f'Failed to run {msg.underline(c)}')
if not msg.ask_retry():
print()
break
# msg.press_continue()
if __name__ == '__main__':
if sys.hexversion < 0x03060000:
print('Python version >= 3.6 is required')
exit(1)
os.chdir(os.path.dirname(__file__))
parser = argparse.ArgumentParser(
description='The automatic grading script for CSC 116. '
'GitHub Repository: '
'https://github.com/apo5698/ags-cli')
parser.add_argument('-e', '--exercise', help='grade an exercise')
parser.add_argument('-p', '--project', help='grade a project')
parser.add_argument('-hw', '--homework', help='grade a homework')
parser.add_argument('-i', '--init',
nargs='?',
const=' ',
help='initialize system')
parser.add_argument('-r', '--reset',
help='reset current assignment',
action='store_true')
parser.add_argument('-v', '--version',
help='display the current version',
action='store_true')
parser.add_argument('-c', '--checkstyle',
help='run checkstyle',
action='store_true')
parser.add_argument('-j', '--junit',
help='compile tests with junit',
action='store_true')
parser.add_argument('-t', '--tstest',
help='teaching staff tests provided')
parser.add_argument('-a', '--argument',
help='add argument for running src')
parser.add_argument('-nc', '--nocompile',
help='do not compile',
action='store_true')
parser.add_argument('-nr', '--norun',
help='do not run',
action='store_true')
parser.add_argument('-ns', '--nostacktrace',
help='do not print stacktrace',
action='store_true')
args = parser.parse_args()
if args.version:
print(open('./VERSION').read())
exit(0)
msg.info('Starting service...')
if args.init:
force = True if args.init == 'f' else False
util.init(force)
exit()
asmt_name, asmt_cat, asmt_num = None, None, None
if args.exercise:
asmt_cat = 'exercise'
asmt_name = 'day'
asmt_num = args.exercise.zfill(2)
elif args.project:
asmt_cat = 'project'
asmt_name = 'p'
asmt_num = args.project
elif args.homework:
asmt_cat = 'homework'
asmt_name = 'hw'
asmt_num = args.homework
else:
msg.fatal('Invalid arguments. Use -h or --help for usage.')
asmt_disp_name = f'{asmt_cat.capitalize()} {asmt_num}'
# Path and config
path_asmt = os.path.join('content', asmt_cat, f'{asmt_name}{asmt_num}')
if args.exercise:
util.read_config_asmt(f'{path_asmt.replace("content", "config")}.yaml')
elif args.project:
util.read_config_asmt(
f'{path_asmt.replace("content", "config")}_'
f'{util.current_semester()}.yaml')
util.read_config_glob()
path_cs = util.get_conf_glob('checkstyle')
path_lib = util.get_conf_glob('lib')
default_open = util.get_conf_glob('open')
os.chdir(path_asmt)
if precheck() != 0:
msg.fatal('zip file or /submission not found')
rename()
if args.homework:
hw()
elif args.tstest:
ts_test()
else:
if args.checkstyle:
checkstyle_all()
if not args.nocompile:
javac_all()
if not args.norun:
java_all()