-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrading.py
96 lines (75 loc) · 3.29 KB
/
grading.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
import subprocess
import os
def runCPP(filepath, programInput,processTimeout):
# this will compile the cpp and output result
#filepath = r'/home/j/shareWindows/hw2/Adams, Danielle(da2435)/Submission attachment(s)/da2435_hw2_q1.cpp'
temp = subprocess.call(["g++", filepath])
#temp = subprocess.call("./a.out")
temp1 = subprocess.Popen("./a.out",shell = True, stdout = subprocess.PIPE,stdin = subprocess.PIPE,stderr = subprocess.PIPE)
#out,err = temp.communicate()
try:
out = temp1.communicate(input=programInput,timeout = processTimeout)
except subprocess.TimeoutExpired:
print("Timeout happened.\n")
out = [[],[],["timeout"]]
temp1.kill()
#b'25\n5\n12\n103\n'
return [temp,temp1,out]
def findStudentID(text):
start = text.find("(")
end = text.find(")")
return text[start+1:end]
def cleanCPP(filepath):
try:
filedata = open(filepath, newline='', encoding="utf16").read()
except:
filedata = open(filepath, newline='').read()
newdata = filedata.replace('#include "stdafx.h"',"").replace('뿃뻃',"")
print("converted")
f = open(filepath,'w')
f.write(newdata)
f.close()
return
def getStudentCPPResults(directory, program_input,question,processTimeout):
result = {"results":[]}
print("running")
for file in os.listdir(directory):
subDir1 = directory+file#+"/"
#print(file)
if os.path.isdir(subDir1):
#print(subDir1) # this is the first level
#print (findStudentID(subDir1))
studentID=findStudentID(subDir1)
for file in os.listdir(subDir1+"/"):
subDir2 = subDir1+"/"+file#+"/"
#print(subDir2)
if os.path.isdir(subDir2):
for file in os.listdir(subDir2):
filename = os.fsdecode(file)
#print(filename)
if filename.endswith(question+".cpp"):
# print(os.path.join(directory, filename))
print(subDir2+"/"+filename)
filepath = subDir2+"/"+filename
# get rid of #include "stdafx.h"
cleanCPP(filepath)
temp = runCPP(filepath, program_input,processTimeout)
#print (temp[0].returncode)
if temp[0] == 0:# if no error
#print(temp)
programresult = {"student_id":studentID,"output":temp[2][0]}
result["results"].append(programresult)
#print(filename)
continue
else:
continue
return result
directory = r'/home/user/VMShared/homework #4/' # put location of folder here
question ='1' #question you are grading
program_input = b'4\n4\n' # the stdin
processTimeout = 2
results = getStudentCPPResults(directory, program_input,question,processTimeout)
print("done running")
#below are the results
for x in results["results"]:
print (str(x["student_id"]) + ";"+str(x["output"]) )