pmb: Make RunOutputTypeDefault and RunOutputTypePopen enums

This allows us to get rid of some of the validation in sanity_checks()
as mypy handles this validation at "build time", and any typos in the
enum instantiation would be a runtime error rather than a silent
failure.

Additionally, it allows us to encode some of the behaviour of the
different output types into the type definition itself by using methods.

Part-of: https://gitlab.postmarketos.org/postmarketOS/pmbootstrap/-/merge_requests/2642
This commit is contained in:
Newbyte 2025-07-07 17:00:58 +02:00 committed by Oliver Smith
parent 77b2717d66
commit 7d2f055bcb
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
20 changed files with 167 additions and 84 deletions

View file

@ -16,7 +16,7 @@ import pmb.config
import pmb.helpers.pmaports
import pmb.helpers.run
from pmb.meta import Cache
from pmb.types import PathString
from pmb.types import PathString, RunOutputTypeDefault
re_branch_aports = re.compile(r"^\d+\.\d\d+-stable$")
re_branch_pmaports = re.compile(r"^v\d\d\.\d\d$")
@ -56,7 +56,7 @@ def clone(name_repo: str) -> None:
# Create parent dir and clone
logging.info(f"Clone git repository: {url}")
(get_context().config.work / "cache_git").mkdir(exist_ok=True)
pmb.helpers.run.user(command, output="stdout")
pmb.helpers.run.user(command, output=RunOutputTypeDefault.STDOUT)
# FETCH_HEAD does not exist after initial clone. Create it, so
# is_outdated() can use it.
@ -77,7 +77,9 @@ def rev_parse(
or (with ``--abbrev-ref``): the branch name, e.g. "master"
"""
command = ["git", "rev-parse", *extra_args, revision]
rev = pmb.helpers.run.user_output(command, path, output="null" if silent else "log")
rev = pmb.helpers.run.user_output(
command, path, output=RunOutputTypeDefault.NULL if silent else RunOutputTypeDefault.LOG
)
return rev.rstrip()
@ -95,12 +97,17 @@ def can_fast_forward(path: Path, branch_upstream: str, branch: str = "HEAD") ->
def clean_worktree(path: Path, silent: bool = False) -> bool:
"""Check if there are not any modified files in the git dir."""
command = ["git", "status", "--porcelain"]
return pmb.helpers.run.user_output(command, path, output="null" if silent else "log") == ""
return (
pmb.helpers.run.user_output(
command, path, output=RunOutputTypeDefault.NULL if silent else RunOutputTypeDefault.LOG
)
== ""
)
def list_remotes(aports: Path) -> list[str]:
command = ["git", "remote", "-v"]
output = pmb.helpers.run.user_output(command, aports, output="null")
output = pmb.helpers.run.user_output(command, aports, output=RunOutputTypeDefault.NULL)
return output.splitlines()
@ -159,7 +166,7 @@ def set_remote_url(repo: Path, remote_name: str, remote_url: str, remote_type: R
"--push" if remote_type == RemoteType.PUSH else "--no-push",
]
pmb.helpers.run.user(command, output="stdout")
pmb.helpers.run.user(command, output=RunOutputTypeDefault.STDOUT)
# Intentionally lower case for case-insensitive comparison
@ -215,7 +222,9 @@ def parse_channels_cfg(aports: Path) -> dict:
cfg = configparser.ConfigParser()
remote = get_upstream_remote(aports)
command = ["git", "show", f"{remote}/master:channels.cfg"]
stdout = pmb.helpers.run.user_output(command, aports, output="null", check=False)
stdout = pmb.helpers.run.user_output(
command, aports, output=RunOutputTypeDefault.NULL, check=False
)
try:
cfg.read_string(stdout)
except configparser.MissingSectionHeaderError:
@ -329,7 +338,7 @@ def pull(repo_name: str) -> int:
# Fast-forward now (should not fail due to checks above, so it's fine to
# throw an exception on error)
command = ["git", "merge", "--ff-only", branch_upstream]
pmb.helpers.run.user(command, repo, "stdout")
pmb.helpers.run.user(command, repo, RunOutputTypeDefault.STDOUT)
return 0