treewide: adopt pathlib.Path and type hinting (MR 2252)

With the new chroot type, we can now write fancy paths in the pythonic
way. Convert most of the codebase over, as well as adding various other
type hints.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-04-04 06:14:14 +02:00 committed by Oliver Smith
parent 00383bf354
commit 31cc898dd5
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
64 changed files with 513 additions and 385 deletions

View file

@ -3,15 +3,29 @@
import logging
import os
import sys
from typing import TextIO
import pmb.config
from pmb.core.types import PmbArgs
logfd = None
logfd: TextIO
CRITICAL = logging.CRITICAL
FATAL = logging.FATAL
ERROR = logging.ERROR
WARNING = logging.WARNING
WARN = logging.WARN
INFO = logging.INFO
DEBUG = logging.DEBUG
NOTSET = logging.NOTSET
VERBOSE = 5
class log_handler(logging.StreamHandler):
"""Write to stdout and to the already opened log file."""
_args = None
_args: PmbArgs
def __init__(self, args: PmbArgs):
super().__init__()
self._args = args
def emit(self, record):
try:
@ -80,13 +94,13 @@ def add_verbose_log_level():
All stackoverflow user contributions are licensed as CC-BY-SA:
https://creativecommons.org/licenses/by-sa/3.0/
"""
logging.VERBOSE = 5
logging.addLevelName(logging.VERBOSE, "VERBOSE")
logging.Logger.verbose = lambda inst, msg, * \
args, **kwargs: inst.log(logging.VERBOSE, msg, *args, **kwargs)
logging.verbose = lambda msg, *args, **kwargs: logging.log(logging.VERBOSE,
setattr(logging, "VERBOSE", VERBOSE)
logging.addLevelName(VERBOSE, "VERBOSE")
setattr(logging.Logger, "verbose", lambda inst, msg, * \
args, **kwargs: inst.log(VERBOSE, msg, *args, **kwargs))
setattr(logging, "verbose", lambda msg, *args, **kwargs: logging.log(VERBOSE,
msg, *args,
**kwargs)
**kwargs))
def init(args: PmbArgs):
@ -118,11 +132,10 @@ def init(args: PmbArgs):
add_verbose_log_level()
root_logger.setLevel(logging.DEBUG)
if args.verbose:
root_logger.setLevel(logging.VERBOSE)
root_logger.setLevel(VERBOSE)
# Add a custom log handler
handler = log_handler()
log_handler._args = args
handler = log_handler(args)
handler.setFormatter(formatter)
root_logger.addHandler(handler)