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.
This commit is contained in:
Caleb Connolly 2023-06-29 05:25:00 +01:00 committed by Oliver Smith
parent b31dee9ec3
commit 198f302a36
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
90 changed files with 1600 additions and 1121 deletions

View file

@ -10,7 +10,11 @@ See also:
import os
import hashlib
import logging
from pathlib import Path
from typing import List
import pmb.config.pmaports
from pmb.core.types import PmbArgs
import pmb.helpers.http
import pmb.helpers.run
@ -51,7 +55,7 @@ def urls(args, user_repository=True, postmarketos_mirror=True, alpine=True):
:returns: list of mirror strings, like ["/mnt/pmbootstrap/packages",
"http://...", ...]
"""
ret = []
ret: List[str] = []
# Get mirrordirs from channels.cfg (postmarketOS mirrordir is the same as
# the pmaports branch of the channel, no need to make it more complicated)
@ -83,11 +87,12 @@ def urls(args, user_repository=True, postmarketos_mirror=True, alpine=True):
directories.append("testing")
for dir in directories:
ret.append(f"{args.mirror_alpine}{mirrordir_alpine}/{dir}")
return ret
def apkindex_files(args, arch=None, user_repository=True, pmos=True,
alpine=True):
def apkindex_files(args: PmbArgs, arch=None, user_repository=True, pmos=True,
alpine=True) -> List[Path]:
"""Get a list of outside paths to all resolved APKINDEX.tar.gz files for a specific arch.
:param arch: defaults to native
@ -103,12 +108,11 @@ def apkindex_files(args, arch=None, user_repository=True, pmos=True,
# Local user repository (for packages compiled with pmbootstrap)
if user_repository:
channel = pmb.config.pmaports.read_config(args)["channel"]
ret = [f"{args.work}/packages/{channel}/{arch}/APKINDEX.tar.gz"]
ret = [pmb.config.work / "packages" / channel / arch / "APKINDEX.tar.gz"]
# Resolve the APKINDEX.$HASH.tar.gz files
for url in urls(args, False, pmos, alpine):
ret.append(args.work + "/cache_apk_" + arch + "/APKINDEX." +
hash(url) + ".tar.gz")
ret.append(pmb.config.work / f"cache_apk_{arch}" / f"APKINDEX.{hash(url)}.tar.gz")
return ret
@ -146,8 +150,8 @@ def update(args, arch=None, force=False, existing_only=False):
for arch in architectures:
# APKINDEX file name from the URL
url_full = url + "/" + arch + "/APKINDEX.tar.gz"
cache_apk_outside = args.work + "/cache_apk_" + arch
apkindex = cache_apk_outside + "/APKINDEX." + hash(url) + ".tar.gz"
cache_apk_outside = pmb.config.work / f"cache_apk_{arch}"
apkindex = cache_apk_outside / f"APKINDEX.{hash(url)}.tar.gz"
# Find update reason, possibly skip non-existing or known 404 files
reason = None
@ -213,5 +217,5 @@ def alpine_apkindex_path(args, repo="main", arch=None):
# Find it on disk
channel_cfg = pmb.config.pmaports.read_config_channel(args)
repo_link = f"{args.mirror_alpine}{channel_cfg['mirrordir_alpine']}/{repo}"
cache_folder = args.work + "/cache_apk_" + arch
cache_folder = pmb.config.work / "cache_apk_" + arch
return cache_folder + "/APKINDEX." + hash(repo_link) + ".tar.gz"