stages: replace toml imports with our util module

This commit is contained in:
Achilleas Koutsou 2024-08-15 14:17:08 +02:00 committed by Michael Vogt
parent 123b23fb66
commit 292d4ad0fe
3 changed files with 9 additions and 38 deletions

View file

@ -2,12 +2,8 @@
import pathlib import pathlib
import sys import sys
try:
import toml
except ModuleNotFoundError:
import pytoml as toml
import osbuild.api import osbuild.api
from osbuild.util import toml
def main(tree, options): def main(tree, options):
@ -17,8 +13,7 @@ def main(tree, options):
path = pathlib.Path(tree) / "usr/lib/bootc/install" / filename path = pathlib.Path(tree) / "usr/lib/bootc/install" / filename
path.parent.mkdir(parents=True, exist_ok=True) path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "w", encoding="utf8") as config_file: toml.dump_to_file(config, path)
toml.dump(config, config_file)
return 0 return 0

View file

@ -4,12 +4,8 @@ import os
import sys import sys
from typing import Dict from typing import Dict
try:
import toml
except ModuleNotFoundError:
import pytoml as toml
import osbuild.api import osbuild.api
from osbuild.util import toml
DEFAULT_LOCATION = "/etc/containers/storage.conf" DEFAULT_LOCATION = "/etc/containers/storage.conf"
@ -50,14 +46,6 @@ def merge_config(section: str, data: Dict, config: Dict):
have.update(want) have.update(want)
def write_comment(f, comment: list):
if not comment:
return
data = "\n".join(map(lambda c: f"# {c}", comment))
f.write(data + "\n\n")
def main(tree, options): def main(tree, options):
location = options.get("filename", DEFAULT_LOCATION) location = options.get("filename", DEFAULT_LOCATION)
config = options["config"] config = options["config"]
@ -69,24 +57,17 @@ def main(tree, options):
# if a filebase was specified, we use it as base # if a filebase was specified, we use it as base
if filebase: if filebase:
with open(filebase, "r", encoding="utf8") as f: data = toml.load_from_file(filebase)
data = toml.load(f)
# if the target exists, we merge it # if the target exists, we merge it
with contextlib.suppress(FileNotFoundError): with contextlib.suppress(FileNotFoundError):
with open(path, "r", encoding="utf8") as f: have = toml.load_from_file(path)
have = toml.load(f) merge_config("storage", data, have)
merge_config("storage", data, have)
# now merge our configuration into data # now merge our configuration into data
merge_config("storage", data, config) merge_config("storage", data, config)
with open(path, "w", encoding="utf8") as f: toml.dump_to_file(data, path, header=HEADER + ["\n"] + comment)
write_comment(f, HEADER)
write_comment(f, comment)
toml.dump(data, f)
return 0 return 0

View file

@ -5,12 +5,8 @@ import re
import pytest import pytest
try:
import toml
except ModuleNotFoundError:
import pytoml as toml
from osbuild import testutil from osbuild import testutil
from osbuild.util import toml
TEST_INPUT = [ TEST_INPUT = [
({}, {"additionalimagestores": ["/path/to/store"]}, [("storage.options.additionalimagestores", ["/path/to/store"])]), ({}, {"additionalimagestores": ["/path/to/store"]}, [("storage.options.additionalimagestores", ["/path/to/store"])]),
@ -49,8 +45,7 @@ def test_containers_storage_conf_integration(tmp_path, stage_module, test_filena
assert os.path.exists(confpath) assert os.path.exists(confpath)
conf = None conf = None
with open(confpath, 'r', encoding="utf-8") as f: conf = toml.load_from_file(confpath)
conf = toml.load(f)
assert conf is not None assert conf is not None