From db3a9ca62281bf5824123ebf11521770e96f006e Mon Sep 17 00:00:00 2001 From: dw-0 Date: Tue, 13 Aug 2024 19:28:37 +0200 Subject: [PATCH] refactor: allow to also check for timer unit files Signed-off-by: Dominik Willner --- .../klipper_backup/klipper_backup_extension.py | 4 ++-- kiauh/utils/sys_utils.py | 15 +++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/kiauh/extensions/klipper_backup/klipper_backup_extension.py b/kiauh/extensions/klipper_backup/klipper_backup_extension.py index 19d439d..e1597f7 100644 --- a/kiauh/extensions/klipper_backup/klipper_backup_extension.py +++ b/kiauh/extensions/klipper_backup/klipper_backup_extension.py @@ -23,7 +23,7 @@ from extensions.klipper_backup import ( ) from utils.fs_utils import check_file_exist from utils.input_utils import get_confirm -from utils.sys_utils import service_instance_exists +from utils.sys_utils import unit_file_exists # noinspection PyMethodMayBeStatic @@ -114,7 +114,7 @@ class KlipperbackupExtension(BaseExtension): Logger.print_status( f"Check whether the {service_name} service is installed ..." ) - if service_instance_exists(service_name): + if unit_file_exists(service_name, "service"): Logger.print_info(f"Service {service_name} detected.") if uninstall_service(service_name): Logger.print_ok( diff --git a/kiauh/utils/sys_utils.py b/kiauh/utils/sys_utils.py index 2573c58..9db103d 100644 --- a/kiauh/utils/sys_utils.py +++ b/kiauh/utils/sys_utils.py @@ -401,15 +401,18 @@ def cmd_sysctl_manage(action: SysCtlManageAction) -> None: raise -def service_instance_exists(name: str, exclude: List[str] | None = None) -> bool: +def unit_file_exists( + name: str, suffix: Literal["service", "timer"], exclude: List[str] | None = None +) -> bool: """ - Checks if a systemd service instance exists. - :param name: the service name - :param exclude: List of strings of service names to exclude - :return: True if the service exists, False otherwise + Checks if a systemd unit file of the provided suffix exists. + :param name: the name of the unit file + :param suffix: suffix of the unit file, either "service" or "timer" + :param exclude: List of strings of names to exclude + :return: True if the unit file exists, False otherwise """ exclude = exclude or [] - pattern = re.compile(f"^{name}(-[0-9a-zA-Z]+)?.service$") + pattern = re.compile(f"^{name}(-[0-9a-zA-Z]+)?.{suffix}$") service_list = [ Path(SYSTEMD, service) for service in SYSTEMD.iterdir()