-
Notifications
You must be signed in to change notification settings - Fork 6
/
judge.go
88 lines (79 loc) · 2.37 KB
/
judge.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
package controller
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
log "github.com/Sirupsen/logrus"
"github.com/VOID001/D-judge/config"
"github.com/VOID001/D-judge/request"
"github.com/docker/engine-api/client"
"github.com/pkg/errors"
)
const (
ExitAC = 42
ExitWA = 43
)
func (w *Worker) judge(ctx context.Context, rank int64, tid int64) (err error) {
// Create testcase dir, use to store result
execdir := filepath.Join(w.WorkDir, "execdir")
testcasedir := filepath.Join(w.WorkDir, fmt.Sprintf("testcase%03d", rank))
err = os.Mkdir(testcasedir, DirPerm)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("Judge error on Run#%d case %d", w.JudgeInfo.SubmitID, rank))
return
}
// Build the judge script
cli, er := client.NewClient(config.GlobalConfig.DockerServer, config.GlobalConfig.DockerVersion, nil, nil)
if er != nil {
err = errors.Wrap(er, fmt.Sprintf("Judge error on Run#%d case %d", w.JudgeInfo.SubmitID, rank))
return
}
cmd := fmt.Sprintf("compare/run execdir/testcase.in execdir/testcase.out testcase001 < execdir/program.out 2> compare.err >compare.out")
log.Debugf("executing command %s", cmd)
info, err := w.execcmdAttach(ctx, cli, "root", cmd)
// time.Sleep(time.Second * 10)
code := info.ExitCode
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("Judge error on Run#%d case %d", w.JudgeInfo.SubmitID, rank))
return
}
res := config.RunResult{}
res.RunTime = 0 // This is uneccessary
res.TestcaseID = tid
res.JudgingID = w.JudgeInfo.JudgingID
// Parse system meta and send to output system
// For Domjudge compability
// Save for Judge use
data, er := ioutil.ReadFile(filepath.Join(execdir, "program.meta"))
if er != nil {
err = errors.Wrap(er, "judge error")
return
}
res.OutputSystem = fmt.Sprintf("%s", data)
switch code {
case ExitWA:
res.RunResult = config.ResWA
// Report Accepted
case ExitAC:
res.RunResult = config.ResAC
// Report Wrong Answer
default:
err = errors.New(fmt.Sprintf("Judge return unexpected exit code %d", code))
return
}
// Remove execdir for next time use
oldexecdir := fmt.Sprintf("%s%03d", execdir, rank)
err = request.PostResult(ctx, res)
if err != nil {
err = errors.Wrap(err, "Judge error")
return
}
err = os.Rename(execdir, oldexecdir)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("Judge error on Run#%d case %d", w.JudgeInfo.SubmitID, rank))
return
}
return
}