From 961ce3077f7fbaf90fe6b8cf3ec2dd73c7686997 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Mon, 22 Feb 2021 19:51:28 +0100 Subject: [PATCH] tools: add lorax-template-pkg.py helper Add a simple helper that is meant to gather the list of packages to be installed via a lorax template that uses the "installpkg" directives. A prominent example is the 'runtime-install.tmpl' script from lorax-templates-generic, used to create boot isos. --- tools/lorax-template-pkgs.py | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tools/lorax-template-pkgs.py diff --git a/tools/lorax-template-pkgs.py b/tools/lorax-template-pkgs.py new file mode 100644 index 00000000..6cabb747 --- /dev/null +++ b/tools/lorax-template-pkgs.py @@ -0,0 +1,64 @@ +#!/usr/bin/python3 +"""Collect to be installed packages of a lorax template script + +This simple tool intercepts all `installpkg` commands of a lorax +template script like `runtime-install.tmpl` in order to collect +all to be installed packages. The result is presented on stdout +in form of a JSON array. +""" + +import argparse +import json +import sys + +from osbuild.util.lorax import render_template + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--basearch", help="Set the `basearch` variable", default="x86_64") + parser.add_argument("--product", help="Set the `product` variable", default="fedora") + parser.add_argument("FILE", help="The template to process") + args = parser.parse_args() + + variables = { + "basearch": args.basearch, + "product": args.product + } + + txt = render_template(args.FILE, variables) + + packages = [] + optional = [] + excludes = [] + + parser = argparse.ArgumentParser() + parser.add_argument("--optional", action="append") + parser.add_argument("--except", dest="excludes", action="append") + parser.add_argument("packages", help="The template to process", nargs="*") + + for line in txt: + cmd, args = line[0], parser.parse_args(line[1:]) + + if cmd != "installpkg": + print(f"{cmd} ignored", file=sys.stderr) + continue + + if args.optional: + optional += args.optional + if args.excludes: + excludes += args.excludes + if args.packages: + packages += args.packages + + data = { + "packages": packages, + "optional": optional, + "except": excludes + } + + json.dump(data, sys.stdout, indent=2) + + +if __name__ == "__main__": + main()