Skip to content

Instantly share code, notes, and snippets.

@smoser
Last active August 21, 2024 20:49
Show Gist options
  • Save smoser/7da4ed38fb06e80d199ce28e07c20d1b to your computer and use it in GitHub Desktop.
Save smoser/7da4ed38fb06e80d199ce28e07c20d1b to your computer and use it in GitHub Desktop.
python editing melange yaml

editing melange yaml with python

Sometimes its helpful to edit lots of melange files quickly.

This is something that I used to do that.

notes

  • uses ruamel.yaml this is pretty good. I have seen one case where messed up roundtrip (py3-botocore.yaml)
  • running 'yam' afterwards fixed up more of the small differences with indentation.
#!/usr/bin/python3.12
import os
import sys
import subprocess
from ruamel.yaml import YAML
syspath = sys.path
sys.path = [path for path in sys.path if
not (path == os.getcwd() or path == ".")]
yaml = YAML(typ='safe')
mytestdata = yaml.load("""\
test:
environment:
contents:
packages:
- busybox
pipeline:
- uses: py/pip-check
""")
for f in sys.argv[1:]:
yaml2 = YAML()
with open(f, "rb") as fp:
content = fp.read()
y = yaml2.load(content)
#if "data" in y:
# if "items" in y["data"][0]:
# v = y["data"][0]["items"].get(3.1)
# if v is not None:
# y["data"][0]["items"]["3.10"] = v
# del y["data"][0]["items"][3.1]
is_subpkg = False
pkg = y
if "subpackages" in y and y["subpackages"][0].get("range") == "py-versions":
pkg = y["subpackages"][0]
is_subpkg = True
if "test" not in pkg:
pkg["test"] = mytestdata["test"]
op = "add-test"
elif "pipeline" not in pkg["test"]:
pkg["test"]["pipeline"] = mytestdata["test"]["pipeline"]
op = "add-pipeline"
else:
found = False
for k in pkg["test"]["pipeline"]:
if k.get("uses") == "py/pip-check":
found = True
if found:
op = "done"
else:
pkg["test"]["pipeline"].extend(mytestdata["test"]["pipeline"])
op = "add-uses"
print("%s: %s%s" % (f, op, "[sub]" if is_subpkg else ""))
if op == "" or op == "done":
continue
with open(f, "w") as fp:
yaml2.dump(y, fp)
subprocess.check_output(["yam", f])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment