From 4df89c9917d1165ca52ef0a4f64a764dda84bca5 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Thu, 27 Jun 2024 20:23:34 +0200 Subject: [PATCH] refactor: oe uninstaller Signed-off-by: Dominik Willner --- .../octoeverywhere/octoeverywhere.py | 14 ++++++--- .../octoeverywhere/octoeverywhere_setup.py | 30 +++++++++---------- kiauh/utils/fs_utils.py | 24 +++++++++++++++ 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/kiauh/components/octoeverywhere/octoeverywhere.py b/kiauh/components/octoeverywhere/octoeverywhere.py index a4e530b..29b3c29 100644 --- a/kiauh/components/octoeverywhere/octoeverywhere.py +++ b/kiauh/components/octoeverywhere/octoeverywhere.py @@ -62,11 +62,17 @@ class Octoeverywhere(BaseInstance): raise def delete(self) -> None: - Logger.print_status("Removing OctoEverywhere for Klipper Instance ...") + service_file = self.get_service_file_name(extension=True) + service_file_path = self.get_service_file_path() + + Logger.print_status( + f"Deleting OctoEverywhere for Klipper Instance: {service_file}" + ) try: - cmd = f"OE_REMOVE_SCRIPT {self.cfg_dir}/moonraker.conf" - run(cmd, check=True, shell=True) + command = ["sudo", "rm", "-f", service_file_path] + run(command, check=True) + Logger.print_ok(f"Service file deleted: {service_file_path}") except CalledProcessError as e: - Logger.print_error(f"Error deleting instance: {e}") + Logger.print_error(f"Error deleting service file: {e}") raise diff --git a/kiauh/components/octoeverywhere/octoeverywhere_setup.py b/kiauh/components/octoeverywhere/octoeverywhere_setup.py index 518288b..61a3ba9 100644 --- a/kiauh/components/octoeverywhere/octoeverywhere_setup.py +++ b/kiauh/components/octoeverywhere/octoeverywhere_setup.py @@ -7,7 +7,7 @@ # This file may be distributed under the terms of the GNU GPLv3 license # # ======================================================================= # import json -import shutil +from pathlib import Path from typing import List from components.moonraker.moonraker import Moonraker @@ -28,7 +28,7 @@ from utils.config_utils import ( add_config_section, remove_config_section, ) -from utils.fs_utils import remove_file +from utils.fs_utils import run_remove_routines from utils.git_utils import git_clone_wrapper, git_pull_wrapper from utils.input_utils import get_confirm from utils.logger import DialogType, Logger @@ -47,8 +47,6 @@ def install_octoeverywhere() -> None: if not moonraker_exists(): return - # if obico is already installed, ask if the user wants to repair an - # incomplete installation or link to the obico server oe_im = InstanceManager(Octoeverywhere) oe_instances: List[Octoeverywhere] = oe_im.instances if oe_instances: @@ -59,7 +57,6 @@ def install_octoeverywhere() -> None: "It is save to run the installer again to link your " "printer or repair any issues.", ], - end="", ) if not get_confirm("Re-run OctoEverywhere installation?"): Logger.print_info("Exiting OctoEverywhere for Klipper installation ...") @@ -80,7 +77,6 @@ def install_octoeverywhere() -> None: "\n\n", "The setup will apply the same names to OctoEverywhere!", ], - end="", ) if not get_confirm( @@ -193,36 +189,40 @@ def remove_oe_instances( def remove_oe_dir() -> None: + Logger.print_status("Removing OctoEverywhere for Klipper directory ...") + if not OE_DIR.exists(): Logger.print_info(f"'{OE_DIR}' does not exist. Skipped ...") return - try: - shutil.rmtree(OE_DIR) - except OSError as e: - Logger.print_error(f"Unable to delete '{OE_DIR}':\n{e}") + run_remove_routines(OE_DIR) def remove_oe_env() -> None: + Logger.print_status("Removing OctoEverywhere for Klipper environment ...") + if not OE_ENV_DIR.exists(): Logger.print_info(f"'{OE_ENV_DIR}' does not exist. Skipped ...") return - try: - shutil.rmtree(OE_ENV_DIR) - except OSError as e: - Logger.print_error(f"Unable to delete '{OE_ENV_DIR}':\n{e}") + run_remove_routines(OE_ENV_DIR) def delete_oe_logs(instances: List[Octoeverywhere]) -> None: Logger.print_status("Removing OctoEverywhere logs ...") + all_logfiles = [] for instance in instances: all_logfiles = list(instance.log_dir.glob(f"{OE_LOG_NAME}*")) + + install_log = Path.home().joinpath("octoeverywhere-installer.log") + if install_log.exists(): + all_logfiles.append(install_log) + if not all_logfiles: Logger.print_info("No OctoEverywhere logs found. Skipped ...") return for log in all_logfiles: Logger.print_status(f"Remove '{log}'") - remove_file(log) + run_remove_routines(log) diff --git a/kiauh/utils/fs_utils.py b/kiauh/utils/fs_utils.py index 111294b..c2466c2 100644 --- a/kiauh/utils/fs_utils.py +++ b/kiauh/utils/fs_utils.py @@ -79,6 +79,30 @@ def remove_file(file_path: Path, sudo=False) -> None: raise +def run_remove_routines(file: Path) -> None: + try: + if not file.exists(): + Logger.print_info(f"File '{file}' does not exist. Skipped ...") + return + + if file.is_dir(): + shutil.rmtree(file) + elif file.is_file(): + file.unlink() + else: + raise OSError(f"File '{file}' is neither a file nor a directory!") + Logger.print_ok("Successfully removed!") + except OSError as e: + Logger.print_error(f"Unable to delete '{file}':\n{e}") + try: + Logger.print_info("Trying to remove with sudo ...") + remove_with_sudo(file) + Logger.print_ok("Successfully removed!") + except CalledProcessError as e: + Logger.print_error(f"Error deleting '{file}' with sudo:\n{e}") + Logger.print_error("Remove this directory manually!") + + def unzip(filepath: Path, target_dir: Path) -> None: """ Helper function to unzip a zip-archive into a target directory |