-
Notifications
You must be signed in to change notification settings - Fork 0
/
Autograder.py
92 lines (73 loc) · 2.26 KB
/
Autograder.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
import subprocess as sp
import os
import re
import sys
cwd = '~'
submission_folder = 'Submission attachment(s)'
classpath = '.:'
directory = 'bin'
compile_cmd = 'javac'
junit_cmd = 'java org.junit.runner.JUnitCore'
checkstyle_cmd = 'java -jar checkstyle-6.2.jar -c CS1332-checkstyle.xml '
test = ' '
def setup():
global cwd, classpath, directory, compile_cmd, checkstyle_cmd, test
cwd = os.getcwd()
if not os.path.exists(cwd+'/bin'):
os.makedirs('bin')
if not os.path.exists(cwd+'/given'):
os.makedirs('given')
if not os.path.exists(cwd+'/tests'):
os.makedirs('tests')
if not os.path.exists(cwd+'/subs'):
os.makedirs('subs')
classpath = '.:' + cwd +'/junit-4.11.jar:' + cwd + '/given:'
directory = cwd + '/bin'
compile_cmd = 'javac -cp ' + classpath + ' -d ' + directory + ' *.java ' + cwd + '/tests/*.java'
checkstyle_cmd = 'java -jar ' + cwd + '/checkstyle-6.2.jar -c ' + cwd + '/CS1332-checkstyle.xml '
test = [test for test in os.listdir('tests') if re.match('.*java', test)]
test = test[0][:-5]
def compile():
print 'Compiling...'
try:
out = sp.check_output(compile_cmd, shell=True)
except sp.CalledProcessError as e:
print 'Did not compile'
return False
return True
def checkstyle(student_file):
print 'Running checkstyle...'
try:
out = sp.check_output(checkstyle_cmd + '*.java', shell=True)
student_file.write('0 checkstyle errors\n')
except sp.CalledProcessError as e:
errors = len(str.splitlines(e.output)) - 2
student_file.write(str(errors) + ' checkstyle errors\n')
def junit(student_file):
print 'Running JUnits...'
prev_dir = os.getcwd()
os.chdir(cwd + '/bin')
try:
out = sp.check_output(junit_cmd + ' ' + test, shell=True)
student_file.write(out)
except sp.CalledProcessError as e:
student_file.write(e.output)
os.chdir(prev_dir)
def grade():
os.chdir(cwd + '/subs')
students = [folder for folder in os.listdir('.') if os.path.isdir(folder)]
for folder in students:
print '\nGrading student: ' + folder
os.chdir(folder+"/"+submission_folder)
student_file = open('graded.txt', 'w')
if compile():
checkstyle(student_file)
junit(student_file)
else:
student_file.write('Did not compile')
student_file.close()
os.chdir(cwd + '/subs')
if __name__ == "__main__":
setup()
if(len(sys.argv) <= 1):
grade()