MRE/scripts/render_templates.py

49 lines
1.9 KiB
Python
Raw Normal View History

2025-03-28 18:52:14 +00:00
import os
import jinja2
import argparse
def render_template(template_file: str,
out_path: str,
**kwargs) -> None:
folder, file = os.path.split(template_file)
template_path = os.path.abspath(folder)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path),
trim_blocks=True,
lstrip_blocks=True)
template = env.get_template(file)
with open(out_path, 'w+') as save:
save.write(template.render(**kwargs))
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.')
parser.add_argument('var_file_path',
help='The path to the file containing the variables to use for rendering the tempalte(s).')
2025-03-28 18:52:14 +00:00
parser.add_argument('out_path',
help='The path to write the rendered template.')
parser.add_argument('-k',
'--kwargs',
required=False,
type=str,
default='',
help='Additional kwargs to include if needed (format: key1=value1,key2=value2...).')
parser.add_argument('-o',
'--overwrite',
action='store_true',
help='If set the previous template will be overwritten.')
args = parser.parse_args()
if not os.path.exists(args.template_path):
print(f'The given template path {args.template_path} does not exist.')
exit(1)
if not os.path.exists(args.var_file_path):
print(f'The given template path {args.var_file_path} does not exist.')
exit(1)