This way we don't need to repeat the prefix string.
flatten,
is_power_of_2,
make_executable,
+ startswith,
)
from mkosi.versioncomp import GenericVersion
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]:
">": 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
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
# 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"):
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):
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}")
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
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: