OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Renders one or more template files using the Jinja template engine.""" | 7 """Renders one or more template files using the Jinja template engine.""" |
8 | 8 |
9 import codecs | 9 import codecs |
10 import argparse | 10 import argparse |
(...skipping 26 matching lines...) Expand all Loading... |
37 """Allows easy rendering of jinja templates with input file tracking.""" | 37 """Allows easy rendering of jinja templates with input file tracking.""" |
38 def __init__(self, loader_base_dir, variables=None): | 38 def __init__(self, loader_base_dir, variables=None): |
39 self.loader_base_dir = loader_base_dir | 39 self.loader_base_dir = loader_base_dir |
40 self.variables = variables | 40 self.variables = variables |
41 self.loader = _RecordingFileSystemLoader(loader_base_dir) | 41 self.loader = _RecordingFileSystemLoader(loader_base_dir) |
42 self.env = jinja2.Environment(loader=self.loader) | 42 self.env = jinja2.Environment(loader=self.loader) |
43 self.env.undefined = jinja2.StrictUndefined | 43 self.env.undefined = jinja2.StrictUndefined |
44 self.env.line_comment_prefix = '##' | 44 self.env.line_comment_prefix = '##' |
45 self.env.trim_blocks = True | 45 self.env.trim_blocks = True |
46 self.env.lstrip_blocks = True | 46 self.env.lstrip_blocks = True |
| 47 self._template_cache = {} # Map of path -> Template |
47 | 48 |
48 def Render(self, input_filename, variables=None): | 49 def Render(self, input_filename, variables=None): |
49 input_rel_path = os.path.relpath(input_filename, self.loader_base_dir) | 50 input_rel_path = os.path.relpath(input_filename, self.loader_base_dir) |
50 template = self.env.get_template(input_rel_path) | 51 template = self._template_cache.get(input_rel_path) |
| 52 if not template: |
| 53 template = self.env.get_template(input_rel_path) |
| 54 self._template_cache[input_rel_path] = template |
51 return template.render(variables or self.variables) | 55 return template.render(variables or self.variables) |
52 | 56 |
53 def GetLoadedTemplates(self): | 57 def GetLoadedTemplates(self): |
54 return list(self.loader.loaded_templates) | 58 return list(self.loader.loaded_templates) |
55 | 59 |
56 | 60 |
57 def _ProcessFile(processor, input_filename, output_filename): | 61 def _ProcessFile(processor, input_filename, output_filename): |
58 output = processor.Render(input_filename) | 62 output = processor.Render(input_filename) |
59 with codecs.open(output_filename, 'w', 'utf-8') as output_file: | 63 with codecs.open(output_filename, 'w', 'utf-8') as output_file: |
60 output_file.write(output) | 64 output_file.write(output) |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 options.outputs_zip) | 132 options.outputs_zip) |
129 | 133 |
130 if options.depfile: | 134 if options.depfile: |
131 output = options.output or options.outputs_zip | 135 output = options.output or options.outputs_zip |
132 deps = processor.GetLoadedTemplates() | 136 deps = processor.GetLoadedTemplates() |
133 build_utils.WriteDepfile(options.depfile, output, deps) | 137 build_utils.WriteDepfile(options.depfile, output, deps) |
134 | 138 |
135 | 139 |
136 if __name__ == '__main__': | 140 if __name__ == '__main__': |
137 main() | 141 main() |
OLD | NEW |