feat: dummy binary + parser example
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
张泊明518370910136 2024-03-12 16:22:21 -04:00
parent a5643c1556
commit 27a1e4420e
GPG Key ID: D47306D7062CDA9D
6 changed files with 120 additions and 8 deletions

26
cmd/dummy/main.go Normal file
View File

@ -0,0 +1,26 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/pkg/dummy"
)
func main() {
score := flag.Int("score", 0, "score")
flag.Parse()
res, err := dummy.Run(dummy.Conf{Score: *score})
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
b, err := json.Marshal(res)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
fmt.Printf("%s", b)
}

27
examples/dummy/conf.toml Normal file
View File

@ -0,0 +1,27 @@
[[stages]]
name = "dummy"
[stages.executor]
name = "sandbox"
[stages.executor.with.default]
args = ["./dummy", "--score", "100"]
env = ["PATH=/usr/bin:/bin"]
cpuLimit = 10_000_000_000
memoryLimit = 104_857_600
procLimit = 50
copyInCwd = true
[stages.executor.with.default.copyIn.dummy]
src = "./../../build/dummy"
copyOut = ["stdout", "stderr"]
[stages.executor.with.default.stdin]
content = ""
[stages.executor.with.default.stdout]
name = "stdout"
max = 4_096
[stages.executor.with.default.stderr]
name = "stderr"
max = 4_096
[stages.parser]
name = "dummy"
[stages.parser.with]
score = 10
comment = " + comment from toml conf"

10
examples/dummy/run.sh Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -xe
DIRNAME=`dirname -- "$0"`
# cd to make CopyInCwd work
cd $DIRNAME
./../../build/joj3
cat ./joj3_result.json
rm -f ./joj3_result.json
cd -

View File

@ -2,7 +2,7 @@ package dummy
import "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" import "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage"
var name = "result status" var name = "dummy"
func init() { func init() {
stage.RegisterParser(name, &Dummy{}) stage.RegisterParser(name, &Dummy{})

View File

@ -1,9 +1,12 @@
package dummy package dummy
import ( import (
"encoding/json"
"fmt" "fmt"
"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage"
"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/pkg/dummy"
"github.com/criyle/go-judge/envexec"
) )
type Conf struct { type Conf struct {
@ -13,6 +16,32 @@ type Conf struct {
type Dummy struct{} type Dummy struct{}
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
stdout := executorResult.Files["stdout"]
stderr := executorResult.Files["stderr"]
if executorResult.Status != stage.Status(envexec.StatusAccepted) {
return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf(
"Unexpected executor status: %s.\nStderr: %s",
executorResult.Status, stderr,
),
}
}
var dummyResult dummy.Result
err := json.Unmarshal([]byte(stdout), &dummyResult)
if err != nil {
return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf("Failed to parse result: %s", err),
}
}
return stage.ParserResult{
Score: dummyResult.Score + conf.Score,
Comment: dummyResult.Comment + conf.Comment,
}
}
func (*Dummy) Run(results []stage.ExecutorResult, confAny any) ( func (*Dummy) Run(results []stage.ExecutorResult, confAny any) (
[]stage.ParserResult, bool, error, []stage.ParserResult, bool, error,
) { ) {
@ -22,13 +51,7 @@ func (*Dummy) Run(results []stage.ExecutorResult, confAny any) (
} }
var res []stage.ParserResult var res []stage.ParserResult
for _, result := range results { for _, result := range results {
res = append(res, stage.ParserResult{ res = append(res, Parse(result, *conf))
Score: conf.Score,
Comment: fmt.Sprintf(
"%s, executor status: run time: %d ns, memory: %d bytes",
conf.Comment, result.RunTime, result.Memory,
),
})
} }
return res, false, nil return res, false, nil
} }

26
pkg/dummy/dummy.go Normal file
View File

@ -0,0 +1,26 @@
package dummy
import (
"fmt"
"log/slog"
)
type Conf struct {
Score int
}
type Result struct {
Score int
Comment string
}
func Run(conf Conf) (res Result, err error) {
if conf.Score < 0 {
slog.Error("dummy negative score", "score", conf.Score)
err = fmt.Errorf("dummy negative score: %d", conf.Score)
return
}
res.Score = conf.Score
res.Comment = "dummy comment"
return
}