From ab5bfc6cfeed697467de3a2000ff4e3f62bac2a6 Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Tue, 25 Feb 2025 14:05:00 -0500 Subject: [PATCH] fix: find repo.toml recursively --- joj3_config_generator/main.py | 37 ++++++++++++++++------------ joj3_config_generator/models/repo.py | 2 ++ joj3_config_generator/models/task.py | 2 ++ 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/joj3_config_generator/main.py b/joj3_config_generator/main.py index 9d57dfe..91d879a 100644 --- a/joj3_config_generator/main.py +++ b/joj3_config_generator/main.py @@ -59,19 +59,24 @@ def convert( Convert given dir of JOJ3 toml config files to JOJ3 json config files """ logger.info(f"Converting files in {root.absolute()}") - repo_toml_path = root / "repo.toml" - repo_obj = rtoml.loads( - repo_toml_path.read_text() if repo_toml_path.exists() else "" - ) - for task_toml_path in root.glob("**/*.toml"): - toml_name = task_toml_path.name.removesuffix(".toml") - if toml_name == "repo": - continue - result_json_path = task_toml_path.parent / f"{toml_name}.json" - logger.info(f"Converting {task_toml_path} to {result_json_path}") - task_obj = rtoml.loads(task_toml_path.read_text()) - result_model = convert_conf(repo.Config(**repo_obj), task.Config(**task_obj)) - 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") + for repo_toml_path in root.glob("**/repo.toml"): + 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) + task_conf.path = task_toml_path + result_model = convert_conf(repo_conf, task_conf) + 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") diff --git a/joj3_config_generator/models/repo.py b/joj3_config_generator/models/repo.py index 4befab6..288096f 100644 --- a/joj3_config_generator/models/repo.py +++ b/joj3_config_generator/models/repo.py @@ -1,3 +1,4 @@ +from pathlib import Path from typing import List, Optional from pydantic import BaseModel, Field @@ -11,6 +12,7 @@ class Files(BaseModel): class Config(BaseModel): + path: Path = Path(".") teaching_team: List[str] max_size: float = Field(..., ge=0) release_tags: List[str] diff --git a/joj3_config_generator/models/task.py b/joj3_config_generator/models/task.py index 09a37fc..ab15bf7 100644 --- a/joj3_config_generator/models/task.py +++ b/joj3_config_generator/models/task.py @@ -1,4 +1,5 @@ from datetime import datetime +from pathlib import Path from typing import List, Optional from pydantic import BaseModel, Field @@ -32,6 +33,7 @@ class Release(BaseModel): class Config(BaseModel): + path: Path = Path(".") task: str # Task name (e.g., hw3 ex5) release: Release # Release configuration stages: List[Stage] # list of stage configurations