core: introduce global context (MR 2252)

We can't totally escape the need for some runtime state defined by args.
To make the migration easier, introduce a global "Context" class and
move some of the read-only global options there.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-05-24 17:57:31 +02:00 committed by Oliver Smith
parent b82c4eb167
commit 2a7c769e14
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
6 changed files with 56 additions and 11 deletions

View file

@ -1,6 +1,7 @@
# Copyright 2023 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
import fcntl
from pmb.core import get_context
from pmb.core.types import PathString, Env
from pmb.helpers import logging
import os
@ -175,6 +176,7 @@ def foreground_pipe(args: PmbArgs, cmd, working_dir=None, output_to_stdout=False
* output: ""
* output: full program output string (output_return is True)
"""
context = pmb.core.get_context()
# Start process in background (stdout and stderr combined)
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, cwd=working_dir,
@ -193,7 +195,7 @@ def foreground_pipe(args: PmbArgs, cmd, working_dir=None, output_to_stdout=False
output_buffer: list[bytes] = []
sel = selectors.DefaultSelector()
sel.register(stdout, selectors.EVENT_READ)
timeout = args.timeout
timeout = context.command_timeout
while process.poll() is None:
wait_start = time.perf_counter()
sel.select(timeout)
@ -203,12 +205,12 @@ def foreground_pipe(args: PmbArgs, cmd, working_dir=None, output_to_stdout=False
# timeout was not reached.)
if output_timeout:
wait_end = time.perf_counter()
if wait_end - wait_start >= args.timeout:
if wait_end - wait_start >= timeout:
logging.info("Process did not write any output for " +
str(args.timeout) + " seconds. Killing it.")
str(timeout) + " seconds. Killing it.")
logging.info("NOTE: The timeout can be increased with"
" 'pmbootstrap -t'.")
kill_command(args, process.pid, sudo)
kill_command(process.pid, sudo)
continue
# Read all currently available output