From 096f363fd2797e008a6020d736d540b2429511a5 Mon Sep 17 00:00:00 2001 From: Chris Diesch Date: Fri, 28 Mar 2025 14:08:58 -0600 Subject: [PATCH] Finished intitial script for rendering templates --- scripts/render_templates.py | 55 +++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/scripts/render_templates.py b/scripts/render_templates.py index 7a758be..ba6c745 100644 --- a/scripts/render_templates.py +++ b/scripts/render_templates.py @@ -1,6 +1,7 @@ import os import jinja2 import argparse +import traceback def render_template(template_file: str, out_path: str, @@ -15,16 +16,26 @@ def render_template(template_file: str, save.write(template.render(**kwargs)) +def load_template_vars_file(var_file_path: str) -> dict: + result = {} + with open(var_file_path) as reader: + for line in reader: + key, value = line.split('=', 1) + result[key] = value + + return result + + if __name__ == '__main__': parser = argparse.ArgumentParser(prog='render_templates.py', description='A script to render the docker jinja templates.', add_help=True) parser.add_argument('template_path', - help='The path to the template to render.') + help='The path to the template(s) to render (Can be either a directory or a file).') parser.add_argument('var_file_path', help='The path to the file containing the variables to use for rendering the tempalte(s).') parser.add_argument('out_path', - help='The path to write the rendered template.') + help='The path to write the rendered template (MUST be a directory).') parser.add_argument('-k', '--kwargs', required=False, @@ -38,6 +49,7 @@ if __name__ == '__main__': args = parser.parse_args() + # make sure we have a template file and a variable file. if not os.path.exists(args.template_path): print(f'The given template path {args.template_path} does not exist.') exit(1) @@ -46,3 +58,42 @@ if __name__ == '__main__': print(f'The given template path {args.var_file_path} does not exist.') exit(1) + run_on_dir = False + # if the template path is a directory, make sure output path exists + if os.path.isdir(args.template_path): + run_on_dir = True + if not os.path.exists(args.out_path): + os.makedirs(args.out_path) + else: + print('The output path must be a directory since the template path is.') + exit(1) + + args.template_path = [os.path.join(args.template_path, p) + for p in filter(lambda x: x.ends_with('.tmpl'), + os.listdir(args.template_path))] + # if the tempalte path is a file + else: + # wrap it in a list + args.template_path = [args.template_path] + + # load the variables from the variable file + kwargs = load_template_vars_file(args.var_file_path) + args.kwargs = {a.split('=')[0]: a.split('=')[1] for a in args.kwargs.split(',')} if args.kwargs else {} + for k in kwargs: + # The assumption made here is that anything passed on the commandline should be prioritized + # over values that appear in the template variables file. + if k not in args.kwargs: + args.kwargs[k] = kwargs[k] + + for template in args.template_path: + out_file_name = os.path.join(args.out_path, + os.path.split(template.remove('.tmpl'))) + # if the output exists and we aren't overwritting files, then move on. + if os.path.exists(out_file_name) and not args.overwrite: + continue + + try: + render_template(template, out_file_name, **args.kwargs) + except Exception as ex: + print(f'Failed to render template "{template}":') + print(f'Error: {str(ex)}\n{traceback.format_exec()}')