forked from Mirror/pmbootstrap
In 0b4fb9119f
(chroot: always run apk static v2 (MR 2423)) we adjusted
install_run_apk() to run apk static on the host and pass in the local
binary repo with "--repository". This function can call apk in two ways,
either with the progress bar handling or without, the second case was
never updated and still ran apk inside the chroot incorrectly and with
an incorrect --repository flag.
Let's finish the job by refactoring helpers/apk.py to support all our
usecases and pointing everything to it, removing the last few situations
where we call "pmb.chroot.root(["apk", ...]).
The apk_with_progress() function is replaced by a generic "run()"
function which takes a boolean to indicate if we should render apk
progress.
Additionally, a new cache_clean() function is added so that "pmbootstrap
zap --pkgs-online-mismatch" can FINALLY be refactored to not rely on a
chroot existing. This requires some hacks but nothing serious, see the
comments in the function for details.
The chroot.init() code is now simplified since handling the --root,
--arch, --cache-dir, and --repository flags is now all done by
apk._prepare_cmd() as and when appropriate.
Lastly, this fixes a (previously unnoticed) bug where apk.static was
actually using /var/cache/apk on your host machine for its cache... This
is definitely not good behaviour....
Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
# Copyright 2023 Oliver Smith
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
import os
|
|
from pmb.core.pkgrepo import pkgrepo_iglob
|
|
from pmb.helpers import logging
|
|
|
|
import pmb.config
|
|
import pmb.chroot.apk
|
|
from pmb.core import Chroot
|
|
|
|
|
|
def list_chroot(suffix: Chroot, remove_prefix: bool = True) -> list[str]:
|
|
ret = []
|
|
prefix = pmb.config.initfs_hook_prefix
|
|
for pkgname in pmb.chroot.apk.installed(suffix).keys():
|
|
if pkgname.startswith(prefix):
|
|
if remove_prefix:
|
|
ret.append(pkgname[len(prefix) :])
|
|
else:
|
|
ret.append(pkgname)
|
|
return ret
|
|
|
|
|
|
def list_aports() -> list[str]:
|
|
ret = []
|
|
prefix = pmb.config.initfs_hook_prefix
|
|
for path in pkgrepo_iglob(f"*/{prefix}*"):
|
|
ret.append(os.path.basename(path)[len(prefix) :])
|
|
return ret
|
|
|
|
|
|
def ls(suffix: Chroot) -> None:
|
|
hooks_chroot = list_chroot(suffix)
|
|
hooks_aports = list_aports()
|
|
|
|
for hook in hooks_aports:
|
|
line = f"* {hook} ({'' if hook in hooks_chroot else 'not '}installed)"
|
|
logging.info(line)
|
|
|
|
|
|
def add(hook: str, suffix: Chroot) -> None:
|
|
if hook not in list_aports():
|
|
raise RuntimeError(
|
|
"Invalid hook name!" " Run 'pmbootstrap initfs hook_ls'" " to get a list of all hooks."
|
|
)
|
|
prefix = pmb.config.initfs_hook_prefix
|
|
pmb.chroot.apk.install([f"{prefix}{hook}"], suffix)
|
|
|
|
|
|
def delete(hook: str, suffix: Chroot) -> None:
|
|
if hook not in list_chroot(suffix):
|
|
raise RuntimeError("There is no such hook installed!")
|
|
prefix = pmb.config.initfs_hook_prefix
|
|
pmb.helpers.apk.run(["del", f"{prefix}{hook}"], suffix)
|
|
|
|
|
|
def update(suffix: Chroot) -> None:
|
|
"""
|
|
Rebuild and update all hooks that are out of date
|
|
"""
|
|
pmb.chroot.apk.install(list_chroot(suffix, False), suffix)
|