aos: Support generated replacements in jinja2_template
I have a rule that generates replacements for a Jinja2 template. I
can't use any of the current mechanisms to do this so I decided to
enhance the rule.
You can now specify a `parameters_file` attribute whose contents will
be added to the available variables inside the template.
This came up when I wanted to copy the spyglass colors into a foxglove
script.
I couldn't figure out a good way to write a build test for this
because the //build_tests package in BRT's vendored copy of AOS
doesn't load properly.
Change-Id: Ib046817e5b698d5358b787e67953503ed2b57f78
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/tools/build_rules/jinja2_generator.py b/tools/build_rules/jinja2_generator.py
index 047a805..7bb9c75 100644
--- a/tools/build_rules/jinja2_generator.py
+++ b/tools/build_rules/jinja2_generator.py
@@ -18,6 +18,13 @@
type=Path,
help="File to use for template.")
parser.add_argument(
+ "--replacements_file",
+ type=Path,
+ help=("File containing a dictionary of parameters to replace "
+ "in the template. The behaviour is undefined if keys are "
+ "duplicated between this file and the `replacements` argument."),
+ )
+ parser.add_argument(
"replacements",
type=json.loads,
help="Dictionary of parameters to replace in the template.")
@@ -34,7 +41,12 @@
env = jinja2.Environment(loader=jinja2.FileSystemLoader(args.include_dir))
template = env.from_string(args.template.read_text())
- args.output.write_text(template.render(args.replacements))
+ replacements = args.replacements.copy()
+ if args.replacements_file:
+ with args.replacements_file.open() as file:
+ replacements.update(json.load(file))
+
+ args.output.write_text(template.render(replacements))
if __name__ == '__main__':