From 6407664e3ec53790bba13dba25f7d4adca040eb5 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Thu, 2 May 2024 16:02:27 +0200 Subject: [PATCH] refactor: extract check for python version into function Signed-off-by: Dominik Willner --- kiauh/components/moonraker/moonraker_setup.py | 13 +++++-------- kiauh/utils/sys_utils.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/kiauh/components/moonraker/moonraker_setup.py b/kiauh/components/moonraker/moonraker_setup.py index d186b5c..67350a9 100644 --- a/kiauh/components/moonraker/moonraker_setup.py +++ b/kiauh/components/moonraker/moonraker_setup.py @@ -8,7 +8,6 @@ # ======================================================================= # import subprocess -import sys from pathlib import Path from components.webui_client.client_utils import ( @@ -48,6 +47,7 @@ from utils.sys_utils import ( install_python_requirements, update_system_package_lists, install_system_packages, + check_python_version, ) @@ -116,18 +116,15 @@ def install_moonraker() -> None: def check_moonraker_install_requirements() -> bool: - if not (sys.version_info.major >= 3 and sys.version_info.minor >= 7): - Logger.print_error("Versioncheck failed!") - Logger.print_error("Python 3.7 or newer required to run Moonraker.") - return False + def check_klipper_instances() -> bool: + if len(InstanceManager(Klipper).instances) >= 1: + return True - kl_instance_count = len(InstanceManager(Klipper).instances) - if kl_instance_count < 1: Logger.print_warn("Klipper not installed!") Logger.print_warn("Moonraker cannot be installed! Install Klipper first.") return False - return True + return check_python_version(3, 7) and check_klipper_instances() def setup_moonraker_prerequesites() -> None: diff --git a/kiauh/utils/sys_utils.py b/kiauh/utils/sys_utils.py index 80cc569..24a120e 100644 --- a/kiauh/utils/sys_utils.py +++ b/kiauh/utils/sys_utils.py @@ -39,6 +39,20 @@ def kill(opt_err_msg: str = "") -> None: sys.exit(1) +def check_python_version(major: int, minor: int) -> bool: + """ + Checks the python version and returns True if it's at least the given version + :param major: the major version to check + :param minor: the minor version to check + :return: bool + """ + if not (sys.version_info.major >= major and sys.version_info.minor >= minor): + Logger.print_error("Versioncheck failed!") + Logger.print_error(f"Python {major}.{minor} or newer required.") + return False + return True + + def parse_packages_from_file(source_file: Path) -> List[str]: """ Read the package names from bash scripts, when defined like: