blob: 91f64d49e0918220e5f6fb8ff07fb74ad6f1035b [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2017 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Expands template using Mustache template engine."""
import argparse
import codecs
import json
import os
import sys
#Import chevron from //third_party
src_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
os.pardir, os.pardir)
sys.path.insert(1, os.path.join(src_dir, 'third_party'))
import chevron
sys.path.insert(1, os.path.join(src_dir, 'build/android/gyp'))
from util import build_utils # pylint: disable=import-error
def _AppendParsedVariables(initial_variable_list, variables_arg, error_func):
variables = initial_variable_list
for v in build_utils.ParseGnList(variables_arg):
if '=' not in v:
error_func('--variables argument must contain "=": ' + v)
name, _, value = v.partition('=')
if value == "false":
value = False
variables[name] = value
return variables
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--template', required=True,
help='The template file to process.')
parser.add_argument('--output', required=True,
help='The output file to generate.')
help='JSON file with values to put into template.')
parser.add_argument('--delta_config_file', help='JSON file with '
'substitutions to |config_file|.')
parser.add_argument('--extra_variables', help='Variables to be made '
'available in the template processing environment (in '
'addition to those specified in config file), as a GN '
'list (e.g. --extra_variables "channel=beta mstone=39")',
options = parser.parse_args()
config = {}
if options.config_file:
with open(options.config_file, 'r') as f:
config = json.loads(
if options.delta_config_file:
with open(options.delta_config_file, 'r') as f:
variables = config
variables = _AppendParsedVariables(variables, options.extra_variables,
with open(options.template, 'r') as f:
rendered = chevron.render(f, variables)
with open(options.output, 'w') as output_file:
if __name__ == '__main__':