From ba0d9df68e8f8210a1d905456896e1646e9e63be Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 31 Mar 2025 18:06:56 +0200 Subject: [PATCH] util: add new `util.experimentalflags.get_{bool,string}` helpers This commit adds two new helpers: - util.experimentalflags.get_bool() - util.experimentalflags.get_string() similar to what we added in the images library in PR: https://github.com/osbuild/images/pull/1248 The idea is that we provide experimentalflags for osbuild via an environment like `OSBUILD_EXPERIMENTAL` and for those we make no API promises. This will be initially used for better debug of qemu-user. --- osbuild/util/experimentalflags.py | 31 ++++++++++++++++++++ test/mod/test_util_experimentalflags.py | 39 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 osbuild/util/experimentalflags.py create mode 100644 test/mod/test_util_experimentalflags.py diff --git a/osbuild/util/experimentalflags.py b/osbuild/util/experimentalflags.py new file mode 100644 index 00000000..c9172c4b --- /dev/null +++ b/osbuild/util/experimentalflags.py @@ -0,0 +1,31 @@ +"""Handling of experimental environment flags""" + +import os +from typing import Any, Dict + + +def _experimental_env_map() -> Dict[str, Any]: + env_map: Dict[str, Any] = {} + for exp_opt in os.environ.get("OSBUILD_EXPERIMENTAL", "").split(","): + l = exp_opt.split("=", maxsplit=1) + if len(l) == 1: + env_map[exp_opt] = "true" + elif len(l) == 2: + env_map[l[0]] = l[1] + return env_map + + +def get_bool(option: str) -> bool: + env_map = _experimental_env_map() + opt = env_map.get(option, "") + # sadly python as no strconv.ParseBool() like golang so we roll our own + if opt.upper() in {"1", "T", "TRUE"}: + return True + if opt.upper() in {"", "0", "F", "FALSE"}: + return False + raise RuntimeError(f"unsupport bool val {opt}") + + +def get_string(option: str) -> str: + env_map = _experimental_env_map() + return str(env_map.get(option, "")) diff --git a/test/mod/test_util_experimentalflags.py b/test/mod/test_util_experimentalflags.py new file mode 100644 index 00000000..6688f3a5 --- /dev/null +++ b/test/mod/test_util_experimentalflags.py @@ -0,0 +1,39 @@ +# +# Test for the util.experimentalflags +# +import pytest + +from osbuild.util import experimentalflags + + +@pytest.mark.parametrize("env,expected_foo", [ + # implicit false + ("", False), + ("bar", False), + # explicit true + ("foo", True), + ("foo,bar", True), + ("foo=1", True), + ("foo=1,bar", True), + ("foo=true", True), + ("foo=t", True), + # explicit falgs + ("foo=false", False), + ("foo=0", False), + ("foo=f", False), + ("foo=F", False), + ("foo=FALSE", False), +]) +def test_experimentalflags_bool(monkeypatch, env, expected_foo): + monkeypatch.setenv("OSBUILD_EXPERIMENTAL", env) + assert experimentalflags.get_bool("foo") == expected_foo + + +@pytest.mark.parametrize("env,expected_key", [ + ("", ""), + ("key=val", "val"), + ("foo,key=val,bar", "val"), +]) +def test_experimentalflags_string(monkeypatch, env, expected_key): + monkeypatch.setenv("OSBUILD_EXPERIMENTAL", env) + assert experimentalflags.get_string("key") == expected_key