| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2017 The Chromium Authors. All rights reserved. | 3 # Copyright 2017 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 """Expands the ShellApk's AndroidManifest.xml using Mustache template engine.""" | 7 """Expands the ShellApk's AndroidManifest.xml using Mustache template engine.""" |
| 8 | 8 |
| 9 import argparse |
| 9 import codecs | 10 import codecs |
| 10 import argparse | 11 import json |
| 11 import os | 12 import os |
| 12 import sys | 13 import sys |
| 13 | 14 |
| 14 # Import motemplate from third_party/motemplate | 15 #Import pystache from //third_party/pystache |
| 15 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, | 16 src_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, |
| 16 os.pardir, os.pardir, 'third_party', 'motemplate')) | 17 os.pardir, os.pardir) |
| 17 import motemplate | 18 sys.path.append(os.path.join(src_dir, 'third_party')) |
| 18 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, | 19 import pystache |
| 19 os.pardir, os.pardir, 'build', 'android', 'gyp', | 20 sys.path.append(os.path.join(src_dir, 'build', 'android', 'gyp', 'util')) |
| 20 'util')) | |
| 21 import build_utils | 21 import build_utils |
| 22 | 22 |
| 23 | 23 |
| 24 def _ParseVariables(variables_arg, error_func): | 24 def _AppendParsedVariables(initial_variable_list, variables_arg, error_func): |
| 25 variables = {} | 25 variables = initial_variable_list |
| 26 for v in build_utils.ParseGnList(variables_arg): | 26 for v in build_utils.ParseGnList(variables_arg): |
| 27 if '=' not in v: | 27 if '=' not in v: |
| 28 error_func('--variables argument must contain "=": ' + v) | 28 error_func('--variables argument must contain "=": ' + v) |
| 29 name, _, value = v.partition('=') | 29 name, _, value = v.partition('=') |
| 30 variables[name] = value | 30 variables[name] = value |
| 31 return variables | 31 return variables |
| 32 | 32 |
| 33 | |
| 34 ''' | |
| 35 Rewrite conditional sections to be 'Verted Sections' as defined in handlerbars. | |
| 36 This allows variables to be used in the AndroidManifest to conditionally define | |
| 37 include segments. | |
| 38 ''' | |
| 39 def _RewriteConditionals(template_text): | |
| 40 return template_text.replace('{{#', '{{?') | |
| 41 | |
| 42 | |
| 43 def main(): | 33 def main(): |
| 44 parser = argparse.ArgumentParser() | 34 parser = argparse.ArgumentParser() |
| 45 parser.add_argument('--template', required=True, | 35 parser.add_argument('--template', required=True, |
| 46 help='The template file to process.') | 36 help='The template file to process.') |
| 37 parser.add_argument('--config_file', required=True, |
| 38 help='JSON file with values to put into template.') |
| 47 parser.add_argument('--output', required=True, | 39 parser.add_argument('--output', required=True, |
| 48 help='The output file to generate.') | 40 help='The output file to generate.') |
| 49 parser.add_argument('--variables', help='Variables to be made available in ' | 41 parser.add_argument('--extra_variables', help='Variables to be made ' |
| 50 'the template processing environment, as a GN list ' | 42 'available in the template processing environment (in ' |
| 51 '(e.g. --variables "channel=beta mstone=39")', default='') | 43 'addition to those specified in config file), as a GN ' |
| 44 'list (e.g. --extra_variables "channel=beta mstone=39")', |
| 45 default='') |
| 52 options = parser.parse_args() | 46 options = parser.parse_args() |
| 53 | 47 |
| 54 variables = _ParseVariables(options.variables, parser.error) | 48 variables = {} |
| 49 with open(options.config_file, 'r') as f: |
| 50 variables = json.loads(f.read()) |
| 51 variables = _AppendParsedVariables(variables, options.extra_variables, |
| 52 parser.error) |
| 53 |
| 55 with open(options.template, 'r') as f: | 54 with open(options.template, 'r') as f: |
| 56 template = motemplate.Motemplate(_RewriteConditionals(f.read())) | 55 render = pystache.render(f.read(), variables) |
| 57 render = template.render(variables) | |
| 58 with codecs.open(options.output, 'w', 'utf-8') as output_file: | 56 with codecs.open(options.output, 'w', 'utf-8') as output_file: |
| 59 output_file.write(render.text) | 57 output_file.write(render) |
| 60 | 58 |
| 61 | 59 |
| 62 if __name__ == '__main__': | 60 if __name__ == '__main__': |
| 63 main() | 61 main() |
| OLD | NEW |