Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Generate java source files from protobuf files. | 6 """Generate java source files from protobuf files. |
| 7 | 7 |
| 8 Usage: | 8 Usage: |
| 9 protoc_java.py {protoc} {proto_path} {java_out} {stamp_file} {proto_files} | 9 protoc_java.py --protoc={protoc} --proto-dir={proto_path} {proto_files} |
|
nyquist
2014/09/22 21:25:16
I think you mean proto-path instead of proto-dir.
cjhopman
2014/09/26 23:39:10
Now that we are using an OptionParser I don't thin
| |
| 10 | 10 |
| 11 This is a helper file for the genproto_java action in protoc_java.gypi. | 11 This is a helper file for the genproto_java action in protoc_java.gypi. |
| 12 | 12 |
| 13 It performs the following steps: | 13 It performs the following steps: |
|
nyquist
2014/09/22 21:25:16
These do not exactly match the steps below, but ma
cjhopman
2014/09/26 23:39:10
Done.
| |
| 14 1. Deletes all old sources (ensures deleted classes are not part of new jars). | 14 1. Deletes all old sources (ensures deleted classes are not part of new jars). |
| 15 2. Creates source directory. | 15 2. Creates source directory. |
| 16 3. Generates Java files using protoc. | 16 3. Generates Java files using protoc. |
| 17 4. Creates a new stamp file. | 17 4. Creates a new stamp file. |
| 18 """ | 18 """ |
| 19 | 19 |
| 20 import os | 20 import os |
| 21 import optparse | |
| 21 import shutil | 22 import shutil |
| 22 import subprocess | 23 import subprocess |
| 23 import sys | 24 import sys |
| 24 | 25 |
| 26 sys.path.append(os.path.join(os.path.dirname(__file__), "android", "gyp")) | |
| 27 from util import build_utils | |
| 28 | |
| 25 def main(argv): | 29 def main(argv): |
| 26 if len(argv) < 5: | 30 parser = optparse.OptionParser() |
| 27 usage() | 31 build_utils.AddDepfileOption(parser) |
| 32 parser.add_option("--protoc", help="Path to protoc binary.") | |
| 33 parser.add_option("--proto-path", help="Path to proto directory.") | |
| 34 parser.add_option("--java-out-dir", | |
| 35 help="Path to output directory for java files.") | |
| 36 parser.add_option("--srcjar", help="Path to output srcjar.") | |
| 37 parser.add_option("--stamp", help="File to touch on success.") | |
| 38 options, args = parser.parse_args(argv) | |
| 39 | |
| 40 build_utils.CheckOptions(options, parser, ['protoc', 'proto_path']) | |
| 41 if not options.java_out_dir and not options.srcjar: | |
| 42 print 'One of --java-out-dir or --srcjar must be specified.' | |
| 28 return 1 | 43 return 1 |
| 29 | 44 |
| 30 protoc_path, proto_path, java_out, stamp_file = argv[1:5] | 45 with build_utils.TempDir() as temp_dir: |
| 31 proto_files = argv[5:] | 46 # Specify arguments to the generator. |
| 47 generator_args = ['optional_field_style=reftypes', | |
| 48 'store_unknown_fields=true'] | |
| 49 out_arg = '--javanano_out=' + ','.join(generator_args) + ':' + temp_dir | |
| 50 # Generate Java files using protoc. | |
| 51 build_utils.CheckOutput( | |
| 52 [options.protoc, '--proto_path', options.proto_path, out_arg] | |
| 53 + args) | |
| 32 | 54 |
| 33 # Delete all old sources. | 55 if options.java_out_dir: |
| 34 if os.path.exists(java_out): | 56 build_utils.DeleteDirectory(options.java_out_dir) |
| 35 shutil.rmtree(java_out) | 57 shutil.copytree(temp_dir, options.java_out_dir) |
| 58 else: | |
| 59 build_utils.ZipDir(options.srcjar, temp_dir) | |
| 36 | 60 |
| 37 # Create source directory. | 61 if options.depfile: |
|
nyquist
2014/09/22 21:25:16
Should depfile be specified as a parser option?
cjhopman
2014/09/26 23:39:10
It's added by AddDepfileOption above
| |
| 38 os.makedirs(java_out) | 62 build_utils.WriteDepfile( |
| 63 options.depfile, | |
| 64 args + [options.protoc] + build_utils.GetPythonDependencies()) | |
| 39 | 65 |
| 40 # Specify arguments to the generator. | 66 if options.stamp: |
| 41 generator_args = ['optional_field_style=reftypes', | 67 build_utils.Touch(options.stamp) |
| 42 'store_unknown_fields=true'] | |
| 43 out_arg = '--javanano_out=' + ','.join(generator_args) + ':' + java_out | |
| 44 | |
| 45 # Generate Java files using protoc. | |
| 46 ret = subprocess.call( | |
| 47 [protoc_path, '--proto_path', proto_path, out_arg] + proto_files) | |
| 48 | |
| 49 if ret == 0: | |
| 50 # Create a new stamp file. | |
| 51 with file(stamp_file, 'a'): | |
| 52 os.utime(stamp_file, None) | |
| 53 | |
| 54 return ret | |
| 55 | |
| 56 def usage(): | |
| 57 print(__doc__); | |
| 58 | 68 |
| 59 if __name__ == '__main__': | 69 if __name__ == '__main__': |
| 60 sys.exit(main(sys.argv)) | 70 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |