37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
|
|
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('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.')
|
||
|
|
|