|
| 1 | +import os |
| 2 | +import pathlib |
| 3 | + |
| 4 | +from loguru import logger |
| 5 | + |
| 6 | +from finecode.workspace_manager import context, services |
| 7 | +from finecode.workspace_manager.config import read_configs, dump_configs |
| 8 | +from finecode.workspace_manager.runner import manager as runner_manager |
| 9 | + |
| 10 | + |
| 11 | +class DumpFailed(Exception): |
| 12 | + def __init__(self, message: str) -> None: |
| 13 | + self.message = message |
| 14 | + |
| 15 | + |
| 16 | +async def dump_config(workdir_path: pathlib.Path, project_name: str): |
| 17 | + ws_context = context.WorkspaceContext([workdir_path]) |
| 18 | + # it could be optimized by looking for concrete project instead of all |
| 19 | + await read_configs.read_projects_in_dir( |
| 20 | + dir_path=workdir_path, ws_context=ws_context |
| 21 | + ) |
| 22 | + |
| 23 | + # project is provided. Filter out other projects if there are more, they would |
| 24 | + # not be used (run can be started in a workspace with also other projects) |
| 25 | + ws_context.ws_projects = { |
| 26 | + project_dir_path: project |
| 27 | + for project_dir_path, project in ws_context.ws_projects.items() |
| 28 | + if project.name == project_name |
| 29 | + } |
| 30 | + |
| 31 | + # start runner to init project config |
| 32 | + try: |
| 33 | + try: |
| 34 | + await runner_manager.update_runners(ws_context) |
| 35 | + except runner_manager.RunnerFailedToStart as exception: |
| 36 | + raise DumpFailed( |
| 37 | + f"One or more projects are misconfigured, runners for them didn't" |
| 38 | + f" start: {exception.message}. Check logs for details." |
| 39 | + ) |
| 40 | + |
| 41 | + # Some tools like IDE extensions for syntax highlighting rely on |
| 42 | + # file name. Keep file name of config the same and save in subdirectory |
| 43 | + project_dir_path = list(ws_context.ws_projects.keys())[0] |
| 44 | + dump_dir_path = project_dir_path / 'finecode_config_dump' |
| 45 | + dump_file_path = dump_dir_path / 'pyproject.toml' |
| 46 | + |
| 47 | + project_raw_config = ws_context.ws_projects_raw_configs[project_dir_path] |
| 48 | + raw_config_str = dump_configs.dump_config(project_raw_config) |
| 49 | + |
| 50 | + os.makedirs(dump_dir_path, exist_ok=True) |
| 51 | + with open(dump_file_path, 'w') as dump_file: |
| 52 | + dump_file.write(raw_config_str) |
| 53 | + |
| 54 | + logger.info(f"Dumped config into {dump_file_path}") |
| 55 | + finally: |
| 56 | + services.on_shutdown(ws_context) |
0 commit comments