OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 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 |
| 5 # found in the LICENSE file. |
| 6 |
| 7 """Expands the ShellApk's AndroidManifest.xml using Mustache template engine.""" |
| 8 |
| 9 import codecs |
| 10 import argparse |
| 11 import os |
| 12 import sys |
| 13 |
| 14 # Import motemplate from third_party/motemplate |
| 15 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, |
| 16 os.pardir, os.pardir, 'third_party', 'motemplate')) |
| 17 import motemplate |
| 18 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, |
| 19 os.pardir, os.pardir, 'build', 'android', 'gyp', |
| 20 'util')) |
| 21 import build_utils |
| 22 |
| 23 |
| 24 def _ParseVariables(variables_arg, error_func): |
| 25 variables = {} |
| 26 for v in build_utils.ParseGnList(variables_arg): |
| 27 if '=' not in v: |
| 28 error_func('--variables argument must contain "=": ' + v) |
| 29 name, _, value = v.partition('=') |
| 30 if value == 'True': |
| 31 # The python implementation of Mustache doesn't consider True to be a |
| 32 # truthy value. Wrap it up as a list to enable us to specify a boolean to |
| 33 # expand a template section. |
| 34 variables[name] = ['True'] |
| 35 else: |
| 36 variables[name] = value |
| 37 return variables |
| 38 |
| 39 |
| 40 def main(): |
| 41 parser = argparse.ArgumentParser() |
| 42 parser.add_argument('--template', required=True, |
| 43 help='The template file to process.') |
| 44 parser.add_argument('--output', required=True, |
| 45 help='The output file to generate.') |
| 46 parser.add_argument('--variables', help='Variables to be made available in ' |
| 47 'the template processing environment, as a GN list ' |
| 48 '(e.g. --variables "channel=beta mstone=39")', default='') |
| 49 options = parser.parse_args() |
| 50 |
| 51 variables = _ParseVariables(options.variables, parser.error) |
| 52 with open(options.template, 'r') as f: |
| 53 template = motemplate.Motemplate(f.read()) |
| 54 render = template.render(variables) |
| 55 with codecs.open(options.output, 'w', 'utf-8') as output_file: |
| 56 output_file.write(render.text) |
| 57 |
| 58 |
| 59 if __name__ == '__main__': |
| 60 main() |
OLD | NEW |