From: Kuntal Majumder <12135951+hellozee@users.noreply.github.com> Date: Sat, 16 Dec 2023 17:02:56 +0000 (+0100) Subject: Add support for loading environment files X-Git-Tag: v20~59 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c69670cb32817b621d3ee8287ac0bc4a1dff435a;p=thirdparty%2Fmkosi.git Add support for loading environment files Fixes #738 --- diff --git a/mkosi/config.py b/mkosi/config.py index c733ef879..abf560dd9 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -954,6 +954,7 @@ class MkosiConfig: build_sources: list[ConfigTree] build_sources_ephemeral: bool environment: dict[str, str] + environment_files: list[Path] with_tests: bool with_network: bool @@ -1677,6 +1678,16 @@ SETTINGS = ( parse=config_make_list_parser(delimiter=" ", unescape=True), help="Set an environment variable when running scripts", ), + MkosiConfigSetting( + dest="environment_files", + long="--env-file", + metavar="PATH", + section="Content", + parse=config_make_list_parser(delimiter=",", parse=make_path_parser()), + paths=("mkosi.env",), + path_default=False, + help="Enviroment files to set when running scripts", + ), MkosiConfigSetting( dest="with_tests", short="-T", @@ -2879,8 +2890,10 @@ def load_environment(args: argparse.Namespace) -> dict[str, str]: if dnf := os.getenv("MKOSI_DNF"): env["MKOSI_DNF"] = dnf - for s in args.environment: + entries = [line for envfile in args.environment_files for line in envfile.read_text().strip().splitlines()] + for s in entries + args.environment: key, sep, value = s.partition("=") + key, value = key.strip(), value.strip() value = value if sep else os.getenv(key, "") env[key] = value @@ -3077,6 +3090,7 @@ def summary(config: MkosiConfig) -> str: Build Sources: {line_join_tree_list(config.build_sources)} Build Sources Ephemeral: {yes_no(config.build_sources_ephemeral)} Script Environment: {line_join_list(env)} + Environment Files: {line_join_list(config.environment_files)} Run Tests in Build Scripts: {yes_no(config.with_tests)} Scripts With Network: {yes_no(config.with_network)} diff --git a/mkosi/resources/mkosi.md b/mkosi/resources/mkosi.md index 2c8dd3e03..4a484df7c 100644 --- a/mkosi/resources/mkosi.md +++ b/mkosi/resources/mkosi.md @@ -976,6 +976,14 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`, listed variables will be set. If the same variable is set twice, the later setting overrides the earlier one. +`EnvironmentFiles=`, `--env-file=` + +: Takes a comma-separated list of paths to files that contain enviroment + variable definitions to be added to the scripting environment. Uses + `mkosi.env` if it is found in the local directory. The variables are + first read from `mkosi.env` if it exists, then from the given list of + files and then from the `Environment=` settings. + `WithTests=`, `--without-tests`, `-T` : If set to false (or when the command-line option is used), the diff --git a/tests/test_config.py b/tests/test_config.py index 7539347d1..b02bbf0b1 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -753,3 +753,45 @@ def test_specifiers(tmp_path: Path) -> None: def test_deterministic() -> None: assert MkosiConfig.default() == MkosiConfig.default() + + +def test_environment(tmp_path: Path) -> None: + d = tmp_path + + (d / "mkosi.conf").write_text( + """\ + [Content] + Environment=TestValue2=300 + TestValue3=400 + EnvironmentFiles=other.env + """ + ) + + (d / "mkosi.env").write_text( + """\ + TestValue1=90 + TestValue4=99 + """ + ) + + (d / "other.env").write_text( + """\ + TestValue1=100 + TestValue2=200 + """ + ) + + with chdir(d): + _, [config] = parse_config() + + expected = { + "TestValue1": "100", # from other.env + "TestValue2": "300", # from mkosi.conf + "TestValue3": "400", # from mkosi.conf + "TestValue4": "99", # from mkosi.env + } + + # Only check values for keys from expected, as config.environment contains other items as well + assert {k: config.environment[k] for k in expected.keys()} == expected + + assert config.environment_files == [Path.cwd() / "mkosi.env", Path.cwd() / "other.env"] diff --git a/tests/test_json.py b/tests/test_json.py index 660d0a97d..41e159f98 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -117,6 +117,7 @@ def test_config() -> None: ], "Distribution": "fedora", "Environment": {}, + "EnvironmentFiles": [], "Ephemeral": true, "ExtraSearchPaths": [], "ExtraTrees": [], @@ -308,6 +309,7 @@ def test_config() -> None: dependencies = ("dep1",), distribution = Distribution.fedora, environment = {}, + environment_files = [], ephemeral = True, extra_search_paths = [], extra_trees = [],