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

@ -4,7 +4,8 @@ import copy
import os
from pathlib import Path
import pmb.config
from pmb.core.types import PmbArgs
from pmb.core.context import Context
from pmb.types import PmbArgs
import pmb.helpers.git
import pmb.helpers.args
@ -32,7 +33,7 @@ import pmb.helpers.args
Examples:
args.aports ("$WORK/cache_git/pmaports", override with --aports)
args.device ("samsung-i9100", "qemu-amd64" etc.)
pmb.config.work ("/home/user/.local/var/pmbootstrap", override with --work)
get_context().config.work ("/home/user/.local/var/pmbootstrap", override with --work)
3. Parsed configs
Similar to the cache above, specific config files get parsed and added
@ -46,28 +47,6 @@ import pmb.helpers.args
"""
def fix_mirrors_postmarketos(args: PmbArgs):
"""Fix args.mirrors_postmarketos when it is supposed to be empty or the default value.
In pmb/parse/arguments.py, we set the -mp/--mirror-pmOS argument to
action="append" and start off with an empty list. That way, users can
specify multiple custom mirrors by specifying -mp multiple times on the
command line. Here we fix the default and no mirrors case.
NOTE: we don't use nargs="+", because it does not play nicely with
subparsers: <https://bugs.python.org/issue9338>
"""
# -mp not specified: use default mirrors
if not args.mirrors_postmarketos:
cfg = pmb.config.load(args)
args.mirrors_postmarketos = \
cfg["pmbootstrap"]["mirrors_postmarketos"].split(",")
# -mp="": use no postmarketOS mirrors (build everything locally)
if args.mirrors_postmarketos == [""]:
args.mirrors_postmarketos = []
def check_pmaports_path(args: PmbArgs):
"""Make sure that args.aports exists when it was overridden by --aports.
@ -79,28 +58,28 @@ def check_pmaports_path(args: PmbArgs):
f" not exist: {args.aports}")
def replace_placeholders(args: PmbArgs):
"""Replace $WORK and ~ (for path variables) in variables from any config.
# def replace_placeholders(args: PmbArgs):
# """Replace $WORK and ~ (for path variables) in variables from any config.
(user's config file, default config settings or config parameters specified on commandline)
"""
# Replace $WORK
for key, value in pmb.config.defaults.items():
if key not in args:
continue
old = getattr(args, key)
if isinstance(old, str):
setattr(args, key, old.replace("$WORK", str(pmb.config.work)))
# (user's config file, default config settings or config parameters specified on commandline)
# """
# # Replace $WORK
# for key, value in pmb.config.defaults.items():
# if key not in args:
# continue
# old = getattr(args, key)
# if isinstance(old, str):
# setattr(args, key, old.replace("$WORK", str(get_context().config.work)))
# Replace ~ (path variables only)
for key in ["aports", "config", "work"]:
if key in args:
setattr(args, key, Path(getattr(args, key)).expanduser())
# # Replace ~ (path variables only)
# for key in ["aports", "config", "work"]:
# if key in args:
# setattr(args, key, Path(getattr(args, key)).expanduser())
def add_deviceinfo(args: PmbArgs):
"""Add and verify the deviceinfo (only after initialization)"""
setattr(args, "deviceinfo", pmb.parse.deviceinfo(args))
setattr(args, "deviceinfo", pmb.parse.deviceinfo())
arch = args.deviceinfo["arch"]
if (arch != pmb.config.arch_native and
arch not in pmb.config.build_device_architectures):
@ -111,20 +90,28 @@ def add_deviceinfo(args: PmbArgs):
def init(args: PmbArgs) -> PmbArgs:
# Basic initialization
fix_mirrors_postmarketos(args)
pmb.config.merge_with_args(args)
replace_placeholders(args)
config = pmb.config.load(args)
# pmb.config.merge_with_args(args)
# replace_placeholders(args)
# Configure runtime context
context = pmb.core.get_context()
context = Context(config)
context.command_timeout = args.timeout
context.details_to_stdout = args.details_to_stdout
context.sudo_timer = args.sudo_timer
context.quiet = args.quiet
context.offline = args.offline
context.command = args.action
context.cross = args.cross
if args.mirrors_postmarketos:
context.config.mirrors_postmarketos = args.mirrors_postmarketos
if args.mirror_alpine:
context.config.mirror_alpine = args.mirror_alpine
if args.aports:
print(f"Using pmaports from: {args.aports}")
context.aports = args.aports
context.config.aports = args.aports
# Initialize context
pmb.core.set_context(context)
# Initialize logs (we could raise errors below)
pmb.helpers.logging.init(args)
@ -133,19 +120,22 @@ def init(args: PmbArgs) -> PmbArgs:
check_pmaports_path(args)
if args.action not in ["init", "checksum", "config", "bootimg_analyze", "log",
"pull", "shutdown", "zap"]:
pmb.config.pmaports.read_config(args)
pmb.config.pmaports.read_config()
add_deviceinfo(args)
pmb.helpers.git.parse_channels_cfg()
pmb.helpers.git.parse_channels_cfg(config.aports)
context.device_arch = args.deviceinfo["arch"]
# Remove attributes from args so they don't get used by mistake
delattr(args, "timeout")
delattr(args, "details_to_stdout")
delattr(args, "sudo_timer")
delattr(args, "log")
delattr(args, "quiet")
delattr(args, "offline")
delattr(args, "aports")
delattr(args, "mirrors_postmarketos")
delattr(args, "mirror_alpine")
# args.work is deprecated!
delattr(args, "work")
return args