Compare commits
3 Commits
887c06b40e
...
f556801884
Author | SHA1 | Date | |
---|---|---|---|
f556801884 | |||
30a22dba49 | |||
26b128d2c7 |
|
@ -1,11 +1,15 @@
|
|||
import json
|
||||
from pathlib import Path
|
||||
from typing import Tuple, Type, cast
|
||||
from typing import Any, Dict, Tuple, Type, cast
|
||||
|
||||
import inquirer
|
||||
import tomli
|
||||
import yaml
|
||||
from pydantic import BaseModel
|
||||
|
||||
from joj3_config_generator.models import answer, joj1, repo, task
|
||||
from joj3_config_generator.models.common import Memory, Time
|
||||
from joj3_config_generator.utils.logger import logger
|
||||
|
||||
|
||||
def load_joj3_task_toml_answers() -> answer.Answers:
|
||||
|
@ -39,6 +43,115 @@ def load_joj1_yaml(yaml_path: Path) -> joj1.Config:
|
|||
def load_joj3_toml(
|
||||
root_path: Path, repo_toml_path: Path, task_toml_path: Path
|
||||
) -> Tuple[repo.Config, task.Config]:
|
||||
def check_unnecessary_fields(
|
||||
pydantic_model_type: Type[BaseModel],
|
||||
input_dict: Dict[str, Any],
|
||||
file_path: Path,
|
||||
current_path: str = "",
|
||||
) -> None:
|
||||
def format_value_for_toml_warning(value: Any) -> str:
|
||||
if isinstance(value, str):
|
||||
escaped_value = value.replace("\\", "\\\\").replace('"', '\\"')
|
||||
return f'"{escaped_value}"'
|
||||
elif isinstance(value, bool):
|
||||
return str(value).lower()
|
||||
elif isinstance(value, (int, float)):
|
||||
return str(value)
|
||||
elif isinstance(value, Path):
|
||||
escaped_value = str(value).replace("\\", "\\\\").replace('"', '\\"')
|
||||
return f'"{escaped_value}"'
|
||||
elif isinstance(value, list):
|
||||
formatted_elements = [
|
||||
format_value_for_toml_warning(item) for item in value
|
||||
]
|
||||
return f"[{', '.join(formatted_elements)}]"
|
||||
elif isinstance(value, dict):
|
||||
return json.dumps(value, separators=(",", ":"))
|
||||
elif value is None:
|
||||
return "None"
|
||||
else:
|
||||
return repr(value)
|
||||
|
||||
default_instance = pydantic_model_type.model_construct()
|
||||
for field_name, field_info in pydantic_model_type.model_fields.items():
|
||||
should_warn = False
|
||||
full_field_path = (
|
||||
f"{current_path}.{field_name}" if current_path else field_name
|
||||
)
|
||||
toml_field_name = field_name
|
||||
if field_info.alias in input_dict:
|
||||
toml_field_name = field_info.alias
|
||||
if toml_field_name not in input_dict:
|
||||
continue
|
||||
toml_value = input_dict[toml_field_name]
|
||||
default_value = getattr(default_instance, field_name)
|
||||
# Handle List[Pydantic.BaseModel]
|
||||
if (
|
||||
field_info.annotation is not None
|
||||
and hasattr(field_info.annotation, "__origin__")
|
||||
and field_info.annotation.__origin__ is list
|
||||
and hasattr(field_info.annotation, "__args__")
|
||||
and len(field_info.annotation.__args__) == 1
|
||||
and isinstance(field_info.annotation.__args__[0], type)
|
||||
and issubclass(field_info.annotation.__args__[0], BaseModel)
|
||||
):
|
||||
nested_model_type = field_info.annotation.__args__[0]
|
||||
# Ensure the TOML value is a list (as expected for this type)
|
||||
if isinstance(toml_value, list):
|
||||
for i, toml_item in enumerate(toml_value):
|
||||
if isinstance(toml_item, dict):
|
||||
check_unnecessary_fields(
|
||||
nested_model_type,
|
||||
toml_item,
|
||||
file_path,
|
||||
f"{full_field_path}[{i}]",
|
||||
)
|
||||
continue
|
||||
# Handle directly nested Pydantic models (non-list)
|
||||
if isinstance(field_info.annotation, type) and issubclass(
|
||||
field_info.annotation, BaseModel
|
||||
):
|
||||
if isinstance(toml_value, dict):
|
||||
check_unnecessary_fields(
|
||||
field_info.annotation,
|
||||
toml_value,
|
||||
file_path,
|
||||
full_field_path,
|
||||
)
|
||||
continue
|
||||
# Handle Path type
|
||||
elif (
|
||||
isinstance(toml_value, str)
|
||||
and isinstance(default_value, Path)
|
||||
and Path(toml_value) == default_value
|
||||
):
|
||||
should_warn = True
|
||||
# Handle Time type
|
||||
elif isinstance(default_value, Time) and Time(toml_value) == default_value:
|
||||
should_warn = True
|
||||
# Handle Memory type
|
||||
elif (
|
||||
isinstance(default_value, Memory)
|
||||
and Memory(toml_value) == default_value
|
||||
):
|
||||
should_warn = True
|
||||
# Handle non-model list types (e.g., List[str], List[int])
|
||||
elif (
|
||||
isinstance(toml_value, list)
|
||||
and isinstance(default_value, list)
|
||||
and toml_value == default_value
|
||||
):
|
||||
should_warn = True
|
||||
# Handle other basic types (str, int, float, bool, dict)
|
||||
elif toml_value == default_value and toml_value != {}:
|
||||
should_warn = True
|
||||
if should_warn:
|
||||
logger.warning(
|
||||
f"In file {file_path}, unnecessary field "
|
||||
f"`{full_field_path} = {format_value_for_toml_warning(toml_value)}`"
|
||||
" can be removed as it matches the default value"
|
||||
)
|
||||
|
||||
repo_obj = tomli.loads(repo_toml_path.read_text())
|
||||
task_obj = tomli.loads(task_toml_path.read_text())
|
||||
repo_conf = repo.Config(**repo_obj)
|
||||
|
@ -47,4 +160,6 @@ def load_joj3_toml(
|
|||
task_conf = task.Config(**task_obj)
|
||||
task_conf.root = root_path
|
||||
task_conf.path = task_toml_path.relative_to(root_path)
|
||||
check_unnecessary_fields(repo.Config, repo_obj, repo_toml_path)
|
||||
check_unnecessary_fields(task.Config, task_obj, task_toml_path)
|
||||
return repo_conf, task_conf
|
||||
|
|
|
@ -7,7 +7,7 @@ limit.stdout = "65m"
|
|||
parsers = [ "clangtidy", "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.weight = [ 5, 20, 20, 20, 10, 5, 5, 5, 15, 5, 5, 5, 5, 5, 5, 5, 5, 5]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stdout = true
|
||||
result-detail.time = false
|
||||
result-detail.mem = false
|
||||
|
|
|
@ -6,7 +6,7 @@ limit.stderr = "65m"
|
|||
parsers = [ "cppcheck", "result-detail" ]
|
||||
cppcheck.keyword = ["error", "warning", "portability", "performance", "style"]
|
||||
cppcheck.weight = [15, 5, 5, 5, 5]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stderr = true
|
||||
result-detail.time = false
|
||||
result-detail.mem = false
|
||||
|
|
|
@ -6,7 +6,7 @@ limit.stdout = "65m"
|
|||
parsers = ["cpplint", "result-detail"]
|
||||
cpplint.keyword = ["runtime", "readability", "build"]
|
||||
cpplint.weight = [5, 20, 10]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stderr = true
|
||||
result-detail.time = false
|
||||
result-detail.mem = false
|
||||
|
|
|
@ -258,7 +258,11 @@ def get_testcases(
|
|||
testcases = set()
|
||||
for testcases_path in (task_root / task_path).parent.glob("**/*.in"):
|
||||
if not testcases_path.with_suffix(".out").exists():
|
||||
logger.warning(f"Testcase {testcases_path} has no corresponding .out file")
|
||||
logger.warning(
|
||||
f"In file {task_root / task_path}, "
|
||||
f"testcase {testcases_path} has no corresponding .out file, "
|
||||
"skipped"
|
||||
)
|
||||
continue
|
||||
testcases.add(
|
||||
str(
|
||||
|
|
|
@ -14,7 +14,7 @@ score = 1
|
|||
# compile parsers
|
||||
parsers = [ "result-detail", "result-status" ]
|
||||
result-status.comment = "Congratulations! Your code compiled successfully."
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stderr = true
|
||||
result-detail.time = false
|
||||
result-detail.mem = false
|
||||
|
@ -28,7 +28,7 @@ files.import = [ "tools/filelength" ]
|
|||
parsers = [ "keyword", "result-detail" ]
|
||||
keyword.keyword = [ "max", "recommended"]
|
||||
keyword.weight = [ 20, 10 ]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stdout = true
|
||||
result-detail.time = false
|
||||
result-detail.mem = false
|
||||
|
@ -42,7 +42,7 @@ limit.stdout = "4m"
|
|||
parsers = [ "clangtidy", "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.weight = [ 5, 20, 20, 20, 10, 5, 5, 5, 15, 5, 5, 5, 5, 5, 5, 5, 5, 5]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stdout = true
|
||||
result-detail.time = false
|
||||
result-detail.mem = false
|
||||
|
@ -55,7 +55,7 @@ limit.stderr = "8m"
|
|||
parsers = [ "keyword", "cppcheck", "clangtidy", "result-detail", "cpplint", "result-status", "file", "dummy", "diff" ]
|
||||
cppcheck.keyword = ["error", "warning", "portability", "performance", "style"]
|
||||
cppcheck.weight = [15, 5, 5, 5, 5]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stderr = true
|
||||
result-detail.time = false
|
||||
result-detail.mem = false
|
||||
|
@ -68,7 +68,7 @@ limit.stdout = "65m"
|
|||
parsers = [ "cpplint", "result-detail" ]
|
||||
cpplint.keyword = [ "runtime", "readability", "build" ]
|
||||
cpplint.weight = [ 5, 20, 10]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stderr = true
|
||||
result-detail.time = false
|
||||
result-detail.mem = false
|
||||
|
@ -80,7 +80,7 @@ files.import = [ "h7/build/ex2-asan" ]
|
|||
limit.mem = "128m"
|
||||
|
||||
parsers = [ "diff", "result-detail" ]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stderr = true
|
||||
|
||||
# will be removed as long as the name is fixed
|
||||
|
|
|
@ -13,7 +13,7 @@ limit.stdout = "65m"
|
|||
parsers = [ "clangtidy", "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.weight = [ 5, 20, 20, 20, 10, 5, 5, 5, 15, 5, 5, 5, 5, 5, 5, 5, 5, 5]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stdout = true
|
||||
result-detail.time = false
|
||||
result-detail.mem = false
|
||||
|
|
|
@ -12,7 +12,7 @@ limit.stderr = "65m"
|
|||
parsers = [ "cppcheck", "result-detail" ]
|
||||
cppcheck.keyword = ["error", "warning", "portability", "performance", "style"]
|
||||
cppcheck.weight = [15, 5, 5, 5, 5]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stderr = true
|
||||
result-detail.time = false
|
||||
result-detail.mem = false
|
||||
|
|
|
@ -12,7 +12,7 @@ limit.stdout = "65m"
|
|||
parsers = [ "cpplint", "result-detail" ]
|
||||
cpplint.keyword = [ "runtime", "readability", "build" ]
|
||||
cpplint.weight = [ 5, 20, 10]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stderr = true
|
||||
result-detail.time = false
|
||||
result-detail.mem = false
|
||||
|
|
|
@ -14,7 +14,7 @@ limit.stdout = "10m"
|
|||
limit.stderr = "10m"
|
||||
|
||||
parsers = [ "diff", "result-detail" ]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stderr = true
|
||||
|
||||
diff.default_score = 100
|
||||
|
|
|
@ -12,7 +12,7 @@ files.import = [ "tools/filelength" ]
|
|||
parsers = [ "keyword", "result-detail" ]
|
||||
keyword.keyword = [ "max", "recommended"]
|
||||
keyword.weight = [ 20, 10 ]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stdout = true
|
||||
result-detail.time = false
|
||||
result-detail.mem = false
|
||||
|
|
|
@ -10,7 +10,7 @@ command = "./tools/filelength 400 300 *.cpp *.h"
|
|||
files.import = [ "tools/filelength" ]
|
||||
|
||||
parsers = [ "result-detail" ]
|
||||
result-detail.exitstatus = true
|
||||
result-detail.exit_status = true
|
||||
result-detail.stdout = true
|
||||
result-detail.stderr = true
|
||||
result-detail.time = false
|
||||
|
|
2
tests/convert/unnecessary/repo.toml
Normal file
2
tests/convert/unnecessary/repo.toml
Normal file
|
@ -0,0 +1,2 @@
|
|||
force_skip_health_check_on_test = true
|
||||
force_skip_teapot_on_test = true
|
95
tests/convert/unnecessary/task.json
Normal file
95
tests/convert/unnecessary/task.json
Normal file
|
@ -0,0 +1,95 @@
|
|||
{
|
||||
"name": "hw7 ex2",
|
||||
"logPath": "/home/tt/.cache/joj3/joj3.log",
|
||||
"expireUnixTimestamp": 0,
|
||||
"effectiveUnixTimestamp": 0,
|
||||
"actorCsvPath": "/home/tt/.config/joj/students.csv",
|
||||
"maxTotalScore": 100,
|
||||
"stage": {
|
||||
"sandboxExecServer": "172.17.0.1:5051",
|
||||
"sandboxToken": "",
|
||||
"outputPath": "/tmp/joj3_result.json",
|
||||
"stages": [
|
||||
{
|
||||
"name": "[cq] Filelength",
|
||||
"group": "cq",
|
||||
"executor": {
|
||||
"name": "sandbox",
|
||||
"with": {
|
||||
"default": {
|
||||
"args": [
|
||||
"./tools/filelength",
|
||||
"400",
|
||||
"300",
|
||||
"*.cpp",
|
||||
"*.h"
|
||||
],
|
||||
"env": [
|
||||
"PATH=/usr/bin:/bin:/usr/local/bin"
|
||||
],
|
||||
"stdin": {
|
||||
"content": ""
|
||||
},
|
||||
"stdout": {
|
||||
"name": "stdout",
|
||||
"max": 33554432,
|
||||
"pipe": true
|
||||
},
|
||||
"stderr": {
|
||||
"name": "stderr",
|
||||
"max": 33554432,
|
||||
"pipe": true
|
||||
},
|
||||
"cpuLimit": 1000000000,
|
||||
"clockLimit": 2000000000,
|
||||
"memoryLimit": 268435456,
|
||||
"stackLimit": 0,
|
||||
"procLimit": 50,
|
||||
"cpuRateLimit": 0,
|
||||
"cpuSetLimit": "",
|
||||
"copyIn": {
|
||||
"tools/filelength": {
|
||||
"src": "/home/tt/.config/joj/tools/filelength"
|
||||
}
|
||||
},
|
||||
"copyInCached": {},
|
||||
"copyInDir": ".",
|
||||
"copyOut": [
|
||||
"stdout",
|
||||
"stderr"
|
||||
],
|
||||
"copyOutCached": [],
|
||||
"copyOutMax": 0,
|
||||
"copyOutDir": "",
|
||||
"tty": false,
|
||||
"strictMemoryLimit": false,
|
||||
"dataSegmentLimit": false,
|
||||
"addressSpaceLimit": false
|
||||
},
|
||||
"cases": []
|
||||
}
|
||||
},
|
||||
"parsers": [
|
||||
{
|
||||
"name": "result-detail",
|
||||
"with": {
|
||||
"score": 0,
|
||||
"comment": "",
|
||||
"showExxecutorStatus": true,
|
||||
"showExitStatus": true,
|
||||
"showError": false,
|
||||
"showTime": true,
|
||||
"showMemory": true,
|
||||
"showRuntime": true,
|
||||
"showFiles": [],
|
||||
"filesInCodeBlock": true,
|
||||
"maxFileLength": 2048
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"preStages": [],
|
||||
"postStages": []
|
||||
}
|
||||
}
|
15
tests/convert/unnecessary/task.toml
Normal file
15
tests/convert/unnecessary/task.toml
Normal file
|
@ -0,0 +1,15 @@
|
|||
# general task configuration
|
||||
task.name = "hw7 ex2" # task name
|
||||
|
||||
[[stages]]
|
||||
name = "[cq] Filelength"
|
||||
command = "./tools/filelength 400 300 *.cpp *.h"
|
||||
files.import = ["tools/filelength"]
|
||||
|
||||
parsers = ["result-detail"]
|
||||
result-detail.cpu_time = true
|
||||
result-detail.time = true
|
||||
result-detail.mem = true
|
||||
result-detail.stdout = false
|
||||
result-detail.stderr = false
|
||||
result-detail.exit_status = true
|
Loading…
Reference in New Issue
Block a user