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 from datetime import date | 8 from datetime import date |
9 import re | 9 import re |
10 import optparse | 10 import optparse |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 'PACKAGE': enum_definition.enum_package, | 333 'PACKAGE': enum_definition.enum_package, |
334 'INT_DEF': enum_names_string, | 334 'INT_DEF': enum_names_string, |
335 'ANNOTATION': annotation_name, | 335 'ANNOTATION': annotation_name, |
336 'SCRIPT_NAME': GetScriptName(), | 336 'SCRIPT_NAME': GetScriptName(), |
337 'SOURCE_PATH': source_path, | 337 'SOURCE_PATH': source_path, |
338 'YEAR': str(date.today().year) | 338 'YEAR': str(date.today().year) |
339 } | 339 } |
340 return template.substitute(values) | 340 return template.substitute(values) |
341 | 341 |
342 | 342 |
343 def AssertFilesList(output_paths, assert_files_list): | |
344 actual = set(output_paths) | |
345 expected = set(assert_files_list) | |
346 if not actual == expected: | |
347 need_to_add = list(actual - expected) | |
348 need_to_remove = list(expected - actual) | |
349 raise Exception('Output files list does not match expectations. Please ' | |
350 'add %s and remove %s.' % (need_to_add, need_to_remove)) | |
351 | |
352 def DoMain(argv): | 343 def DoMain(argv): |
353 usage = 'usage: %prog [options] [output_dir] input_file(s)...' | 344 usage = 'usage: %prog [options] [output_dir] input_file(s)...' |
354 parser = optparse.OptionParser(usage=usage) | 345 parser = optparse.OptionParser(usage=usage) |
355 build_utils.AddDepfileOption(parser) | 346 build_utils.AddDepfileOption(parser) |
356 | 347 |
357 parser.add_option('--assert_file', action="append", default=[], | |
358 dest="assert_files_list", help='Assert that the given ' | |
359 'file is an output. There can be multiple occurrences of ' | |
360 'this flag.') | |
361 parser.add_option('--srcjar', | 348 parser.add_option('--srcjar', |
362 help='When specified, a .srcjar at the given path is ' | 349 help='When specified, a .srcjar at the given path is ' |
363 'created instead of individual .java files.') | 350 'created instead of individual .java files.') |
364 parser.add_option('--print_output_only', help='Only print output paths.', | |
365 action='store_true') | |
366 parser.add_option('--verbose', help='Print more information.', | |
367 action='store_true') | |
368 | 351 |
369 options, args = parser.parse_args(argv) | 352 options, args = parser.parse_args(argv) |
370 | 353 |
371 if options.srcjar: | 354 if not args: |
372 if not args: | 355 parser.error('Need to specify at least one input file') |
373 parser.error('Need to specify at least one input file') | 356 input_paths = args |
374 input_paths = args | 357 |
375 else: | 358 with zipfile.ZipFile(options.srcjar, 'w', zipfile.ZIP_STORED) as srcjar: |
376 if len(args) < 2: | 359 for output_path, data in DoGenerate(input_paths): |
377 parser.error( | 360 build_utils.AddToZipHermetic(srcjar, output_path, data=data) |
378 'Need to specify output directory and at least one input file') | |
379 output_dir = args[0] | |
380 input_paths = args[1:] | |
381 | 361 |
382 if options.depfile: | 362 if options.depfile: |
383 python_deps = build_utils.GetPythonDependencies() | 363 build_utils.WriteDepfile(options.depfile, options.srcjar) |
384 build_utils.WriteDepfile(options.depfile, input_paths + python_deps) | |
385 | |
386 if options.srcjar: | |
387 if options.print_output_only: | |
388 parser.error('--print_output_only does not work with --srcjar') | |
389 if options.assert_files_list: | |
390 parser.error('--assert_file does not work with --srcjar') | |
391 | |
392 with zipfile.ZipFile(options.srcjar, 'w', zipfile.ZIP_STORED) as srcjar: | |
393 for output_path, data in DoGenerate(input_paths): | |
394 build_utils.AddToZipHermetic(srcjar, output_path, data=data) | |
395 else: | |
396 # TODO(agrieve): Delete this non-srcjar branch once GYP is gone. | |
397 output_paths = [] | |
398 for output_path, data in DoGenerate(input_paths): | |
399 full_path = os.path.join(output_dir, output_path) | |
400 output_paths.append(full_path) | |
401 if not options.print_output_only: | |
402 build_utils.MakeDirectory(os.path.dirname(full_path)) | |
403 with open(full_path, 'w') as out_file: | |
404 out_file.write(data) | |
405 | |
406 if options.assert_files_list: | |
407 AssertFilesList(output_paths, options.assert_files_list) | |
408 | |
409 if options.verbose: | |
410 print 'Output paths:' | |
411 print '\n'.join(output_paths) | |
412 | |
413 # Used by GYP. | |
414 return ' '.join(output_paths) | |
415 | 364 |
416 | 365 |
417 if __name__ == '__main__': | 366 if __name__ == '__main__': |
418 DoMain(sys.argv[1:]) | 367 DoMain(sys.argv[1:]) |
OLD | NEW |