diff --git a/kiauh/core/menus/main_menu.py b/kiauh/core/menus/main_menu.py index bc77121..908d77d 100644 --- a/kiauh/core/menus/main_menu.py +++ b/kiauh/core/menus/main_menu.py @@ -18,7 +18,12 @@ from kiauh.core.menus.install_menu import InstallMenu from kiauh.core.menus.remove_menu import RemoveMenu from kiauh.core.menus.settings_menu import SettingsMenu from kiauh.core.menus.update_menu import UpdateMenu -from kiauh.utils.constants import COLOR_MAGENTA, COLOR_CYAN, RESET_FORMAT +from kiauh.modules.klipper import KLIPPER_DIR, KLIPPER_ENV_DIR +from kiauh.modules.klipper.klipper import Klipper +from kiauh.modules.moonraker import MOONRAKER_DIR, MOONRAKER_ENV_DIR +from kiauh.modules.moonraker.moonraker import Moonraker +from kiauh.utils.common import get_repo_name, get_install_status_common +from kiauh.utils.constants import COLOR_MAGENTA, COLOR_CYAN, RESET_FORMAT, COLOR_RED class MainMenu(BaseMenu): @@ -36,8 +41,40 @@ class MainMenu(BaseMenu): }, footer_type=QUIT_FOOTER, ) + self.kl_status = None + self.kl_repo = None + self.mr_status = None + self.mr_repo = None + self.ms_status = None + self.fl_status = None + self.ks_status = None + self.mb_status = None + self.cn_status = None + self.tg_status = None + self.ob_status = None + self.oe_status = None + self.init_status() + + def init_status(self) -> None: + status_vars = ["kl", "mr", "ms", "fl", "ks", "mb", "cn", "tg", "ob", "oe"] + for var in status_vars: + setattr(self, f"{var}_status", f"{COLOR_RED}Not installed!{RESET_FORMAT}") + + def fetch_status(self) -> None: + # klipper + self.kl_status = get_install_status_common( + Klipper, KLIPPER_DIR, KLIPPER_ENV_DIR + ) + self.kl_repo = get_repo_name(KLIPPER_DIR) + # moonraker + self.mr_status = get_install_status_common( + Moonraker, MOONRAKER_DIR, MOONRAKER_ENV_DIR + ) + self.mr_repo = get_repo_name(MOONRAKER_DIR) def print_menu(self): + self.fetch_status() + header = " [ Main Menu ] " footer1 = "KIAUH v6.0.0" footer2 = f"Changelog: {COLOR_MAGENTA}https://git.io/JnmlX{RESET_FORMAT}" @@ -48,21 +85,21 @@ class MainMenu(BaseMenu): /=======================================================\\ | {color}{header:~^{count}}{RESET_FORMAT} | |-------------------------------------------------------| - | 0) [Log-Upload] | Klipper: | - | | Repo: | - | 1) [Install] | | - | 2) [Update] | Moonraker: | - | 3) [Remove] | Repo: | - | 4) [Advanced] | | - | 5) [Backup] | Mainsail: | - | | Fluidd: | - | 6) [Settings] | KlipperScreen: | - | | Mobileraker: | + | 0) [Log-Upload] | Klipper: {self.kl_status:<32} | + | | Repo: {self.kl_repo:<32} | + | 1) [Install] |------------------------------------| + | 2) [Update] | Moonraker: {self.mr_status:<32} | + | 3) [Remove] | Repo: {self.mr_repo:<32} | + | 4) [Advanced] |------------------------------------| + | 5) [Backup] | Mainsail: {self.ms_status:<26} | + | | Fluidd: {self.fl_status:<26} | + | 6) [Settings] | KlipperScreen: {self.ks_status:<26} | + | | Mobileraker: {self.mb_status:<26} | | | | - | | Crowsnest: | - | | Telegram Bot: | - | | Obico: | - | | OctoEverywhere: | + | | Crowsnest: {self.cn_status:<26} | + | | Telegram Bot: {self.tg_status:<26} | + | | Obico: {self.ob_status:<26} | + | | OctoEverywhere: {self.oe_status:<26} | |-------------------------------------------------------| | {COLOR_CYAN}{footer1:^16}{RESET_FORMAT} | {footer2:^43} | """ diff --git a/kiauh/utils/common.py b/kiauh/utils/common.py index 949d6a4..04aa746 100644 --- a/kiauh/utils/common.py +++ b/kiauh/utils/common.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import subprocess # ======================================================================= # # Copyright (C) 2020 - 2023 Dominik Willner # @@ -10,9 +11,18 @@ # ======================================================================= # from datetime import datetime -from typing import Dict, Literal, List +from pathlib import Path +from typing import Dict, Literal, List, Type -from kiauh.utils.constants import COLOR_CYAN, RESET_FORMAT +from kiauh.core.instance_manager.base_instance import BaseInstance +from kiauh.core.instance_manager.instance_manager import InstanceManager +from kiauh.utils.constants import ( + COLOR_CYAN, + RESET_FORMAT, + COLOR_YELLOW, + COLOR_GREEN, + COLOR_RED, +) from kiauh.utils.logger import Logger from kiauh.utils.system_utils import check_package_install, install_system_packages @@ -43,3 +53,44 @@ def check_install_dependencies(deps: List[str]) -> None: for _ in requirements: print(f"{COLOR_CYAN}● {_}{RESET_FORMAT}") install_system_packages(requirements) + + +def get_repo_name(repo_dir: str) -> str: + """ + Helper method to extract the organisation and name of a repository | + :param repo_dir: repository to extract the values from + :return: String in form of "/" + """ + try: + cmd = ["git", "-C", repo_dir, "config", "--get", "remote.origin.url"] + result = subprocess.check_output(cmd, stderr=subprocess.DEVNULL) + result = "/".join(result.decode().strip().split("/")[-2:]) + return f"{COLOR_CYAN}{result}{RESET_FORMAT}" + except subprocess.CalledProcessError: + return f"{COLOR_YELLOW}Unknown{RESET_FORMAT}" + + +def get_install_status_common( + instance_type: Type[BaseInstance], repo_dir: str, env_dir: str +) -> str: + """ + Helper method to get the installation status of software components, + which only consist of 3 major parts and if those parts exist, the + component can be considered as "installed". Typically, Klipper or + Moonraker match that criteria. + :param instance_type: The component type + :param repo_dir: the repository directory + :param env_dir: the python environment directory + :return: formatted string, containing the status + """ + im = InstanceManager(instance_type) + dir_exist = Path(repo_dir).exists() + env_dir_exist = Path(env_dir).exists() + instances_exist = len(im.instances) > 0 + status = [dir_exist, env_dir_exist, instances_exist] + if all(status): + return f"{COLOR_GREEN}Installed: {len(im.instances)}{RESET_FORMAT}" + elif not any(status): + return f"{COLOR_RED}Not installed!{RESET_FORMAT}" + else: + return f"{COLOR_YELLOW}Incomplete!{RESET_FORMAT}"