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 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, |
16 os.pardir, os.pardir, 'third_party', 'motemplate')) | 17 'third_party', 'pystache')) |
17 import motemplate | 18 import pystache |
18 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, | 19 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, |
19 os.pardir, os.pardir, 'build', 'android', 'gyp', | 20 os.pardir, os.pardir, 'build', 'android', 'gyp', |
20 'util')) | 21 'util')) |
21 import build_utils | 22 import build_utils |
22 | 23 |
23 | 24 |
24 def _ParseVariables(variables_arg, error_func): | 25 def _AppendParsedVariables(initial_variable_list, variables_arg, error_func): |
25 variables = {} | 26 variables = initial_variable_list |
26 for v in build_utils.ParseGnList(variables_arg): | 27 for v in build_utils.ParseGnList(variables_arg): |
27 if '=' not in v: | 28 if '=' not in v: |
28 error_func('--variables argument must contain "=": ' + v) | 29 error_func('--variables argument must contain "=": ' + v) |
29 name, _, value = v.partition('=') | 30 name, _, value = v.partition('=') |
30 variables[name] = value | 31 variables[name] = value |
31 return variables | 32 return variables |
32 | 33 |
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(): | 34 def main(): |
44 parser = argparse.ArgumentParser() | 35 parser = argparse.ArgumentParser() |
45 parser.add_argument('--template', required=True, | 36 parser.add_argument('--template', required=True, |
46 help='The template file to process.') | 37 help='The template file to process.') |
| 38 parser.add_argument('--config_file', required=True, |
| 39 help='JSON file with values to put into template.') |
47 parser.add_argument('--output', required=True, | 40 parser.add_argument('--output', required=True, |
48 help='The output file to generate.') | 41 help='The output file to generate.') |
49 parser.add_argument('--variables', help='Variables to be made available in ' | 42 parser.add_argument('--extra_variables', help='Variables to be made ' |
50 'the template processing environment, as a GN list ' | 43 'available the template processing environment (in ' |
51 '(e.g. --variables "channel=beta mstone=39")', default='') | 44 'addition to those specified in config file), as a GN ' |
| 45 'list (e.g. --extra_variables "channel=beta mstone=39")', |
| 46 default='') |
52 options = parser.parse_args() | 47 options = parser.parse_args() |
53 | 48 |
54 variables = _ParseVariables(options.variables, parser.error) | 49 variables = {} |
| 50 with open(options.config_file, 'r') as f: |
| 51 variables = json.loads(f.read()) |
| 52 variables = _AppendParsedVariables(variables, options.extra_variables, |
| 53 parser.error) |
| 54 |
55 with open(options.template, 'r') as f: | 55 with open(options.template, 'r') as f: |
56 template = motemplate.Motemplate(_RewriteConditionals(f.read())) | 56 render = pystache.render(f.read(), variables) |
57 render = template.render(variables) | |
58 with codecs.open(options.output, 'w', 'utf-8') as output_file: | 57 with codecs.open(options.output, 'w', 'utf-8') as output_file: |
59 output_file.write(render.text) | 58 output_file.write(render) |
60 | 59 |
61 | 60 |
62 if __name__ == '__main__': | 61 if __name__ == '__main__': |
63 main() | 62 main() |
OLD | NEW |