WIP: start ripping out args (MR 2252)

Cease merging pmbootstrap.cfg into args, implement a Context type to let
us pull globals out of thin air (as an intermediate workaround) and rip
args out of a lot of the codebase.

This is just a first pass, after this we can split all the state that
leaked over into Context into types with narrower scopes (like a
BuildContext(), etc).

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-05-25 03:59:04 +02:00 committed by Oliver Smith
parent bfea00e03a
commit 34dd9d42ba
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
129 changed files with 1393 additions and 1300 deletions

View file

@ -1,5 +1,6 @@
# Copyright 2023 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
from pmb.core import get_context
from pmb.core.chroot import Chroot
from pmb.helpers import logging
import glob
@ -11,7 +12,7 @@ from typing import Any, List
import pmb.aportgen
import pmb.config
import pmb.config.pmaports
from pmb.core.types import PmbArgs
from pmb.types import PmbArgs
import pmb.helpers.cli
import pmb.helpers.devices
import pmb.helpers.git
@ -67,7 +68,7 @@ def ask_for_work_path(args: PmbArgs):
while True:
try:
work = os.path.expanduser(pmb.helpers.cli.ask(
"Work path", None, pmb.config.work, False))
"Work path", None, get_context().config.work, False))
work = os.path.realpath(work)
exists = os.path.exists(work)
@ -107,7 +108,7 @@ def ask_for_channel(args: PmbArgs):
:returns: channel name (e.g. "edge", "v21.03")
"""
channels_cfg = pmb.helpers.git.parse_channels_cfg(args)
channels_cfg = pmb.helpers.git.parse_channels_cfg(get_context().config.aports)
count = len(channels_cfg["channels"])
# List channels
@ -123,7 +124,7 @@ def ask_for_channel(args: PmbArgs):
# Otherwise, if valid: channel from pmaports.cfg of current branch
# The actual channel name is not saved in pmbootstrap.cfg, because then we
# would need to sync it with what is checked out in pmaports.git.
default = pmb.config.pmaports.read_config(args)["channel"]
default = pmb.config.pmaports.read_config()["channel"]
choices = channels_cfg["channels"].keys()
if args.is_default_channel or default not in choices:
default = channels_cfg["meta"]["recommended"]
@ -145,7 +146,7 @@ def ask_for_ui(args: PmbArgs, info):
if not device_is_accelerated:
for i in reversed(range(len(ui_list))):
pkgname = f"postmarketos-ui-{ui_list[i][0]}"
apkbuild = pmb.helpers.pmaports.get(args, pkgname,
apkbuild = pmb.helpers.pmaports.get(pkgname,
subpackages=False,
must_exist=False)
if apkbuild and "pmb:gpu-accel" in apkbuild["options"]:
@ -176,7 +177,7 @@ def ask_for_ui(args: PmbArgs, info):
def ask_for_ui_extras(args: PmbArgs, ui):
apkbuild = pmb.helpers.pmaports.get(args, f"postmarketos-ui-{ui}",
apkbuild = pmb.helpers.pmaports.get(f"postmarketos-ui-{ui}",
subpackages=False, must_exist=False)
if not apkbuild:
return False
@ -193,15 +194,15 @@ def ask_for_ui_extras(args: PmbArgs, ui):
def ask_for_systemd(args: PmbArgs, ui):
if "systemd" not in pmb.config.pmaports.read_config_repos(args):
if "systemd" not in pmb.config.pmaports.read_config_repos():
return args.systemd
if pmb.helpers.ui.check_option(args, ui, "pmb:systemd-never"):
if pmb.helpers.ui.check_option(ui, "pmb:systemd-never"):
logging.info("Based on your UI selection, OpenRC will be used as init"
" system. This UI does not support systemd.")
return args.systemd
default_is_systemd = pmb.helpers.ui.check_option(args, ui, "pmb:systemd")
default_is_systemd = pmb.helpers.ui.check_option(ui, "pmb:systemd")
not_str = " " if default_is_systemd else " not "
logging.info("Based on your UI selection, 'default' will result"
f" in{not_str}installing systemd.")
@ -267,7 +268,7 @@ def ask_for_provider_select(args: PmbArgs, apkbuild, providers_cfg):
providers. Updated with new providers after selection
"""
for select in apkbuild["_pmb_select"]:
providers = pmb.helpers.pmaports.find_providers(args, select)
providers = pmb.helpers.pmaports.find_providers(select)
logging.info(f"Available providers for {select} ({len(providers)}):")
has_default = False
@ -322,7 +323,7 @@ def ask_for_provider_select_pkg(args: PmbArgs, pkgname, providers_cfg):
:param providers_cfg: the configuration section with previously selected
providers. Updated with new providers after selection
"""
apkbuild = pmb.helpers.pmaports.get(args, pkgname,
apkbuild = pmb.helpers.pmaports.get(pkgname,
subpackages=False, must_exist=False)
if not apkbuild:
return
@ -330,7 +331,7 @@ def ask_for_provider_select_pkg(args: PmbArgs, pkgname, providers_cfg):
ask_for_provider_select(args, apkbuild, providers_cfg)
def ask_for_device_kernel(args: PmbArgs, device):
def ask_for_device_kernel(args: PmbArgs, device: str):
"""Ask for the kernel that should be used with the device.
:param device: code name, e.g. "lg-mako"
@ -341,7 +342,7 @@ def ask_for_device_kernel(args: PmbArgs, device):
"""
# Get kernels
kernels = pmb.parse._apkbuild.kernels(args, device)
kernels = pmb.parse._apkbuild.kernels(device)
if not kernels:
return args.kernel
@ -383,7 +384,7 @@ def ask_for_device(args: PmbArgs):
* device_exists: bool indicating if device port exists in repo
* kernel: type of kernel (downstream, etc)
"""
vendors = sorted(pmb.helpers.devices.list_vendors(args))
vendors = sorted(pmb.helpers.devices.list_vendors(get_context().config.aports))
logging.info("Choose your target device vendor (either an "
"existing one, or a new one for porting).")
logging.info(f"Available vendors ({len(vendors)}): {', '.join(vendors)}")
@ -409,7 +410,7 @@ def ask_for_device(args: PmbArgs):
else:
# Archived devices can be selected, but are not displayed
devices = sorted(pmb.helpers.devices.list_codenames(
args, vendor, archived=False))
get_context().config.aports, vendor, archived=False))
# Remove "vendor-" prefixes from device list
codenames = [x.split('-', 1)[1] for x in devices]
logging.info(f"Available codenames ({len(codenames)}): " +
@ -422,7 +423,7 @@ def ask_for_device(args: PmbArgs):
codenames)
device = f"{vendor}-{codename}"
device_path = pmb.helpers.devices.find_path(args, device, 'deviceinfo')
device_path = pmb.helpers.devices.find_path(device, 'deviceinfo')
if device_path is None:
if device == args.device:
raise RuntimeError(
@ -460,7 +461,7 @@ def ask_for_additional_options(args: PmbArgs, cfg):
f" parallel jobs: {args.jobs},"
f" ccache per arch: {args.ccache_size},"
f" sudo timer: {context.sudo_timer},"
f" mirror: {','.join(args.mirrors_postmarketos)}")
f" mirror: {','.join(context.config.mirrors_postmarketos)}")
if not pmb.helpers.cli.confirm(args, "Change them?",
default=False):
@ -517,7 +518,7 @@ def ask_for_additional_options(args: PmbArgs, cfg):
# Mirrors
# prompt for mirror change
logging.info("Selected mirror:"
f" {','.join(args.mirrors_postmarketos)}")
f" {','.join(context.config.mirrors_postmarketos)}")
if pmb.helpers.cli.confirm(args, "Change mirror?", default=False):
mirrors = ask_for_mirror(args)
cfg["pmbootstrap"]["mirrors_postmarketos"] = ",".join(mirrors)
@ -527,7 +528,7 @@ def ask_for_mirror(args: PmbArgs):
regex = "^[1-9][0-9]*$" # single non-zero number only
json_path = pmb.helpers.http.download(
args, "https://postmarketos.org/mirrors.json", "pmos_mirrors",
"https://postmarketos.org/mirrors.json", "pmos_mirrors",
cache=False)
with open(json_path, "rt") as handle:
s = handle.read()
@ -558,7 +559,7 @@ def ask_for_mirror(args: PmbArgs):
urls.append(link_list[0])
mirror_indexes = []
for mirror in args.mirrors_postmarketos:
for mirror in get_context().config.mirrors_postmarketos:
for i in range(len(urls)):
if urls[i] == mirror:
mirror_indexes.append(str(i + 1))
@ -647,64 +648,64 @@ def frontend(args: PmbArgs):
require_programs()
# Work folder (needs to be first, so we can create chroots early)
cfg = pmb.config.load(args)
work, work_exists = ask_for_work_path(args)
cfg["pmbootstrap"]["work"] = work
config = pmb.config.load(args)
config.work, work_exists = ask_for_work_path(args)
# Update args and save config (so chroots and 'pmbootstrap log' work)
pmb.helpers.args.update_work(args, work)
pmb.config.save(args, cfg)
pmb.helpers.args.update_work(args, config.work)
pmb.config.save(args.config, config)
# Migrate work dir if necessary
pmb.helpers.other.migrate_work_folder(args)
# Clone pmaports
pmb.config.pmaports.init(args)
pmb.config.pmaports.init()
# Choose release channel, possibly switch pmaports branch
channel = ask_for_channel(args)
pmb.config.pmaports.switch_to_channel_branch(args, channel)
cfg["pmbootstrap"]["is_default_channel"] = "False"
# FIXME: ???
config.is_default_channel = False
# Copy the git hooks if master was checked out. (Don't symlink them and
# only do it on master, so the git hooks don't change unexpectedly when
# having a random branch checked out.)
branch_current = pmb.helpers.git.rev_parse(args.aports,
branch_current = pmb.helpers.git.rev_parse(get_context().config.aports,
extra_args=["--abbrev-ref"])
if branch_current == "master":
logging.info("NOTE: pmaports is on master branch, copying git hooks.")
pmb.config.pmaports.install_githooks(args)
pmb.config.pmaports.install_githooks()
# Device
device, device_exists, kernel = ask_for_device(args)
cfg["pmbootstrap"]["device"] = device
cfg["pmbootstrap"]["kernel"] = kernel
config.device = device
config.kernel = kernel
info = pmb.parse.deviceinfo(args, device)
apkbuild_path = pmb.helpers.devices.find_path(args, device, 'APKBUILD')
apkbuild_path = pmb.helpers.devices.find_path(device, 'APKBUILD')
if apkbuild_path:
apkbuild = pmb.parse.apkbuild(apkbuild_path)
ask_for_provider_select(args, apkbuild, cfg["providers"])
ask_for_provider_select(args, apkbuild, config.providers)
# Device keymap
if device_exists:
cfg["pmbootstrap"]["keymap"] = ask_for_keymaps(args, info)
config.keymap = ask_for_keymaps(args, info)
cfg["pmbootstrap"]["user"] = ask_for_username(args)
ask_for_provider_select_pkg(args, "postmarketos-base", cfg["providers"])
ask_for_provider_select_pkg(args, "postmarketos-base-ui", cfg["providers"])
config.user = ask_for_username(args)
ask_for_provider_select_pkg(args, "postmarketos-base", config.providers)
ask_for_provider_select_pkg(args, "postmarketos-base-ui", config.providers)
# UI and various build options
ui = ask_for_ui(args, info)
cfg["pmbootstrap"]["ui"] = ui
cfg["pmbootstrap"]["ui_extras"] = str(ask_for_ui_extras(args, ui))
config.ui = ui
config.ui_extras = ask_for_ui_extras(args, ui)
# systemd
cfg["pmbootstrap"]["systemd"] = ask_for_systemd(args, ui)
config.systemd = ask_for_systemd(args, ui)
ask_for_provider_select_pkg(args, f"postmarketos-ui-{ui}",
cfg["providers"])
ask_for_additional_options(args, cfg)
config.providers)
ask_for_additional_options(args, config)
# Extra packages to be installed to rootfs
logging.info("Additional packages that will be installed to rootfs."
@ -713,29 +714,28 @@ def frontend(args: PmbArgs):
extra = pmb.helpers.cli.ask("Extra packages", None,
args.extra_packages,
validation_regex=r"^([-.+\w]+)(,[-.+\w]+)*$")
cfg["pmbootstrap"]["extra_packages"] = extra
config.extra_packages = extra
# Configure timezone info
cfg["pmbootstrap"]["timezone"] = ask_for_timezone(args)
config.timezone = ask_for_timezone(args)
# Locale
cfg["pmbootstrap"]["locale"] = ask_for_locale(args)
config.locale = ask_for_locale(args)
# Hostname
cfg["pmbootstrap"]["hostname"] = ask_for_hostname(args, device)
config.hostname = ask_for_hostname(args, device)
# SSH keys
cfg["pmbootstrap"]["ssh_keys"] = str(ask_for_ssh_keys(args))
config.ssh_keys = ask_for_ssh_keys(args)
# pmaports path (if users change it with: 'pmbootstrap --aports=... init')
cfg["pmbootstrap"]["aports"] = args.aports
config.aports = get_context().config.aports
# Build outdated packages in pmbootstrap install
cfg["pmbootstrap"]["build_pkgs_on_install"] = str(
ask_build_pkgs_on_install(args))
config.build_pkgs_on_install = ask_build_pkgs_on_install(args)
# Save config
pmb.config.save(args, cfg)
pmb.config.save(args.config, config)
# Zap existing chroots
if (work_exists and device_exists and