diff --git a/Makefile b/Makefile index d5ba0a4..cea7f45 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,23 @@ -.PHONY: all clean test +.PHONY: all build clean prepare-test test BUILD_DIR = ./build +TMP_DIR = ./tmp APPS := $(notdir $(wildcard ./cmd/*)) FLAGS := "-s -w" -all: +all: build + +build: $(foreach APP,$(APPS), go build -ldflags=$(FLAGS) -o $(BUILD_DIR)/$(APP) ./cmd/$(APP);) clean: rm -rf $(BUILD_DIR)/* + rm -rf $(TMP_DIR)/* rm -rf *.out prepare-test: git submodule update --init --remote test: - ./scripts/prepare_test_repos.sh + ./scripts/prepare_test_repos.sh $(TMP_DIR) go test -coverprofile cover.out -v ./... diff --git a/README.md b/README.md index cbca578..296d0eb 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ $ go build -o ./tmp/go-judge ./cmd/go-judge 5. Run `go-judge`. ```bash $ # make sure you are in go-judge directory -$ ./tmp/go-judge -enable-grpc -enable-debug -enable-metrics +$ ./tmp/go-judge -http-addr 0.0.0.0:5050 -grpc-addr 0.0.0.0:5051 -monitor-addr 0.0.0.0:5052 -enable-grpc -enable-debug -enable-metrics ``` 6. Pull submodules. It might be slow, so only run it when necessary. diff --git a/cmd/joj3/conf.go b/cmd/joj3/conf.go index 40dff6b..4f9951f 100644 --- a/cmd/joj3/conf.go +++ b/cmd/joj3/conf.go @@ -10,9 +10,11 @@ import ( ) type Conf struct { - LogLevel int `default:"0"` - OutputPath string `default:"joj3_result.json"` - Stages []struct { + SandboxExecServer string `default:"localhost:5051"` + SandboxToken string `default:""` + LogLevel int `default:"0"` + OutputPath string `default:"joj3_result.json"` + Stages []struct { Name string Executor struct { Name string diff --git a/cmd/joj3/main.go b/cmd/joj3/main.go index e21a52d..b8e0e05 100644 --- a/cmd/joj3/main.go +++ b/cmd/joj3/main.go @@ -5,7 +5,7 @@ import ( "log/slog" "os" - _ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/executors" + "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/executors" _ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers" "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" @@ -77,6 +77,7 @@ func main() { os.Exit(1) } setupSlog(conf) + executors.InitWithConf(conf.SandboxExecServer, conf.SandboxToken) stages := generateStages(conf) defer stage.Cleanup() results := stage.Run(stages) diff --git a/cmd/joj3/main_test.go b/cmd/joj3/main_test.go index daea3cf..1ea8029 100644 --- a/cmd/joj3/main_test.go +++ b/cmd/joj3/main_test.go @@ -66,7 +66,7 @@ func TestMain(t *testing.T) { scoreboard := scoreboard.Scoreboard{} scoreboard.Init() var tests []string - root := "../../tmp/submodules/JOJ3-examples" + root := "../../tmp/submodules/JOJ3-examples/examples/" err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { return err diff --git a/internal/executors/all.go b/internal/executors/all.go index bd860e6..76a7f96 100644 --- a/internal/executors/all.go +++ b/internal/executors/all.go @@ -2,8 +2,13 @@ package executors import ( _ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/executors/dummy" - _ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/executors/sandbox" + "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/executors/sandbox" ) // this file does nothing but imports to ensure all the init() functions // in the subpackages are called + +// overwrite the default registered executors +func InitWithConf(sandboxExecServer, sandboxToken string) { + sandbox.InitWithConf(sandboxExecServer, sandboxToken) +} diff --git a/internal/executors/sandbox/convert.go b/internal/executors/sandbox/convert.go index e5b786f..827dd15 100644 --- a/internal/executors/sandbox/convert.go +++ b/internal/executors/sandbox/convert.go @@ -1,6 +1,7 @@ package sandbox import ( + "log/slog" "os" "path/filepath" "strings" @@ -113,7 +114,12 @@ func convertPBFile(i stage.CmdFile) *pb.Request_File { i.Src = &absPath } } - return &pb.Request_File{File: &pb.Request_File_Local{Local: &pb.Request_LocalFile{Src: *i.Src}}} + s, err := os.ReadFile(*i.Src) + if err != nil { + s = []byte{} + slog.Error("read file error", "path", *i.Src, "error", err) + } + return &pb.Request_File{File: &pb.Request_File_Memory{Memory: &pb.Request_MemoryFile{Content: s}}} case i.Content != nil: s := strToBytes(*i.Content) return &pb.Request_File{File: &pb.Request_File_Memory{Memory: &pb.Request_MemoryFile{Content: s}}} diff --git a/internal/executors/sandbox/executor.go b/internal/executors/sandbox/executor.go index 188c4b9..92c0eae 100644 --- a/internal/executors/sandbox/executor.go +++ b/internal/executors/sandbox/executor.go @@ -18,6 +18,7 @@ type Sandbox struct { func (e *Sandbox) Run(cmds []stage.Cmd) ([]stage.ExecutorResult, error) { var err error if e.execClient == nil { + slog.Debug("create exec client", "server", e.execServer) e.execClient, err = createExecClient(e.execServer, e.token) if err != nil { return nil, err diff --git a/internal/executors/sandbox/meta.go b/internal/executors/sandbox/meta.go index 2702646..e984184 100644 --- a/internal/executors/sandbox/meta.go +++ b/internal/executors/sandbox/meta.go @@ -8,9 +8,17 @@ var name = "sandbox" func init() { stage.RegisterExecutor(name, &Sandbox{ - // TODO: read from conf execServer: "localhost:5051", token: "", cachedMap: make(map[string]string), }) } + +// overwrite the default registered executor +func InitWithConf(execServer, token string) { + stage.RegisterExecutor(name, &Sandbox{ + execServer: execServer, + token: token, + cachedMap: make(map[string]string), + }) +} diff --git a/scripts/prepare_test_repos.sh b/scripts/prepare_test_repos.sh index fc877b4..21f0558 100755 --- a/scripts/prepare_test_repos.sh +++ b/scripts/prepare_test_repos.sh @@ -2,7 +2,7 @@ set -ex declare -A repo_names -tmp_dir="./tmp" +tmp_dir=${1:-./tmp} submodules_dir="$tmp_dir/submodules" rm -rf $submodules_dir mkdir -p $submodules_dir @@ -24,6 +24,7 @@ for submodule in $submodules; do repo_names[$repo_name]=1 cd $repo_dir git checkout -q $branch + git reset -q --hard origin/$branch cd - submodule_dir="$submodules_dir/$repo_name/$submodule" mkdir -p $submodule_dir diff --git a/scripts/run_foreach_test_repos.sh b/scripts/run_foreach_test_repos.sh new file mode 100755 index 0000000..3c9ce55 --- /dev/null +++ b/scripts/run_foreach_test_repos.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +set -ex +tmp_dir=${1:-./tmp} +JOJ3=$(git rev-parse --show-toplevel)/build/joj3 +command=${2:-$JOJ3} +submodules_dir="$tmp_dir/submodules" +submodules=$(git config --file .gitmodules --get-regexp path | awk '{ print $2 }') +for submodule in $submodules; do + url=$(git config --file .gitmodules --get-regexp "submodule.$submodule.url" | awk '{ print $2 }') + repo_name=$(echo $url | rev | cut -d'/' -f 1 | rev | cut -d'.' -f 1) + submodule_dir="$submodules_dir/$repo_name/$submodule" + cd $submodule_dir + eval "$command" + if [[ $command == $JOJ3 ]]; then + if [ -f "./expected.json" ]; then + mv -f "joj3_result.json" "expected.json" + fi + fi + cd - +done