From d50e86fad01974b43c1556d69fd38a5a2d5b722d Mon Sep 17 00:00:00 2001 From: Kivi Kaitaniemi Date: Mon, 13 May 2024 15:25:47 +0300 Subject: [PATCH] Add possibility to raise errors in source --- example/source.py | 5 ++++- src/decman/__init__.py | 9 +++++++++ src/decman/app.py | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/example/source.py b/example/source.py index 786ff4d..2174786 100644 --- a/example/source.py +++ b/example/source.py @@ -10,7 +10,7 @@ import decman import decman.config # This is fine since the thing being imported is a class and not a global variable. -from decman import UserPackage, File, Directory +from decman import UserPackage, File, Directory, UserRaisedError # Configuring what packages are installed is easy. # Duplicates are OK, so if you have multiple modules that want to ensure a package is installed, @@ -113,6 +113,9 @@ if socket.gethostname() == "arch-1": # This executes code defined in MyModule which can affect for example what packages are # installed as a part of this module. my_own_mod.enable_my_custom_feature(True) +else: + # If you want to abort running decman from your config because something is wrong, raise a UserRaisedError + raise UserRaisedError("Unknown hostname!") decman.modules += [my_own_mod] diff --git a/src/decman/__init__.py b/src/decman/__init__.py index ca79bac..79d2548 100644 --- a/src/decman/__init__.py +++ b/src/decman/__init__.py @@ -11,6 +11,15 @@ import subprocess import decman.error +class UserRaisedError(Exception): + """ + Error raised by running source + """ + + def __init__(self, message) -> None: + super().__init__(message) + + def sh(sh_cmd: str, user: typing.Optional[str] = None, env_overrides: typing.Optional[dict[str, str]] = None): diff --git a/src/decman/app.py b/src/decman/app.py index f7ad33f..4be229c 100644 --- a/src/decman/app.py +++ b/src/decman/app.py @@ -79,6 +79,10 @@ def main(): for line in traceback.format_exc().splitlines(): l.print_debug(line) sys.exit(1) + except decman.UserRaisedError as user_error: + l.print_error( + f"Error encountered while running the source: {user_error}") + sys.exit(1) # Save even when an error has occurred, since this avoids repeating steps like building pkgs. try: