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

@ -5,7 +5,6 @@ import os
from pathlib import Path
import sys
from typing import Any, Final, TextIO
import pmb.config
from pmb.meta import Cache
logfd: TextIO
@ -29,6 +28,12 @@ class log_handler(logging.StreamHandler):
self.details_to_stdout = details_to_stdout
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):
try:
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):
stream = self.stream
styles = pmb.config.styles
styles = self.styles
msg_col = (
msg.replace(
@ -147,7 +152,6 @@ def init(logfile: Path, verbose: bool, details_to_stdout: bool = False) -> None:
handler.setFormatter(formatter)
root_logger.addHandler(handler)
logging.debug(f"Pmbootstrap v{pmb.__version__} (Python {sys.version})")
if "--password" in sys.argv:
sys.argv[sys.argv.index("--password") + 1] = "[REDACTED]"
logging.debug(f"$ pmbootstrap {' '.join(sys.argv)}")