feat: distribute logic and flags
This commit is contained in:
		
							parent
							
								
									8264caf7f8
								
							
						
					
					
						commit
						70366f467f
					
				|  | @ -18,12 +18,12 @@ from joj3_config_generator.processers.task import ( | ||||||
|     fix_keyword, |     fix_keyword, | ||||||
|     fix_result_detail, |     fix_result_detail, | ||||||
|     get_conf_stage, |     get_conf_stage, | ||||||
|     get_executorWithConfig, |     get_executor_with_config, | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def convert( | def convert( | ||||||
|     repo_conf: repo.Config, task_conf: task.Config, conf_root: Path |     repo_conf: repo.Config, task_conf: task.Config, repo_root: Path | ||||||
| ) -> result.Config: | ) -> result.Config: | ||||||
|     # Create the base ResultConf object |     # Create the base ResultConf object | ||||||
|     result_conf = result.Config( |     result_conf = result.Config( | ||||||
|  | @ -45,20 +45,18 @@ def convert( | ||||||
|     ) |     ) | ||||||
| 
 | 
 | ||||||
|     # Construct healthcheck stage |     # Construct healthcheck stage | ||||||
|     healthcheck_stage = get_healthcheck_config(repo_conf) |     healthcheck_stage = get_healthcheck_config(repo_conf, repo_root) | ||||||
|     result_conf.stage.stages.append(healthcheck_stage) |     result_conf.stage.stages.append(healthcheck_stage) | ||||||
|     stages: List[str] = [] |     stages: List[str] = [] | ||||||
|     # 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, stages = get_executorWithConfig( |         executor_with_config, stages = get_executor_with_config(task_stage, stages) | ||||||
|             task_stage, stages, conf_root |  | ||||||
|         ) |  | ||||||
|         conf_stage = get_conf_stage(task_stage, executor_with_config) |         conf_stage = get_conf_stage(task_stage, executor_with_config) | ||||||
|         conf_stage = fix_result_detail(task_stage, conf_stage) |         conf_stage = fix_result_detail(task_stage, conf_stage) | ||||||
|         conf_stage = fix_dummy(task_stage, conf_stage) |         conf_stage = fix_dummy(task_stage, conf_stage) | ||||||
|         conf_stage = fix_keyword(task_stage, conf_stage) |         conf_stage = fix_keyword(task_stage, conf_stage) | ||||||
|         conf_stage = fix_file(task_stage, conf_stage) |         conf_stage = fix_file(task_stage, conf_stage) | ||||||
|         conf_stage = fix_diff(task_stage, conf_stage, task_conf, conf_root) |         conf_stage = fix_diff(task_stage, conf_stage, task_conf) | ||||||
|         result_conf.stage.stages.append(conf_stage) |         result_conf.stage.stages.append(conf_stage) | ||||||
| 
 | 
 | ||||||
|     return result_conf |     return result_conf | ||||||
|  | @ -75,7 +73,7 @@ def convert_joj1(joj1_conf: joj1.Config) -> task.Config: | ||||||
|     ) |     ) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def distribute_json(folder_path: str, repo_obj: Any, conf_root: Path) -> None: | def distribute_json(folder_path: str, repo_obj: Any, repo_conf: Path) -> None: | ||||||
|     for root, _, files in os.walk(folder_path): |     for root, _, files in os.walk(folder_path): | ||||||
|         for file in files: |         for file in files: | ||||||
|             if file.endswith(".toml"):  # to pass test here |             if file.endswith(".toml"):  # to pass test here | ||||||
|  | @ -85,7 +83,7 @@ def distribute_json(folder_path: str, repo_obj: Any, conf_root: Path) -> None: | ||||||
|                     task_toml = toml_file.read() |                     task_toml = toml_file.read() | ||||||
|                 task_obj = rtoml.loads(task_toml) |                 task_obj = rtoml.loads(task_toml) | ||||||
|                 result_model = convert( |                 result_model = convert( | ||||||
|                     repo.Config(**repo_obj), task.Config(**task_obj), conf_root |                     repo.Config(**repo_obj), task.Config(**task_obj), repo_conf | ||||||
|                 ) |                 ) | ||||||
|                 result_dict = result_model.model_dump(by_alias=True, exclude_none=True) |                 result_dict = result_model.model_dump(by_alias=True, exclude_none=True) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -35,40 +35,50 @@ def convert( | ||||||
|         Path("."), |         Path("."), | ||||||
|         "--conf-root", |         "--conf-root", | ||||||
|         "-c", |         "-c", | ||||||
|         help="This should be consistent with the root of how you run JOJ3", |         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( |     repo_path: Path = typer.Option( | ||||||
|         Path("."), |         Path("."), | ||||||
|         "--repo-root", |         "--repo-root", | ||||||
|         "-r", |         "-r", | ||||||
|         help="This would be where you put your repo.toml file", |         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( |     distribute: bool = typer.Option( | ||||||
|         False, "--distribute", "-d", help="This flag determine whether to distribute" |         False, "--distribute", "-d", help="This flag determine whether to distribute" | ||||||
|     ), |     ), | ||||||
| ) -> Dict[str, Any]: | ) -> None: | ||||||
|     logger.info(f"Converting files in {root.absolute()}") |     logger.info(f"Converting files in {root.absolute()}") | ||||||
|     repo_toml_path = os.path.join(repo_path.absolute(), "basic", "repo.toml") |     if distribute is False: | ||||||
|     task_toml_path = os.path.join(root.absolute(), "basic", "task.toml") |         repo_toml_path = os.path.join(repo_path.absolute(), "basic", "repo.toml") | ||||||
|     result_json_path = os.path.join(root.absolute(), "basic", "task.json") |     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: |     with open(repo_toml_path, encoding=None) as repo_file: | ||||||
|         repo_toml = repo_file.read() |         repo_toml = repo_file.read() | ||||||
|     with open(task_toml_path, encoding=None) as task_file: |  | ||||||
|         task_toml = task_file.read() |  | ||||||
|     repo_obj = rtoml.loads(repo_toml) |     repo_obj = rtoml.loads(repo_toml) | ||||||
|     task_obj = rtoml.loads(task_toml) |     if distribute is False: | ||||||
|     result_model = convert_conf(repo.Config(**repo_obj), task.Config(**task_obj), root) |         task_toml_path = os.path.join(root.absolute(), "basic", "task.toml") | ||||||
|     result_dict = result_model.model_dump(by_alias=True, exclude_none=True) |         result_json_path = os.path.join(root.absolute(), "basic", "task.json") | ||||||
| 
 | 
 | ||||||
|     with open(result_json_path, "w", encoding=None) as result_file: |         with open(task_toml_path, encoding=None) as task_file: | ||||||
|         json.dump(result_dict, result_file, ensure_ascii=False, indent=4) |             task_toml = task_file.read() | ||||||
|         result_file.write("\n") | 
 | ||||||
|  |         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 |     # distribution on json | ||||||
|     # need a get folder path function |     # need a get folder path function | ||||||
|     if distribute: |     else: | ||||||
|         folder_path = "/home/tt/.config/joj" |         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/engr151-joj/home/tt/.config/joj/homework" | ||||||
|         folder_path = f"{Path.home()}/Desktop/FOCS/JOJ3-config-generator/tests/convert/" |         folder_path = f"{Path.home()}/Desktop/FOCS/JOJ3-config-generator/tests/convert/" | ||||||
|         distribute_json(folder_path, repo_obj, conf_root=root) |         # to be used in real action | ||||||
|     return result_dict |         folder_path = f"{root}" | ||||||
|  |         distribute_json(folder_path, repo_obj, repo_path) | ||||||
|  |  | ||||||
|  | @ -37,7 +37,7 @@ def get_teapot_stage(repo_conf: repo.Config) -> result.StageDetail: | ||||||
|     return stage_conf |     return stage_conf | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def get_healthcheck_args(repo_conf: repo.Config) -> str: | def get_healthcheck_args(repo_conf: repo.Config, repo_root: Path) -> str: | ||||||
|     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)} " | ||||||
|  | @ -59,7 +59,7 @@ def get_healthcheck_args(repo_conf: repo.Config) -> str: | ||||||
|     for meta in required_files: |     for meta in required_files: | ||||||
|         args = args + meta |         args = args + meta | ||||||
| 
 | 
 | ||||||
|     args = args + get_hash(immutable) |     args = args + get_hash(immutable, repo_root) | ||||||
| 
 | 
 | ||||||
|     args = args + immutable_files |     args = args + immutable_files | ||||||
| 
 | 
 | ||||||
|  | @ -84,7 +84,9 @@ def get_debug_args(repo_conf: repo.Config) -> str: | ||||||
|     return args |     return args | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def get_healthcheck_config(repo_conf: repo.Config) -> result.StageDetail: | def get_healthcheck_config( | ||||||
|  |     repo_conf: repo.Config, repo_root: Path | ||||||
|  | ) -> result.StageDetail: | ||||||
|     healthcheck_stage = result.StageDetail( |     healthcheck_stage = result.StageDetail( | ||||||
|         name="healthcheck", |         name="healthcheck", | ||||||
|         group="", |         group="", | ||||||
|  | @ -94,7 +96,7 @@ def get_healthcheck_config(repo_conf: repo.Config) -> result.StageDetail: | ||||||
|                 default=result.Cmd(), |                 default=result.Cmd(), | ||||||
|                 cases=[ |                 cases=[ | ||||||
|                     result.OptionalCmd( |                     result.OptionalCmd( | ||||||
|                         args=shlex.split(get_healthcheck_args(repo_conf)), |                         args=shlex.split(get_healthcheck_args(repo_conf, repo_root)), | ||||||
|                     ), |                     ), | ||||||
|                     result.OptionalCmd( |                     result.OptionalCmd( | ||||||
|                         args=shlex.split(get_debug_args(repo_conf)), |                         args=shlex.split(get_debug_args(repo_conf)), | ||||||
|  | @ -119,14 +121,17 @@ def calc_sha256sum(file_path: str) -> str: | ||||||
|     return sha256_hash.hexdigest() |     return sha256_hash.hexdigest() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def get_hash(immutable_files: list[str]) -> str:  # input should be a list | def get_hash( | ||||||
|  |     immutable_files: list[str], repo_root: Path | ||||||
|  | ) -> str:  # input should be a list | ||||||
|     # FIXME: should be finalized when get into the server |     # FIXME: should be finalized when get into the server | ||||||
|     current_file_path = Path(__file__).resolve() |     current_file_path = Path(__file__).resolve() | ||||||
|     project_root = current_file_path.parents[2] |     project_root = current_file_path.parents[2] | ||||||
|     # FIXME: givin the path |     # FIXME: givin the path | ||||||
|     file_path = f"{project_root}/tests/immutable_file/" |     file_path = f"{project_root}/tests/immutable_file/" | ||||||
|     # default value as hardcoded |  | ||||||
|     # file_path = "{Path.home()}/.cache/immutable" |     # file_path = "{Path.home()}/.cache/immutable" | ||||||
|  |     # to be use | ||||||
|  |     # file_path = f"/home/tt/.config/joj/{repo_root}/" | ||||||
|     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] | ||||||
|  |  | ||||||
|  | @ -33,8 +33,8 @@ def get_conf_stage( | ||||||
|     return conf_stage |     return conf_stage | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def get_executorWithConfig( | def get_executor_with_config( | ||||||
|     task_stage: task.Stage, cached: List[str], conf_root: Path |     task_stage: task.Stage, cached: List[str] | ||||||
| ) -> Tuple[result.ExecutorWith, List[str]]: | ) -> Tuple[result.ExecutorWith, List[str]]: | ||||||
|     file_import = ( |     file_import = ( | ||||||
|         task_stage.files.import_ |         task_stage.files.import_ | ||||||
|  | @ -61,7 +61,7 @@ def get_executorWithConfig( | ||||||
|                 else [] |                 else [] | ||||||
|             ), |             ), | ||||||
|             copy_in={ |             copy_in={ | ||||||
|                 file: result.CmdFile(src=f"/home/tt/{conf_root}/tools/{file}") |                 file: result.CmdFile(src=f"/home/tt/.config/joj/tools/{file}") | ||||||
|                 # all copyin files store in this tools folder |                 # all copyin files store in this tools folder | ||||||
|                 # are there any corner cases |                 # are there any corner cases | ||||||
|                 for file in copy_in_files |                 for file in copy_in_files | ||||||
|  | @ -238,7 +238,6 @@ def fix_diff( | ||||||
|     task_stage: task.Stage, |     task_stage: task.Stage, | ||||||
|     conf_stage: result.StageDetail, |     conf_stage: result.StageDetail, | ||||||
|     task_conf: task.Config, |     task_conf: task.Config, | ||||||
|     conf_root: Path, |  | ||||||
| ) -> result.StageDetail: | ) -> result.StageDetail: | ||||||
|     if task_stage.parsers is not None and "diff" in task_stage.parsers: |     if task_stage.parsers is not None and "diff" in task_stage.parsers: | ||||||
|         diff_parser = next((p for p in conf_stage.parsers if p.name == "diff"), None) |         diff_parser = next((p for p in conf_stage.parsers if p.name == "diff"), None) | ||||||
|  | @ -277,7 +276,7 @@ def fix_diff( | ||||||
|             stage_cases.append( |             stage_cases.append( | ||||||
|                 result.OptionalCmd( |                 result.OptionalCmd( | ||||||
|                     stdin=result.CmdFile( |                     stdin=result.CmdFile( | ||||||
|                         src=f"/home/tt/{conf_root}/{task_conf.task.type_}/{stdin}" |                         src=f"/home/tt/.config/joj/{task_conf.task.type_}/{stdin}" | ||||||
|                     ), |                     ), | ||||||
|                     args=(shlex.split(command) if command is not None else None), |                     args=(shlex.split(command) if command is not None else None), | ||||||
|                     cpu_limit=cpu_limit, |                     cpu_limit=cpu_limit, | ||||||
|  | @ -300,7 +299,7 @@ def fix_diff( | ||||||
|                             { |                             { | ||||||
|                                 "score": diff_output.score, |                                 "score": diff_output.score, | ||||||
|                                 "fileName": "stdout", |                                 "fileName": "stdout", | ||||||
|                                 "answerPath": f"/home/tt/{conf_root}/{task_conf.task.type_}/{stdout}", |                                 "answerPath": f"/home/tt/.config/joj/{task_conf.task.type_}/{stdout}", | ||||||
|                                 "forceQuitOnDiff": diff_output.forcequit, |                                 "forceQuitOnDiff": diff_output.forcequit, | ||||||
|                                 "alwaysHide": diff_output.hide, |                                 "alwaysHide": diff_output.hide, | ||||||
|                                 "compareSpace": not diff_output.ignorespaces, |                                 "compareSpace": not diff_output.ignorespaces, | ||||||
|  |  | ||||||
|  | @ -29,7 +29,7 @@ def read_convert_files( | ||||||
| 
 | 
 | ||||||
| def load_case(case_name: str) -> None: | def load_case(case_name: str) -> None: | ||||||
|     repo, task, expected_result = read_convert_files(case_name) |     repo, task, expected_result = read_convert_files(case_name) | ||||||
|     result = convert(repo, task, conf_root=Path(".")).model_dump( |     result = convert(repo, task, repo_root=Path(".")).model_dump( | ||||||
|         mode="json", by_alias=True, exclude_none=True |         mode="json", by_alias=True, exclude_none=True | ||||||
|     ) |     ) | ||||||
|     assert result == expected_result |     assert result == expected_result | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user