#------------------------------------------------------------------------------- # Copyright (c) 2018-2019, Arm Limited. All rights reserved. # Copyright (c) 2019 Texas Instruments Incorporated # # SPDX-License-Identifier: BSD-3-Clause # #------------------------------------------------------------------------------- import os import sys import io from jinja2 import Environment, BaseLoader, select_autoescape try: import yaml except ImportError as e: print (str(e) + " To install it, type:") print ("pip install PyYAML") exit(1) donotedit_warning = \ "/*********** " + \ "WARNING: This is an auto-generated file. Do not edit!" + \ " ***********/" class TemplateLoader(BaseLoader): """ Template loader class. An instance of this class is passed to the template engine. It is responsible for reading the template file """ def __init__(self): pass def get_source(self, environment, template): """ This function reads the template files. For detailed documentation see: http://jinja.pocoo.org/docs/2.10/api/#jinja2.BaseLoader.get_source Please note that this function always return 'false' as 'uptodate' value, so the output file will always be generated. """ if not os.path.isfile(template): raise TemplateNotFound(template) with open(template) as f: source = f.read() return source, template, False def load_manifest_list(file): """ Load the substitution data from the manifests. Parameters ---------- file : file A yaml file containing the manifest list Returns ------- list The list of the contents of the manifest files, as generated by the yaml parser. """ db = [] manifest_list = yaml.load(file) for item in manifest_list["manifest_list"]: manifest_path = os.path.expandvars(item['manifest']) try: file = open(manifest_path) manifest = yaml.load(file) db.append({"manifest": manifest, "attr": item}) except IOError: raise Exception ("Manifest for "+item['name']+" cannot be opened at path "+item['manifest']) return db def generate_manifestfilename(env, mpath, outd): """ Generate manifestfilename header file. Parameters ---------- env : The instance of Environment. """ manifest_header_list = [] with open(mpath) as manifest_list_yaml_file: manifest_list = yaml.load(manifest_list_yaml_file) templatefile_name = 'secure_fw/services/manifestfilename.template' template = env.get_template(templatefile_name) for manifest_file in manifest_list["manifest_list"]: manifest_path = os.path.expandvars(manifest_file['manifest']) file = open(manifest_path) manifest = yaml.load(file) utilities = {} utilities['donotedit_warning']=donotedit_warning context = {} context['manifest'] = manifest context['attr'] = manifest_file context['utilities'] = utilities manifest_dir, sep, manifest_name = manifest_path.rpartition('/') imaged, sep, spd = manifest_dir.rpartition('/') if outd != '': manifest_dir = outd + sep + manifest_dir outfile_name = manifest_name.replace('yaml', 'h') outfile_path = manifest_dir + sep + "psa_manifest/" + outfile_name context['file_name'] = outfile_name.replace('.h', '') manifest_header_list.append(outfile_path) print ("Generating " + outfile_path) if not os.path.exists(os.path.dirname(outfile_path)): try: os.makedirs(os.path.dirname(outfile_path)) except OSError: raise Exception ("Failed to create folder" + os.path.dirname(outfile_path)) outfile = io.open(outfile_path, "w", newline='\n') outfile.write(template.render(context)) outfile.close() return manifest_header_list def main(argv): """ The entry point of the script. Generates the output files based on the templates and the manifests. """ env = Environment( loader = TemplateLoader(), autoescape = select_autoescape(['html', 'xml']), lstrip_blocks = True, trim_blocks = True, keep_trailing_newline = True ) manifest = 'tfm_manifest_list.yaml' templates = 'tfm_generated_file_list.yaml' path_manifest = '' path_templates = '' outd = '' if len(sys.argv) == 4: path_manifest = argv[1] path_templates = argv[2] outd = argv[3] elif len(sys.argv) == 1: path_manifest = os.path.join('tools', manifest) path_templates = os.path.join('tools', templates) else: print("usage: " + argv[0] + " [ ]") exit(1) # Generate manifestfilename manifest_header_list = generate_manifestfilename(env, path_manifest, outd) utilities = {} context = {} with open(path_manifest) as manifest_list_yaml_file: # Read manifest list file, build database db = load_manifest_list(manifest_list_yaml_file) utilities['donotedit_warning']=donotedit_warning utilities['manifest_header_list']=manifest_header_list context['manifests'] = db context['utilities'] = utilities with open(path_templates) as file_list_yaml_file: # read list of files that need to be generated from templates using db file_list_yaml = yaml.load(file_list_yaml_file) file_list = file_list_yaml["file_list"] for file in file_list: outfile_name = file["output"] templatefile_name = outfile_name + '.template' if outd != '': outfile_name = os.path.join(outd, outfile_name) os.makedirs(os.path.dirname(outfile_name), exist_ok=True) print ("Generating " + outfile_name) template = env.get_template(templatefile_name) outfile = io.open(outfile_name, "w", newline='\n') outfile.write(template.render(context)) outfile.close() print ("Generation of files done") if __name__ == "__main__": main(sys.argv)