helpers: logging: reduce the risk of a cyclical import (MR 2463)

Lazy load pmb.config.styles and move the pmb.__version__ print elsewhere
so the logging module is (closer to) a standalone entity. This is
necessary to be able to import it in pmb/helpers/apk.py otherwise we get
a cyclical dependency.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-10-27 23:13:29 +01:00
parent 2f39edd584
commit af1bf53867
No known key found for this signature in database
GPG key ID: 0583312B195F64B6
2 changed files with 10 additions and 3 deletions

View file

@ -1,5 +1,7 @@
# Copyright 2023 Oliver Smith # Copyright 2023 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import sys
import pmb.config import pmb.config
from pmb.core.context import Context from pmb.core.context import Context
from pmb.core.pkgrepo import pkgrepo_default_path from pmb.core.pkgrepo import pkgrepo_default_path
@ -89,6 +91,7 @@ def init(args: PmbArgs) -> PmbArgs:
# Initialize logs (we could raise errors below) # Initialize logs (we could raise errors below)
pmb.helpers.logging.init(context.log, args.verbose, context.details_to_stdout) pmb.helpers.logging.init(context.log, args.verbose, context.details_to_stdout)
pmb.helpers.logging.debug(f"Pmbootstrap v{pmb.__version__} (Python {sys.version})")
# Initialization code which may raise errors # Initialization code which may raise errors
if args.action not in [ if args.action not in [

View file

@ -5,7 +5,6 @@ import os
from pathlib import Path from pathlib import Path
import sys import sys
from typing import Any, Final, TextIO from typing import Any, Final, TextIO
import pmb.config
from pmb.meta import Cache from pmb.meta import Cache
logfd: TextIO logfd: TextIO
@ -29,6 +28,12 @@ class log_handler(logging.StreamHandler):
self.details_to_stdout = details_to_stdout self.details_to_stdout = details_to_stdout
self.quiet = False self.quiet = False
# FIXME: importing pmb.config pulls in a whole lot of stuff
# and can easily lead to circular imports, so we defer it until here.
import pmb.config
self.styles = pmb.config.styles
def emit(self, record): def emit(self, record):
try: try:
msg = self.format(record) msg = self.format(record)
@ -37,7 +42,7 @@ class log_handler(logging.StreamHandler):
if self.details_to_stdout or (not self.quiet and record.levelno >= logging.INFO): if self.details_to_stdout or (not self.quiet and record.levelno >= logging.INFO):
stream = self.stream stream = self.stream
styles = pmb.config.styles styles = self.styles
msg_col = ( msg_col = (
msg.replace( msg.replace(
@ -147,7 +152,6 @@ def init(logfile: Path, verbose: bool, details_to_stdout: bool = False) -> None:
handler.setFormatter(formatter) handler.setFormatter(formatter)
root_logger.addHandler(handler) root_logger.addHandler(handler)
logging.debug(f"Pmbootstrap v{pmb.__version__} (Python {sys.version})")
if "--password" in sys.argv: if "--password" in sys.argv:
sys.argv[sys.argv.index("--password") + 1] = "[REDACTED]" sys.argv[sys.argv.index("--password") + 1] = "[REDACTED]"
logging.debug(f"$ pmbootstrap {' '.join(sys.argv)}") logging.debug(f"$ pmbootstrap {' '.join(sys.argv)}")