pmbootstrap-meow/pmb/config/merge_with_args.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

46 lines
1.7 KiB
Python

# Copyright 2023 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
from pathlib import Path
import pmb.config
from pmb.core.types import PmbArgs
def merge_with_args(args: PmbArgs):
"""We have the internal config (pmb/config/__init__.py) and the user config
(usually ~/.config/pmbootstrap.cfg, can be changed with the '-c'
parameter).
Args holds the variables parsed from the commandline (e.g. -j fills out
args.jobs), and values specified on the commandline count the most.
In case it is not specified on the commandline, for the keys in
pmb.config.config_keys, we look into the value set in the the user config.
When that is empty as well (e.g. just before pmbootstrap init), or the key
is not in pmb.config_keys, we use the default value from the internal
config.
"""
# Use defaults from the user's config file
cfg = pmb.config.load(args)
for key in cfg["pmbootstrap"]:
if key not in args or getattr(args, key) is None:
value = cfg["pmbootstrap"][key]
if key in pmb.config.defaults:
default = pmb.config.defaults[key]
if isinstance(default, bool):
value = (value.lower() == "true")
setattr(args, key, value)
setattr(args, 'selected_providers', cfg['providers'])
# Use defaults from pmb.config.defaults
for key, value in pmb.config.defaults.items():
if key not in args or getattr(args, key) is None:
setattr(args, key, value)
pmb.config.work_dir(Path(cfg["pmbootstrap"]["work"]))
# Make sure args.aports is a Path object
setattr(args, "aports", Path(args.aports))
# args.work is deprecated!
delattr(args, "work")