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 """ |
8 Expands the ShellApk's AndroidManifest.xml using Motemplate template engine. | |
8 | 9 |
10 To convert Motemplate to "Mustache template" replace: | |
Yaron
2017/06/28 18:47:54
Oh I just saw that explicitly called this out :/
| |
11 1) {{#section:section}} with {{#section}} | |
12 2) {{section.key}} with {{key}} | |
13 """ | |
14 | |
15 import argparse | |
9 import codecs | 16 import codecs |
10 import argparse | 17 import json |
11 import os | 18 import os |
12 import sys | 19 import sys |
13 | 20 |
14 # Import motemplate from third_party/motemplate | 21 #Import motemplate from third_party / motemplate |
15 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, | 22 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, |
16 os.pardir, os.pardir, 'third_party', 'motemplate')) | 23 os.pardir, os.pardir, 'third_party', 'motemplate')) |
17 import motemplate | 24 import motemplate |
18 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, | 25 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, |
19 os.pardir, os.pardir, 'build', 'android', 'gyp', | 26 os.pardir, os.pardir, 'build', 'android', 'gyp', |
20 'util')) | 27 'util')) |
21 import build_utils | 28 import build_utils |
22 | 29 |
23 | 30 |
24 def _ParseVariables(variables_arg, error_func): | 31 def _AppendParsedVariables(initial_variable_list, variables_arg, error_func): |
25 variables = {} | 32 variables = initial_variable_list |
26 for v in build_utils.ParseGnList(variables_arg): | 33 for v in build_utils.ParseGnList(variables_arg): |
27 if '=' not in v: | 34 if '=' not in v: |
28 error_func('--variables argument must contain "=": ' + v) | 35 error_func('--variables argument must contain "=": ' + v) |
29 name, _, value = v.partition('=') | 36 name, _, value = v.partition('=') |
30 variables[name] = value | 37 variables[name] = value |
31 return variables | 38 return variables |
32 | 39 |
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(): | 40 def main(): |
44 parser = argparse.ArgumentParser() | 41 parser = argparse.ArgumentParser() |
45 parser.add_argument('--template', required=True, | 42 parser.add_argument('--template', required=True, |
46 help='The template file to process.') | 43 help='The template file to process.') |
44 parser.add_argument('--config_file', required=True, | |
45 help='JSON file with values to put into template.') | |
47 parser.add_argument('--output', required=True, | 46 parser.add_argument('--output', required=True, |
48 help='The output file to generate.') | 47 help='The output file to generate.') |
49 parser.add_argument('--variables', help='Variables to be made available in ' | 48 parser.add_argument('--extra_variables', help='Variables to be made ' |
50 'the template processing environment, as a GN list ' | 49 'available the template processing environment (in ' |
51 '(e.g. --variables "channel=beta mstone=39")', default='') | 50 'addition to those specified in config file), as a GN ' |
51 'list (e.g. --extra_variables "channel=beta mstone=39")', | |
52 default='') | |
52 options = parser.parse_args() | 53 options = parser.parse_args() |
53 | 54 |
54 variables = _ParseVariables(options.variables, parser.error) | 55 variables = {} |
56 with open(options.config_file, 'r') as f: | |
57 variables = json.loads(f.read()) | |
58 variables = _AppendParsedVariables(variables, options.extra_variables, | |
59 parser.error) | |
60 | |
55 with open(options.template, 'r') as f: | 61 with open(options.template, 'r') as f: |
56 template = motemplate.Motemplate(_RewriteConditionals(f.read())) | 62 template = motemplate.Motemplate(f.read()) |
57 render = template.render(variables) | 63 render = template.render(variables) |
58 with codecs.open(options.output, 'w', 'utf-8') as output_file: | 64 with codecs.open(options.output, 'w', 'utf-8') as output_file: |
59 output_file.write(render.text) | 65 output_file.write(render.text) |
60 | 66 |
61 | 67 |
62 if __name__ == '__main__': | 68 if __name__ == '__main__': |
63 main() | 69 main() |
OLD | NEW |