refactor/main #11

Merged
李衍志523370910113 merged 23 commits from refactor/main into dev 2025-02-27 15:51:32 +08:00
14 changed files with 114 additions and 136 deletions
Showing only changes of commit c19801f9a1 - Show all commits

View File

@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: https://gitea.com/BoYanZh/checkout@focs
uses: actions/checkout@focs
- name: Display Python3 version
run: python3 --version
- name: Install PDM

View File

@ -15,6 +15,7 @@
3. Change dir to the repo, `cd JOJ3-config-generator`
4. Install deps by `pdm install && pdm run pre-commit install`
5. Run the cli app by `pdm run app --help`
<<<<<<< HEAD
## How to use?
@ -28,3 +29,6 @@
```shell
joj3-config-generator convert -d -c /home/tt/.config/joj/ -r immutable_files
```
=======
6. Check other commands or scripts with `pdm run --list`
>>>>>>> master

View File

@ -22,9 +22,7 @@ from joj3_config_generator.processers.task import (
)
def convert(
repo_conf: repo.Config, task_conf: task.Config, repo_root: Path
) -> result.Config:
def convert(repo_conf: repo.Config, task_conf: task.Config) -> result.Config:
# Create the base ResultConf object
result_conf = result.Config(
name=task_conf.task.name,
@ -49,7 +47,7 @@ def convert(
not repo_conf.force_skip_heatlh_check_on_test
or os.environ.get("PYTEST_CURRENT_TEST") is None
):
healthcheck_stage = get_healthcheck_config(repo_conf, repo_root)
healthcheck_stage = get_healthcheck_config(repo_conf)
result_conf.stage.stages.append(healthcheck_stage)
stages: List[str] = []
# Convert each stage in the task configuration
@ -86,9 +84,7 @@ def distribute_json(folder_path: str, repo_obj: Any, repo_conf: Path) -> None:
with open(toml_file_path) as toml_file:
task_toml = toml_file.read()
task_obj = rtoml.loads(task_toml)
result_model = convert(
repo.Config(**repo_obj), task.Config(**task_obj), repo_conf
)
result_model = convert(repo.Config(**repo_obj), task.Config(**task_obj))
result_dict = result_model.model_dump(by_alias=True, exclude_none=True)
with open(json_file_path, "w") as result_file:

View File

@ -1,21 +1,37 @@
import json
import os
from pathlib import Path
from typing import Any, Dict
import inquirer
import rtoml
import typer
import yaml
from typing_extensions import Annotated
from joj3_config_generator.convert import convert as convert_conf
from joj3_config_generator.convert import convert_joj1 as convert_joj1_conf
from joj3_config_generator.convert import distribute_json
from joj3_config_generator.models import joj1, repo, task
from joj3_config_generator.utils.logger import logger
app = typer.Typer(add_completion=False)
@app.command()
def create(toml: typer.FileTextWrite) -> None:
"""
Create a new JOJ3 toml config file
"""
logger.info("Creating")
questions = [
inquirer.List(
"size",
message="What size do you need?",
choices=["Jumbo", "Large", "Standard", "Medium", "Small", "Micro"],
),
]
answers = inquirer.prompt(questions)
logger.info(answers)
@app.command()
def convert_joj1(yaml_file: typer.FileText, toml_file: typer.FileTextWrite) -> None:
"""
@ -25,60 +41,42 @@ def convert_joj1(yaml_file: typer.FileText, toml_file: typer.FileTextWrite) -> N
joj1_obj = yaml.safe_load(yaml_file.read())
joj1_model = joj1.Config(**joj1_obj)
task_model = convert_joj1_conf(joj1_model)
result_dict = task_model.model_dump(by_alias=True, exclude_none=True)
result_dict = task_model.model_dump(by_alias=True)
toml_file.write(rtoml.dumps(result_dict))
@app.command()
def convert(
root: Path = typer.Option(
Path("."),
"--conf-root",
"-c",
help="This is where you want to put all your 'task.toml' type folders, default choice for your input can be '/home/tt/.config/joj/'",
),
repo_path: Path = typer.Option(
Path("."),
"--repo-root",
"-r",
help="This would be where you put your 'repo.toml' file as well as your 'immutable files', they should all be at same place, default choice for your input can be 'immutable_files', which is the folder at the position '/home/tt/.config/joj/'",
),
distribute: bool = typer.Option(
False, "--distribute", "-d", help="This flag determine whether to distribute"
),
root: Annotated[
Path,
typer.Argument(
help="root directory of config files, "
"located at /home/tt/.config/joj in JTC"
),
] = Path(".")
) -> None:
"""
Convert given dir of JOJ3 toml config files to JOJ3 json config files
"""
logger.info(f"Converting files in {root.absolute()}")
if distribute is False:
repo_toml_path = os.path.join(repo_path.absolute(), "basic", "repo.toml")
else:
repo_toml_path = os.path.join("/home/tt/.config/joj", repo_path, "repo.toml")
repo_toml_path = os.path.join(repo_path, "repo.toml")
with open(repo_toml_path, encoding=None) as repo_file:
repo_toml = repo_file.read()
repo_obj = rtoml.loads(repo_toml)
if distribute is False:
task_toml_path = os.path.join(root.absolute(), "basic", "task.toml")
result_json_path = os.path.join(root.absolute(), "basic", "task.json")
with open(task_toml_path, encoding=None) as task_file:
task_toml = task_file.read()
task_obj = rtoml.loads(task_toml)
result_model = convert_conf(
repo.Config(**repo_obj), task.Config(**task_obj), repo_path
)
result_dict = result_model.model_dump(by_alias=True, exclude_none=True)
with open(result_json_path, "w", encoding=None) as result_file:
json.dump(result_dict, result_file, ensure_ascii=False, indent=4)
result_file.write("\n")
# distribution on json
# need a get folder path function
else:
folder_path = "/home/tt/.config/joj"
folder_path = f"{Path.home()}/Desktop/engr151-joj/home/tt/.config/joj/homework"
folder_path = f"{Path.home()}/Desktop/FOCS/JOJ3-config-generator/tests/convert/"
# to be used in real action
folder_path = f"{root}"
distribute_json(folder_path, repo_obj, repo_path)
for repo_toml_path in root.glob("**/repo.toml"):

@bomingzh can we consder here to make repo_conf.path to store the abosolute value?

@bomingzh can we consder here to make `repo_conf.path` to store the abosolute value?
repo_path = repo_toml_path.parent
repo_obj = rtoml.loads(repo_toml_path.read_text())
for task_toml_path in repo_path.glob("**/*.toml"):
if repo_toml_path == task_toml_path:
continue
toml_name = task_toml_path.name.removesuffix(".toml")
result_json_path = task_toml_path.parent / f"{toml_name}.json"
logger.info(
f"Converting {repo_toml_path} & {task_toml_path} to {result_json_path}"
)
task_obj = rtoml.loads(task_toml_path.read_text())
repo_conf = repo.Config(**repo_obj)
repo_conf.path = repo_toml_path
task_conf = task.Config(**task_obj)

repo_conf.path as absolute

`repo_conf.path` as absolute
task_conf.path = task_toml_path
result_model = convert_conf(repo_conf, task_conf)

task_conf.path as absolute

`task_conf.path` as absolute
result_dict = result_model.model_dump(by_alias=True, exclude_none=True)
with result_json_path.open("w") as result_file:
json.dump(result_dict, result_file, ensure_ascii=False, indent=4)
result_file.write("\n")

View File

@ -1,4 +1,5 @@
from typing import List
from pathlib import Path
from typing import List, Optional
from pydantic import BaseModel, Field
@ -21,3 +22,4 @@ class Config(BaseModel):
max_total_score: int = Field(100)
force_skip_heatlh_check_on_test: bool = False
groups: Groups = Groups()
path: Path = Path(".")

View File

@ -1,7 +1,8 @@
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional, Type
from pydantic import BaseModel, Field, model_serializer, model_validator
from pydantic import BaseModel, Field, model_validator
class ParserResultDetail(BaseModel):
@ -109,4 +110,5 @@ class Task(BaseModel):
class Config(BaseModel):
task: Task
release: Release
path: Path = Path(".")
stages: List[Stage] # list of stage configurations

View File

@ -37,7 +37,7 @@ def get_teapot_stage(repo_conf: repo.Config) -> result.StageDetail:
return stage_conf
def get_healthcheck_args(repo_conf: repo.Config, repo_root: Path) -> str:
def get_healthcheck_args(repo_conf: repo.Config) -> str:
repoSize = repo_conf.max_size
immutable = repo_conf.files.immutable
repo_size = f"-repoSize={str(repoSize)} "
@ -59,7 +59,7 @@ def get_healthcheck_args(repo_conf: repo.Config, repo_root: Path) -> str:
for meta in required_files:
args = args + meta
args = args + get_hash(immutable, repo_root)
args = args + get_hash(immutable, repo_conf)
args = args + immutable_files
@ -84,9 +84,7 @@ def get_debug_args(repo_conf: repo.Config) -> str:
return args
def get_healthcheck_config(
repo_conf: repo.Config, repo_root: Path
) -> result.StageDetail:
def get_healthcheck_config(repo_conf: repo.Config) -> result.StageDetail:
healthcheck_stage = result.StageDetail(
name="healthcheck",
group="",
@ -96,7 +94,7 @@ def get_healthcheck_config(
default=result.Cmd(),
cases=[
result.OptionalCmd(
args=shlex.split(get_healthcheck_args(repo_conf, repo_root)),
args=shlex.split(get_healthcheck_args(repo_conf)),
),
result.OptionalCmd(
args=shlex.split(get_debug_args(repo_conf)),
@ -122,7 +120,7 @@ def calc_sha256sum(file_path: str) -> str:
def get_hash(
immutable_files: list[str], repo_root: Path
immutable_files: list[str], repo_conf: repo.Config
) -> str: # input should be a list
# FIXME: should be finalized when get into the server
current_file_path = Path(__file__).resolve()
@ -131,7 +129,7 @@ def get_hash(
file_path = f"{project_root}/tests/immutable_file/"
# file_path = "{Path.home()}/.cache/immutable"
# to be use
# file_path = f"/home/tt/.config/joj/{repo_root}/"
# file_path = repo_conf.path
immutable_hash = []
for i, file in enumerate(immutable_files):
immutable_files[i] = file_path + file.rsplit("/", 1)[-1]

View File

@ -7,7 +7,7 @@
"maxTotalScore": 100,
"stage": {
"sandboxExecServer": "172.17.0.1:5051",
"sandboxToken": "test",
"sandboxToken": "",
"outputPath": "/tmp/joj3_result.json",
"stages": [
{
@ -60,11 +60,8 @@
"args": [
"/usr/local/bin/repo-health-checker",
"-root=.",
"-repoSize=50.5",
"-meta=README.md",
"-meta=Changelog.md",
"-checkFileSumList=a5b63323a692d3d8b952442969649b4f823d58dae26429494f613df160710dfc,b1bbad25b830db0a77b15a033f9ca1b7ab44c1d2d05056412bd3e4421645f0bf,f6740081487ca34963a005209e2e9adfdf6f3561719af082d40fe80145e0cceb,bbeca1491c2f8364821a328a6677c0c5d59ccd60250abac3cec0887eeb9bde3e",
"-checkFileNameList=.gitignore,.gitattributes,.gitea/workflows/push.yaml,.gitea/workflows/release.yaml"
"-repoSize=10",
"-checkFileSumList=-checkFileNameList="
],
"env": [
"PATH=/usr/bin:/bin:/usr/local/bin"
@ -86,7 +83,7 @@
"--grading-repo-name",
"ece280-joj",
"--group-config",
"joj=1000:24,run=1000:24,=100:24"
"=100:24"
],
"env": [
"LOG_FILE_PATH=/home/tt/.cache/joint-teapot-debug.log"
@ -159,11 +156,11 @@
"cpuSetLimit": "",
"copyIn": {
"tests/homework/h7/.clang-tidy": {
"src": "/home/tt/.config/joj/tools/tests/homework/h7/.clang-tidy",
"src": "/home/tt/.config/joj/tests/homework/h7/.clang-tidy",
"max": 419430400
},
"h7/build/compile_commands.json": {
"src": "/home/tt/.config/joj/tools/h7/build/compile_commands.json",
"src": "/home/tt/.config/joj/h7/build/compile_commands.json",
"max": 419430400
}
},

View File

@ -7,7 +7,7 @@
"maxTotalScore": 100,
"stage": {
"sandboxExecServer": "172.17.0.1:5051",
"sandboxToken": "test",
"sandboxToken": "",
"outputPath": "/tmp/joj3_result.json",
"stages": [
{
@ -60,11 +60,8 @@
"args": [
"/usr/local/bin/repo-health-checker",
"-root=.",
"-repoSize=50.5",
"-meta=README.md",
"-meta=Changelog.md",
"-checkFileSumList=a5b63323a692d3d8b952442969649b4f823d58dae26429494f613df160710dfc,b1bbad25b830db0a77b15a033f9ca1b7ab44c1d2d05056412bd3e4421645f0bf,f6740081487ca34963a005209e2e9adfdf6f3561719af082d40fe80145e0cceb,bbeca1491c2f8364821a328a6677c0c5d59ccd60250abac3cec0887eeb9bde3e",
"-checkFileNameList=.gitignore,.gitattributes,.gitea/workflows/push.yaml,.gitea/workflows/release.yaml"
"-repoSize=10",
"-checkFileSumList=-checkFileNameList="
],
"env": [
"PATH=/usr/bin:/bin:/usr/local/bin"
@ -86,7 +83,7 @@
"--grading-repo-name",
"ece280-joj",
"--group-config",
"joj=1000:24,run=1000:24,=100:24"
"=100:24"
],
"env": [
"LOG_FILE_PATH=/home/tt/.cache/joint-teapot-debug.log"

View File

@ -7,7 +7,7 @@
"maxTotalScore": 100,
"stage": {
"sandboxExecServer": "172.17.0.1:5051",
"sandboxToken": "test",
"sandboxToken": "",
"outputPath": "/tmp/joj3_result.json",
"stages": [
{
@ -60,11 +60,8 @@
"args": [
"/usr/local/bin/repo-health-checker",
"-root=.",
"-repoSize=50.5",
"-meta=README.md",
"-meta=Changelog.md",
"-checkFileSumList=a5b63323a692d3d8b952442969649b4f823d58dae26429494f613df160710dfc,b1bbad25b830db0a77b15a033f9ca1b7ab44c1d2d05056412bd3e4421645f0bf,f6740081487ca34963a005209e2e9adfdf6f3561719af082d40fe80145e0cceb,bbeca1491c2f8364821a328a6677c0c5d59ccd60250abac3cec0887eeb9bde3e",
"-checkFileNameList=.gitignore,.gitattributes,.gitea/workflows/push.yaml,.gitea/workflows/release.yaml"
"-repoSize=10",
"-checkFileSumList=-checkFileNameList="
],
"env": [
"PATH=/usr/bin:/bin:/usr/local/bin"
@ -86,7 +83,7 @@
"--grading-repo-name",
"ece280-joj",
"--group-config",
"joj=1000:24,run=1000:24,=100:24"
"=100:24"
],
"env": [
"LOG_FILE_PATH=/home/tt/.cache/joint-teapot-debug.log"

View File

@ -7,7 +7,7 @@
"maxTotalScore": 100,
"stage": {
"sandboxExecServer": "172.17.0.1:5051",
"sandboxToken": "test",
"sandboxToken": "",
"outputPath": "/tmp/joj3_result.json",
"stages": [
{
@ -60,11 +60,8 @@
"args": [
"/usr/local/bin/repo-health-checker",
"-root=.",
"-repoSize=50.5",
"-meta=README.md",
"-meta=Changelog.md",
"-checkFileSumList=a5b63323a692d3d8b952442969649b4f823d58dae26429494f613df160710dfc,b1bbad25b830db0a77b15a033f9ca1b7ab44c1d2d05056412bd3e4421645f0bf,f6740081487ca34963a005209e2e9adfdf6f3561719af082d40fe80145e0cceb,bbeca1491c2f8364821a328a6677c0c5d59ccd60250abac3cec0887eeb9bde3e",
"-checkFileNameList=.gitignore,.gitattributes,.gitea/workflows/push.yaml,.gitea/workflows/release.yaml"
"-repoSize=10",
"-checkFileSumList=-checkFileNameList="
],
"env": [
"PATH=/usr/bin:/bin:/usr/local/bin"
@ -86,7 +83,7 @@
"--grading-repo-name",
"ece280-joj",
"--group-config",
"joj=1000:24,run=1000:24,=100:24"
"=100:24"
],
"env": [
"LOG_FILE_PATH=/home/tt/.cache/joint-teapot-debug.log"
@ -150,7 +147,7 @@
"cpuSetLimit": "",
"copyIn": {
"h7/build/ex2-asan": {
"src": "/home/tt/.config/joj/tools/h7/build/ex2-asan",
"src": "/home/tt/.config/joj/h7/build/ex2-asan",
"max": 419430400
}
},

View File

@ -7,7 +7,7 @@
"maxTotalScore": 100,
"stage": {
"sandboxExecServer": "172.17.0.1:5051",
"sandboxToken": "test",
"sandboxToken": "",
"outputPath": "/tmp/joj3_result.json",
"stages": [
{
@ -60,11 +60,8 @@
"args": [
"/usr/local/bin/repo-health-checker",
"-root=.",
"-repoSize=50.5",
"-meta=README.md",
"-meta=Changelog.md",
"-checkFileSumList=a5b63323a692d3d8b952442969649b4f823d58dae26429494f613df160710dfc,b1bbad25b830db0a77b15a033f9ca1b7ab44c1d2d05056412bd3e4421645f0bf,f6740081487ca34963a005209e2e9adfdf6f3561719af082d40fe80145e0cceb,bbeca1491c2f8364821a328a6677c0c5d59ccd60250abac3cec0887eeb9bde3e",
"-checkFileNameList=.gitignore,.gitattributes,.gitea/workflows/push.yaml,.gitea/workflows/release.yaml"
"-repoSize=10",
"-checkFileSumList=-checkFileNameList="
],
"env": [
"PATH=/usr/bin:/bin:/usr/local/bin"
@ -86,7 +83,7 @@
"--grading-repo-name",
"ece280-joj",
"--group-config",
"joj=1000:24,run=1000:24,=100:24"
"=100:24"
],
"env": [
"LOG_FILE_PATH=/home/tt/.cache/joint-teapot-debug.log"
@ -157,7 +154,7 @@
"cpuSetLimit": "",
"copyIn": {
"tools/filelength": {
"src": "/home/tt/.config/joj/tools/tools/filelength",
"src": "/home/tt/.config/joj/tools/filelength",
"max": 419430400
}
},

View File

@ -1,5 +1,4 @@
import json
import os
from pathlib import Path
from typing import Any, Dict, Tuple
@ -12,24 +11,22 @@ from joj3_config_generator.models import repo, task
def read_convert_files(
case_name: str,
) -> Tuple[repo.Config, task.Config, Dict[str, Any]]:
root = os.path.dirname(os.path.realpath(__file__))
repo_toml_path = os.path.join(root, case_name, "repo.toml")
with open(repo_toml_path) as f:
repo_toml = f.read()
task_toml_path = os.path.join(root, case_name, "task.toml")
with open(task_toml_path) as f:
task_toml = f.read()
result_json_path = os.path.join(root, case_name, "task.json")
with open(result_json_path) as f:
result: Dict[str, Any] = json.load(f)
repo_obj = rtoml.loads(repo_toml)
task_obj = rtoml.loads(task_toml)
return repo.Config(**repo_obj), task.Config(**task_obj), result
root = Path(__file__).resolve().parent
repo_toml_path = root / case_name / "repo.toml"
repo_toml = repo_toml_path.read_text() if repo_toml_path.exists() else ""
task_toml_path = root / case_name / "task.toml"
task_toml = task_toml_path.read_text() if task_toml_path.exists() else ""
result = json.loads((root / case_name / "task.json").read_text())
return (
repo.Config(**rtoml.loads(repo_toml)),
task.Config(**rtoml.loads(task_toml)),
result,
)
def load_case(case_name: str) -> None:
repo, task, expected_result = read_convert_files(case_name)
result = convert(repo, task, repo_root=Path(".")).model_dump(
result = convert(repo, task).model_dump(
mode="json", by_alias=True, exclude_none=True
)
assert result == expected_result

View File

@ -1,4 +1,4 @@
import os
from pathlib import Path
from typing import Any, Dict, Tuple
import rtoml
@ -9,16 +9,12 @@ from joj3_config_generator.models import joj1
def read_convert_joj1_files(case_name: str) -> Tuple[joj1.Config, Dict[str, Any]]:
root = os.path.dirname(os.path.realpath(__file__))
task_yaml_path = os.path.join(root, case_name, "task.yaml")
with open(task_yaml_path) as f:
task_yaml = f.read()
task_toml_path = os.path.join(root, case_name, "task.toml")
with open(task_toml_path) as f:
task_toml = f.read()
joj1_obj = yaml.safe_load(task_yaml)
task_obj = rtoml.loads(task_toml)
return joj1.Config(**joj1_obj), task_obj
root = Path(__file__).resolve().parent
task_yaml_path = root / case_name / "task.yaml"
task_yaml = task_yaml_path.read_text()
task_toml_path = root / case_name / "task.toml"
task_toml = task_toml_path.read_text()
return joj1.Config(**yaml.safe_load(task_yaml)), rtoml.loads(task_toml)
def load_case(case_name: str) -> None: