refactor: replace remove_file with remove_with_sudo

This commit is contained in:
dw-0
2026-07-21 19:53:13 +02:00
parent 428a53829c
commit ab60f8cddc
6 changed files with 11 additions and 61 deletions
@@ -41,7 +41,7 @@ from core.simple_config_parser.simple_config_parser import (
from core.types.color import Color from core.types.color import Color
from core.types.component_status import ComponentStatus from core.types.component_status import ComponentStatus
from utils.common import get_install_status 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 ( from utils.git_utils import (
get_latest_remote_tag, get_latest_remote_tag,
get_latest_unstable_tag, get_latest_unstable_tag,
@@ -353,7 +353,7 @@ def create_nginx_cfg(
source = NGINX_SITES_AVAILABLE.joinpath(cfg_name) source = NGINX_SITES_AVAILABLE.joinpath(cfg_name)
target = NGINX_SITES_ENABLED.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) generate_nginx_cfg_from_template(cfg_name, template_src=template_src, **kwargs)
create_symlink(source, target, True) create_symlink(source, target, True)
set_nginx_permissions() set_nginx_permissions()
-25
View File
@@ -1,25 +0,0 @@
# ======================================================================= #
# Copyright (C) 2020 - 2026 Dominik Willner <dev.dw-0@proton.me> #
# #
# 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
@@ -16,7 +16,7 @@ from core.logger import DialogType, Logger
from extensions.base_extension import BaseExtension from extensions.base_extension import BaseExtension
from utils.common import check_install_dependencies from utils.common import check_install_dependencies
from utils.fs_utils import ( from utils.fs_utils import (
remove_file, remove_with_sudo,
) )
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_number_input from utils.input_utils import get_number_input
@@ -91,8 +91,12 @@ class PrettyGcodeExtension(BaseExtension):
# remove pgc dir # remove pgc dir
shutil.rmtree(PGC_DIR) shutil.rmtree(PGC_DIR)
# remove nginx config # remove nginx config
remove_file(NGINX_SITES_AVAILABLE.joinpath(PGC_CONF), True) remove_with_sudo(
remove_file(NGINX_SITES_ENABLED.joinpath(PGC_CONF), True) [
NGINX_SITES_AVAILABLE.joinpath(PGC_CONF),
NGINX_SITES_ENABLED.joinpath(PGC_CONF),
]
)
# restart nginx # restart nginx
cmd_sysctl_service("nginx", "restart") cmd_sysctl_service("nginx", "restart")
@@ -24,7 +24,7 @@ from extensions.telegram_bot.moonraker_telegram_bot import (
) )
from utils.common import check_install_dependencies from utils.common import check_install_dependencies
from utils.config_utils import add_config_section, remove_config_section 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.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.instance_utils import get_instances from utils.instance_utils import get_instances
@@ -227,4 +227,4 @@ class TelegramBotExtension(BaseExtension):
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) remove_with_sudo(log)
-12
View File
@@ -20,7 +20,6 @@ from typing import List
from zipfile import ZipFile from zipfile import ZipFile
from core import backends from core import backends
from core.decorators import deprecated
from core.logger import Logger from core.logger import Logger
# Delegate to the shared backends module so tests can substitute # 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 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: def run_remove_routines(file: Path) -> bool:
try: try:
if not backends.filesystem.is_symlink(file) and not backends.filesystem.exists(file): if not backends.filesystem.is_symlink(file) and not backends.filesystem.exists(file):
-17
View File
@@ -20,7 +20,6 @@ from utils.fs_utils import (
create_folders, create_folders,
create_symlink, create_symlink,
get_data_dir, get_data_dir,
remove_file,
remove_with_sudo, remove_with_sudo,
run_remove_routines, run_remove_routines,
unzip, 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: class TestRunRemoveRoutines:
def test_returns_false_for_missing(self, tmp_path: Path) -> None: def test_returns_false_for_missing(self, tmp_path: Path) -> None:
assert run_remove_routines(tmp_path / "missing") is False assert run_remove_routines(tmp_path / "missing") is False