def paas_manifest(environment, app, vars_file, out_file): """Generate a PaaS manifest file from a Jinja2 template""" variables = load_variables(environment, vars_file, { 'environment': environment, 'app': app.replace('_', '-') }) template_content = load_file('paas/{}.j2'.format(app)) variables = merge_dicts(variables, variables[app]) manifest_content = template_string(template_content, variables, templates_path='paas/') if out_file is not None: with open(out_file, 'w') as f: f.write(manifest_content) os.chmod(out_file, 0o600) else: print(manifest_content)
def paas_manifest(environment, app, vars_file, var, out_file): """Generate a PaaS manifest file from a Jinja2 template""" variables = load_variables(environment, vars_file, { 'environment': environment, 'app': app.replace('_', '-') }) template_file = f"paas/{app}.j2" template_content = load_file(template_file) variables = merge_dicts(variables, variables[app]) try: variables = merge_dicts( variables, get_variables_from_command_line_or_environment(var)) except KeyError as e: sys.exit( f"""Error: Command line variable "--var '{e.args[0]}'" was not set by the flag""" """ and was not found in environment variables. Please check your environment is correctly configured.""" ) try: manifest_content = template_string(template_content, variables, templates_path='paas/') except UndefinedError as e: # the UndefinedError.message is usually something like "'VAR' is undefined" sys.exit( f"""Error: The template '{template_file}' thinks that the variable {e.message}.""" """ Please check you have included all of the var files and command line vars that you need.""" ) if out_file is not None: with open(out_file, 'w') as f: f.write(manifest_content) os.chmod(out_file, 0o600) else: print(manifest_content)
def test_template_loader_default_path(self, jinja_loader): template_string('string', {}) jinja_loader.assert_called_with(DEFAULT_TEMPLATES_PATH)
def test_template_loader(self): assert template_string('{% extends "base.j2" %}', {}, templates_path="tests/templates/")
def test_missing_variable(self): with pytest.raises(ValueError): assert template_string("{{ var }} string", {}) == "a string"
def test_template_dot_accessor(self): assert( template_string("{{ var.name }} string", {"var": {"name": "a"}}) == "a string" )
def test_template_string(self): assert template_string("{{ var }} string", {"var": "a"}) == "a string"
def test_simple_string(self): assert template_string("string", {}) == "string"