aboutsummaryrefslogtreecommitdiff
path: root/tools/tfm_parse_manifest_list.py
diff options
context:
space:
mode:
authorMate Toth-Pal <mate.toth-pal@arm.com>2018-11-08 16:12:51 +0100
committerMate Toth-Pal <mate.toth-pal@arm.com>2019-03-19 08:53:38 +0100
commit36f2184daea9196aa6c209b8d325c5b724d34d84 (patch)
tree6f81a0e0a8b63b24ff8bce9202566ba723f32cc9 /tools/tfm_parse_manifest_list.py
parenta5a2a5bc32bc50f4def8df9a037b4d67175763b9 (diff)
downloadtrusted-firmware-m-36f2184daea9196aa6c209b8d325c5b724d34d84.tar.gz
Manifest: Use Jinja2 for template generation
Use Jinja2 template engine to generate code from templates. This makes the custom generator scripts in the tools directory obsolete. For more info on Jina please see http://jinja.pocoo.org/ Details: - rewrite template files to use jinja2 syntax - remove fixmes for limitations of the template generator script - fix crypto and attest partition manifest yaml file - remove obsolete template generator scripts - rewrite tfm_parse_manifest_list.py to use the jinja2 template engine - update documentation Change-Id: Iccceae7a5e14e5c5d6c6b5bcdf08a53137d81458 Signed-off-by: Mate Toth-Pal <mate.toth-pal@arm.com>
Diffstat (limited to 'tools/tfm_parse_manifest_list.py')
-rw-r--r--tools/tfm_parse_manifest_list.py96
1 files changed, 82 insertions, 14 deletions
diff --git a/tools/tfm_parse_manifest_list.py b/tools/tfm_parse_manifest_list.py
index 3129bde998..ea6d118d29 100644
--- a/tools/tfm_parse_manifest_list.py
+++ b/tools/tfm_parse_manifest_list.py
@@ -1,28 +1,61 @@
#-------------------------------------------------------------------------------
-# Copyright (c) 2018, Arm Limited. All rights reserved.
+# Copyright (c) 2018-2019, Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
#-------------------------------------------------------------------------------
import os
-from keyword_substitution import Verbosity, log_print
-from generate_from_template import generate_from_template_file
+import io
+from jinja2 import Environment, BaseLoader, select_autoescape
try:
import yaml
except ImportError as e:
- print e, "To install it, type:"
- print "pip install PyYAML"
+ print (e + " To install it, type:")
+ print ("pip install PyYAML")
exit(1)
-VERBOSITY = Verbosity.warning
-log_print(Verbosity.debug, "Setting verbosity to", VERBOSITY, verbosity=VERBOSITY)
+class TemplateLoader(BaseLoader):
+ """
+ Template loader class.
-# All operations assume tf-m repo root as active working directory
+ 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
-# Functions
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"]:
@@ -32,24 +65,59 @@ def load_manifest_list(file):
manifest = yaml.load(file)
db.append({"manifest": manifest, "attr": item})
except IOError:
- print "Manifest for "+item['name']+" cannot be opened at path "+item['manifest']
+ raise Exception ("Manifest for "+item['name']+" cannot be opened at path "+item['manifest'])
+
return db
-# def load_manifest_list
-# main
+
def main():
+ """
+ The entry point of the script.
+
+ Generates the output files based on the templates and the manifests.
+ """
+ donotedit_warning = \
+ "/*********** " + \
+ "WARNING: This is an auto-generated file. Do not edit!" + \
+ " ***********/"
+
+ env = Environment(
+ loader = TemplateLoader(),
+ autoescape = select_autoescape(['html', 'xml']),
+ lstrip_blocks = True,
+ trim_blocks = True,
+ keep_trailing_newline = True
+ )
+
with open(os.path.join('tools', 'tfm_manifest_list.yaml')) \
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"]
- generate_from_template_file(db, file_list)
- print "Generation of files done"
+ 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()
+ print ("Generation of files done")
if __name__ == "__main__":
main()