blob: ea6d118d2997b7d60a784fdc4b4b0f41442c84cb [file] [log] [blame]
Miklos Balint470919c2018-05-22 17:51:29 +02001#-------------------------------------------------------------------------------
Mate Toth-Pal36f21842018-11-08 16:12:51 +01002# Copyright (c) 2018-2019, Arm Limited. All rights reserved.
Miklos Balint470919c2018-05-22 17:51:29 +02003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8import os
Mate Toth-Pal36f21842018-11-08 16:12:51 +01009import io
10from jinja2 import Environment, BaseLoader, select_autoescape
Miklos Balint470919c2018-05-22 17:51:29 +020011
12try:
13 import yaml
14except ImportError as e:
Mate Toth-Pal36f21842018-11-08 16:12:51 +010015 print (e + " To install it, type:")
16 print ("pip install PyYAML")
Miklos Balint470919c2018-05-22 17:51:29 +020017 exit(1)
18
Mate Toth-Pal36f21842018-11-08 16:12:51 +010019class TemplateLoader(BaseLoader):
20 """
21 Template loader class.
Miklos Balint470919c2018-05-22 17:51:29 +020022
Mate Toth-Pal36f21842018-11-08 16:12:51 +010023 An instance of this class is passed to the template engine. It is
24 responsible for reading the template file
25 """
26 def __init__(self):
27 pass
Miklos Balint470919c2018-05-22 17:51:29 +020028
Mate Toth-Pal36f21842018-11-08 16:12:51 +010029 def get_source(self, environment, template):
30 """
31 This function reads the template files.
32 For detailed documentation see:
33 http://jinja.pocoo.org/docs/2.10/api/#jinja2.BaseLoader.get_source
34
35 Please note that this function always return 'false' as 'uptodate'
36 value, so the output file will always be generated.
37 """
38 if not os.path.isfile(template):
39 raise TemplateNotFound(template)
40 with open(template) as f:
41 source = f.read()
42 return source, template, False
43
Miklos Balint470919c2018-05-22 17:51:29 +020044def load_manifest_list(file):
Mate Toth-Pal36f21842018-11-08 16:12:51 +010045 """
46 Load the substitution data from the manifests.
47
48 Parameters
49 ----------
50 file : file
51 A yaml file containing the manifest list
52
53 Returns
54 -------
55 list
56 The list of the contents of the manifest files, as generated by the yaml
57 parser.
58 """
Miklos Balint470919c2018-05-22 17:51:29 +020059 db = []
60 manifest_list = yaml.load(file)
61 for item in manifest_list["manifest_list"]:
Miklos Balint3a05c9d2018-05-31 09:31:27 +020062 manifest_path = item['manifest']
Miklos Balint470919c2018-05-22 17:51:29 +020063 try:
64 file = open(manifest_path)
65 manifest = yaml.load(file)
66 db.append({"manifest": manifest, "attr": item})
67 except IOError:
Mate Toth-Pal36f21842018-11-08 16:12:51 +010068 raise Exception ("Manifest for "+item['name']+" cannot be opened at path "+item['manifest'])
Miklos Balint470919c2018-05-22 17:51:29 +020069
Mate Toth-Pal36f21842018-11-08 16:12:51 +010070 return db
71
72
Miklos Balint470919c2018-05-22 17:51:29 +020073def main():
Mate Toth-Pal36f21842018-11-08 16:12:51 +010074 """
75 The entry point of the script.
76
77 Generates the output files based on the templates and the manifests.
78 """
79 donotedit_warning = \
80 "/*********** " + \
81 "WARNING: This is an auto-generated file. Do not edit!" + \
82 " ***********/"
83
84 env = Environment(
85 loader = TemplateLoader(),
86 autoescape = select_autoescape(['html', 'xml']),
87 lstrip_blocks = True,
88 trim_blocks = True,
89 keep_trailing_newline = True
90 )
91
Miklos Balint3a05c9d2018-05-31 09:31:27 +020092 with open(os.path.join('tools', 'tfm_manifest_list.yaml')) \
93 as manifest_list_yaml_file:
Miklos Balint470919c2018-05-22 17:51:29 +020094 # Read manifest list file, build database
95 db = load_manifest_list(manifest_list_yaml_file)
96
Mate Toth-Pal36f21842018-11-08 16:12:51 +010097 utilities = {}
98 utilities['donotedit_warning']=donotedit_warning
99
100 context = {}
101 context['manifests'] = db
102 context['utilities'] = utilities
103
Miklos Balint3a05c9d2018-05-31 09:31:27 +0200104 with open(os.path.join('tools', 'tfm_generated_file_list.yaml')) \
105 as file_list_yaml_file:
106 # read list of files that need to be generated from templates using db
107 file_list_yaml = yaml.load(file_list_yaml_file)
108 file_list = file_list_yaml["file_list"]
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100109 for file in file_list:
110 outfile_name = file["output"]
111 templatefile_name = outfile_name + '.template'
112
113 print ("Generating " + outfile_name)
114
115 template = env.get_template(templatefile_name)
116
117 outfile = io.open(outfile_name, "w", newline='\n')
118 outfile.write(template.render(context))
119 outfile.close()
120 print ("Generation of files done")
Miklos Balint470919c2018-05-22 17:51:29 +0200121
122if __name__ == "__main__":
123 main()