From e67a34914be8e4d3dd03649d5b8e5602acc228c9 Mon Sep 17 00:00:00 2001 From: actionless Date: Fri, 7 Jun 2024 08:30:52 +0200 Subject: [PATCH] refactor(main, core): move check_runtime_deps() to avoid potential cyclic import --- pikaur/core.py | 47 +-------------------------------------- pikaur/getpkgbuild_cli.py | 4 ++-- pikaur/main.py | 46 +++++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 49 deletions(-) diff --git a/pikaur/core.py b/pikaur/core.py index 26e179660..90748e165 100644 --- a/pikaur/core.py +++ b/pikaur/core.py @@ -14,7 +14,6 @@ import pyalpm from .args import parse_args -from .config import PikaurConfig, RunningAsRoot, UsingDynamicUsers from .i18n import translate from .pprint import ColorsHighlight, bold_line, color_line, print_error, print_stderr from .privilege import sudo @@ -40,7 +39,6 @@ class SpawnArgs(TypedDict): DEFAULT_INPUT_ENCODING: "Final" = "utf-8" DEFAULT_TIMEZONE: "Final" = datetime.datetime.now().astimezone().tzinfo PIPE: "Final" = subprocess.PIPE -SYSTEMD_MIN_VERSION: "Final" = 235 READ_MODE: "Final" = "r" @@ -336,50 +334,7 @@ def dirname(path: str | Path) -> Path: return Path(path).parent if path else Path() -def get_local_pkg(pkg_name: str) -> pyalpm.Package | None: - # pylint: disable=import-outside-toplevel - from .pacman import PackageDB # noqa: PLC0415 - return PackageDB.get_local_pkg_uncached(pkg_name) - - -def check_systemd_dynamic_users_version() -> bool: # pragma: no cover - # @TODO: remove this check later as systemd v 235 is quite OLD already - # pylint: disable=import-outside-toplevel - from .version import split_version # noqa: PLC0415 - pkg = get_local_pkg("systemd") - if not pkg: - return False - version = int(split_version(pkg.version)[0]) - return version >= SYSTEMD_MIN_VERSION - - -def check_runtime_deps(dep_names: list[str] | None = None) -> None: - if sys.version_info < (3, 7): - print_error( - translate("pikaur requires Python >= 3.7 to run."), - ) - sys.exit(65) - if ( - (PikaurConfig().build.DynamicUsers.get_str() != "never" and not parse_args().user_id) - and (UsingDynamicUsers()() and not check_systemd_dynamic_users_version()) - ): - print_error( - translate("pikaur requires systemd >= 235 (dynamic users) to be run as root."), - ) - sys.exit(65) - if not dep_names: - privilege_escalation_tool = PikaurConfig().misc.PrivilegeEscalationTool.get_str() - dep_names = ( - [privilege_escalation_tool] if not RunningAsRoot()() else [] - ) - if not get_local_pkg("base-devel"): - print_error( - translate( - "Read damn arch-wiki before borking your computer", - ), - ) - sys.exit(65) - +def check_executables(dep_names: list[str]) -> None: for dep_bin in dep_names: if not shutil.which(dep_bin): message = translate("executable not found") diff --git a/pikaur/getpkgbuild_cli.py b/pikaur/getpkgbuild_cli.py index d3fc8b876..e469343ae 100644 --- a/pikaur/getpkgbuild_cli.py +++ b/pikaur/getpkgbuild_cli.py @@ -5,7 +5,7 @@ from .args import parse_args from .aur import AURPackageInfo, find_aur_packages, get_repo_url from .aur_deps import get_aur_deps_list -from .core import check_runtime_deps, interactive_spawn +from .core import check_executables, interactive_spawn from .exceptions import PackagesNotFoundInRepoError from .i18n import translate from .pacman import PackageDB @@ -85,7 +85,7 @@ def cli_getpkgbuild() -> None: repo_pkgs.append(repo_pkg) if repo_pkgs: - check_runtime_deps(["pkgctl"]) + check_executables(["pkgctl"]) if not_found_repo_pkgs: print_not_found_packages(not_found_repo_pkgs) diff --git a/pikaur/main.py b/pikaur/main.py index 09d7ecc0b..cfa722761 100644 --- a/pikaur/main.py +++ b/pikaur/main.py @@ -22,12 +22,14 @@ CacheRoot, DataRoot, PikaurConfig, + RunningAsRoot, + UsingDynamicUsers, _OldAurReposCachePath, _UserCacheRoot, ) from .core import ( DEFAULT_INPUT_ENCODING, - check_runtime_deps, + check_executables, interactive_spawn, mkdir, spawn, @@ -39,6 +41,7 @@ from .info_cli import cli_info_packages from .install_cli import InstallPackagesCLI from .logging import create_logger +from .pacman import PackageDB from .pikspect import PikspectSignalHandler from .pkg_cache_cli import cli_clean_packages_cache from .pprint import TTYRestore, print_error, print_stderr, print_warning @@ -55,6 +58,7 @@ from .search_cli import cli_search_packages, search_packages from .updates import print_upgradeable from .urllib_helper import ProxyInitSocks5Error, init_proxy +from .version import split_version if TYPE_CHECKING: from collections.abc import Callable @@ -74,6 +78,7 @@ def init_readline() -> None: init_readline() +SYSTEMD_MIN_VERSION: "Final" = 235 logger = create_logger(f"main_{os.getuid()}") @@ -412,6 +417,45 @@ def __exit__(self, *_exc_details: object) -> None: pass +def get_local_pkg(pkg_name: str) -> pyalpm.Package | None: + return PackageDB.get_local_pkg_uncached(pkg_name) + + +def check_systemd_dynamic_users_version() -> bool: # pragma: no cover + # @TODO: remove this check later as systemd v 235 is quite OLD already + pkg = get_local_pkg("systemd") + if not pkg: + return False + version = int(split_version(pkg.version)[0]) + return version >= SYSTEMD_MIN_VERSION + + +def check_runtime_deps() -> None: + if sys.version_info < (3, 7): + print_error( + translate("pikaur requires Python >= 3.7 to run."), + ) + sys.exit(65) + if ( + (PikaurConfig().build.DynamicUsers.get_str() != "never" and not parse_args().user_id) + and (UsingDynamicUsers()() and not check_systemd_dynamic_users_version()) + ): + print_error( + translate("pikaur requires systemd >= 235 (dynamic users) to be run as root."), + ) + sys.exit(65) + if not get_local_pkg("base-devel"): + print_error( + translate( + "Read damn arch-wiki before borking your computer", + ), + ) + sys.exit(65) + if not RunningAsRoot()(): + privilege_escalation_tool = PikaurConfig().misc.PrivilegeEscalationTool.get_str() + check_executables([privilege_escalation_tool]) + + def main(*, embed: bool = False) -> None: wrapper: type[AbstractContextManager[None]] = OutputEncodingWrapper TTYRestore.save()