From ab60f8cddc2cf4a2ba1123021ce8e298fbbb461f Mon Sep 17 00:00:00 2001 From: dw-0 Date: Tue, 21 Jul 2026 19:53:13 +0200 Subject: [PATCH] refactor: replace remove_file with remove_with_sudo --- kiauh/components/webui_client/client_utils.py | 4 +-- kiauh/core/decorators.py | 25 ------------------- .../pretty_gcode/pretty_gcode_extension.py | 10 +++++--- .../moonraker_telegram_bot_extension.py | 4 +-- kiauh/utils/fs_utils.py | 12 --------- kiauh/utils/tests/test_fs_utils.py | 17 ------------- 6 files changed, 11 insertions(+), 61 deletions(-) delete mode 100644 kiauh/core/decorators.py diff --git a/kiauh/components/webui_client/client_utils.py b/kiauh/components/webui_client/client_utils.py index 1934f796..0d364025 100644 --- a/kiauh/components/webui_client/client_utils.py +++ b/kiauh/components/webui_client/client_utils.py @@ -41,7 +41,7 @@ from core.simple_config_parser.simple_config_parser import ( from core.types.color import Color from core.types.component_status import ComponentStatus from utils.common import get_install_status -from utils.fs_utils import create_symlink, remove_file +from utils.fs_utils import create_symlink, remove_with_sudo from utils.git_utils import ( get_latest_remote_tag, get_latest_unstable_tag, @@ -353,7 +353,7 @@ def create_nginx_cfg( source = NGINX_SITES_AVAILABLE.joinpath(cfg_name) target = NGINX_SITES_ENABLED.joinpath(cfg_name) - remove_file(Path("/etc/nginx/sites-enabled/default"), True) + remove_with_sudo(Path("/etc/nginx/sites-enabled/default")) generate_nginx_cfg_from_template(cfg_name, template_src=template_src, **kwargs) create_symlink(source, target, True) set_nginx_permissions() diff --git a/kiauh/core/decorators.py b/kiauh/core/decorators.py deleted file mode 100644 index abd232b4..00000000 --- a/kiauh/core/decorators.py +++ /dev/null @@ -1,25 +0,0 @@ -# ======================================================================= # -# Copyright (C) 2020 - 2026 Dominik Willner # -# # -# 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 # -# ======================================================================= # - -from __future__ import annotations - -import warnings -from typing import Callable - - -def deprecated(info: str = "", replaced_by: Callable | None = None) -> Callable: - def decorator(func) -> Callable: - def wrapper(*args, **kwargs): - msg = f"{info}{replaced_by.__name__ if replaced_by else ''}" - warnings.warn(msg, category=DeprecationWarning, stacklevel=2) - return func(*args, **kwargs) - - return wrapper - - return decorator diff --git a/kiauh/extensions/pretty_gcode/pretty_gcode_extension.py b/kiauh/extensions/pretty_gcode/pretty_gcode_extension.py index 3add01d1..0e8533c9 100644 --- a/kiauh/extensions/pretty_gcode/pretty_gcode_extension.py +++ b/kiauh/extensions/pretty_gcode/pretty_gcode_extension.py @@ -16,7 +16,7 @@ from core.logger import DialogType, Logger from extensions.base_extension import BaseExtension from utils.common import check_install_dependencies from utils.fs_utils import ( - remove_file, + remove_with_sudo, ) from utils.git_utils import git_clone_wrapper, git_pull_wrapper from utils.input_utils import get_number_input @@ -91,8 +91,12 @@ class PrettyGcodeExtension(BaseExtension): # remove pgc dir shutil.rmtree(PGC_DIR) # remove nginx config - remove_file(NGINX_SITES_AVAILABLE.joinpath(PGC_CONF), True) - remove_file(NGINX_SITES_ENABLED.joinpath(PGC_CONF), True) + remove_with_sudo( + [ + NGINX_SITES_AVAILABLE.joinpath(PGC_CONF), + NGINX_SITES_ENABLED.joinpath(PGC_CONF), + ] + ) # restart nginx cmd_sysctl_service("nginx", "restart") diff --git a/kiauh/extensions/telegram_bot/moonraker_telegram_bot_extension.py b/kiauh/extensions/telegram_bot/moonraker_telegram_bot_extension.py index 5d1ba8d9..90357128 100644 --- a/kiauh/extensions/telegram_bot/moonraker_telegram_bot_extension.py +++ b/kiauh/extensions/telegram_bot/moonraker_telegram_bot_extension.py @@ -24,7 +24,7 @@ from extensions.telegram_bot.moonraker_telegram_bot import ( ) from utils.common import check_install_dependencies from utils.config_utils import add_config_section, remove_config_section -from utils.fs_utils import remove_file +from utils.fs_utils import remove_with_sudo from utils.git_utils import git_clone_wrapper, git_pull_wrapper from utils.input_utils import get_confirm from utils.instance_utils import get_instances @@ -227,4 +227,4 @@ class TelegramBotExtension(BaseExtension): for log in all_logfiles: Logger.print_status(f"Remove '{log}'") - remove_file(log) + remove_with_sudo(log) diff --git a/kiauh/utils/fs_utils.py b/kiauh/utils/fs_utils.py index dd1db262..ec6494b2 100644 --- a/kiauh/utils/fs_utils.py +++ b/kiauh/utils/fs_utils.py @@ -20,7 +20,6 @@ from typing import List from zipfile import ZipFile from core import backends -from core.decorators import deprecated from core.logger import Logger # Delegate to the shared backends module so tests can substitute @@ -110,17 +109,6 @@ def remove_with_sudo(files: Path | List[Path]) -> bool: return len(_removed) > 0 -@deprecated(info="Use remove_with_sudo instead", replaced_by=remove_with_sudo) -def remove_file(file_path: Path, sudo=False) -> None: - try: - cmd = f"{'sudo ' if sudo else ''}rm -f {file_path}" - run(cmd, stderr=PIPE, check=True, shell=True) - except CalledProcessError as e: - log = f"Cannot remove file {file_path}: {e.stderr.decode()}" - Logger.print_error(log) - raise - - def run_remove_routines(file: Path) -> bool: try: if not backends.filesystem.is_symlink(file) and not backends.filesystem.exists(file): diff --git a/kiauh/utils/tests/test_fs_utils.py b/kiauh/utils/tests/test_fs_utils.py index 75566e4a..aa70115b 100644 --- a/kiauh/utils/tests/test_fs_utils.py +++ b/kiauh/utils/tests/test_fs_utils.py @@ -20,7 +20,6 @@ from utils.fs_utils import ( create_folders, create_symlink, get_data_dir, - remove_file, remove_with_sudo, run_remove_routines, unzip, @@ -147,22 +146,6 @@ class TestRemoveWithSudo: ] -class TestRemoveFile: - def test_calls_shell_rm(self, monkeypatch) -> None: - runs: List[Any] = [] - - def fake_run(cmd: str, **kwargs: Any) -> Any: - runs.append((cmd, kwargs.get("shell"))) - return None - - monkeypatch.setattr("utils.fs_utils.run", fake_run) - - with pytest.warns(DeprecationWarning): - remove_file(Path("/some/file"), sudo=True) - - assert runs == [(f"sudo rm -f {Path('/some/file')}", True)] - - class TestRunRemoveRoutines: def test_returns_false_for_missing(self, tmp_path: Path) -> None: assert run_remove_routines(tmp_path / "missing") is False