feat: migrate repo & init classes

This commit is contained in:
李衍志523370910113 2024-10-23 18:53:41 +08:00
parent 60668f86df
commit b45d65250d
7 changed files with 325 additions and 696 deletions

View File

@ -19,35 +19,76 @@ from joj3_config_generator.lib.task import (
get_executorWithConfig, get_executorWithConfig,
) )
from joj3_config_generator.models import joj1, repo, result, task from joj3_config_generator.models import joj1, repo, result, task
from joj3_config_generator.lib.repo import getHealthcheckConfig, getTeapotConfig
from joj3_config_generator.models import (
Cmd,
CmdFile,
ExecutorConfig,
ExecutorWithConfig,
ParserConfig,
Repo,
ResultConfig,
Stage,
StageConfig,
Task,
TeapotConfig,
)
# FIXME: LLM generated convert function, only for demostration
def convert(repo_conf: repo.Config, task_conf: task.Config) -> result.Config: def convert(repo_conf: repo.Config, task_conf: task.Config) -> result.Config:
# Create the base ResultConf object # Create the base ResultConf object
result_conf = result.Config( result_conf = result.Config(
name=task_conf.task, name=task_conf.task,
# TODO: specify the exact folder difference # TODO: specify the exact folder difference
log_path=f"{task_conf.task.replace(' ', '-')}.log", log_path=f"{task_conf.task.replace(' ', '-')}.log",
# TODO: specify the exact folder difference
log_path=f"{task_conf.task.replace(' ', '-')}.log",
expire_unix_timestamp=( expire_unix_timestamp=(
int(task_conf.release.deadline.timestamp()) int(task_conf.release.deadline.timestamp())
if task_conf.release.deadline if task_conf.release.deadline
else -1 else -1
), ),
stage=result.Stage(stages=[], sandbox_token=repo_conf.sandbox_token), stage=result.Stage(stages=[], sandbox_token=repo_conf.sandbox_token),
teapot=result.Teapot(),
stage=StageConfig(stages=[], sandbox_token=repo_conf.sandbox_token),
teapot=getTeapotConfig(repo_conf, task_conf), teapot=getTeapotConfig(repo_conf, task_conf),
) )
# Construct healthcheck stage # Construct healthcheck stage
healthcheck_stage = getHealthcheckConfig(repo_conf) healthcheck_stage = getHealthcheckConfig(repo_conf, task_conf)
result_conf.stage.stages.append(healthcheck_stage) result_conf.stage.stages.append(healthcheck_stage)
cached: list[str] = [] cached = []
# Convert each stage in the task configuration # Convert each stage in the task configuration
for task_stage in task_conf.stages: for task_stage in task_conf.stages:
executor_with_config, cached = get_executorWithConfig(task_stage, cached) executor_with_config = result.ExecutorWith(
conf_stage = get_conf_stage(task_stage, executor_with_config) default=result.Cmd(
conf_stage = fix_result_detail(task_stage, conf_stage) args=task_stage.command.split(),
conf_stage = fix_dummy(task_stage, conf_stage) copy_in={
conf_stage = fix_keyword(task_stage, conf_stage) file: result.CmdFile(src=file) for file in task_stage.files.import_
conf_stage = fix_diff(task_stage, conf_stage) },
copy_out_cached=task_stage.files.export,
),
cases=[], # You can add cases if needed
)
conf_stage = result.StageDetail(
name=task_stage.name,
group=task_conf.task,
executor=result.Executor(
name="sandbox",
with_=executor_with_config,
),
parsers=[
result.Parser(name=parser, with_={}) for parser in task_stage.parsers
],
)
if "result-detail" in task_stage.parsers:
result_detail_parser = next(
p for p in conf_stage.parsers if p.name == "result-detail"
)
if task_stage.result_detail is not None:
result_detail_parser.with_.update(task_stage.result_detail)
result_conf.stage.stages.append(conf_stage) result_conf.stage.stages.append(conf_stage)
return result_conf return result_conf

View File

@ -0,0 +1,11 @@
from joj3_config_generator.models.repo import Repo as Repo
from joj3_config_generator.models.result import Cmd as Cmd
from joj3_config_generator.models.result import CmdFile as CmdFile
from joj3_config_generator.models.result import ExecutorConfig as ExecutorConfig
from joj3_config_generator.models.result import ExecutorWithConfig as ExecutorWithConfig
from joj3_config_generator.models.result import ParserConfig as ParserConfig
from joj3_config_generator.models.result import ResultConfig as ResultConfig
from joj3_config_generator.models.result import Stage as Stage
from joj3_config_generator.models.result import StageConfig as StageConfig
from joj3_config_generator.models.result import TeapotConfig as TeapotConfig
from joj3_config_generator.models.task import Task as Task

View File

@ -1,18 +1,40 @@
import hashlib import hashlib
import shlex import os
import socket import tempfile
from pathlib import Path
from joj3_config_generator.models import joj1, repo, result, task from dotenv import load_dotenv
from joj3_config_generator.models import (
Cmd,
CmdFile,
ExecutorConfig,
ExecutorWithConfig,
ParserConfig,
Repo,
ResultConfig,
Stage,
StageConfig,
Task,
TeapotConfig,
)
def get_temp_directory() -> str:
return tempfile.mkdtemp(prefix="repo-checker-")
def getGradingRepoName() -> str: def getGradingRepoName() -> str:
host_name = socket.gethostname() path = os.path.expanduser("~/.config/teapot/teapot.env")
return f"{host_name.split('-')[0]}-joj" if os.path.exists(path):
load_dotenv(path)
repo_name = os.environ.get("GITEA_ORG_NAME")
if repo_name is not None:
return f"{repo_name.split('-')[0]}-joj"
return "ece482-joj"
def getTeapotConfig(repo_conf: repo.Config, task_conf: task.Config) -> result.Teapot: def getTeapotConfig(repo_conf: Repo, task_conf: Task) -> TeapotConfig:
teapot = result.Teapot( teapot = TeapotConfig(
# TODO: fix the log path # TODO: fix the log path
log_path=f"{task_conf.task.replace(' ', '-')}-joint-teapot-debug.log", log_path=f"{task_conf.task.replace(' ', '-')}-joint-teapot-debug.log",
scoreboard_path=f"{task_conf.task.replace(' ', '-')}-scoreboard.csv", scoreboard_path=f"{task_conf.task.replace(' ', '-')}-scoreboard.csv",
@ -22,7 +44,7 @@ def getTeapotConfig(repo_conf: repo.Config, task_conf: task.Config) -> result.Te
return teapot return teapot
def getHealthcheckCmd(repo_conf: repo.Config) -> result.Cmd: def getHealthcheckCmd(repo_conf: Repo) -> Cmd:
repoSize = repo_conf.max_size repoSize = repo_conf.max_size
immutable = repo_conf.files.immutable immutable = repo_conf.files.immutable
repo_size = f"-repoSize={str(repoSize)} " repo_size = f"-repoSize={str(repoSize)} "
@ -38,7 +60,7 @@ def getHealthcheckCmd(repo_conf: repo.Config) -> result.Cmd:
else: else:
immutable_files = immutable_files + name + "," immutable_files = immutable_files + name + ","
# FIXME: need to make solution and make things easier to edit with global scope # FIXME: need to make solution and make things easier to edit with global scope
chore = f"./repo-health-checker -root=. " chore = f"/{get_temp_directory}/repo-health-checker -root=. "
args = "" args = ""
args = args + chore args = args + chore
args = args + repo_size args = args + repo_size
@ -49,25 +71,27 @@ def getHealthcheckCmd(repo_conf: repo.Config) -> result.Cmd:
args = args + immutable_files args = args + immutable_files
cmd = result.Cmd( cmd = Cmd(
args=shlex.split(args), args=args.split(),
# FIXME: easier to edit within global scope # FIXME: easier to edit within global scope
copy_in={ copy_in={
f"./repo-health-checker": result.CmdFile(src=f"./repo-health-checker") f"/{get_temp_directory()}/repo-health-checker": CmdFile(
src=f"/{get_temp_directory()}/repo-health-checker"
)
}, },
) )
return cmd return cmd
def getHealthcheckConfig(repo_conf: repo.Config) -> result.StageDetail: def getHealthcheckConfig(repo_conf: Repo, task_conf: Task) -> Stage:
healthcheck_stage = result.StageDetail( healthcheck_stage = Stage(
name="healthcheck", name="healthcheck",
group="", group="",
executor=result.Executor( executor=ExecutorConfig(
name="sandbox", name="sandbox",
with_=result.ExecutorWith(default=getHealthcheckCmd(repo_conf), cases=[]), with_=ExecutorWithConfig(default=getHealthcheckCmd(repo_conf), cases=[]),
), ),
parsers=[result.Parser(name="healthcheck", with_={"score": 0, "comment": ""})], parsers=[ParserConfig(name="healthcheck", with_={"score": 0, "comment": ""})],
) )
return healthcheck_stage return healthcheck_stage
@ -81,10 +105,7 @@ def calc_sha256sum(file_path: str) -> str:
def get_hash(immutable_files: list[str]) -> str: # input should be a list def get_hash(immutable_files: list[str]) -> str: # input should be a list
# FIXME: should be finalized when get into the server file_path = "../immutable_file/" # TODO: change this when things are on the server
current_file_path = Path(__file__).resolve()
project_root = current_file_path.parents[2]
file_path = f"{project_root}/tests/immutable_file/"
immutable_hash = [] immutable_hash = []
for i, file in enumerate(immutable_files): for i, file in enumerate(immutable_files):
immutable_files[i] = file_path + file.rsplit("/", 1)[-1] immutable_files[i] = file_path + file.rsplit("/", 1)[-1]

View File

@ -14,31 +14,17 @@ class ParserResultDetail(BaseModel):
class ParserDummy(BaseModel): class ParserDummy(BaseModel):
comment: Optional[str] = "" comment: Optional[str] = ""
score: Optional[int] = 0
forcequit: Optional[bool] = True
class ParserKeyword(BaseModel): class ParserKeyword(BaseModel):
keyword: Optional[list[str]] = [] keyword: Optional[list[str]] = None
weight: Optional[list[int]] = [] weight: Optional[list[int]] = None
class Outputs(BaseModel):
score: Optional[int] = 0
ignorespaces: Optional[bool] = False
hide: Optional[bool] = False
forcequit: Optional[bool] = True
class ParserDiff(BaseModel):
output: Optional[Outputs] = Outputs()
class Files(BaseModel): class Files(BaseModel):
import_: Optional[List[str]] = Field( import_: Optional[List[str]] = Field(serialization_alias="import", validation_alias="import")
[], serialization_alias="import", validation_alias="import" export: Optional[List[str]]
)
export: Optional[List[str]] = []
class Limit(BaseModel): class Limit(BaseModel):
@ -49,36 +35,20 @@ class Limit(BaseModel):
class Stage(BaseModel): class Stage(BaseModel):
name: Optional[str] = None # Stage name name: str # Stage name
command: Optional[str] = None # Command to run command: str # Command to run
files: Optional[Files] = None files: Optional[Files] = None
score: Optional[int] = 0 score: Optional[int] = 0
parsers: Optional[list[str]] = [] # list of parsers parsers: list[str] # list of parsers
limit: Optional[Limit] = Limit() limit: Optional[Limit] = None
dummy: Optional[ParserDummy] = ParserDummy() dummy: Optional[ParserDummy] = ParserDummy()
result_status: Optional[ParserDummy] = Field(ParserDummy(), alias="result-status")
keyword: Optional[ParserKeyword] = ParserKeyword() keyword: Optional[ParserKeyword] = ParserKeyword()
clangtidy: Optional[ParserKeyword] = ParserKeyword() clangtidy: Optional[ParserKeyword] = ParserKeyword()
cppcheck: Optional[ParserKeyword] = ParserKeyword() cppcheck: Optional[ParserKeyword] = ParserKeyword()
# FIXME: determine cpplint type
cpplint: Optional[ParserKeyword] = ParserKeyword() cpplint: Optional[ParserKeyword] = ParserKeyword()
result_detail: Optional[ParserResultDetail] = Field( result_detail: Optional[ParserResultDetail] = Field(
ParserResultDetail(), alias="result-detail" ParserResultDetail(), alias="result-detail"
) )
skip: Optional[list[str]] = []
diff: Optional[ParserDiff] = ParserDiff()
cases: Optional[Dict[str, "Stage"]] = {}
class Config:
extra = "allow"
@root_validator(pre=True)
def gather_cases(cls: Type["Stage"], values: Dict[str, Any]) -> Dict[str, Any]:
cases = {k: v for k, v in values.items() if k.startswith("case")}
for key in cases:
values.pop(key)
values["cases"] = {k: Stage(**v) for k, v in cases.items()}
return values
class Release(BaseModel): class Release(BaseModel):

View File

@ -6,5 +6,5 @@ sandbox_token = "test"
[files] [files]
whitelist_patterns = ["*.py", "*.txt", "*.md"] whitelist_patterns = ["*.py", "*.txt", "*.md"]
whitelist_file = ".whitelist" whitelist_file = ".whitelist"
required = [ "Changelog.md", "Readme.md" ] required = ["main.py", "README.md"]
immutable = [".gitignore", ".gitattributes", ".gitea/workflows/push.yaml", ".gitea/workflows/release.yaml" ] immutable = [".gitignore", ".gitattributes", "push.yaml", "release.yaml"]

File diff suppressed because it is too large Load Diff

View File

@ -1,31 +1,20 @@
# p2 repo config # general task configuration
task="Homework 1 exercise 2" # task name
task="p2 m3" # task name
release.deadline = 2024-10-12 23:59:00+08:00 release.deadline = 2024-10-12 23:59:00+08:00
release.stages = [ "compile" ] release.stages = [ "compile" ]
[files]
immutable = [".gitignore", ".gitattributes", ".gitea/workflows/push.yaml", ".gitea/workflows/release.yaml" ]
required = [ "Changelog.md", "Readme.md" ]
[[stages]]
name = "Abuse of strings detected"
command = "./strdetect src/"
files.import = [ "tools/strdetec" ]
parsers = [ "result-status" ]
[[stages]] [[stages]]
name = "Compilation" name = "Compilation"
command = "compile" command = "make.sh" # eg. script running cmake commands
files.import = [ "tools/compile" ] files.import = [ "tools/make.sh", "src/main.c", "src/task.h", "srcCMakelist.txt" ]
files.export = [ "build/onecard", "build/asan", "build/ubsan", "build/msan", "build/compile_commands.json" ] files.export = [ "driver", "p2", "p2-msan" ]
limit.cpu = 180 # p2 takes long to compile
limit.stderr = 128
# compile parsers
parsers = [ "result-detail", "dummy", "result-status" ] parsers = [ "result-detail", "dummy", "result-status" ]
result-status.comment = "Congratulations! Your code compiled successfully." result-status.comment = "Congratulations! Your code compiled successfully."
result-status.score = 1
dummy.comment = "\n\n### Details\n" dummy.comment = "\n\n### Details\n"
result-detail.exitstatus = true result-detail.exitstatus = true
result-detail.stderr = true result-detail.stderr = true
@ -33,28 +22,26 @@ result-detail.time = false
result-detail.mem = false result-detail.mem = false
[[stages]] [[stages]]
name = "[cq] Filelength" name = "File length check"
command = "./file-length 400 300 *.c *.h" command = "./file-length 500 400 *.c *.h" # command to run
files.import = [ "tools/filelength" ] files.import = [ "tools/file-length" ]
parsers = [ "keyword", "dummy", "result-detail" ] parsers = [ "keyword", "dummy", "result-detail" ]
keyword.keyword = [ "max", "recommended"] keyword.keyword = [ "max", "recommend"] # keywords caught by corresponding JOJ plugin
keyword.weight = [ 20, 10 ] keyword.weight = [ 50, 20 ] # weight of each keyword
dummy.comment = "\n\n### Details\n" result-detail.exitstatus = false
result-detail.exitstatus = true result-detail.stderr = true
result-detail.stdout = true
result-detail.time = false result-detail.time = false
result-detail.mem = false result-detail.mem = false
[[stages]] [[stages]]
name = "[cq] Clang-tidy" name = "Clang-tidy checks"
command = "run-clang-tidy-18 -header-filter=.* -quiet -load=/usr/local/lib/libcodequality.so -p build" command = "run-clang-tidy-18 -header-filter=.* -quiet -load=/usr/local/lib/libcodequality.so -p build"
files.import = [ "projects/p2/.clang-tidy", "build/compile_commands.json" ]
limit.stdout = 65 limit.stdout = 65
parsers = [ "clangtidy", "dummy", "result-detail" ] parsers = [ "clangtidy", "dummy", "result-detail" ]
clangtidy.keyword = [ "codequality-unchecked-malloc-result", "codequality-no-global-variables", "codequality-no-header-guard", "codequality-no-fflush-stdin", "readability-function-size", "readability-duplicate-include", "readability-identifier-naming", "readability-redundant", "readability-misleading-indentation", "readability-misplaced-array-index", "cppcoreguidelines-init-variables", "bugprone-suspicious-string-compare", "google-global-names-in-headers", "clang-diagnostic", "clang-analyzer", "misc", "performance", "portability" ] clangtidy.keyword = [ "codequality-no-global-variables", "codequality-no-header-guard", "readability-function-size", "readability-duplicate-include", "readability-identifier-naming", "readability-redundant", "readability-misleading-indentation", "readability-misplaced-array-index", "cppcoreguidelines-init-variables", "bugprone-suspicious-string-compare", "google-global-names-in-headers", "clang-diagnostic", "clang-analyzer", "misc performance" ]
clangtidy.weight = [ 5, 20, 20, 20, 10, 5, 5, 5, 15, 5, 5, 5, 5, 5, 5, 5, 5, 5] clangtidy.weight = [10, 10, 50, 10, 5, 5, 10, 5, 5, 8, 5, 5, 5, 5, 8]
dummy.comment = "\n\n### Details\n" dummy.comment = "\n\n### Details\n"
result-detail.exitstatus = true result-detail.exitstatus = true
result-detail.stdout = true result-detail.stdout = true
@ -62,13 +49,13 @@ result-detail.time = false
result-detail.mem = false result-detail.mem = false
[[stages]] [[stages]]
name = "[cq] Cppcheck" name = "Cppcheck check"
command = "cppcheck --template='{\"file\":\"{file}\",\"line\":{line}, \"column\":{column}, \"severity\":\"{severity}\", \"message\":\"{message}\", \"id\":\"{id}\"}' --force --enable=all --suppress=missingIncludeSystem --quiet ./" command = "cppcheck --template='{\"file\":\"{file}\",\"line\":{line}, \"column\":{column}, \"severity\":\"{severity}\", \"message\":\"{message}\", \"id\":\"{id}\"}' --force --enable=all --quiet ./"
limit.stderr = 65 limit.stderr = 65
parsers = [ "cppcheck", "dummy", "result-detail" ] parsers = [ "cppcheck", "dummy", "result-detail" ]
cppcheck.keyword = ["error", "warning", "portability", "performance", "style"] cppcheck.keyword = ["error", "warning", "portability", "performance", "style"]
cppcheck.weight = [15, 5, 5, 5, 5] cppcheck.weight = [20, 10, 15, 15, 10]
dummy.comment = "\n\n### Details\n" dummy.comment = "\n\n### Details\n"
result-detail.exitstatus = true result-detail.exitstatus = true
result-detail.stderr = true result-detail.stderr = true
@ -76,62 +63,57 @@ result-detail.time = false
result-detail.mem = false result-detail.mem = false
[[stages]] [[stages]]
name = "[cq] Cpplint" name = "Cpplint check"
command = "cpplint --linelength=120 --filter=-legal,-readability/casting,-whitespace,-runtime/printf,-runtime/threadsafe_fn,-readability/todo,-build/include_subdir,-build/header_guard --recursive --exclude=build ." command = "cpplint --linelength=120 --filter=-legal,-readability/casting,-whitespace,-runtime/printf,-runtime/threadsafe_fn,-readability/todo,-build/include_subdir,-build/header_guard --recursive --exclude=build ."
limit.stdout = 65 limit.stdout = 65
parsers = [ "cpplint", "dummy", "result-detail" ] parsers = [ "cpplint", "dummy", "result-detail" ]
cpplint.keyword = [ "runtime", "readability", "build" ] cpplint.keyword = [ "runtime", "readability", "build" ]
cpplint.weight = [ 5, 20, 10] cpplint.weight = [ 10, 20, 15]
dummy.comment = "\n\n### Details\n" dummy.comment = "\n\n### Details\n"
result-detail.exitstatus = true result-detail.exitstatus = true
result-detail.stderr = true result-detail.stdout = true
result-detail.time = false result-detail.time = false
result-detail.mem = false result-detail.mem = false
[[stages]] [[stages]]
name = "[run] onecard" name = "judge-base"
group = "run" command="./driver ./mumsh"
command="./onecard -a" limit.cpu = 3
files.import = [ "build/onecard" ] limit.mem = 75
score = 10
parsers = [ "result-status", "result-detail" ] parsers = ["diff", "dummy", "result-detail"]
result-status.score = 1 dummy.comment = "\n\n### Details\n"
result-status.forcequit = false
result-detail.exitstatus = true result-detail.exitstatus = true
result-detail.stderr = true result-detail.stderr = true
case4.score = 15
case4.limit.cpu = 30
case4.limit.mem = 10
case4.limit.stdout = 8
case5.score = 25
case8.limit.stderr = 128
[[stages]] [[stages]]
name = "[run] address sanitizer" name = "judge-msan"
group = "run" command="./driver ./mumsh-msan"
command="./asan -a" limit.cpu = 10 # default cpu limit (in sec) for each test case
files.import = [ "build/asan" ] limit.mem = 500 # set default mem limit (in MB) for all OJ test cases
score = 10
skip = ["case0", "case11"]
parsers = [ "result-status", "result-detail" ] parsers = ["diff", "dummy", "result-detail"]
result-status.score = 1 dummy.comment = "\n\n### Details\n"
result-status.forcequit = false
result-detail.exitstatus = true result-detail.exitstatus = true
result-detail.stderr = true result-detail.stderr = true
[[stages]] case4.score = 15
name = "[run] memory sanitizer" case4.limit.cpu = 30
group = "run" case4.limit.mem = 10
command="./msan -a"
files.import = [ "build/msan" ]
parsers = [ "result-status", "result-detail" ] case5.diff.output.ignorespaces = false
result-status.score = 1
result-status.forcequit = false
result-detail.exitstatus = true
result-detail.stderr = true
[[stages]] case6.diff.output.hide = true
name = "[run] undefined behavior sanitizer"
command="./ubsan -a"
files.import = [ "build/ubsan" ]
parsers = [ "result-status", "result-detail" ]
result-status.score = 1
result-status.forcequit = false
result-detail.exitstatus = true
result-detail.stderr = true