pmbootstrap-meow/pmb/chroot/initfs_hooks.py
Caleb Connolly 198f302a36
treewide: add a Chroot type and adopt pathlib.Path (MR 2252)
Introduce a new module: pmb.core to contain explicitly typed pmbootstrap
API. The first component being Suffix and SuffixType. This explicitly
defines what suffixes are possible, future changes should aim to further
constrain this API (e.g. by validating against available device
codenames or architectures for buildroot suffixes).

Additionally, migrate the entire codebase over to using pathlib.Path.
This is a relatively new part of the Python standard library that uses a
more object oriented model for path handling. It also uses strong type
hinting and has other features that make it much cleaner and easier to
work with than pure f-strings. The Chroot class overloads the "/"
operator the same way the Path object does, allowing one to write paths
relative to a given chroot as:

builddir = chroot / "home/pmos/build"

The Chroot class also has a string representation ("native", or
"rootfs_valve-jupiter"), and a .path property for directly accessing the
absolute path (as a Path object).

The general idea here is to encapsulate common patterns into type hinted
code, and gradually reduce the amount of assumptions made around the
codebase so that future changes are easier to implement.

As the chroot suffixes are now part of the Chroot class, we also
implement validation for them, this encodes the rules on suffix naming
and will cause a runtime exception if a suffix doesn't follow the rules.
2024-06-23 12:38:37 +02:00

62 lines
1.9 KiB
Python

# Copyright 2023 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
import os
import glob
import logging
import pmb.config
import pmb.chroot.apk
from pmb.core import Chroot
from pmb.core.types import PmbArgs
def list_chroot(args: PmbArgs, suffix: Chroot, remove_prefix=True):
ret = []
prefix = pmb.config.initfs_hook_prefix
for pkgname in pmb.chroot.apk.installed(args, suffix).keys():
if pkgname.startswith(prefix):
if remove_prefix:
ret.append(pkgname[len(prefix):])
else:
ret.append(pkgname)
return ret
def list_aports(args: PmbArgs):
ret = []
prefix = pmb.config.initfs_hook_prefix
for path in glob.glob(f"{args.aports}/*/{prefix}*"):
ret.append(os.path.basename(path)[len(prefix):])
return ret
def ls(args: PmbArgs, suffix: Chroot):
hooks_chroot = list_chroot(args, suffix)
hooks_aports = list_aports(args)
for hook in hooks_aports:
line = f"* {hook} ({'' if hook in hooks_chroot else 'not '}installed)"
logging.info(line)
def add(args: PmbArgs, hook, suffix: Chroot):
if hook not in list_aports(args):
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(args, [f"{prefix}{hook}"], suffix)
def delete(args: PmbArgs, hook, suffix: Chroot):
if hook not in list_chroot(args, suffix):
raise RuntimeError("There is no such hook installed!")
prefix = pmb.config.initfs_hook_prefix
pmb.chroot.root(args, ["apk", "del", f"{prefix}{hook}"], suffix)
def update(args: PmbArgs, suffix: Chroot):
"""
Rebuild and update all hooks that are out of date
"""
pmb.chroot.apk.install(args, list_chroot(args, suffix, False), suffix)