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 import collections | 7 import collections |
8 import re | 8 import re |
9 import optparse | 9 import optparse |
10 import os | 10 import os |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
221 original_enum_name=enum_start.groups()[1], | 221 original_enum_name=enum_start.groups()[1], |
222 fixed_type=enum_start.groups()[3]) | 222 fixed_type=enum_start.groups()[3]) |
223 self._in_enum = True | 223 self._in_enum = True |
224 | 224 |
225 def GetScriptName(): | 225 def GetScriptName(): |
226 script_components = os.path.abspath(sys.argv[0]).split(os.path.sep) | 226 script_components = os.path.abspath(sys.argv[0]).split(os.path.sep) |
227 build_index = script_components.index('build') | 227 build_index = script_components.index('build') |
228 return os.sep.join(script_components[build_index:]) | 228 return os.sep.join(script_components[build_index:]) |
229 | 229 |
230 | 230 |
231 def DoGenerate(options, source_paths): | 231 def DoGenerate(options, output_dir_and_source_paths): |
cjhopman
2015/02/17 21:38:25
extracting out bits of the list like this is odd,
mnaganov (inactive)
2015/02/23 11:35:26
Done.
| |
232 output_dir = output_dir_and_source_paths[0] | |
233 source_paths = output_dir_and_source_paths[1:] | |
232 output_paths = [] | 234 output_paths = [] |
233 for source_path in source_paths: | 235 for source_path in source_paths: |
234 enum_definitions = DoParseHeaderFile(source_path) | 236 enum_definitions = DoParseHeaderFile(source_path) |
237 if not enum_definitions: | |
238 raise Exception('No enums found in %s\n' | |
239 'Did you forget prefixing enums with ' | |
240 '"// GENERATED_JAVA_ENUM_PACKAGE: foo"?' % | |
241 source_path) | |
235 for enum_definition in enum_definitions: | 242 for enum_definition in enum_definitions: |
236 package_path = enum_definition.enum_package.replace('.', os.path.sep) | 243 package_path = enum_definition.enum_package.replace('.', os.path.sep) |
237 file_name = enum_definition.class_name + '.java' | 244 file_name = enum_definition.class_name + '.java' |
238 output_path = os.path.join(options.output_dir, package_path, file_name) | 245 output_path = os.path.join(output_dir, package_path, file_name) |
239 output_paths.append(output_path) | 246 output_paths.append(output_path) |
240 if not options.print_output_only: | 247 if not options.print_output_only: |
cjhopman
2015/02/17 21:38:25
We could totally get rid of the options argument h
mnaganov (inactive)
2015/02/23 11:35:26
Done.
| |
241 build_utils.MakeDirectory(os.path.dirname(output_path)) | 248 build_utils.MakeDirectory(os.path.dirname(output_path)) |
242 DoWriteOutput(source_path, output_path, enum_definition) | 249 DoWriteOutput(source_path, output_path, enum_definition) |
243 return output_paths | 250 return output_paths |
244 | 251 |
245 | 252 |
246 def DoParseHeaderFile(path): | 253 def DoParseHeaderFile(path): |
247 with open(path) as f: | 254 with open(path) as f: |
248 return HeaderParser(f.readlines(), path).ParseDefinitions() | 255 return HeaderParser(f.readlines(), path).ParseDefinitions() |
249 | 256 |
250 | 257 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
293 def AssertFilesList(output_paths, assert_files_list): | 300 def AssertFilesList(output_paths, assert_files_list): |
294 actual = set(output_paths) | 301 actual = set(output_paths) |
295 expected = set(assert_files_list) | 302 expected = set(assert_files_list) |
296 if not actual == expected: | 303 if not actual == expected: |
297 need_to_add = list(actual - expected) | 304 need_to_add = list(actual - expected) |
298 need_to_remove = list(expected - actual) | 305 need_to_remove = list(expected - actual) |
299 raise Exception('Output files list does not match expectations. Please ' | 306 raise Exception('Output files list does not match expectations. Please ' |
300 'add %s and remove %s.' % (need_to_add, need_to_remove)) | 307 'add %s and remove %s.' % (need_to_add, need_to_remove)) |
301 | 308 |
302 def DoMain(argv): | 309 def DoMain(argv): |
303 parser = optparse.OptionParser() | 310 usage = 'usage: %prog [options] output_dir input_file(s)...' |
311 parser = optparse.OptionParser(usage=usage) | |
304 | 312 |
305 parser.add_option('--assert_file', action="append", default=[], | 313 parser.add_option('--assert_file', action="append", default=[], |
306 dest="assert_files_list", help='Assert that the given ' | 314 dest="assert_files_list", help='Assert that the given ' |
307 'file is an output. There can be multiple occurrences of ' | 315 'file is an output. There can be multiple occurrences of ' |
308 'this flag.') | 316 'this flag.') |
309 parser.add_option('--output_dir', help='Base path for generated files.') | |
310 parser.add_option('--print_output_only', help='Only print output paths.', | 317 parser.add_option('--print_output_only', help='Only print output paths.', |
311 action='store_true') | 318 action='store_true') |
312 | 319 |
313 options, args = parser.parse_args(argv) | 320 options, args = parser.parse_args(argv) |
314 | 321 if len(args) < 2: |
322 parser.error('Need to specify output directory and at least one input file') | |
315 output_paths = DoGenerate(options, args) | 323 output_paths = DoGenerate(options, args) |
316 | 324 |
317 if options.assert_files_list: | 325 if options.assert_files_list: |
318 AssertFilesList(output_paths, options.assert_files_list) | 326 AssertFilesList(output_paths, options.assert_files_list) |
319 | 327 |
320 return " ".join(output_paths) | 328 return " ".join(output_paths) |
321 | 329 |
322 if __name__ == '__main__': | 330 if __name__ == '__main__': |
323 DoMain(sys.argv[1:]) | 331 print DoMain(sys.argv[1:]) |
cjhopman
2015/02/17 21:38:25
We try to limit the output in a normal build, so I
mnaganov (inactive)
2015/02/23 11:35:26
I see. Added a flag then.
| |
OLD | NEW |