Tools: Add output argument for manifest parsing tool
This patch allows user to specify a custom location to put the files
generated by the manifest parsing tool.
Note that the argument is optional. The tool can be used as before
without argument and the functionality is not changed.
For detailed usage, try "python3 tfm_parse_manifest_list.py -h"
Change-Id: I26e77314c76ac68787c30a135cefeb414f9a941a
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
Co-authored-by: Shawn Shan <shawn.shan@arm.com>
Co-authored-by: Alan DeMars <ademars@ti.com>
diff --git a/tools/tfm_parse_manifest_list.py b/tools/tfm_parse_manifest_list.py
index 19aa4c2..26a57e2 100644
--- a/tools/tfm_parse_manifest_list.py
+++ b/tools/tfm_parse_manifest_list.py
@@ -7,6 +7,8 @@
import os
import io
+import sys
+import argparse
from jinja2 import Environment, BaseLoader, select_autoescape
try:
@@ -65,7 +67,7 @@
db = []
manifest_list = yaml.safe_load(file)
for item in manifest_list["manifest_list"]:
- manifest_path = item['manifest']
+ manifest_path = os.path.expandvars(item['manifest'])
try:
file = open(manifest_path)
manifest = yaml.safe_load(file)
@@ -75,7 +77,7 @@
return db
-def generate_manifestfilename(env):
+def generate_manifestfilename(env, out_dir):
"""
Generate manifestfilename header file.
@@ -83,7 +85,10 @@
----------
env :
The instance of Environment.
+ out_dir:
+ The root directory that files are generated to.
"""
+
manifest_header_list = []
with open(manifest_list_yaml_file_path) as manifest_list_yaml_file:
manifest_list = yaml.safe_load(manifest_list_yaml_file)
@@ -91,7 +96,7 @@
template = env.get_template(templatefile_name)
for manifest_file in manifest_list["manifest_list"]:
- manifest_path = manifest_file['manifest']
+ manifest_path = os.path.expandvars(manifest_file['manifest'])
file = open(manifest_path)
manifest = yaml.safe_load(file)
@@ -103,23 +108,23 @@
context['attr'] = manifest_file
context['utilities'] = utilities
- manifest_dir, sep, manifest_name = manifest_path.rpartition('/')
+ manifest_dir, manifest_name = os.path.split(manifest_path)
outfile_name = manifest_name.replace('yaml', 'h').replace('json', 'h')
- outfile_path = manifest_dir + sep + "psa_manifest/" + outfile_name
-
context['file_name'] = outfile_name.replace('.h', '')
+ outfile_name = os.path.join(manifest_dir, "psa_manifest", outfile_name)
- manifest_header_list.append(outfile_path)
+ manifest_header_list.append(outfile_name)
- print ("Generating " + outfile_path)
+ if out_dir is not None:
+ outfile_name = os.path.join(out_dir, outfile_name)
- 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_path = os.path.dirname(outfile_name)
+ if not os.path.exists(outfile_path):
+ os.makedirs(outfile_path)
- outfile = io.open(outfile_path, "w", newline='\n')
+ print ("Generating " + outfile_name)
+
+ outfile = io.open(outfile_name, "w", newline='\n')
outfile.write(template.render(context))
outfile.close()
return manifest_header_list
@@ -130,6 +135,32 @@
Generates the output files based on the templates and the manifests.
"""
+
+ parser = argparse.ArgumentParser(description='Parse secure partition manifest list and generate files listed by the file list')
+ parser.add_argument('-o', '--outdir'
+ , dest='outdir'
+ , required=False
+ , default=None
+ , metavar='out_dir'
+ , help='The root directory for generated files, the default is TF-M base folder.')
+
+ args = parser.parse_args()
+
+ out_dir = args.outdir
+
+ # Arguments could be relative path.
+ # Convert to absolute path as we are going to change directory later
+ if out_dir is not None:
+ out_dir = os.path.abspath(out_dir)
+ """
+ Relative path to TF-M base folder is supported in the manifests
+ and default value of manifest list and generated file list are relative to TF-M base folder as well,
+ so first change directory to TF-M base folder.
+ By doing this, the script can be executed anywhere
+ The script is located in <TF-M base folder>/tools, so sys.path[0]<location of the script>/.. is TF-M base folder.
+ """
+ os.chdir(os.path.join(sys.path[0], ".."))
+
env = Environment(
loader = TemplateLoader(),
autoescape = select_autoescape(['html', 'xml']),
@@ -139,7 +170,7 @@
)
# Generate manifestfilename
- manifest_header_list = generate_manifestfilename(env)
+ manifest_header_list = generate_manifestfilename(env, out_dir)
utilities = {}
context = {}
@@ -159,9 +190,16 @@
file_list_yaml = yaml.safe_load(file_list_yaml_file)
file_list = file_list_yaml["file_list"]
for file in file_list:
- outfile_name = file["output"]
+ outfile_name = os.path.expandvars(file["output"])
templatefile_name = outfile_name + '.template'
+ if out_dir is not None:
+ outfile_name = os.path.join(out_dir, outfile_name)
+
+ outfile_path = os.path.dirname(outfile_name)
+ if not os.path.exists(outfile_path):
+ os.makedirs(outfile_path)
+
print ("Generating " + outfile_name)
template = env.get_template(templatefile_name)