From: Zbigniew Jędrzejewski-Szmek Date: Sat, 2 Mar 2024 11:01:54 +0000 (+0100) Subject: Add little helper that combines str.startswith and str.removeprefix X-Git-Tag: v21~8^2 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2451%2Fhead;p=thirdparty%2Fmkosi.git Add little helper that combines str.startswith and str.removeprefix This way we don't need to repeat the prefix string. --- diff --git a/mkosi/config.py b/mkosi/config.py index 5eaef6b1b..d40c39ed4 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -41,6 +41,7 @@ from mkosi.util import ( flatten, is_power_of_2, make_executable, + startswith, ) from mkosi.versioncomp import GenericVersion @@ -637,9 +638,11 @@ def config_default_release(namespace: argparse.Namespace) -> str: def config_default_source_date_epoch(namespace: argparse.Namespace) -> Optional[int]: for env in namespace.environment: - if env.startswith("SOURCE_DATE_EPOCH="): - return config_parse_source_date_epoch(env.removeprefix("SOURCE_DATE_EPOCH="), None) - return config_parse_source_date_epoch(os.environ.get("SOURCE_DATE_EPOCH"), None) + if s := startswith(env, "SOURCE_DATE_EPOCH="): + break + else: + s = os.environ.get("SOURCE_DATE_EPOCH") + return config_parse_source_date_epoch(s, None) def config_default_kernel_command_line(namespace: argparse.Namespace) -> list[str]: @@ -723,9 +726,9 @@ def config_match_version(match: str, value: str) -> bool: ">": operator.gt, "<": operator.lt, }.items(): - if match.startswith(sigil): + if (rhs := startswith(match, sigil)) is not None: op = opfunc - comp_version = GenericVersion(match[len(sigil):]) + comp_version = GenericVersion(rhs) break else: # default to equality if no operation is specified diff --git a/mkosi/distributions/fedora.py b/mkosi/distributions/fedora.py index 05bea47e4..35626af02 100644 --- a/mkosi/distributions/fedora.py +++ b/mkosi/distributions/fedora.py @@ -16,7 +16,7 @@ from mkosi.installer import PackageManager from mkosi.installer.dnf import Dnf from mkosi.installer.rpm import RpmRepository, find_rpm_gpgkey, setup_rpm from mkosi.log import die -from mkosi.util import listify, tuplify +from mkosi.util import listify, startswith, tuplify @tuplify @@ -28,8 +28,8 @@ def find_fedora_rpm_gpgkeys(context: Context) -> Iterable[str]: # During branching, there is always a kerfuffle with the key transition. # For Rawhide, try to load the N+1 key, just in case our local configuration # still indicates that Rawhide==N, but really Rawhide==N+1. - if context.config.release == "rawhide" and key1.startswith("file://"): - path = Path(key1.removeprefix("file://")).resolve() + if context.config.release == "rawhide" and (rhs := startswith(key1, "file://")): + path = Path(rhs).resolve() if m := re.match(r"RPM-GPG-KEY-fedora-(\d+)-(primary|secondary)", path.name): version = int(m.group(1)) if key3 := find_rpm_gpgkey(context, key=f"RPM-GPG-KEY-fedora-{version + 1}-primary"): diff --git a/mkosi/sandbox.py b/mkosi/sandbox.py index f716effad..c6098f490 100644 --- a/mkosi/sandbox.py +++ b/mkosi/sandbox.py @@ -9,7 +9,7 @@ from typing import Optional, Protocol from mkosi.types import PathString from mkosi.user import INVOKING_USER -from mkosi.util import flatten, one_zero +from mkosi.util import flatten, one_zero, startswith class SandboxProtocol(Protocol): @@ -27,8 +27,8 @@ class Capability(enum.Enum): def have_effective_cap(capability: Capability) -> bool: for line in Path("/proc/self/status").read_text().splitlines(): - if line.startswith("CapEff:"): - hexcap = line.removeprefix("CapEff:").strip() + if rhs := startswith(line, "CapEff:"): + hexcap = rhs.strip() break else: logging.warning(f"\"CapEff:\" not found in /proc/self/status, assuming we don't have {capability}") diff --git a/mkosi/util.py b/mkosi/util.py index 38d07608a..16c613455 100644 --- a/mkosi/util.py +++ b/mkosi/util.py @@ -18,7 +18,7 @@ import tempfile from collections.abc import Iterable, Iterator, Mapping, Sequence from pathlib import Path from types import ModuleType -from typing import Any, Callable, TypeVar, no_type_check +from typing import Any, Callable, Optional, TypeVar, no_type_check from mkosi.types import PathString @@ -59,6 +59,12 @@ def round_up(x: int, blocksize: int = 4096) -> int: return (x + blocksize - 1) // blocksize * blocksize +def startswith(s: str, prefix: str) -> Optional[str]: + if s.startswith(prefix): + return s.removeprefix(prefix) + return None + + @dictify def read_env_file(path: Path) -> Iterator[tuple[str, str]]: with path.open() as f: