Files
kiauh/kiauh/extensions/klipper_backup/klipper_backup_extension.py
2024-04-15 20:32:42 +02:00

137 lines
6.7 KiB
Python

# ======================================================================= #
# Copyright (C) 2023 - 2024 Staubgeborener and Tylerjet #
# https://github.com/Staubgeborener/klipper-backup #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
import os
import shutil
import subprocess
from components.moonraker import MOONRAKER_DIR
from extensions.base_extension import BaseExtension
from extensions.klipper_backup import (
KLIPPERBACKUP_REPO_URL,
KLIPPERBACKUP_DIR,
KLIPPERBACKUP_CONFIG_DIR,
)
from utils.filesystem_utils import check_file_exist
from utils.input_utils import get_confirm
from utils.logger import Logger
# noinspection PyMethodMayBeStatic
class KlipperbackupExtension(BaseExtension):
def install_extension(self, **kwargs) -> None:
if not KLIPPERBACKUP_DIR.exists():
subprocess.run(["git", "clone", str(KLIPPERBACKUP_REPO_URL), str(KLIPPERBACKUP_DIR)])
subprocess.run(["git", "-C", str(KLIPPERBACKUP_DIR), "checkout", "installer-dev"])
subprocess.run(["chmod", "+x", str(KLIPPERBACKUP_DIR / "install.sh")])
subprocess.run([str(KLIPPERBACKUP_DIR / "install.sh")])
def update_extension(self, **kwargs) -> None:
extension_installed = check_file_exist(KLIPPERBACKUP_DIR)
if not extension_installed:
Logger.print_info("Extension does not seem to be installed! Skipping ...")
return
else:
subprocess.run([str(KLIPPERBACKUP_DIR / "install.sh"), "check_updates"])
def remove_extension(self, **kwargs) -> None:
def is_service_installed(service_names):
installed = True
for service_name in service_names:
try:
with open(os.devnull, 'w') as devnull:
subprocess.run(["systemctl", "status", service_name], stdout=devnull, stderr=devnull, check=True)
except subprocess.CalledProcessError:
installed = False
break
return installed
def uninstall_service(service_names):
for service_name in service_names:
try:
subprocess.run(["sudo", "systemctl", "disable", service_name], check=True)
subprocess.run(["sudo", "systemctl", "stop", service_name], check=True)
Logger.print_ok(f"The service {service_name} has been successfully uninstalled.")
except subprocess.CalledProcessError:
Logger.print_error(f"Error uninstalling the service {service_name}.")
def check_crontab_entry(entry):
try:
crontab_content = subprocess.check_output(["crontab", "-l"], stderr=subprocess.DEVNULL, text=True)
except subprocess.CalledProcessError:
return False
for line in crontab_content.splitlines():
if entry in line:
return True
return False
extension_installed = check_file_exist(KLIPPERBACKUP_DIR)
if not extension_installed:
Logger.print_info("Extension does not seem to be installed! Skipping ...")
return
def remove_moonraker_entry():
original_file_path = os.path.join(str(MOONRAKER_DIR), 'printer_data', 'config', 'moonraker.conf')
comparison_file_path = os.path.join(str(KLIPPERBACKUP_DIR), 'install-files', 'moonraker.conf')
if not os.path.exists(original_file_path) or not os.path.exists(comparison_file_path):
Logger.print_error(f"Unknown error, either the moonraker.conf is not found or the klipper-backup entry under ~/klipper-backup/install-files/moonraker.conf. Skipping ...")
return
with open(original_file_path, 'r') as original_file, open(comparison_file_path, 'r') as comparison_file:
original_content = original_file.read()
comparison_content = comparison_file.read()
if comparison_content in original_content:
modified_content = original_content.replace(comparison_content, '')
with open(original_file_path, 'w') as original_file:
original_file.write(modified_content)
Logger.print_ok("Klipper backup entry in moonraker.conf removed)
else:
Logger.print_ok(f"Klipper-Backup entry not found in moonraker.conf. Skipping ...")
question = "Do you really want to remove the extension?"
if get_confirm(question, True, False):
# Remove Klipper-Backup services
service_names = ["klipper-backup-on-boot.service", "klipper-backup-filewatch.service"]
for service_name in service_names:
Logger.print_info(f"Check whether the service {service_name} is installed ...")
if is_service_installed(service_name):
Logger.print_ok(f"Service {service_name} detected.")
uninstall_service(service_name)
else:
Logger.print_info(f"The service {service_name} is not installed. Skipping ...")
# Remove Klipper-Backup cron
entry_to_check = "$HOME/klipper-backup/script.sh"
if check_crontab_entry(entry_to_check):
command = "crontab -l | grep -v '$HOME/klipper-backup/script.sh' | crontab -"
subprocess.run(command, shell=True, check=True)
Logger.print_ok("The klipper-backup entry has been removed from the crontab.")
else:
Logger.print_info("The klipper-backup entry is not present in the crontab. Skipping ...")
# Remove Moonraker entry
Logger.print_ok("Check for klipper-backup moonraker entry ...")
remove_moonraker_entry()
# Remove Klipper-Backup
try:
Logger.print_status(f"Removing '{KLIPPERBACKUP_DIR}' ...")
shutil.rmtree(KLIPPERBACKUP_DIR)
config_backup_exists = check_file_exist(KLIPPERBACKUP_CONFIG_DIR)
if config_backup_exists:
shutil.rmtree(KLIPPERBACKUP_CONFIG_DIR)
Logger.print_ok("Extension successfully removed!")
except OSError as e:
Logger.print_error(f"Unable to remove extension: {e}")