WIP: feat: scoreboard (#15) #31
							
								
								
									
										10
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								Makefile
									
									
									
									
									
								
							| 
						 | 
					@ -1,19 +1,23 @@
 | 
				
			||||||
.PHONY: all clean test
 | 
					.PHONY: all build clean prepare-test test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
BUILD_DIR = ./build
 | 
					BUILD_DIR = ./build
 | 
				
			||||||
 | 
					TMP_DIR = ./tmp
 | 
				
			||||||
APPS := $(notdir $(wildcard ./cmd/*))
 | 
					APPS := $(notdir $(wildcard ./cmd/*))
 | 
				
			||||||
FLAGS := "-s -w"
 | 
					FLAGS := "-s -w"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
all:
 | 
					all: build
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					build:
 | 
				
			||||||
	$(foreach APP,$(APPS), go build -ldflags=$(FLAGS) -o $(BUILD_DIR)/$(APP) ./cmd/$(APP);)
 | 
						$(foreach APP,$(APPS), go build -ldflags=$(FLAGS) -o $(BUILD_DIR)/$(APP) ./cmd/$(APP);)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
clean:
 | 
					clean:
 | 
				
			||||||
	rm -rf $(BUILD_DIR)/*
 | 
						rm -rf $(BUILD_DIR)/*
 | 
				
			||||||
 | 
						rm -rf $(TMP_DIR)/*
 | 
				
			||||||
	rm -rf *.out
 | 
						rm -rf *.out
 | 
				
			||||||
 | 
					
 | 
				
			||||||
prepare-test:
 | 
					prepare-test:
 | 
				
			||||||
	git submodule update --init --remote
 | 
						git submodule update --init --remote
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test:
 | 
					test:
 | 
				
			||||||
	./scripts/prepare_test_repos.sh
 | 
						./scripts/prepare_test_repos.sh $(TMP_DIR)
 | 
				
			||||||
	go test -coverprofile cover.out -v ./...
 | 
						go test -coverprofile cover.out -v ./...
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,7 +17,7 @@ $ go build -o ./tmp/go-judge ./cmd/go-judge
 | 
				
			||||||
5. Run `go-judge`.
 | 
					5. Run `go-judge`.
 | 
				
			||||||
```bash
 | 
					```bash
 | 
				
			||||||
$ # make sure you are in go-judge directory
 | 
					$ # 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.
 | 
					6. Pull submodules. It might be slow, so only run it when necessary.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,9 +10,11 @@ import (
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Conf struct {
 | 
					type Conf struct {
 | 
				
			||||||
	LogLevel   int    `default:"0"`
 | 
						SandboxExecServer string `default:"localhost:5051"`
 | 
				
			||||||
	OutputPath string `default:"joj3_result.json"`
 | 
						SandboxToken      string `default:""`
 | 
				
			||||||
	Stages     []struct {
 | 
						LogLevel          int    `default:"0"`
 | 
				
			||||||
 | 
						OutputPath        string `default:"joj3_result.json"`
 | 
				
			||||||
 | 
						Stages            []struct {
 | 
				
			||||||
		Name     string
 | 
							Name     string
 | 
				
			||||||
		Executor struct {
 | 
							Executor struct {
 | 
				
			||||||
			Name string
 | 
								Name string
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,7 +5,7 @@ import (
 | 
				
			||||||
	"log/slog"
 | 
						"log/slog"
 | 
				
			||||||
	"os"
 | 
						"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/parsers"
 | 
				
			||||||
	"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage"
 | 
						"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -77,6 +77,7 @@ func main() {
 | 
				
			||||||
		os.Exit(1)
 | 
							os.Exit(1)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	setupSlog(conf)
 | 
						setupSlog(conf)
 | 
				
			||||||
 | 
						executors.InitWithConf(conf.SandboxExecServer, conf.SandboxToken)
 | 
				
			||||||
	stages := generateStages(conf)
 | 
						stages := generateStages(conf)
 | 
				
			||||||
	defer stage.Cleanup()
 | 
						defer stage.Cleanup()
 | 
				
			||||||
	results := stage.Run(stages)
 | 
						results := stage.Run(stages)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -66,7 +66,7 @@ func TestMain(t *testing.T) {
 | 
				
			||||||
	scoreboard := scoreboard.Scoreboard{}
 | 
						scoreboard := scoreboard.Scoreboard{}
 | 
				
			||||||
	scoreboard.Init()
 | 
						scoreboard.Init()
 | 
				
			||||||
	var tests []string
 | 
						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 {
 | 
						err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,8 +2,13 @@ package executors
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/executors/dummy"
 | 
						_ "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
 | 
					// this file does nothing but imports to ensure all the init() functions
 | 
				
			||||||
// in the subpackages are called
 | 
					// in the subpackages are called
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// overwrite the default registered executors
 | 
				
			||||||
 | 
					func InitWithConf(sandboxExecServer, sandboxToken string) {
 | 
				
			||||||
 | 
						sandbox.InitWithConf(sandboxExecServer, sandboxToken)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
package sandbox
 | 
					package sandbox
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"log/slog"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
| 
						 | 
					@ -113,7 +114,12 @@ func convertPBFile(i stage.CmdFile) *pb.Request_File {
 | 
				
			||||||
				i.Src = &absPath
 | 
									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:
 | 
						case i.Content != nil:
 | 
				
			||||||
		s := strToBytes(*i.Content)
 | 
							s := strToBytes(*i.Content)
 | 
				
			||||||
		return &pb.Request_File{File: &pb.Request_File_Memory{Memory: &pb.Request_MemoryFile{Content: s}}}
 | 
							return &pb.Request_File{File: &pb.Request_File_Memory{Memory: &pb.Request_MemoryFile{Content: s}}}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -18,6 +18,7 @@ type Sandbox struct {
 | 
				
			||||||
func (e *Sandbox) Run(cmds []stage.Cmd) ([]stage.ExecutorResult, error) {
 | 
					func (e *Sandbox) Run(cmds []stage.Cmd) ([]stage.ExecutorResult, error) {
 | 
				
			||||||
	var err error
 | 
						var err error
 | 
				
			||||||
	if e.execClient == nil {
 | 
						if e.execClient == nil {
 | 
				
			||||||
 | 
							slog.Debug("create exec client", "server", e.execServer)
 | 
				
			||||||
		e.execClient, err = createExecClient(e.execServer, e.token)
 | 
							e.execClient, err = createExecClient(e.execServer, e.token)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,9 +8,17 @@ var name = "sandbox"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
	stage.RegisterExecutor(name, &Sandbox{
 | 
						stage.RegisterExecutor(name, &Sandbox{
 | 
				
			||||||
		// TODO: read from conf
 | 
					 | 
				
			||||||
		execServer: "localhost:5051",
 | 
							execServer: "localhost:5051",
 | 
				
			||||||
		token:      "",
 | 
							token:      "",
 | 
				
			||||||
		cachedMap:  make(map[string]string),
 | 
							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),
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,7 +2,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
set -ex
 | 
					set -ex
 | 
				
			||||||
declare -A repo_names
 | 
					declare -A repo_names
 | 
				
			||||||
tmp_dir="./tmp"
 | 
					tmp_dir=${1:-./tmp}
 | 
				
			||||||
submodules_dir="$tmp_dir/submodules"
 | 
					submodules_dir="$tmp_dir/submodules"
 | 
				
			||||||
rm -rf $submodules_dir
 | 
					rm -rf $submodules_dir
 | 
				
			||||||
mkdir -p $submodules_dir
 | 
					mkdir -p $submodules_dir
 | 
				
			||||||
| 
						 | 
					@ -24,6 +24,7 @@ for submodule in $submodules; do
 | 
				
			||||||
    repo_names[$repo_name]=1
 | 
					    repo_names[$repo_name]=1
 | 
				
			||||||
    cd $repo_dir
 | 
					    cd $repo_dir
 | 
				
			||||||
    git checkout -q $branch
 | 
					    git checkout -q $branch
 | 
				
			||||||
 | 
					    git reset -q --hard origin/$branch
 | 
				
			||||||
    cd -
 | 
					    cd -
 | 
				
			||||||
    submodule_dir="$submodules_dir/$repo_name/$submodule"
 | 
					    submodule_dir="$submodules_dir/$repo_name/$submodule"
 | 
				
			||||||
    mkdir -p $submodule_dir
 | 
					    mkdir -p $submodule_dir
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										21
									
								
								scripts/run_foreach_test_repos.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										21
									
								
								scripts/run_foreach_test_repos.sh
									
									
									
									
									
										Executable file
									
								
							| 
						 | 
					@ -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
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user