pmbootstrap-meow/pmb/export/symlinks.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

76 lines
2.7 KiB
Python

# Copyright 2023 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
import logging
from pathlib import Path
from typing import List
import pmb.build
import pmb.chroot.apk
import pmb.config
import pmb.config.pmaports
from pmb.core.types import PmbArgs
import pmb.flasher
import pmb.helpers.file
from pmb.core import Chroot, ChrootType
def symlinks(args: PmbArgs, flavor, folder: Path):
"""
Create convenience symlinks to the rootfs and boot files.
"""
# Backwards compatibility with old mkinitfs (pma#660)
suffix = f"-{flavor}"
pmaports_cfg = pmb.config.pmaports.read_config(args)
if pmaports_cfg.get("supported_mkinitfs_without_flavors", False):
suffix = ""
# File descriptions
info = {
f"boot.img{suffix}": ("Fastboot compatible boot.img file,"
" contains initramfs and kernel"),
"dtbo.img": "Fastboot compatible dtbo image",
f"initramfs{suffix}": "Initramfs",
f"initramfs{suffix}-extra": "Extra initramfs files in /boot",
f"uInitrd{suffix}": "Initramfs, legacy u-boot image format",
f"uImage{suffix}": "Kernel, legacy u-boot image format",
f"vmlinuz{suffix}": "Linux kernel",
f"{args.device}.img": "Rootfs with partitions for /boot and /",
f"{args.device}-boot.img": "Boot partition image",
f"{args.device}-root.img": "Root partition image",
f"pmos-{args.device}.zip": "Android recovery flashable zip",
"lk2nd.img": "Secondary Android bootloader",
}
# Generate a list of patterns
chroot_native = Chroot.native()
path_boot = Chroot(ChrootType.ROOTFS, args.device) / "boot"
chroot_buildroot = Chroot(ChrootType.BUILDROOT, args.deviceinfo['arch'])
files: List[Path] = [
path_boot / f"boot.img{suffix}",
path_boot / f"uInitrd{suffix}",
path_boot / f"uImage{suffix}",
path_boot / f"vmlinuz{suffix}",
path_boot / "dtbo.img",
chroot_native / "home/pmos/rootfs" / f"{args.device}.img",
chroot_native / "home/pmos/rootfs" / f"{args.device}-boot.img",
chroot_native / "home/pmos/rootfs" / f"{args.device}-root.img",
chroot_buildroot / "var/libpostmarketos-android-recovery-installer" /
f"pmos-{args.device}.zip",
path_boot / "lk2nd.img"
]
files += list(path_boot.glob(f"initramfs{suffix}*"))
# Iterate through all files
for file in files:
basename = file.name
link = folder / basename
# Display a readable message
msg = " * " + basename
if basename in info:
msg += " (" + info[basename] + ")"
logging.info(msg)
pmb.helpers.file.symlink(args, file, link)