From: Zbigniew Jędrzejewski-Szmek Date: Sat, 2 Mar 2024 10:38:49 +0000 (+0100) Subject: util: gather three small utility functions together X-Git-Tag: v21~8^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d64b3bc38c632d75c50f03c7194091b6bc15fca;p=thirdparty%2Fmkosi.git util: gather three small utility functions together Esp. the two math-related ones can be considered related. --- diff --git a/mkosi/util.py b/mkosi/util.py index a375dd805..38d07608a 100644 --- a/mkosi/util.py +++ b/mkosi/util.py @@ -47,6 +47,18 @@ def tuplify(f: Callable[..., Iterable[T]]) -> Callable[..., tuple[T, ...]]: return functools.update_wrapper(wrapper, f) +def one_zero(b: bool) -> str: + return "1" if b else "0" + + +def is_power_of_2(x: int) -> bool: + return x > 0 and (x & x - 1 == 0) + + +def round_up(x: int, blocksize: int = 4096) -> int: + return (x + blocksize - 1) // blocksize * blocksize + + @dictify def read_env_file(path: Path) -> Iterator[tuple[str, str]]: with path.open() as f: @@ -155,10 +167,6 @@ class StrEnum(enum.Enum): return list(map(str, cls)) -def one_zero(b: bool) -> str: - return "1" if b else "0" - - @contextlib.contextmanager def umask(mask: int) -> Iterator[None]: old = os.umask(mask) @@ -168,10 +176,6 @@ def umask(mask: int) -> Iterator[None]: os.umask(old) -def is_power_of_2(x: int) -> bool: - return x > 0 and (x & x - 1 == 0) - - @contextlib.contextmanager def resource_path(mod: ModuleType) -> Iterator[Path]: @@ -268,7 +272,3 @@ def resource_path(mod: ModuleType) -> Iterator[Path]: t = importlib.resources.files(mod) with as_file(t) as p: yield p - - -def round_up(x: int, blocksize: int = 4096) -> int: - return (x + blocksize - 1) // blocksize * blocksize