-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRunJavaUtils.py
More file actions
200 lines (177 loc) · 6.65 KB
/
RunJavaUtils.py
File metadata and controls
200 lines (177 loc) · 6.65 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
import shutil
import os
from shutil import move
from shutil import copyfile
import subprocess
import re
import tempfile
import io
import ExcelWriter
from unittest.test import test_result
def Create_Temp_Dir():
return tempfile.mkdtemp()
def Clean_And_Remove_Temp_Dir(tempDir):
shutil.rmtree(tempDir)
#
# We need the source lines to be surrounded by " " in order for the
# display in the .csv to be correct
def Convert_Source_To_Excel_Compat_List(sourceName):
try:
sourceFile = open(sourceName, "r")
result = sourceFile.readlines();
sourceFile.close()
except UnicodeDecodeError:
result = ["File cannot be read as text"]
return result
def Compare_Lines(goldenList, studentList):
maxLen = len(goldenList)
maxLen = max(len(studentList), maxLen)
mismatch=[]
anyMismatch=False
for i in range(0, maxLen):
if (i >= len(goldenList) or i >= len(studentList)):
mismatch.append("->")
anyMismatch=True
elif (goldenList[i].strip() != studentList[i].strip()):
mismatch.append("->")
anyMismatch=True
else:
mismatch.append(" ")
if (anyMismatch == False):
mismatch=["Perfect Match"]
return mismatch
#
# This is really cheesy. It just grabs the first line with the word package & assumes
# that is the package name.
# same with the className
def Get_Java_Info(fileName):
javaFile = io.open(fileName, "r", encoding="latin-1" )
package = ""
className = ""
author = "Unknown"
for line in javaFile:
if package == "":
m = re.match(r'\s*package\s+([a-zA-Z_]\w*)\s*;', line)
if m is not None:
package = m.group(1)
continue
if className == "":
m = re.search(r'\bclass\s+([a-zA-Z_]\w*)\b', line)
if m is not None:
className = m.group(1)
break
m = re.search(r'@author\b(.*)$', line)
if m is not None:
author = m.group(1).strip()
javaFile.close()
return author, package, className
def Change_Binary_To_String_List(binary):
result=[]
tempStr = ""
if (len(binary) > 0):
for a in binary:
if (isinstance(a, str)):
tempStr = tempStr + a
if (a > 8 and a < 127):
c = chr(a)
if (c != '\r'):
if (c == '\n'):
#remove trailing spaces, they are impossible to mark wrong since they are ambiguous in text
tempStr = tempStr.rstrip()
result.append(tempStr)
tempStr = ""
else:
tempStr = tempStr + str(c)
else:
tempStr = tempStr + "utf(" + str(a) + ")"
if (len(tempStr) != 0):
tempStr = tempStr.rstrip()
result.append(tempStr)
return result
#
# Runs the java file from the command line. The output is in binary format (that is what subprocess returns
# If it doesn't run, output will be equal to a string.
# If we plan to keep using, probably worth fixing this so output is always a string
def Run_Java_File(destName, package, baseName):
className = baseName + ".class"
invokeName = baseName
fullClassName = className
success = True
if (package != ""):
os.mkdir(package)
fullClassName = os.path.join(package, className)
invokeName = package + "." + invokeName
try:
subprocess.check_output(["javac", destName])
except subprocess.CalledProcessError as e:
output = ["build error: " + str(e.output)]
success = False
if (success == True):
try:
if (package != ""):
move(className, fullClassName)
raw_output = subprocess.check_output(["java", invokeName], stderr=subprocess.STDOUT)
output = raw_output.decode('utf-8', 'replace').splitlines()
except subprocess.CalledProcessError as e:
output = ["run error: " + str(e.output)]
success = False
if os.path.exists(fullClassName):
os.remove(fullClassName)
if (package != ""):
os.rmdir(package)
return (success, output)
def Copy_And_Run_Java_File(tempDir, source, classNameArg):
(author, package, className) = Get_Java_Info(source)
if (classNameArg != None):
className = classNameArg
destName = className + ".java"
dest = os.path.join(tempDir, destName)
print("copying " + source + " to " + dest)
copyfile(source, dest)
cwd = os.getcwd()
os.chdir(tempDir)
(success, output) = Run_Java_File(destName, package, className)
os.chdir(cwd)
os.remove(dest)
return (success, author, package, className, output)
def Create_Header(excelWriter, addOutput, addFile, goldLines):
fonts = ['Arial', 'Arial']
header = [["Author"], ["Ran"]]
if (len(goldLines) != 0):
addOutput = True
header.append(["Diff","Lines"])
header.append(["Golden", "Output"])
fonts.append('Courier New')
fonts.append('Courier New')
if (addOutput):
header.append(["Output"])
fonts.append('Courier New')
if (addFile):
header.append(["SourceFile"])
fonts.append('Courier New')
excelWriter.Add_Header(header, fonts, [0, 1])
def Append_Run_Data(successString, success, author, package, className, output, source, excelWriter, addOutput, addFile, goldLines):
excelWriter.Next_Student(author)
excelWriter.Add_String(str(successString),0)
stringLists=[]
if (len(goldLines)):
if (success):
stringLists.append(Compare_Lines(goldLines, output))
stringLists.append(goldLines)
else:
stringLists.append([""])
stringLists.append([""])
if (addOutput):
stringLists.append(output)
for stringList in stringLists:
excelWriter.Add_String_Array(stringList, 8)
if (addFile and source != ""):
excelWriter.Add_String_Array(Convert_Source_To_Excel_Compat_List(source), 4)
# one by one, copy the files from the source dir to the temp dir, run them,
# and store off the results in a .csv
def Copy_And_Run_Files(sourceDir, files, tempDir, excelWriter, addOutput, addFile, goldLines):
Create_Header( excelWriter, addOutput, addFile, goldLines)
for file in files:
source = os.path.join(sourceDir, file)
(success, author, package, className, output) = Copy_And_Run_Java_File(tempDir, source, None)
Append_Run_Data(str(success), success, author, package, className, output, source, excelWriter, addOutput, addFile, goldLines)