|
1 | 1 | import pathlib |
2 | 2 |
|
3 | | -from loguru import logger |
4 | | - |
5 | | -from finecode.api_server import context |
6 | | -from finecode.api_server.services import run_service, shutdown_service |
7 | | -from finecode.api_server.config import config_models, read_configs |
8 | | -from finecode.api_server.runner import runner_manager |
| 3 | +from finecode.api_client import ApiClient, ApiError |
| 4 | +from finecode.api_server import api_server |
9 | 5 |
|
10 | 6 |
|
11 | 7 | class DumpFailed(Exception): |
12 | 8 | def __init__(self, message: str) -> None: |
13 | 9 | self.message = message |
14 | 10 |
|
15 | 11 |
|
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 | | - # read configs without presets, this is required to be able to start runners in |
32 | | - # the next step |
33 | | - for project in ws_context.ws_projects.values(): |
34 | | - try: |
35 | | - await read_configs.read_project_config( |
36 | | - project=project, ws_context=ws_context, resolve_presets=False |
37 | | - ) |
38 | | - except config_models.ConfigurationError as exception: |
39 | | - raise DumpFailed( |
40 | | - f"Reading project configs(without presets) in {project.dir_path} failed: {exception.message}" |
41 | | - ) from exception |
42 | | - |
43 | | - # Some tools like IDE extensions for syntax highlighting rely on |
44 | | - # file name. Keep file name of config the same and save in subdirectory |
45 | | - project_dir_path = list(ws_context.ws_projects.keys())[0] |
46 | | - dump_dir_path = project_dir_path / "finecode_config_dump" |
47 | | - dump_file_path = dump_dir_path / "pyproject.toml" |
48 | | - project_def = ws_context.ws_projects[project_dir_path] |
49 | | - actions_by_projects = {project_dir_path: ["dump_config"]} |
50 | | - |
51 | | - # start runner to init project config |
| 12 | +async def dump_config( |
| 13 | + workdir_path: pathlib.Path, project_name: str, own_server: bool = True |
| 14 | +): |
| 15 | + port_file = None |
52 | 16 | try: |
53 | | - # reread projects configs, now with resolved presets |
54 | | - # to be able to resolve presets, start runners with presets first |
55 | | - try: |
56 | | - await runner_manager.start_runners_with_presets( |
57 | | - projects=[project_def], ws_context=ws_context |
58 | | - ) |
59 | | - except runner_manager.RunnerFailedToStart as exception: |
60 | | - raise DumpFailed( |
61 | | - f"Starting runners with presets failed: {exception.message}" |
62 | | - ) from exception |
| 17 | + if own_server: |
| 18 | + port_file = api_server.start_own_server(workdir_path) |
| 19 | + try: |
| 20 | + port = await api_server.wait_until_ready_from_file(port_file) |
| 21 | + except TimeoutError as exc: |
| 22 | + raise DumpFailed(str(exc)) from exc |
| 23 | + else: |
| 24 | + api_server.ensure_running(workdir_path) |
| 25 | + try: |
| 26 | + port = await api_server.wait_until_ready() |
| 27 | + except TimeoutError as exc: |
| 28 | + raise DumpFailed(str(exc)) from exc |
63 | 29 |
|
| 30 | + client = ApiClient() |
| 31 | + await client.connect("127.0.0.1", port) |
64 | 32 | try: |
65 | | - await run_service.start_required_environments( |
66 | | - actions_by_projects, ws_context |
| 33 | + result = await client.add_dir(workdir_path) |
| 34 | + projects = result.get("projects", []) |
| 35 | + project = next( |
| 36 | + (p for p in projects if p["name"] == project_name), None |
67 | 37 | ) |
68 | | - except run_service.StartingEnvironmentsFailed as exception: |
69 | | - raise DumpFailed( |
70 | | - f"Failed to start environments for running 'dump_config': {exception.message}" |
71 | | - ) from exception |
| 38 | + if project is None: |
| 39 | + raise DumpFailed(f"Project '{project_name}' not found") |
72 | 40 |
|
73 | | - project_raw_config = ws_context.ws_projects_raw_configs[project_dir_path] |
| 41 | + project_dir_path = pathlib.Path(project["path"]) |
| 42 | + source_file_path = project_dir_path / "pyproject.toml" |
| 43 | + target_file_path = project_dir_path / "finecode_config_dump" / "pyproject.toml" |
74 | 44 |
|
75 | | - await run_service.run_action( |
76 | | - action_name="dump_config", |
77 | | - params={ |
78 | | - "source_file_path": project_def.def_path, |
79 | | - "project_raw_config": project_raw_config, |
80 | | - "target_file_path": dump_file_path, |
81 | | - }, |
82 | | - project_def=project_def, |
83 | | - ws_context=ws_context, |
84 | | - result_formats=[run_service.RunResultFormat.STRING], |
85 | | - preprocess_payload=False, |
86 | | - run_trigger=run_service.RunActionTrigger.USER, |
87 | | - dev_env=run_service.DevEnv.CLI, |
88 | | - ) |
89 | | - logger.info(f"Dumped config into {dump_file_path}") |
| 45 | + try: |
| 46 | + project_raw_config = await client.get_project_raw_config(project_name) |
| 47 | + await client.run_action( |
| 48 | + action="dump_config", |
| 49 | + project=project_name, |
| 50 | + params={ |
| 51 | + "source_file_path": str(source_file_path), |
| 52 | + "project_raw_config": project_raw_config, |
| 53 | + "target_file_path": str(target_file_path), |
| 54 | + }, |
| 55 | + options={ |
| 56 | + "result_formats": ["string"], |
| 57 | + "trigger": "user", |
| 58 | + "dev_env": "cli", |
| 59 | + }, |
| 60 | + ) |
| 61 | + except ApiError as exc: |
| 62 | + raise DumpFailed(str(exc)) from exc |
| 63 | + finally: |
| 64 | + await client.close() |
90 | 65 | finally: |
91 | | - shutdown_service.on_shutdown(ws_context) |
| 66 | + if port_file is not None and port_file.exists(): |
| 67 | + port_file.unlink(missing_ok=True) |
0 commit comments