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 """ | 6 """ |
| 7 A simple wrapper for protoc. | 7 A simple wrapper for protoc. |
| 8 | 8 |
| 9 - Adds includes in generated headers. | 9 - Adds includes in generated headers. |
| 10 - Handles building with system protobuf as an option. | 10 - Handles building with system protobuf as an option. |
| 11 """ | 11 """ |
| 12 | 12 |
| 13 import fnmatch | |
| 13 import optparse | 14 import optparse |
| 14 import os.path | 15 import os.path |
| 15 import shutil | 16 import shutil |
| 16 import subprocess | 17 import subprocess |
| 17 import sys | 18 import sys |
| 18 import tempfile | 19 import tempfile |
| 19 | 20 |
| 20 PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n' | 21 PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n' |
| 21 | 22 |
| 22 def ModifyHeader(header_file, extra_header): | 23 def ModifyHeader(header_file, extra_header): |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 34 extra_header_msg = '#include "%s"\n' % extra_header | 35 extra_header_msg = '#include "%s"\n' % extra_header |
| 35 header_contents.append(extra_header_msg) | 36 header_contents.append(extra_header_msg) |
| 36 include_point_found = True; | 37 include_point_found = True; |
| 37 if not include_point_found: | 38 if not include_point_found: |
| 38 return 1 | 39 return 1 |
| 39 | 40 |
| 40 with open(header_file, 'wb') as f: | 41 with open(header_file, 'wb') as f: |
| 41 f.write(''.join(header_contents)) | 42 f.write(''.join(header_contents)) |
| 42 return 0 | 43 return 0 |
| 43 | 44 |
| 45 def ScanForBadFiles(scan_root): | |
| 46 """Scan for bad file names, see crbug/386125 for details. Returns 1 if any | |
|
Paweł Hajdan Jr.
2014/06/20 18:38:22
nit: Please make the link clickable, http://crbug.
Andrew Hayden (chromium.org)
2014/06/25 08:40:24
Done.
| |
| 47 filenames are bad. Outputs errors to stderr. | |
| 48 | |
| 49 |scan_root| is the path to the directory to be recursively scanned. | |
| 50 """ | |
| 51 badname = False | |
| 52 real_scan_root = os.path.realpath(scan_root) | |
| 53 for dirpath, dirnames, filenames in os.walk(real_scan_root): | |
| 54 matches = fnmatch.filter(filenames, '*-*.proto') | |
| 55 if len(matches) > 0: | |
| 56 if not badname: | |
| 57 badname = True | |
| 58 sys.stderr.write('proto files must not have hyphens in their names (' | |
| 59 'see issue 386125 for more information):\n') | |
|
Paweł Hajdan Jr.
2014/06/20 18:38:22
Please make this a link, otherwise people can easi
Andrew Hayden (chromium.org)
2014/06/25 08:40:24
Done.
| |
| 60 for filename in matches: | |
| 61 sys.stderr.write(' ' + os.path.join(real_scan_root, | |
| 62 dirpath, filename) + '\n') | |
| 63 return 1 if badname else 0 | |
|
Paweł Hajdan Jr.
2014/06/20 18:38:22
This is Python, why not return bool? Also update t
Andrew Hayden (chromium.org)
2014/06/25 08:40:25
Done.
| |
| 64 | |
| 44 | 65 |
| 45 def RewriteProtoFilesForSystemProtobuf(path): | 66 def RewriteProtoFilesForSystemProtobuf(path): |
| 46 wrapper_dir = tempfile.mkdtemp() | 67 wrapper_dir = tempfile.mkdtemp() |
| 47 try: | 68 try: |
| 48 for filename in os.listdir(path): | 69 for filename in os.listdir(path): |
| 49 if not filename.endswith('.proto'): | 70 if not filename.endswith('.proto'): |
| 50 continue | 71 continue |
| 51 with open(os.path.join(path, filename), 'r') as src_file: | 72 with open(os.path.join(path, filename), 'r') as src_file: |
| 52 with open(os.path.join(wrapper_dir, filename), 'w') as dst_file: | 73 with open(os.path.join(wrapper_dir, filename), 'w') as dst_file: |
| 53 for line in src_file: | 74 for line in src_file: |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 77 parser.add_option('--proto-in-dir', | 98 parser.add_option('--proto-in-dir', |
| 78 help='The directory containing .proto files.') | 99 help='The directory containing .proto files.') |
| 79 parser.add_option('--proto-in-file', help='Input file to compile.') | 100 parser.add_option('--proto-in-file', help='Input file to compile.') |
| 80 parser.add_option('--use-system-protobuf', type=int, default=0, | 101 parser.add_option('--use-system-protobuf', type=int, default=0, |
| 81 help='Option to use system-installed protobuf ' | 102 help='Option to use system-installed protobuf ' |
| 82 'instead of bundled one.') | 103 'instead of bundled one.') |
| 83 (options, args) = parser.parse_args(sys.argv) | 104 (options, args) = parser.parse_args(sys.argv) |
| 84 if len(args) < 2: | 105 if len(args) < 2: |
| 85 return 1 | 106 return 1 |
| 86 | 107 |
| 108 if ScanForBadFiles(options.proto_in_dir) != 0: | |
| 109 return 1 | |
| 110 | |
| 87 proto_path = options.proto_in_dir | 111 proto_path = options.proto_in_dir |
| 88 if options.use_system_protobuf == 1: | 112 if options.use_system_protobuf == 1: |
| 89 proto_path = RewriteProtoFilesForSystemProtobuf(proto_path) | 113 proto_path = RewriteProtoFilesForSystemProtobuf(proto_path) |
| 90 try: | 114 try: |
| 91 # Run what is hopefully protoc. | 115 # Run what is hopefully protoc. |
| 92 protoc_args = args[1:] | 116 protoc_args = args[1:] |
| 93 protoc_args += ['--proto_path=%s' % proto_path, | 117 protoc_args += ['--proto_path=%s' % proto_path, |
| 94 os.path.join(proto_path, options.proto_in_file)] | 118 os.path.join(proto_path, options.proto_in_file)] |
| 95 ret = subprocess.call(protoc_args) | 119 ret = subprocess.call(protoc_args) |
| 96 if ret != 0: | 120 if ret != 0: |
| 97 return ret | 121 return ret |
| 98 finally: | 122 finally: |
| 99 if options.use_system_protobuf == 1: | 123 if options.use_system_protobuf == 1: |
| 100 # Remove temporary directory holding re-written files. | 124 # Remove temporary directory holding re-written files. |
| 101 shutil.rmtree(proto_path) | 125 shutil.rmtree(proto_path) |
| 102 | 126 |
| 103 # protoc succeeded, check to see if the generated cpp header needs editing. | 127 # protoc succeeded, check to see if the generated cpp header needs editing. |
| 104 if not options.extra_header or not options.generated_header: | 128 if not options.extra_header or not options.generated_header: |
| 105 return 0 | 129 return 0 |
| 106 return ModifyHeader(options.generated_header, options.extra_header) | 130 return ModifyHeader(options.generated_header, options.extra_header) |
| 107 | 131 |
| 108 | 132 |
| 109 if __name__ == '__main__': | 133 if __name__ == '__main__': |
| 110 sys.exit(main(sys.argv)) | 134 sys.exit(main(sys.argv)) |
| OLD | NEW |