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

@ -2,13 +2,14 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import os
from pathlib import Path
import subprocess
import pmb.helpers.run_core
from typing import Any, Dict, List, Optional, Sequence
from pmb.core.types import PathString, PmbArgs
from pmb.core.types import Env, PathString, PmbArgs
def user(args: PmbArgs, cmd: Sequence[PathString], working_dir: Path=Path("/"), output: str="log", output_return: bool=False,
check: Optional[bool]=None, env: Dict[Any, Any]={}, sudo: bool=False) -> str:
def user(args: PmbArgs, cmd: Sequence[PathString], working_dir: Optional[Path] = None, output: str = "log", output_return: bool = False,
check: Optional[bool] = None, env: Env = {}, sudo: bool = False) -> str | int | subprocess.Popen:
"""
Run a command on the host system as user.
@ -22,8 +23,8 @@ def user(args: PmbArgs, cmd: Sequence[PathString], working_dir: Path=Path("/"),
# Readable log message (without all the escaping)
msg = "% "
for key, value in env.items():
msg += key + "=" + value + " "
if working_dir != Path("/"):
msg += f"{key}={value} "
if working_dir is not None:
msg += f"cd {os.fspath(working_dir)}; "
msg += " ".join(cmd_parts)
@ -35,6 +36,15 @@ def user(args: PmbArgs, cmd: Sequence[PathString], working_dir: Path=Path("/"),
return pmb.helpers.run_core.core(args, msg, cmd_parts, working_dir, output,
output_return, check, sudo)
# FIXME: should probably use some kind of wrapper class / builder pattern for all these parameters...
def user_output(args: PmbArgs, cmd: Sequence[PathString], working_dir: Optional[Path] = None, output: str = "log",
check: Optional[bool] = None, env: Env = {}, sudo: bool = False) -> str:
ret = user(args, cmd, working_dir, output, output_return=True, check=check, env=env, sudo=sudo)
if not isinstance(ret, str):
raise TypeError("Expected str output, got " + str(ret))
return ret
def root(args: PmbArgs, cmd: Sequence[PathString], working_dir=None, output="log", output_return=False,
check=None, env={}):