Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: build/android/gyp/jinja_template.py

Issue 1077553002: jinja2: make templates outside of // work again (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 optparse 10 import optparse
(...skipping 16 matching lines...) Expand all
27 def get_source(self, environment, template): 27 def get_source(self, environment, template):
28 contents, filename, uptodate = jinja2.FileSystemLoader.get_source( 28 contents, filename, uptodate = jinja2.FileSystemLoader.get_source(
29 self, environment, template) 29 self, environment, template)
30 self.loaded_templates.add(os.path.relpath(filename)) 30 self.loaded_templates.add(os.path.relpath(filename))
31 return contents, filename, uptodate 31 return contents, filename, uptodate
32 32
33 def get_loaded_templates(self): 33 def get_loaded_templates(self):
34 return list(self.loaded_templates) 34 return list(self.loaded_templates)
35 35
36 36
37 def ProcessFile(env, input_filename, output_filename, variables): 37 def ProcessFile(env, input_filename, loader_base_dir, output_filename,
38 input_rel_path = os.path.relpath(input_filename, build_utils.CHROMIUM_SRC) 38 variables):
39 input_rel_path = os.path.relpath(input_filename, loader_base_dir)
39 template = env.get_template(input_rel_path) 40 template = env.get_template(input_rel_path)
40 output = template.render(variables) 41 output = template.render(variables)
41 with codecs.open(output_filename, 'w', 'utf-8') as output_file: 42 with codecs.open(output_filename, 'w', 'utf-8') as output_file:
42 output_file.write(output) 43 output_file.write(output)
43 44
44 45
45 def ProcessFiles(env, input_filenames, inputs_base_dir, outputs_zip, variables): 46 def ProcessFiles(env, input_filenames, loader_base_dir, inputs_base_dir,
47 outputs_zip, variables):
46 with build_utils.TempDir() as temp_dir: 48 with build_utils.TempDir() as temp_dir:
47 for input_filename in input_filenames: 49 for input_filename in input_filenames:
48 relpath = os.path.relpath(os.path.abspath(input_filename), 50 relpath = os.path.relpath(os.path.abspath(input_filename),
49 os.path.abspath(inputs_base_dir)) 51 os.path.abspath(inputs_base_dir))
50 if relpath.startswith(os.pardir): 52 if relpath.startswith(os.pardir):
51 raise Exception('input file %s is not contained in inputs base dir %s' 53 raise Exception('input file %s is not contained in inputs base dir %s'
52 % input_filename, inputs_base_dir) 54 % input_filename, inputs_base_dir)
53 55
54 output_filename = os.path.join(temp_dir, relpath) 56 output_filename = os.path.join(temp_dir, relpath)
55 parent_dir = os.path.dirname(output_filename) 57 parent_dir = os.path.dirname(output_filename)
56 build_utils.MakeDirectory(parent_dir) 58 build_utils.MakeDirectory(parent_dir)
57 ProcessFile(env, input_filename, output_filename, variables) 59 ProcessFile(env, input_filename, loader_base_dir, output_filename,
60 variables)
58 61
59 build_utils.ZipDir(outputs_zip, temp_dir) 62 build_utils.ZipDir(outputs_zip, temp_dir)
60 63
61 64
62 def main(): 65 def main():
63 parser = optparse.OptionParser() 66 parser = optparse.OptionParser()
64 build_utils.AddDepfileOption(parser) 67 build_utils.AddDepfileOption(parser)
65 parser.add_option('--inputs', help='The template files to process.') 68 parser.add_option('--inputs', help='The template files to process.')
66 parser.add_option('--output', help='The output file to generate. Valid ' 69 parser.add_option('--output', help='The output file to generate. Valid '
67 'only if there is a single input.') 70 'only if there is a single input.')
68 parser.add_option('--outputs-zip', help='A zip file containing the processed ' 71 parser.add_option('--outputs-zip', help='A zip file containing the processed '
69 'templates. Required if there are multiple inputs.') 72 'templates. Required if there are multiple inputs.')
70 parser.add_option('--inputs-base-dir', help='A common ancestor directory of ' 73 parser.add_option('--inputs-base-dir', help='A common ancestor directory of '
71 'the inputs. Each output\'s path in the output zip will ' 74 'the inputs. Each output\'s path in the output zip will '
72 'match the relative path from INPUTS_BASE_DIR to the ' 75 'match the relative path from INPUTS_BASE_DIR to the '
73 'input. Required if --output-zip is given.') 76 'input. Required if --output-zip is given.')
77 parser.add_option('--loader-base-dir', help='Base path used by the template '
78 'loader. Must be a common ancestor directory of '
79 'the inputs. Defaults to CHROMIUM_SRC.',
80 default=build_utils.CHROMIUM_SRC)
74 parser.add_option('--variables', help='Variables to be made available in the ' 81 parser.add_option('--variables', help='Variables to be made available in the '
75 'template processing environment, as a GYP list (e.g. ' 82 'template processing environment, as a GYP list (e.g. '
76 '--variables "channel=beta mstone=39")', default='') 83 '--variables "channel=beta mstone=39")', default='')
77 options, args = parser.parse_args() 84 options, args = parser.parse_args()
78 85
79 build_utils.CheckOptions(options, parser, required=['inputs']) 86 build_utils.CheckOptions(options, parser, required=['inputs'])
80 inputs = build_utils.ParseGypList(options.inputs) 87 inputs = build_utils.ParseGypList(options.inputs)
81 88
82 if (options.output is None) == (options.outputs_zip is None): 89 if (options.output is None) == (options.outputs_zip is None):
83 parser.error('Exactly one of --output and --output-zip must be given') 90 parser.error('Exactly one of --output and --output-zip must be given')
84 if options.output and len(inputs) != 1: 91 if options.output and len(inputs) != 1:
85 parser.error('--output cannot be used with multiple inputs') 92 parser.error('--output cannot be used with multiple inputs')
86 if options.outputs_zip and not options.inputs_base_dir: 93 if options.outputs_zip and not options.inputs_base_dir:
87 parser.error('--inputs-base-dir must be given when --output-zip is used') 94 parser.error('--inputs-base-dir must be given when --output-zip is used')
88 if args: 95 if args:
89 parser.error('No positional arguments should be given.') 96 parser.error('No positional arguments should be given.')
90 97
91 variables = {} 98 variables = {}
92 for v in build_utils.ParseGypList(options.variables): 99 for v in build_utils.ParseGypList(options.variables):
93 if '=' not in v: 100 if '=' not in v:
94 parser.error('--variables argument must contain "=": ' + v) 101 parser.error('--variables argument must contain "=": ' + v)
95 name, _, value = v.partition('=') 102 name, _, value = v.partition('=')
96 variables[name] = value 103 variables[name] = value
97 104
98 loader = RecordingFileSystemLoader(build_utils.CHROMIUM_SRC) 105 loader = RecordingFileSystemLoader(options.loader_base_dir)
99 env = jinja2.Environment(loader=loader, undefined=jinja2.StrictUndefined, 106 env = jinja2.Environment(loader=loader, undefined=jinja2.StrictUndefined,
100 line_comment_prefix='##') 107 line_comment_prefix='##')
101 if options.output: 108 if options.output:
102 ProcessFile(env, inputs[0], options.output, variables) 109 ProcessFile(env, inputs[0], options.loader_base_dir, options.output,
110 variables)
103 else: 111 else:
104 ProcessFiles(env, inputs, options.inputs_base_dir, options.outputs_zip, 112 ProcessFiles(env, inputs, options.loader_base_dir, options.inputs_base_dir,
105 variables) 113 options.outputs_zip, variables)
106 114
107 if options.depfile: 115 if options.depfile:
108 deps = loader.get_loaded_templates() + build_utils.GetPythonDependencies() 116 deps = loader.get_loaded_templates() + build_utils.GetPythonDependencies()
109 build_utils.WriteDepfile(options.depfile, deps) 117 build_utils.WriteDepfile(options.depfile, deps)
110 118
111 119
112 if __name__ == '__main__': 120 if __name__ == '__main__':
113 main() 121 main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698