OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # | |
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 | |
5 # found in the LICENSE file. | |
6 | |
7 """Renders one or more template files using the Jinja template engine.""" | |
8 | |
9 import optparse | |
10 import os | |
11 import sys | |
12 | |
13 from util import build_utils | |
14 | |
15 # Import jinja2 from third_party/jinja2 | |
16 sys.path.append(os.path.join(os.path.dirname(__file__), '../../../third_party')) | |
17 import jinja2 | |
18 | |
19 | |
20 def ProcessFile(input_filename, output_filename, variables): | |
21 with open(input_filename, 'r') as input_file: | |
22 input_ = input_file.read() | |
23 env = jinja2.Environment(undefined=jinja2.StrictUndefined) | |
24 template = env.from_string(input_) | |
25 template.filename = os.path.abspath(input_filename) | |
26 output = template.render(variables) | |
27 with open(output_filename, 'w') as output_file: | |
28 output_file.write(output) | |
29 | |
30 | |
31 def ProcessFiles(input_filenames, inputs_base_dir, outputs_zip, variables): | |
32 with build_utils.TempDir() as temp_dir: | |
33 for input_filename in input_filenames: | |
34 relpath = os.path.relpath(os.path.abspath(input_filename), | |
35 os.path.abspath(inputs_base_dir)) | |
36 if relpath.startswith(os.pardir): | |
cjhopman
2014/10/17 18:54:25
Because you asked before, here's another approach:
newt (away)
2014/10/20 22:45:12
Good point. Though I'd need to ensure that os.path
| |
37 raise Exception('input file %s is not contained in inputs base dir %s' | |
38 % input_filename, inputs_base_dir) | |
39 | |
40 output_filename = os.path.join(temp_dir, relpath) | |
41 parent_dir = os.path.dirname(output_filename) | |
42 build_utils.MakeDirectory(parent_dir) | |
43 ProcessFile(input_filename, output_filename, variables) | |
44 | |
45 build_utils.ZipDir(outputs_zip, temp_dir) | |
46 | |
47 | |
48 def main(): | |
49 parser = optparse.OptionParser() | |
50 build_utils.AddDepfileOption(parser) | |
51 parser.add_option('--inputs', help='The template files to process.') | |
52 parser.add_option('--output', help='The output file to generate. Valid ' | |
53 'only if there is a single input.') | |
54 parser.add_option('--outputs-zip', help='A zip file containing the processed ' | |
55 'templates. Required if there are multiple inputs.') | |
56 parser.add_option('--inputs-base-dir', help='A common ancestor directory of ' | |
57 'the inputs. Each output\'s path in the output zip will ' | |
58 'match the relative path from INPUTS_BASE_DIR to the ' | |
59 'input. Required if --output-zip is given.') | |
60 parser.add_option('--variables', help='Variables to be made available in the ' | |
61 'template processing environment, as a GYP list (e.g. ' | |
62 '--variables "channel=beta mstone=39")', default='') | |
63 options, args = parser.parse_args() | |
64 | |
65 build_utils.CheckOptions(options, parser, required=['inputs']) | |
66 inputs = build_utils.ParseGypList(options.inputs) | |
67 | |
68 if (options.output is None) == (options.outputs_zip is None): | |
69 parser.error('Exactly one of --output and --output-zip must be given') | |
70 if options.output and len(inputs) != 1: | |
71 parser.error('--output cannot be used with multiple inputs') | |
72 if options.outputs_zip and not options.inputs_base_dir: | |
73 parser.error('--inputs-base-dir must be given when --output-zip is used') | |
74 if args: | |
75 parser.error('No positional arguments should be given.') | |
76 | |
77 variables = {} | |
78 for v in build_utils.ParseGypList(options.variables): | |
79 if '=' not in v: | |
80 parser.error('--variables argument must contain "=": ' + v) | |
81 name, _, value = v.partition('=') | |
82 variables[name] = value | |
83 | |
84 if options.output: | |
85 ProcessFile(inputs[0], options.output, variables) | |
86 else: | |
87 ProcessFiles(inputs, options.inputs_base_dir, options.outputs_zip, | |
88 variables) | |
89 | |
90 if options.depfile: | |
91 deps = inputs + build_utils.GetPythonDependencies() | |
92 build_utils.WriteDepfile(options.depfile, deps) | |
93 | |
94 | |
95 if __name__ == '__main__': | |
96 main() | |
OLD | NEW |