refactor: allow to also check for timer unit files

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-08-13 19:28:37 +02:00
parent 3f428df9d6
commit db3a9ca622
2 changed files with 11 additions and 8 deletions

View File

@@ -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(

View File

@@ -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()