From 27a1e4420e1365f0ee268384a164ace0853fe4d6 Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Tue, 12 Mar 2024 16:22:21 -0400 Subject: [PATCH] feat: dummy binary + parser example --- cmd/dummy/main.go | 26 ++++++++++++++++++++++ examples/dummy/conf.toml | 27 +++++++++++++++++++++++ examples/dummy/run.sh | 10 +++++++++ internal/parsers/dummy/meta.go | 2 +- internal/parsers/dummy/parser.go | 37 ++++++++++++++++++++++++++------ pkg/dummy/dummy.go | 26 ++++++++++++++++++++++ 6 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 cmd/dummy/main.go create mode 100644 examples/dummy/conf.toml create mode 100755 examples/dummy/run.sh create mode 100644 pkg/dummy/dummy.go diff --git a/cmd/dummy/main.go b/cmd/dummy/main.go new file mode 100644 index 0000000..63f0d2c --- /dev/null +++ b/cmd/dummy/main.go @@ -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) +} diff --git a/examples/dummy/conf.toml b/examples/dummy/conf.toml new file mode 100644 index 0000000..4c55dc5 --- /dev/null +++ b/examples/dummy/conf.toml @@ -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" diff --git a/examples/dummy/run.sh b/examples/dummy/run.sh new file mode 100755 index 0000000..9088452 --- /dev/null +++ b/examples/dummy/run.sh @@ -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 - diff --git a/internal/parsers/dummy/meta.go b/internal/parsers/dummy/meta.go index 4312dfd..fd9ba75 100644 --- a/internal/parsers/dummy/meta.go +++ b/internal/parsers/dummy/meta.go @@ -2,7 +2,7 @@ package dummy import "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" -var name = "result status" +var name = "dummy" func init() { stage.RegisterParser(name, &Dummy{}) diff --git a/internal/parsers/dummy/parser.go b/internal/parsers/dummy/parser.go index 34abf28..26c75c5 100644 --- a/internal/parsers/dummy/parser.go +++ b/internal/parsers/dummy/parser.go @@ -1,9 +1,12 @@ package dummy import ( + "encoding/json" "fmt" "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 { @@ -13,6 +16,32 @@ type Conf 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) ( []stage.ParserResult, bool, error, ) { @@ -22,13 +51,7 @@ func (*Dummy) Run(results []stage.ExecutorResult, confAny any) ( } var res []stage.ParserResult for _, result := range results { - res = append(res, stage.ParserResult{ - Score: conf.Score, - Comment: fmt.Sprintf( - "%s, executor status: run time: %d ns, memory: %d bytes", - conf.Comment, result.RunTime, result.Memory, - ), - }) + res = append(res, Parse(result, *conf)) } return res, false, nil } diff --git a/pkg/dummy/dummy.go b/pkg/dummy/dummy.go new file mode 100644 index 0000000..1248dd7 --- /dev/null +++ b/pkg/dummy/dummy.go @@ -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 +}