-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
261 lines (243 loc) · 9.63 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"os"
v1 "patpat/api/v1"
"patpat/initialize"
"patpat/judge"
"patpat/run"
"runtime"
"strconv"
"strings"
)
func main() {
goos := runtime.GOOS
stuCmd := flag.NewFlagSet("stu", flag.ExitOnError)
judgePtr := stuCmd.String("judge", "0-12345-hanhan", "Please specify the name of the folder containing Java files to judge.")
onlineModePtr := stuCmd.Bool("online", true, "Online or offline mode")
// stuPkgPtr := stuCmd.Bool("pkg", false, "Use package or not.")
taCmd := flag.NewFlagSet("ta", flag.ExitOnError)
taJudgePtr := taCmd.String("judge", "0-12345-hanhan", "Please specify the name of the folder containing Java files to judge.")
taPwdPtr := taCmd.String("pwd", "888888", "Please enter your password.")
tagPtr := taCmd.String("tag", "test", "Tag for this judge.")
// taPkgPtr := taCmd.Bool("pkg", false, "Use package or not.")
regCmd := flag.NewFlagSet("reg", flag.ExitOnError)
regSidPtr := regCmd.Int("sid", 123456, "Please specify your SID.")
regPwdPtr := regCmd.String("pwd", "888888", "Please enter your password.")
queCmd := flag.NewFlagSet("query", flag.ExitOnError)
queSidPtr := queCmd.Int("sid", 123456, "Please specify your SID.")
quePwdPtr := queCmd.String("pwd", "888888", "Please enter your password.")
switch os.Args[1] {
case "stu":
stuCmd.Parse(os.Args[2:])
if *onlineModePtr {
initialize.InitMySQL()
}
folderName := *judgePtr
paramList := strings.Split(folderName, "-")
num, err := strconv.Atoi(paramList[0])
if err != nil {
fmt.Println("请确保文件夹命名为 'num-sid-name' 的格式!")
return
// panic("Cannot parse num!")
}
sid, err := strconv.Atoi(paramList[1])
if err != nil {
fmt.Println("请确保文件夹命名为 'num-sid-name' 的格式!")
return
// panic("Cannot parse sid!")
}
name := paramList[2]
fmt.Println("Lab: " + strconv.Itoa(num) + " | SID: " + strconv.Itoa(sid) + " | Name: " + name)
tests := initialize.FetchJudgeConfig("test/judge.yaml")
fmt.Println("Test cases:")
for _, t := range tests {
fmt.Println("\t" + t)
}
var exitCode int
switch goos {
case "windows":
// if *stuPkgPtr {
// exitCode = run.CompileJava("javac", "-encoding", "UTF-8", "-cp", "./"+folderName+"/src", "-d", "./"+folderName+"/out", "./"+folderName+"/src"+"/Test.java")
// } else {
// exitCode = run.CompileJava("javac", "-encoding", "UTF-8", folderName+"/src/*.java")
// }
exitCode = run.CompileJava("javac", "-encoding", "UTF-8", "-cp", "./"+folderName+"/src", "-d", "./"+folderName+"/out", "./"+folderName+"/src"+"/*.java")
case "darwin", "linux":
// if *stuPkgPtr {
// exitCode = run.CompileJava("javac", "-encoding", "UTF-8", "-cp", "./"+folderName+"/src", "-d", "./"+folderName+"/out", "./"+folderName+"/src"+"/Test.java")
// } else {
// exitCode = run.CompileJava("/bin/sh", "-c", "javac -encoding UTF-8 "+folderName+"/src/*.java")
// }
exitCode = run.CompileJava("/bin/sh", "-c", "javac -encoding UTF-8 -cp ./"+folderName+"/src -d ./"+folderName+"/out ./"+folderName+"/src/*.java")
}
if exitCode != 0 {
fmt.Println("Compile Error!")
if *onlineModePtr {
judge.GradeUpload(num, sid, name, "testcase", -3)
}
} else {
fmt.Println(strings.Repeat("_", 80))
fmt.Println()
fmt.Println("您本次自测的评测情况如下: ")
for _, t := range tests {
fmt.Println()
testName, testData, err := initialize.FetchTestCase("test/" + t)
if err != nil {
fmt.Println("Cannot fetch test case: " + t)
fmt.Println("Caused by:")
fmt.Println(err)
continue
}
PrintBanner(testName)
testInputList, testInput, testOutputLines, testOutput, mapTable := initialize.ParseTestData(testData)
// var runStatus int
// var actualOutput string
// var actualOutputLines []string
// if *stuPkgPtr {
// runStatus, actualOutput, actualOutputLines = run.RunJava(2, testInput, "java", "-classpath", "./"+folderName+"/out", "Test")
// } else {
// runStatus, actualOutput, actualOutputLines = run.RunJava(2, testInput, "java", "-classpath", folderName+"/src", "Test")
// }
runStatus, actualOutput, actualOutputLines := run.RunJava(2, testInput, "java", "-classpath", "./"+folderName+"/out", "Test")
compareResult, smallerLen, wrongOutputPos := judge.Compare(testOutputLines, actualOutputLines, mapTable)
resultMessage := "Num = " + strconv.Itoa(num) + ", 评测点 = " + t[0:len(t)-5] + ", Grade = " + GradeToString(judge.CalcGrade(runStatus, compareResult))
fmt.Println(resultMessage)
judge.ReportGen(t[0:len(t)-5], runStatus, compareResult, smallerLen, wrongOutputPos, testInputList, testOutputLines, actualOutputLines, testOutput, actualOutput)
if *onlineModePtr {
judge.GradeUpload(num, sid, name, t[0:len(t)-5], judge.CalcGrade(runStatus, compareResult))
}
}
}
case "ta":
initialize.InitMySQL()
taCmd.Parse(os.Args[2:])
folderName := *taJudgePtr
pwd := *taPwdPtr
paramList := strings.Split(folderName, "-")
num, err := strconv.Atoi(paramList[0])
if err != nil {
fmt.Println("请确保文件夹命名为 'num-sid-name' 的格式!")
return
// panic("Cannot parse num!")
}
sid, err := strconv.Atoi(paramList[1])
if err != nil {
fmt.Println("请确保文件夹命名为 'num-sid-name' 的格式!")
return
// panic("Cannot parse sid!")
}
name := paramList[2]
fmt.Println("Lab: " + strconv.Itoa(num) + " | SID: " + strconv.Itoa(sid) + " | Name: " + name)
if v1.Login(sid, pwd) {
formalTestCases := initialize.FetchFormalTestCase(num)
fmt.Println("Test cases:")
for _, formalTestCase := range formalTestCases {
fmt.Println("\t" + formalTestCase.FileName)
}
fmt.Println()
var exitCode int
switch goos {
case "windows":
// if *taPkgPtr {
// exitCode = run.CompileJava("javac", "-encoding", "UTF-8", "-cp", "./"+folderName+"/src", "-d", "./"+folderName+"/out", "./"+folderName+"/src"+"/Test.java")
// } else {
// exitCode = run.CompileJava("javac", "-encoding", "UTF-8", folderName+"/src/*.java")
// }
exitCode = run.CompileJava("javac", "-encoding", "UTF-8", "-cp", "./"+folderName+"/src", "-d", "./"+folderName+"/out", "./"+folderName+"/src"+"/*.java")
case "darwin", "linux":
// if *taPkgPtr {
// exitCode = run.CompileJava("javac", "-encoding", "UTF-8", "-cp", "./"+folderName+"/src", "-d", "./"+folderName+"/out", "./"+folderName+"/src"+"/Test.java")
// } else {
// exitCode = run.CompileJava("/bin/sh", "-c", "javac -encoding UTF-8 "+folderName+"/src/*.java")
// }
exitCode = run.CompileJava("/bin/sh", "-c", "javac -encoding UTF-8 -cp ./"+folderName+"/src -d ./"+folderName+"/out ./"+folderName+"/src/*.java")
}
// exitCode := run.CompileJava("javac", "-encoding", "UTF-8", "-cp", "./"+folderName+"/src", "-d", "./"+folderName+"/out", "./"+folderName+"/src"+"/Test.java")
if exitCode != 0 {
fmt.Println("Compile Error!")
judge.GradeUploadFormal(num, sid, name, "testcase", -3, *tagPtr)
} else {
fmt.Println(strings.Repeat("_", 80))
fmt.Println()
fmt.Println("您本次黑盒测试的评测情况如下:")
for _, t := range formalTestCases {
_, _, testName, testData := initialize.ParseFormalTestCase(t)
// testName, testData := initialize.FetchTestCase("test/" + t)
fmt.Println()
PrintBanner(testName)
testInputList, testInput, testOutputLines, testOutput, mapTable := initialize.ParseTestData(testData)
// var runStatus int
// var actualOutput string
// var actualOutputLines []string
// if *taPkgPtr {
// runStatus, actualOutput, actualOutputLines = run.RunJava(2, testInput, "java", "-classpath", "./"+folderName+"/out", "Test")
// } else {
// runStatus, actualOutput, actualOutputLines = run.RunJava(2, testInput, "java", "-classpath", folderName+"/src", "Test")
// }
runStatus, actualOutput, actualOutputLines := run.RunJava(2, testInput, "java", "-classpath", "./"+folderName+"/out", "Test")
compareResult, smallerLen, wrongOutputPos := judge.Compare(testOutputLines, actualOutputLines, mapTable)
resultMessage := "Num = " + strconv.Itoa(num) + ", 评测点 = " + t.FileName + ", Grade = " + GradeToString(judge.CalcGrade(runStatus, compareResult))
fmt.Println(resultMessage)
judge.TaJudgeReportGen(t.FileName, runStatus, compareResult, smallerLen, wrongOutputPos, testInputList, testOutputLines, actualOutputLines, testOutput, actualOutput)
judge.GradeUploadFormal(num, sid, name, t.FileName, judge.CalcGrade(runStatus, compareResult), *tagPtr)
}
}
}
case "reg":
initialize.InitMySQL()
regCmd.Parse(os.Args[2:])
sid := *regSidPtr
pwd := *regPwdPtr
result := v1.Register(sid, pwd)
fmt.Println(result)
case "query":
initialize.InitMySQL()
queCmd.Parse(os.Args[2:])
sid := *queSidPtr
pwd := *quePwdPtr
if v1.Login(sid, pwd) {
result := v1.QueryResult(sid)
fmt.Print(result)
if err := os.WriteFile("ta_judge_result.txt", []byte(result), 0644); err != nil {
panic(err)
}
}
default:
fmt.Println("Expected 'stu' or 'ta' or 'reg' or 'query' subcommands!")
}
fmt.Println()
}
func PrintBanner(title string) {
extra := int((80 - 2 - len(title)) / 2)
if extra < 0 {
extra = 0
}
fmt.Print(strings.Repeat("=", extra))
fmt.Print(" \033[33m" + title + "\033[0m ")
fmt.Println(strings.Repeat("=", 80-extra-2-len(title)))
}
// (AC: 1, TLE: -1, WA: -2, CE: -3, RE: -4)
func GradeToString(grade int) string {
switch grade {
case 1:
// Green
return "\033[92mAC\033[0m"
case -1:
// Blue
return "\033[36mTLE\033[0m"
case -2:
// Red
return "\033[91mWA\033[0m"
case -3:
// Yellow
return "\033[33mCE\033[0m"
case -4:
// Magenta
return "\033[95mRE\033[0m"
default:
// Skyblue
return "\033[96mN/A\033[0m"
}
}