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 optparse | 10 import optparse |
10 import os | 11 import os |
11 import sys | 12 import sys |
12 | 13 |
13 from util import build_utils | 14 from util import build_utils |
14 | 15 |
15 # Import jinja2 from third_party/jinja2 | 16 # Import jinja2 from third_party/jinja2 |
16 sys.path.append(os.path.join(os.path.dirname(__file__), '../../../third_party')) | 17 sys.path.append(os.path.join(os.path.dirname(__file__), '../../../third_party')) |
17 import jinja2 # pylint: disable=F0401 | 18 import jinja2 # pylint: disable=F0401 |
18 | 19 |
19 | 20 |
20 def ProcessFile(input_filename, output_filename, variables): | 21 def ProcessFile(input_filename, output_filename, variables): |
21 with open(input_filename, 'r') as input_file: | 22 with codecs.open(input_filename, 'r', 'utf-8') as input_file: |
22 input_ = input_file.read() | 23 input_ = input_file.read() |
23 env = jinja2.Environment(undefined=jinja2.StrictUndefined) | 24 env = jinja2.Environment(undefined=jinja2.StrictUndefined) |
24 template = env.from_string(input_) | 25 template = env.from_string(input_) |
25 template.filename = os.path.abspath(input_filename) | 26 template.filename = os.path.abspath(input_filename) |
26 output = template.render(variables) | 27 output = template.render(variables) |
27 with open(output_filename, 'w') as output_file: | 28 with codecs.open(output_filename, 'w', 'utf-8') as output_file: |
28 output_file.write(output) | 29 output_file.write(output) |
29 | 30 |
30 | 31 |
31 def ProcessFiles(input_filenames, inputs_base_dir, outputs_zip, variables): | 32 def ProcessFiles(input_filenames, inputs_base_dir, outputs_zip, variables): |
32 with build_utils.TempDir() as temp_dir: | 33 with build_utils.TempDir() as temp_dir: |
33 for input_filename in input_filenames: | 34 for input_filename in input_filenames: |
34 relpath = os.path.relpath(os.path.abspath(input_filename), | 35 relpath = os.path.relpath(os.path.abspath(input_filename), |
35 os.path.abspath(inputs_base_dir)) | 36 os.path.abspath(inputs_base_dir)) |
36 if relpath.startswith(os.pardir): | 37 if relpath.startswith(os.pardir): |
37 raise Exception('input file %s is not contained in inputs base dir %s' | 38 raise Exception('input file %s is not contained in inputs base dir %s' |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 ProcessFiles(inputs, options.inputs_base_dir, options.outputs_zip, | 88 ProcessFiles(inputs, options.inputs_base_dir, options.outputs_zip, |
88 variables) | 89 variables) |
89 | 90 |
90 if options.depfile: | 91 if options.depfile: |
91 deps = inputs + build_utils.GetPythonDependencies() | 92 deps = inputs + build_utils.GetPythonDependencies() |
92 build_utils.WriteDepfile(options.depfile, deps) | 93 build_utils.WriteDepfile(options.depfile, deps) |
93 | 94 |
94 | 95 |
95 if __name__ == '__main__': | 96 if __name__ == '__main__': |
96 main() | 97 main() |
OLD | NEW |