-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrade.py
executable file
·221 lines (214 loc) · 11.6 KB
/
grade.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
#!/usr/bin/python
import os
print "##########################################################################################################"
print "# Grader Script v1.0 #"
print "# Author: Ameya Gholkar #"
print "#This script will process the Java files which are in its current directory and generate a folder #"
print "# structure based on the Unity IDs. Files should be of the form <unityid>_<exercise>__<program-name>.java#"
print "#This file format is automatically provided by NCSU moodle assignment download. #"
print "# #"
print "#Script also generates a complied PDF file with the source code and its output for analysis #"
print "# Requires enscript, ps2pdf and pdftk #"
print "# #"
print "# #"
print "##########################################################################################################"
legal_file_types = ['java']
legal_conversion_types = ['java', 'txt', 'ps']
ordering = {'java': '0', 'compile': '1', 'output': '2'};
#Add the Exercise Name that you are grading. ****** No underscores allowed here. ******
exerciseName = "Exercise-8"
#Gradesheet filename - needs to be modified if the name is different other than what is mentioned. ****** No underscores allowed here. ******
gradesheet = ''
#Declare path = "" if there no grade-sheet is needed. Make sure you comment the ONLY the path which is declared here; DO NOT comment the Variable.
path_of_gradesheet = '' #os.getcwd() + "/" + gradesheet
#Mostly should be the same. If there is another Test folder that you are bringing in; make sure you replace the folder name here.
#****** No underscores allowed here. ******
testfolder_name = 'Test'
path_to_test_folder = os.getcwd() + "/" + testfolder_name
# Do not Change
# ---------------------------------- #
home_path = os.getcwd()
print home_path
root = exerciseName + "/"
# ---------------------------------- #
# Add the names of files that have staff test files. Please add the exact Name. Spelling mistakes might make the script crash or Stop.
# ***** Important ***** TO be kept [''](empty) if there are no staff tests.
files_to_be_staff_tested = ['']
# Add the names of files that have input test files in Test Folder. Please add the exact Name. Spelling mistakes might make the script crash or Stop.
# ***** Important ***** TO be kept [''](empty) if there are no input tests.
files_to_be_input_tested = ['']
# ***** Script Funtionality Begins - Do not Change *****
# Converts to ps.
def convert2ps(root_path, file_name):
if file_name.find("java") != -1:
prefix = ordering['java']
elif file_name.find("compile") != -1:
prefix = ordering['compile']
elif file_name.find("output") != -1:
prefix = ordering['output']
else:
prefix = '9'
print root_path + " " + file_name
os.system("enscript --header="+file_name+" "+root_path+"/"+file_name+" -o "+root_path+"/"+prefix+"_"+file_name.split(".")[0]+".ps")
os.system("rm "+root_path+"/"+file_name)
return prefix+"_"+file_name.split(".")[0]+".ps"
# Converts ps to pdf
def convert2pdf(root_path, file_name):
os.system("ps2pdf "+root_path+"/"+file_name+" "+root_path+"/"+file_name.split(".")[0]+".pdf")
return
## This method builds the folder strucutre and process the valid Java files. It also takes into consideration any Test files that might have to be input redirected to
## the corresponding program. Remember that all test files should be according to a specified format and in the Test folder.
def process_files( infile, file_type ):
unityid = infile.split('_')
if len(unityid) > 1:
unityId = unityid[0]
else:
unityId = ""
programName = infile.split('__');
if len(programName) > 1:
#print "ProgramName: "+programName[1]
fileName = programName[1]
else:
fileName = programName[0]
className = fileName.split('.')[0]
if unityId != "":
root_of_file = root + unityId+"/"
if not os.path.exists(root + unityId):
os.system("mkdir " + root + unityId)
if file_type == 'java':
path_to_compile_output = root_of_file + unityId + "_" + className + "_compile.txt"
path_to_output = root_of_file + unityId + "_" + className + "_output.txt"
path_to_test_output = root_of_file + unityId + "_"
path_to_compile_test_output = root_of_file + unityId + "_"
path_to_java_file = root_of_file + fileName
tests_listing = os.listdir(path_to_test_folder)
##Following Block executes the required system commands##
os.system("cp " + infile + " " + fileName)
os.system("mv " + infile + " " + path_to_java_file)
print "------------------------------------------------\n Compiling Java File \n------------------------------------------------"
os.system("javac " + fileName + " 2> " + path_to_compile_output)
if os.stat(path_to_compile_output).st_size == 0:
os.system("echo 'No compile Errors' > " + path_to_compile_output)
# Check to see if there are any Test files listed; if yes: process them IF they are valid, if no: continue standard execution.
if fileName in files_to_be_input_tested:
print "------------------------------------------------\n Tests Found - Executing Tests \n------------------------------------------------"
for testFile in tests_listing:
if testFile.find(className) != -1:
os.system("java " + className + " < " + path_to_test_folder+"/"+testFile + " > " + root_of_file + testFile.split('.')[0]+"_"+unityId + "_" + className + "_output.txt")
os.system("java " + className + " < " + path_to_test_folder+"/"+testFile + " 2> " + root_of_file + testFile.split('.')[0]+"_"+unityId + "_" + className + "_Error_Output.txt")
if os.stat(root_of_file + testFile.split('.')[0]+"_"+unityId + "_" + className + "_Error_Output.txt").st_size == 0:
os.system("echo 'No Output Exceptions' > " + root_of_file + testFile.split('.')[0]+"_"+unityId + "_" + className + "_Error_Output.txt")
else:
print "------------------------------------------------\n Test : " + testFile + " not for for the classname : "+className+"\n------------------------------------------------"
#os.system("java " + className + " > " + path_to_output)
else:
print "------------------------------------------------\n No Tests Found - Standard Execution Selected \n------------------------------------------------"
os.system("java " + className + " > " + path_to_output)
# Execute the Staff Test Cases
listing = os.listdir(home_path)
if fileName in files_to_be_staff_tested:
for testfile in listing:
if testfile.find(className + "TSTest") != -1:
print "Found testfile : " + testfile
if testfile.find(".class") != -1:
os.system("rm " + testfile.split(".")[0] + ".class")
print "@@ Removing CLASS file : " + testfile + "@@"
else:
os.system("javac "+ testfile +" 2> "+ path_to_compile_test_output)
if os.stat(path_to_compile_test_output).st_size == 0:
os.system("echo 'No compile Errors. ' > "+ path_to_compile_test_output+testfile.split('.')[0]+"_compile.txt")
os.system("java "+testfile.split(".")[0]+" > "+path_to_test_output+testfile.split('.')[0]+"_output.txt" )
else:
os.system("echo 'Compile Errors Found. No Output. ' > " + path_to_test_output)
#Removing the stale files.
os.system("rm " + fileName)
os.system("rm " + className + ".class")
else:
os.system("echo ' Compile Errors Found. No Output. ' > " + path_to_output)
os.system("cp " + fileName + " " + infile)
os.system("rm " + fileName)
else:
os.system("mv " + infile + " " + root_of_file + fileName)
print "------------------------------------------------\n Finished processing a "+file_type+" file for "+unityId+" \n------------------------------------------------"
else:
if infile != gradesheet:
print "------------------------------------------------\n No unity Id found. - "+infile+" \n------------------------------------------------ "
return
## Main method. Upon call; it calls the process files method and handles any illegal file types.
def main(listing):
for infile in listing:
file_type_list = infile.split('.')
print "Processing "+file_type_list[0]
if len(file_type_list) > 1:
file_type = file_type_list[1]
if file_type in legal_file_types:
process_files(infile,file_type)
else:
print "Illegal file type : "+file_type
unityid_list = infile.split("_")
if len(unityid_list) > 1:
if not os.path.exists(root + unityid_list[0]):
os.system("mkdir " + root + unityid_list[0])
os.system("mv " + infile + " " + root + unityid_list[0]+"/"+ infile)
else:
unityid_list = infile.split("_")
if len(unityid_list) > 1:
if not os.path.exists(root + unityid_list[0]):
os.system("mkdir " + root + unityid_list[0])
os.system("mv " + infile + " " + root + unityid_list[0]+"/"+ infile)
else:
print "Directory found. Skipped."
return
## Second Main method. Processes all the directories made by the 'main' method. Converts the files into ps and then to pdf.
## Also builds the Main feedback PDF file.
def main_dir_processing(listing):
for infile in listing:
file_type_list = infile.split('.')
print "Processing "+file_type_list[0]
if len(file_type_list) > 1:
print "File Found - "+infile+". Skipped."
else:
print "-----------------------------------------------------------------------------------------------------------"
print "Directory found - "+file_type_list[0]
if file_type_list[0] != testfolder_name:
os.chdir(home_path + "/" + file_type_list[0])
#After this point, all items found should be necessarily files.
dir_file_list = os.listdir(os.getcwd())
for infile in dir_file_list:
if len(infile.split('.')) > 1:
if infile.split('.')[1] in legal_conversion_types: #and infile.find(".pdf") == -1:
convert2ps(os.getcwd(),infile)
else: #File found is without a type. Default Action - Convert it
convert2ps(os.getcwd(),infile)
dir_file_list = os.listdir(os.getcwd())
dir_file_list.sort()
for infile in dir_file_list:
if len(infile.split('.')) > 1:
if infile.split('.')[1] in legal_conversion_types: #and infile.find(".pdf") == -1:
convert2pdf(os.getcwd(),infile)
os.system("rm "+infile)
dir_file_list = os.listdir(os.getcwd())
dir_file_list.sort()
finalpdf_name = file_type_list[0]+"_"+exerciseName+".pdf "
pdf_list = ""
for infile in dir_file_list:
if infile.find(".pdf") != -1:
pdf_list = pdf_list + infile + " "
finalpdf_path = "pdftk "+path_of_gradesheet+" "+pdf_list + "cat output " + finalpdf_name
print finalpdf_path
os.system(finalpdf_path)
os.system("rm " + pdf_list)
os.chdir(home_path)
print "-----------------------------------------------------------------------------------------------------------"
return
if os.path.exists(home_path + "/" + exerciseName):
os.system("rm -r " + exerciseName)
#Create Folder of the Exercise Name
os.mkdir(exerciseName)
listing = os.listdir(home_path)
main(listing)
print "-------------------------------------------------------- Finished building Folder structure -------------------------------------------------------- "
home_path = home_path + "/" + root
listing = os.listdir(home_path)
main_dir_processing(listing)
print "-------------------------------------------------------------- Finished Process ------------------------------------------------------------------- "