Skip to content

Commit

Permalink
refactor(main, core): move check_runtime_deps() to avoid potential cy…
Browse files Browse the repository at this point in the history
…clic import
  • Loading branch information
actionless committed Jun 7, 2024
1 parent 0b94fd0 commit e67a349
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 49 deletions.
47 changes: 1 addition & 46 deletions pikaur/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"


Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions pikaur/getpkgbuild_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
46 changes: 45 additions & 1 deletion pikaur/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -74,6 +78,7 @@ def init_readline() -> None:

init_readline()

SYSTEMD_MIN_VERSION: "Final" = 235
logger = create_logger(f"main_{os.getuid()}")


Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit e67a349

Please sign in to comment.