From f62c10dc8bd2a546adb54bfbc4c1b345847b0145 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Sat, 16 Dec 2023 15:38:23 +0100 Subject: [PATCH] feat(kiauh): add helper methods to check for installed packages Signed-off-by: Dominik Willner --- kiauh/utils/common.py | 22 +++++++++++++++++++++- kiauh/utils/system_utils.py | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/kiauh/utils/common.py b/kiauh/utils/common.py index b6a8e87..949d6a4 100644 --- a/kiauh/utils/common.py +++ b/kiauh/utils/common.py @@ -10,7 +10,11 @@ # ======================================================================= # from datetime import datetime -from typing import Dict, Literal +from typing import Dict, Literal, List + +from kiauh.utils.constants import COLOR_CYAN, RESET_FORMAT +from kiauh.utils.logger import Logger +from kiauh.utils.system_utils import check_package_install, install_system_packages def get_current_date() -> Dict[Literal["date", "time"], str]: @@ -23,3 +27,19 @@ def get_current_date() -> Dict[Literal["date", "time"], str]: time: str = now.strftime("%H-%M-%S") return {"date": date, "time": time} + + +def check_install_dependencies(deps: List[str]) -> None: + """ + Common helper method to check if dependencies are installed + and if not, install them automatically | + :param deps: List of strings of package names to check if installed + :return: None + """ + requirements = check_package_install(deps) + if requirements: + Logger.print_status("Installing dependencies ...") + Logger.print_info("The following packages need installation:") + for _ in requirements: + print(f"{COLOR_CYAN}● {_}{RESET_FORMAT}") + install_system_packages(requirements) diff --git a/kiauh/utils/system_utils.py b/kiauh/utils/system_utils.py index 34a2985..39d9cac 100644 --- a/kiauh/utils/system_utils.py +++ b/kiauh/utils/system_utils.py @@ -169,6 +169,26 @@ def update_system_package_lists(silent: bool, rls_info_change=False) -> None: kill(f"Error updating system package list:\n{e.stderr.decode()}") +def check_package_install(packages: List[str]) -> List[str]: + """ + Checks the system for installed packages | + :param packages: List of strings of package names + :return: A list containing the names of packages that are not installed + """ + not_installed = [] + for package in packages: + command = ["dpkg-query", "f'${Status}'", "--show", package] + result = subprocess.run( + command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True + ) + if "installed" not in result.stdout.strip("'").split(): + not_installed.append(package) + else: + Logger.print_ok(f"{package} already installed.") + + return not_installed + + def install_system_packages(packages: List[str]) -> None: """ Installs a list of system packages |