OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 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 """Renders one or more template files using the Jinja template engine.""" | 7 """Renders one or more template files using the Jinja template engine.""" |
8 | 8 |
9 import codecs | 9 import codecs |
10 import argparse | 10 import argparse |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 output_filename = os.path.join(temp_dir, relpath) | 72 output_filename = os.path.join(temp_dir, relpath) |
73 parent_dir = os.path.dirname(output_filename) | 73 parent_dir = os.path.dirname(output_filename) |
74 build_utils.MakeDirectory(parent_dir) | 74 build_utils.MakeDirectory(parent_dir) |
75 _ProcessFile(processor, input_filename, output_filename) | 75 _ProcessFile(processor, input_filename, output_filename) |
76 | 76 |
77 build_utils.ZipDir(outputs_zip, temp_dir) | 77 build_utils.ZipDir(outputs_zip, temp_dir) |
78 | 78 |
79 | 79 |
80 def _ParseVariables(variables_arg, error_func): | 80 def _ParseVariables(variables_arg, error_func): |
81 variables = {} | 81 variables = {} |
82 for v in build_utils.ParseGypList(variables_arg): | 82 for v in build_utils.ParseGnList(variables_arg): |
83 if '=' not in v: | 83 if '=' not in v: |
84 error_func('--variables argument must contain "=": ' + v) | 84 error_func('--variables argument must contain "=": ' + v) |
85 name, _, value = v.partition('=') | 85 name, _, value = v.partition('=') |
86 variables[name] = value | 86 variables[name] = value |
87 return variables | 87 return variables |
88 | 88 |
89 | 89 |
90 def main(): | 90 def main(): |
91 parser = argparse.ArgumentParser() | 91 parser = argparse.ArgumentParser() |
92 parser.add_argument('--inputs', required=True, | 92 parser.add_argument('--inputs', required=True, |
93 help='The template files to process.') | 93 help='The template files to process.') |
94 parser.add_argument('--output', help='The output file to generate. Valid ' | 94 parser.add_argument('--output', help='The output file to generate. Valid ' |
95 'only if there is a single input.') | 95 'only if there is a single input.') |
96 parser.add_argument('--outputs-zip', help='A zip file for the processed ' | 96 parser.add_argument('--outputs-zip', help='A zip file for the processed ' |
97 'templates. Required if there are multiple inputs.') | 97 'templates. Required if there are multiple inputs.') |
98 parser.add_argument('--inputs-base-dir', help='A common ancestor directory ' | 98 parser.add_argument('--inputs-base-dir', help='A common ancestor directory ' |
99 'of the inputs. Each output\'s path in the output zip ' | 99 'of the inputs. Each output\'s path in the output zip ' |
100 'will match the relative path from INPUTS_BASE_DIR to ' | 100 'will match the relative path from INPUTS_BASE_DIR to ' |
101 'the input. Required if --output-zip is given.') | 101 'the input. Required if --output-zip is given.') |
102 parser.add_argument('--loader-base-dir', help='Base path used by the ' | 102 parser.add_argument('--loader-base-dir', help='Base path used by the ' |
103 'template loader. Must be a common ancestor directory of ' | 103 'template loader. Must be a common ancestor directory of ' |
104 'the inputs. Defaults to DIR_SOURCE_ROOT.', | 104 'the inputs. Defaults to DIR_SOURCE_ROOT.', |
105 default=host_paths.DIR_SOURCE_ROOT) | 105 default=host_paths.DIR_SOURCE_ROOT) |
106 parser.add_argument('--variables', help='Variables to be made available in ' | 106 parser.add_argument('--variables', help='Variables to be made available in ' |
107 'the template processing environment, as a GYP list ' | 107 'the template processing environment, as a GYP list ' |
108 '(e.g. --variables "channel=beta mstone=39")', default='') | 108 '(e.g. --variables "channel=beta mstone=39")', default='') |
109 build_utils.AddDepfileOption(parser) | 109 build_utils.AddDepfileOption(parser) |
110 options = parser.parse_args() | 110 options = parser.parse_args() |
111 | 111 |
112 inputs = build_utils.ParseGypList(options.inputs) | 112 inputs = build_utils.ParseGnList(options.inputs) |
113 | 113 |
114 if (options.output is None) == (options.outputs_zip is None): | 114 if (options.output is None) == (options.outputs_zip is None): |
115 parser.error('Exactly one of --output and --output-zip must be given') | 115 parser.error('Exactly one of --output and --output-zip must be given') |
116 if options.output and len(inputs) != 1: | 116 if options.output and len(inputs) != 1: |
117 parser.error('--output cannot be used with multiple inputs') | 117 parser.error('--output cannot be used with multiple inputs') |
118 if options.outputs_zip and not options.inputs_base_dir: | 118 if options.outputs_zip and not options.inputs_base_dir: |
119 parser.error('--inputs-base-dir must be given when --output-zip is used') | 119 parser.error('--inputs-base-dir must be given when --output-zip is used') |
120 | 120 |
121 variables = _ParseVariables(options.variables, parser.error) | 121 variables = _ParseVariables(options.variables, parser.error) |
122 processor = JinjaProcessor(options.loader_base_dir, variables=variables) | 122 processor = JinjaProcessor(options.loader_base_dir, variables=variables) |
123 | 123 |
124 if options.output: | 124 if options.output: |
125 _ProcessFile(processor, inputs[0], options.output) | 125 _ProcessFile(processor, inputs[0], options.output) |
126 else: | 126 else: |
127 _ProcessFiles(processor, inputs, options.inputs_base_dir, | 127 _ProcessFiles(processor, inputs, options.inputs_base_dir, |
128 options.outputs_zip) | 128 options.outputs_zip) |
129 | 129 |
130 if options.depfile: | 130 if options.depfile: |
131 deps = processor.GetLoadedTemplates() + build_utils.GetPythonDependencies() | 131 deps = processor.GetLoadedTemplates() + build_utils.GetPythonDependencies() |
132 build_utils.WriteDepfile(options.depfile, deps) | 132 build_utils.WriteDepfile(options.depfile, deps) |
133 | 133 |
134 | 134 |
135 if __name__ == '__main__': | 135 if __name__ == '__main__': |
136 main() | 136 main() |
OLD | NEW |