From 02f04ba3a824cd2497ce048b71dae1e363c8b461 Mon Sep 17 00:00:00 2001 From: Caleb Connolly Date: Fri, 24 May 2024 18:19:07 +0200 Subject: [PATCH] core: move args.log to context.log (MR 2252) Signed-off-by: Caleb Connolly --- pmb/__init__.py | 6 ++++-- pmb/config/__init__.py | 1 - pmb/core/context.py | 7 +++++++ pmb/helpers/args.py | 2 +- pmb/helpers/cli.py | 5 +++-- pmb/helpers/logging.py | 22 +++++++++++++--------- 6 files changed, 28 insertions(+), 15 deletions(-) diff --git a/pmb/__init__.py b/pmb/__init__.py index fce6e025..8c646130 100644 --- a/pmb/__init__.py +++ b/pmb/__init__.py @@ -15,7 +15,8 @@ from .helpers import frontend from .helpers import logging from .helpers import mount from .helpers import other -from .core import Chroot +from .core import Chroot, get_context +from .commands import run_command # pmbootstrap version __version__ = "2.3.1" @@ -30,7 +31,8 @@ if version < (3, 9): def print_log_hint() -> None: - log = config.get("log") + context = get_context() + log = context.log # Hints about the log file (print to stdout only) log_hint = "Run 'pmbootstrap log' for details." if not os.path.exists(log): diff --git a/pmb/config/__init__.py b/pmb/config/__init__.py index 0414d37f..0c32e5bd 100644 --- a/pmb/config/__init__.py +++ b/pmb/config/__init__.py @@ -160,7 +160,6 @@ defaults = { # times on slower devices due to host systems being MUCH faster than the # target device (see issue #429). "iter_time": "200", - "log": "$WORK/log.txt", } allowed_values = { diff --git a/pmb/core/context.py b/pmb/core/context.py index 4d96d66b..e7010caa 100644 --- a/pmb/core/context.py +++ b/pmb/core/context.py @@ -3,13 +3,20 @@ """Global runtime context""" +import pmb.config +from pathlib import Path + + class Context(): details_to_stdout: bool quiet: bool command_timeout: float sudo_timer: bool + log: Path def __init__(self): self.details_to_stdout = False self.command_timeout = 0 self.sudo_timer = False + self.log = pmb.config.work / "log.txt" + self.quiet = False diff --git a/pmb/helpers/args.py b/pmb/helpers/args.py index 0ae4f609..58b873a5 100644 --- a/pmb/helpers/args.py +++ b/pmb/helpers/args.py @@ -92,7 +92,7 @@ def replace_placeholders(args: PmbArgs): setattr(args, key, old.replace("$WORK", str(pmb.config.work))) # Replace ~ (path variables only) - for key in ["aports", "config", "log", "work"]: + for key in ["aports", "config", "work"]: if key in args: setattr(args, key, Path(getattr(args, key)).expanduser()) diff --git a/pmb/helpers/cli.py b/pmb/helpers/cli.py index 694ae18f..ad07732e 100644 --- a/pmb/helpers/cli.py +++ b/pmb/helpers/cli.py @@ -9,6 +9,7 @@ import sys import pmb.config from pmb.core.types import PmbArgs +from pmb.core import get_context class ReadlineTabCompleter: @@ -128,7 +129,7 @@ def progress_print(args: PmbArgs, progress): filled = "\u2588" * chars empty = " " * (width - chars) percent = int(progress * 100) - if pmb.config.is_interactive and not args.details_to_stdout: + if pmb.config.is_interactive and not get_context().details_to_stdout: sys.stdout.write(f"\u001b7{percent:>3}% {filled}{empty}") sys.stdout.flush() sys.stdout.write("\u001b8\u001b[0K") @@ -139,5 +140,5 @@ def progress_flush(args): This will erase the line. Does nothing in non-interactive mode. """ - if pmb.config.is_interactive and not args.details_to_stdout: + if pmb.config.is_interactive and not get_context().details_to_stdout: sys.stdout.flush() diff --git a/pmb/helpers/logging.py b/pmb/helpers/logging.py index c17afb53..d49cd59d 100644 --- a/pmb/helpers/logging.py +++ b/pmb/helpers/logging.py @@ -5,6 +5,8 @@ import os import sys from typing import TextIO import pmb.config +from pmb.core import get_context +from pmb.core.context import Context from pmb.core.types import PmbArgs logfd: TextIO @@ -21,19 +23,19 @@ VERBOSE = 5 class log_handler(logging.StreamHandler): """Write to stdout and to the already opened log file.""" - _args: PmbArgs + context: Context - def __init__(self, args: PmbArgs): + def __init__(self): super().__init__() - self._args = args + self.context = get_context() def emit(self, record): try: msg = self.format(record) # INFO or higher: Write to stdout - if (not self._args.details_to_stdout and - not self._args.quiet and + if (not self.context.details_to_stdout and + not self.context.quiet and record.levelno >= logging.INFO): stream = self.stream @@ -106,18 +108,20 @@ def add_verbose_log_level(): def init(args: PmbArgs): """Set log format and add the log file descriptor to logfd, add the verbose log level.""" global logfd + context = pmb.core.get_context() # Set log file descriptor (logfd) - if args.details_to_stdout: + if context.details_to_stdout: logfd = sys.stdout else: # Require containing directory to exist (so we don't create the work # folder and break the folder migration logic, which needs to set the # version upon creation) - dir = os.path.dirname(args.log) + dir = os.path.dirname(context.log) if os.path.exists(dir): - logfd = open(args.log, "a+") + logfd = open(context.log, "a+") else: logfd = open(os.devnull, "a+") + # FIXME: what? surely this check shouldn't be needed! if args.action != "init": print(f"WARNING: Can't create log file in '{dir}', path" " does not exist!") @@ -135,7 +139,7 @@ def init(args: PmbArgs): root_logger.setLevel(VERBOSE) # Add a custom log handler - handler = log_handler(args) + handler = log_handler() handler.setFormatter(formatter) root_logger.addHandler(handler)