feat(KIAUH): show installation status of Klipper and Moonraker in MainMenu

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2023-12-23 19:42:34 +01:00
parent 14132fc34b
commit b9479db766
2 changed files with 105 additions and 17 deletions

View File

@@ -18,7 +18,12 @@ from kiauh.core.menus.install_menu import InstallMenu
from kiauh.core.menus.remove_menu import RemoveMenu from kiauh.core.menus.remove_menu import RemoveMenu
from kiauh.core.menus.settings_menu import SettingsMenu from kiauh.core.menus.settings_menu import SettingsMenu
from kiauh.core.menus.update_menu import UpdateMenu 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): class MainMenu(BaseMenu):
@@ -36,8 +41,40 @@ class MainMenu(BaseMenu):
}, },
footer_type=QUIT_FOOTER, 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): def print_menu(self):
self.fetch_status()
header = " [ Main Menu ] " header = " [ Main Menu ] "
footer1 = "KIAUH v6.0.0" footer1 = "KIAUH v6.0.0"
footer2 = f"Changelog: {COLOR_MAGENTA}https://git.io/JnmlX{RESET_FORMAT}" footer2 = f"Changelog: {COLOR_MAGENTA}https://git.io/JnmlX{RESET_FORMAT}"
@@ -48,21 +85,21 @@ class MainMenu(BaseMenu):
/=======================================================\\ /=======================================================\\
| {color}{header:~^{count}}{RESET_FORMAT} | | {color}{header:~^{count}}{RESET_FORMAT} |
|-------------------------------------------------------| |-------------------------------------------------------|
| 0) [Log-Upload] | Klipper: <TODO> | | 0) [Log-Upload] | Klipper: {self.kl_status:<32} |
| | Repo: <TODO> | | | Repo: {self.kl_repo:<32} |
| 1) [Install] | | | 1) [Install] |------------------------------------|
| 2) [Update] | Moonraker: <TODO> | | 2) [Update] | Moonraker: {self.mr_status:<32} |
| 3) [Remove] | Repo: <TODO> | | 3) [Remove] | Repo: {self.mr_repo:<32} |
| 4) [Advanced] | | | 4) [Advanced] |------------------------------------|
| 5) [Backup] | Mainsail: <TODO> | | 5) [Backup] | Mainsail: {self.ms_status:<26} |
| | Fluidd: <TODO> | | | Fluidd: {self.fl_status:<26} |
| 6) [Settings] | KlipperScreen: <TODO> | | 6) [Settings] | KlipperScreen: {self.ks_status:<26} |
| | Mobileraker: <TODO> | | | Mobileraker: {self.mb_status:<26} |
| | | | | |
| | Crowsnest: <TODO> | | | Crowsnest: {self.cn_status:<26} |
| | Telegram Bot: <TODO> | | | Telegram Bot: {self.tg_status:<26} |
| | Obico: <TODO> | | | Obico: {self.ob_status:<26} |
| | OctoEverywhere: <TODO> | | | OctoEverywhere: {self.oe_status:<26} |
|-------------------------------------------------------| |-------------------------------------------------------|
| {COLOR_CYAN}{footer1:^16}{RESET_FORMAT} | {footer2:^43} | | {COLOR_CYAN}{footer1:^16}{RESET_FORMAT} | {footer2:^43} |
""" """

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import subprocess
# ======================================================================= # # ======================================================================= #
# Copyright (C) 2020 - 2023 Dominik Willner <th33xitus@gmail.com> # # Copyright (C) 2020 - 2023 Dominik Willner <th33xitus@gmail.com> #
@@ -10,9 +11,18 @@
# ======================================================================= # # ======================================================================= #
from datetime import datetime 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.logger import Logger
from kiauh.utils.system_utils import check_package_install, install_system_packages 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: for _ in requirements:
print(f"{COLOR_CYAN}{_}{RESET_FORMAT}") print(f"{COLOR_CYAN}{_}{RESET_FORMAT}")
install_system_packages(requirements) 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 "<orga>/<name>"
"""
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}"