Skip to content

Commit 9376a12

Browse files
committed
Improve error handling
1 parent 3fce872 commit 9376a12

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

tricore-cli/pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[project]
22
name = "tricore-cli"
3-
version = "1.6"
3+
version = "1.7"
44
authors = [{ name = "Francesco Mecatti" }]
55
description = "A utility script for interacting with containerized TriCore development environment"
66
readme = "README.md"
77
requires-python = ">=3.10"
8-
dependencies = ["docker>=7.0.0"]
8+
dependencies = ["docker>=7.0.0", "rich>=13.7"]
99

1010
[project.urls]
1111
Homepage = "https://github.com/mc-cat-tty/tricore-dev-env"
@@ -15,5 +15,5 @@ Issues = "https://github.com/mc-cat-tty/tricore-dev-env/issues"
1515
tricorecli = "tricorecli:main"
1616

1717
[build-system]
18-
requires = ["setuptools>=61.0", "rich>=13.7"]
18+
requires = ["setuptools>=61.0"]
1919
build-backend = "setuptools.build_meta"

tricore-cli/src/containers.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from __future__ import annotations
12
import docker
23
import docker.errors
34
from utils import *
45
from docker.models.containers import Container
56
from docker.errors import DockerException
6-
from docker import DockerClient
7+
from docker import DockerClient, APIClient
78
from docker.types import Mount
89

910
IMAGE: str = 'francescomecatti/tricore-dev-env:1.0'
@@ -14,19 +15,28 @@ def __init__(self, image_name: str):
1415
self._client: DockerClient | None = None
1516
self._container: Container | None = None
1617

17-
def __enter__(self):
18+
def __enter__(self) -> DisposableContainer:
1819
try:
1920
self._client = docker.from_env()
2021
except DockerException:
2122
print('Docker engine not found. Make sure to have a running Docker engine!')
2223
exit(ExitCode.ENGINE_NOT_STARTED)
2324

24-
return self._container
25+
return self
2526

26-
def __exit__(self, type, value, tb):
27+
def __exit__(self, type, value, tb) -> None:
2728
assert self._container, "Invalid container"
2829
self._container.stop(timeout=1)
2930
self._container.remove()
31+
32+
def run_async(self, cmd: str, stream_handler_callback) -> int:
33+
if not self._container: raise RuntimeError("Container not initialized")
34+
35+
client = APIClient()
36+
exec_handle = client.exec_create(self._container.id, cmd)
37+
stream = client.exec_start(exec_handle, stream=True)
38+
stream_handler_callback(stream)
39+
return client.exec_inspect(exec_handle['Id']).get('ExitCode')
3040

3141

3242
class BuildDisposableContainer(DisposableContainer):
@@ -36,7 +46,7 @@ def __init__(self, folder: str):
3646
super().__init__(IMAGE)
3747
self.__folder = folder
3848

39-
def __enter__(self):
49+
def __enter__(self) -> DisposableContainer:
4050
super().__enter__()
4151
assert self._client, "Invalid client"
4252

@@ -56,5 +66,4 @@ def __enter__(self):
5666
working_dir = BuildDisposableContainer.SRC_DIR
5767
)
5868

59-
if not self._container: raise RuntimeError("sdfas")
60-
return self._container
69+
return self

tricore-cli/src/tricorecli.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,21 @@ def build(args: BuildHanderArgs) -> None:
3030
with BuildDisposableContainer(abs_path) as build_container:
3131
print(f'Building source with {CPUS} jobs from {abs_path}')
3232

33-
res = build_container.exec_run('cmake -B build --toolchain tricore_toolchain.cmake', stream=True)
34-
print_stream(res)
33+
exit_if_error(
34+
build_container.run_async('cmake -B build --toolchain tricore_toolchain.cmake', print_stream),
35+
"Failed makefiles generation."
36+
)
3537

36-
res = build_container.exec_run(f'cmake --build build --parallel {CPUS}', stream=True)
37-
print_stream(res)
38+
exit_if_error(
39+
build_container.run_async(f'cmake --build build --parallel {CPUS}', print_stream),
40+
"Build failed."
41+
)
3842

39-
print(f"Successfully built artifacts in {build_path}")
43+
print(f"Successfully built artifacts in {build_path}.")
4044

4145

4246
def flash(args: FlashHandlerArgs) -> None:
43-
raise NotImplementedError('Feature not implemented yet')
47+
raise NotImplementedError('Feature not implemented yet.')
4448

4549

4650
def main() -> None:

tricore-cli/src/utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ def check_file_or_exit(file_path: str, error_msg: str = "", exit_code: int = 1):
3535
print(error_msg)
3636
exit(exit_code)
3737

38-
def print_stream(exec_result):
39-
if GlobalConfiguration().verbosity_level < 1: return
40-
41-
for line in exec_result.output:
38+
def exit_if_error(exit_code: int, msg: str):
39+
if exit_code == 0: return
40+
print(msg)
41+
exit(exit_code)
42+
43+
def print_stream(exec_stream):
44+
skip_print: bool = GlobalConfiguration().verbosity_level < 1
45+
46+
for line in exec_stream:
47+
if skip_print: continue
4248
print(line.decode('utf-8'), end='')
4349

4450
def print_pull_progress(stream: Generator):

0 commit comments

Comments
 (0)