refactor: oe uninstaller

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-06-27 20:23:34 +02:00
parent b19ba840ea
commit 4df89c9917
3 changed files with 49 additions and 19 deletions

View File

@@ -62,11 +62,17 @@ class Octoeverywhere(BaseInstance):
raise raise
def delete(self) -> None: 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: try:
cmd = f"OE_REMOVE_SCRIPT {self.cfg_dir}/moonraker.conf" command = ["sudo", "rm", "-f", service_file_path]
run(cmd, check=True, shell=True) run(command, check=True)
Logger.print_ok(f"Service file deleted: {service_file_path}")
except CalledProcessError as e: except CalledProcessError as e:
Logger.print_error(f"Error deleting instance: {e}") Logger.print_error(f"Error deleting service file: {e}")
raise raise

View File

@@ -7,7 +7,7 @@
# This file may be distributed under the terms of the GNU GPLv3 license # # This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= # # ======================================================================= #
import json import json
import shutil from pathlib import Path
from typing import List from typing import List
from components.moonraker.moonraker import Moonraker from components.moonraker.moonraker import Moonraker
@@ -28,7 +28,7 @@ from utils.config_utils import (
add_config_section, add_config_section,
remove_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.git_utils import git_clone_wrapper, git_pull_wrapper
from utils.input_utils import get_confirm from utils.input_utils import get_confirm
from utils.logger import DialogType, Logger from utils.logger import DialogType, Logger
@@ -47,8 +47,6 @@ def install_octoeverywhere() -> None:
if not moonraker_exists(): if not moonraker_exists():
return 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_im = InstanceManager(Octoeverywhere)
oe_instances: List[Octoeverywhere] = oe_im.instances oe_instances: List[Octoeverywhere] = oe_im.instances
if oe_instances: if oe_instances:
@@ -59,7 +57,6 @@ def install_octoeverywhere() -> None:
"It is save to run the installer again to link your " "It is save to run the installer again to link your "
"printer or repair any issues.", "printer or repair any issues.",
], ],
end="",
) )
if not get_confirm("Re-run OctoEverywhere installation?"): if not get_confirm("Re-run OctoEverywhere installation?"):
Logger.print_info("Exiting OctoEverywhere for Klipper installation ...") Logger.print_info("Exiting OctoEverywhere for Klipper installation ...")
@@ -80,7 +77,6 @@ def install_octoeverywhere() -> None:
"\n\n", "\n\n",
"The setup will apply the same names to OctoEverywhere!", "The setup will apply the same names to OctoEverywhere!",
], ],
end="",
) )
if not get_confirm( if not get_confirm(
@@ -193,36 +189,40 @@ def remove_oe_instances(
def remove_oe_dir() -> None: def remove_oe_dir() -> None:
Logger.print_status("Removing OctoEverywhere for Klipper directory ...")
if not OE_DIR.exists(): if not OE_DIR.exists():
Logger.print_info(f"'{OE_DIR}' does not exist. Skipped ...") Logger.print_info(f"'{OE_DIR}' does not exist. Skipped ...")
return return
try: run_remove_routines(OE_DIR)
shutil.rmtree(OE_DIR)
except OSError as e:
Logger.print_error(f"Unable to delete '{OE_DIR}':\n{e}")
def remove_oe_env() -> None: def remove_oe_env() -> None:
Logger.print_status("Removing OctoEverywhere for Klipper environment ...")
if not OE_ENV_DIR.exists(): if not OE_ENV_DIR.exists():
Logger.print_info(f"'{OE_ENV_DIR}' does not exist. Skipped ...") Logger.print_info(f"'{OE_ENV_DIR}' does not exist. Skipped ...")
return return
try: run_remove_routines(OE_ENV_DIR)
shutil.rmtree(OE_ENV_DIR)
except OSError as e:
Logger.print_error(f"Unable to delete '{OE_ENV_DIR}':\n{e}")
def delete_oe_logs(instances: List[Octoeverywhere]) -> None: def delete_oe_logs(instances: List[Octoeverywhere]) -> None:
Logger.print_status("Removing OctoEverywhere logs ...") Logger.print_status("Removing OctoEverywhere logs ...")
all_logfiles = [] all_logfiles = []
for instance in instances: for instance in instances:
all_logfiles = list(instance.log_dir.glob(f"{OE_LOG_NAME}*")) 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: if not all_logfiles:
Logger.print_info("No OctoEverywhere logs found. Skipped ...") Logger.print_info("No OctoEverywhere logs found. Skipped ...")
return return
for log in all_logfiles: for log in all_logfiles:
Logger.print_status(f"Remove '{log}'") Logger.print_status(f"Remove '{log}'")
remove_file(log) run_remove_routines(log)

View File

@@ -79,6 +79,30 @@ def remove_file(file_path: Path, sudo=False) -> None:
raise 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: def unzip(filepath: Path, target_dir: Path) -> None:
""" """
Helper function to unzip a zip-archive into a target directory | Helper function to unzip a zip-archive into a target directory |