blob: 102ff139cf5b3f5b55c0f1494a7561517c7b3289 [file] [log] [blame]
#-------------------------------------------------------------------------------
# Copyright (c) 2018-2019, Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
#-------------------------------------------------------------------------------
import os
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!" + \
" ***********/"
manifest_list_yaml_file_path = os.path.join('tools', 'tfm_manifest_list.yaml')
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 = 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):
"""
Generate manifestfilename header file.
Parameters
----------
env :
The instance of Environment.
"""
with open(manifest_list_yaml_file_path) 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 = 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('/')
outfile_name = manifest_name.replace('yaml', 'h')
outfile_path = manifest_dir + sep + "psa_manifest/" + outfile_name
context['file_name'] = outfile_name.replace('.h', '')
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
def main():
"""
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
)
with open(manifest_list_yaml_file_path) as manifest_list_yaml_file:
# Read manifest list file, build database
db = load_manifest_list(manifest_list_yaml_file)
utilities = {}
utilities['donotedit_warning']=donotedit_warning
context = {}
context['manifests'] = db
context['utilities'] = utilities
with open(os.path.join('tools', 'tfm_generated_file_list.yaml')) \
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'
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()
# Generate manifestfilename
generate_manifestfilename(env)
print ("Generation of files done")
if __name__ == "__main__":
main()