Allow for specifying the output_dir
Allow for passing the output_dir in the process-templates scripts so that it doesn't overwrites the templates in the src dir. This is a desired feature when running the script from a t-h-t installed system-wide. Change-Id: I47994d34f47a4084a11124bc9075cb2f457889ea
This commit is contained in:
parent
5fbe0c5249
commit
275bbe8894
|
@ -14,6 +14,7 @@
|
||||||
import argparse
|
import argparse
|
||||||
import jinja2
|
import jinja2
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
import six
|
import six
|
||||||
import sys
|
import sys
|
||||||
import yaml
|
import yaml
|
||||||
|
@ -33,6 +34,9 @@ def parse_opts(argv):
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help="""Enable safe mode (do not overwrite files).""",
|
help="""Enable safe mode (do not overwrite files).""",
|
||||||
default=False)
|
default=False)
|
||||||
|
parser.add_argument('-o', '--output-dir', metavar='OUTPUT_DIR',
|
||||||
|
help="""Output dir for all the templates""",
|
||||||
|
default='')
|
||||||
opts = parser.parse_args(argv[1:])
|
opts = parser.parse_args(argv[1:])
|
||||||
|
|
||||||
return opts
|
return opts
|
||||||
|
@ -60,7 +64,7 @@ def _j2_render_to_file(j2_template, j2_data, outfile_name=None,
|
||||||
out_f.write(r_template)
|
out_f.write(r_template)
|
||||||
|
|
||||||
|
|
||||||
def process_templates(template_path, role_data_path, overwrite):
|
def process_templates(template_path, role_data_path, output_dir, overwrite):
|
||||||
|
|
||||||
with open(role_data_path) as role_data_file:
|
with open(role_data_path) as role_data_file:
|
||||||
role_data = yaml.safe_load(role_data_file)
|
role_data = yaml.safe_load(role_data_file)
|
||||||
|
@ -69,6 +73,11 @@ def process_templates(template_path, role_data_path, overwrite):
|
||||||
with open(j2_excludes_path) as role_data_file:
|
with open(j2_excludes_path) as role_data_file:
|
||||||
j2_excludes = yaml.safe_load(role_data_file)
|
j2_excludes = yaml.safe_load(role_data_file)
|
||||||
|
|
||||||
|
if output_dir and not os.path.isdir(output_dir):
|
||||||
|
if os.path.exists(output_dir):
|
||||||
|
raise RuntimeError('Output dir %s is not a directory' % output_dir)
|
||||||
|
os.mkdir(output_dir)
|
||||||
|
|
||||||
role_names = [r.get('name') for r in role_data]
|
role_names = [r.get('name') for r in role_data]
|
||||||
r_map = {}
|
r_map = {}
|
||||||
for r in role_data:
|
for r in role_data:
|
||||||
|
@ -90,6 +99,17 @@ def process_templates(template_path, role_data_path, overwrite):
|
||||||
dirs[:] = [d for d in dirs if not d[0] == '.']
|
dirs[:] = [d for d in dirs if not d[0] == '.']
|
||||||
files = [f for f in files if not f[0] == '.']
|
files = [f for f in files if not f[0] == '.']
|
||||||
|
|
||||||
|
# NOTE(flaper87): We could have used shutil.copytree
|
||||||
|
# but it requires the dst dir to not be present. This
|
||||||
|
# approach is safer as it doesn't require us to delete
|
||||||
|
# the output_dir in advance and it allows for running
|
||||||
|
# the command multiple times with the same output_dir.
|
||||||
|
out_dir = subdir
|
||||||
|
if output_dir:
|
||||||
|
out_dir = os.path.join(output_dir, subdir)
|
||||||
|
if not os.path.exists(out_dir):
|
||||||
|
os.mkdir(out_dir)
|
||||||
|
|
||||||
for f in files:
|
for f in files:
|
||||||
file_path = os.path.join(subdir, f)
|
file_path = os.path.join(subdir, f)
|
||||||
# We do two templating passes here:
|
# We do two templating passes here:
|
||||||
|
@ -113,7 +133,7 @@ def process_templates(template_path, role_data_path, overwrite):
|
||||||
[role.lower(),
|
[role.lower(),
|
||||||
os.path.basename(f).replace('.role.j2.yaml',
|
os.path.basename(f).replace('.role.j2.yaml',
|
||||||
'.yaml')])
|
'.yaml')])
|
||||||
out_f_path = os.path.join(subdir, out_f)
|
out_f_path = os.path.join(out_dir, out_f)
|
||||||
if not (out_f_path in excl_templates):
|
if not (out_f_path in excl_templates):
|
||||||
_j2_render_to_file(template_data, j2_data,
|
_j2_render_to_file(template_data, j2_data,
|
||||||
out_f_path, overwrite)
|
out_f_path, overwrite)
|
||||||
|
@ -124,9 +144,12 @@ def process_templates(template_path, role_data_path, overwrite):
|
||||||
with open(file_path) as j2_template:
|
with open(file_path) as j2_template:
|
||||||
template_data = j2_template.read()
|
template_data = j2_template.read()
|
||||||
j2_data = {'roles': role_data}
|
j2_data = {'roles': role_data}
|
||||||
out_f = file_path.replace('.j2.yaml', '.yaml')
|
out_f = os.path.basename(f).replace('.j2.yaml', '.yaml')
|
||||||
_j2_render_to_file(template_data, j2_data, out_f,
|
out_f_path = os.path.join(out_dir, out_f)
|
||||||
|
_j2_render_to_file(template_data, j2_data, out_f_path,
|
||||||
overwrite)
|
overwrite)
|
||||||
|
elif output_dir:
|
||||||
|
shutil.copy(os.path.join(subdir, f), out_dir)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print('Unexpected argument %s' % template_path)
|
print('Unexpected argument %s' % template_path)
|
||||||
|
@ -135,4 +158,4 @@ opts = parse_opts(sys.argv)
|
||||||
|
|
||||||
role_data_path = os.path.join(opts.base_path, opts.roles_data)
|
role_data_path = os.path.join(opts.base_path, opts.roles_data)
|
||||||
|
|
||||||
process_templates(opts.base_path, role_data_path, (not opts.safe))
|
process_templates(opts.base_path, role_data_path, opts.output_dir, (not opts.safe))
|
||||||
|
|
Loading…
Reference in New Issue