| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # Based on: | 5 # Based on: |
| 6 # http://src.chromium.org/viewvc/blink/trunk/Source/build/scripts/template_expan
der.py | 6 # http://src.chromium.org/viewvc/blink/trunk/Source/build/scripts/template_expan
der.py |
| 7 | 7 |
| 8 import imp | 8 import imp |
| 9 import inspect | |
| 10 import os.path | 9 import os.path |
| 11 import sys | 10 import sys |
| 12 | 11 |
| 13 # Disable lint check for finding modules: | 12 # Disable lint check for finding modules: |
| 14 # pylint: disable=F0401 | 13 # pylint: disable=F0401 |
| 15 | 14 |
| 16 def _GetDirAbove(dirname): | 15 def _GetDirAbove(dirname): |
| 17 """Returns the directory "above" this file containing |dirname| (which must | 16 """Returns the directory "above" this file containing |dirname| (which must |
| 18 also be "above" this file).""" | 17 also be "above" this file).""" |
| 19 path = os.path.abspath(__file__) | 18 path = os.path.abspath(__file__) |
| 20 while True: | 19 while True: |
| 21 path, tail = os.path.split(path) | 20 path, tail = os.path.split(path) |
| 22 assert tail | 21 assert tail |
| 23 if tail == dirname: | 22 if tail == dirname: |
| 24 return path | 23 return path |
| 25 | 24 |
| 26 try: | 25 try: |
| 27 imp.find_module("jinja2") | 26 imp.find_module("jinja2") |
| 28 except ImportError: | 27 except ImportError: |
| 29 sys.path.append(os.path.join(_GetDirAbove("mojo"), "third_party")) | 28 sys.path.append(os.path.join(_GetDirAbove("mojo"), "third_party")) |
| 30 import jinja2 | 29 import jinja2 |
| 31 | 30 |
| 32 | 31 |
| 33 def ApplyTemplate(mojo_generator, base_dir, path_to_template, params, | 32 def ApplyTemplate(mojo_generator, path_to_template, params, **kwargs): |
| 34 filters=None, **kwargs): | 33 loader = jinja2.ModuleLoader(os.path.join( |
| 35 template_directory, template_name = os.path.split(path_to_template) | 34 mojo_generator.bytecode_path, "%s.zip" % mojo_generator.GetTemplatePrefix( |
| 36 path_to_templates = os.path.join(base_dir, template_directory) | 35 ))) |
| 37 loader = jinja2.FileSystemLoader([path_to_templates]) | |
| 38 final_kwargs = dict(mojo_generator.GetJinjaParameters()) | 36 final_kwargs = dict(mojo_generator.GetJinjaParameters()) |
| 39 final_kwargs.update(kwargs) | 37 final_kwargs.update(kwargs) |
| 40 jinja_env = jinja2.Environment(loader=loader, keep_trailing_newline=True, | 38 jinja_env = jinja2.Environment(loader=loader, |
| 39 keep_trailing_newline=True, |
| 41 **final_kwargs) | 40 **final_kwargs) |
| 42 jinja_env.globals.update(mojo_generator.GetGlobals()) | 41 jinja_env.globals.update(mojo_generator.GetGlobals()) |
| 43 if filters: | 42 jinja_env.filters.update(mojo_generator.GetFilters()) |
| 44 jinja_env.filters.update(filters) | 43 template = jinja_env.get_template(path_to_template) |
| 45 template = jinja_env.get_template(template_name) | |
| 46 return template.render(params) | 44 return template.render(params) |
| 47 | 45 |
| 48 | 46 |
| 49 def UseJinja(path_to_template, **kwargs): | 47 def UseJinja(path_to_template, **kwargs): |
| 50 # Get the directory of our caller's file. | |
| 51 base_dir = os.path.dirname(inspect.getfile(sys._getframe(1))) | |
| 52 def RealDecorator(generator): | 48 def RealDecorator(generator): |
| 53 def GeneratorInternal(*args, **kwargs2): | 49 def GeneratorInternal(*args, **kwargs2): |
| 54 parameters = generator(*args, **kwargs2) | 50 parameters = generator(*args, **kwargs2) |
| 55 return ApplyTemplate(args[0], base_dir, path_to_template, parameters, | 51 return ApplyTemplate(args[0], path_to_template, parameters, **kwargs) |
| 56 **kwargs) | |
| 57 GeneratorInternal.func_name = generator.func_name | 52 GeneratorInternal.func_name = generator.func_name |
| 58 return GeneratorInternal | 53 return GeneratorInternal |
| 59 return RealDecorator | 54 return RealDecorator |
| 55 |
| 56 |
| 57 def PrecompileTemplates(generator_modules, output_dir): |
| 58 for module in generator_modules.values(): |
| 59 generator = module.Generator(None) |
| 60 jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader([os.path.join( |
| 61 os.path.dirname(module.__file__), generator.GetTemplatePrefix())])) |
| 62 jinja_env.filters.update(generator.GetFilters()) |
| 63 jinja_env.compile_templates( |
| 64 os.path.join(output_dir, "%s.zip" % generator.GetTemplatePrefix()), |
| 65 extensions=["tmpl"], |
| 66 zip="stored", |
| 67 py_compile=True, |
| 68 ignore_errors=False) |
| OLD | NEW |