Index: tools/protoc_wrapper/protoc_wrapper.py |
=================================================================== |
--- tools/protoc_wrapper/protoc_wrapper.py (revision 0) |
+++ tools/protoc_wrapper/protoc_wrapper.py (revision 0) |
@@ -0,0 +1,75 @@ |
+#!/usr/bin/env python |
+# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""A simple wrapper for protoc to add includes in generated cpp headers.""" |
+ |
+import subprocess |
+import sys |
+ |
+PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n' |
+ |
+def modifyHeader(header_file, extra_header): |
Evan Martin
2012/09/11 21:12:26
ModifyHeader
Lei Zhang
2012/09/11 21:51:25
Done.
|
+ """Adds |extra_header| to |header_file|. Returns 0 on success. |
+ |
+ |extra_header| is the name of the header file to include. |
+ |header_file| is a generated protobuf cpp header. |
+ """ |
+ try: |
+ include_point_found = False |
+ header_contents = [] |
+ with open(header_file) as f: |
+ for line in f: |
+ header_contents.append(line) |
+ if line == PROTOC_INCLUDE_POINT: |
+ extra_header_msg = '#include "%s"' % extra_header |
+ header_contents.append(extra_header_msg) |
+ include_point_found = True; |
+ f.close() |
Evan Martin
2012/09/11 21:12:26
the "with..." bit already does this
Lei Zhang
2012/09/11 21:51:25
Good to know, removed.
|
+ except: |
Evan Martin
2012/09/11 21:12:26
this catch will catch more than you expect, like f
Lei Zhang
2012/09/11 21:51:25
Done.
|
+ return 1 |
+ |
+ if not include_point_found: |
+ return 1 |
+ |
+ try: |
+ with open(header_file, 'wb') as f: |
+ f.write('\n'.join(header_contents)) |
+ f.close() |
Evan Martin
2012/09/11 21:12:26
close not needed here too
Lei Zhang
2012/09/11 21:51:25
Done.
|
+ except: |
+ return 1 |
+ return 0 |
+ |
+ |
+def main(argv): |
+ # Expected to be called as: |
+ # protoc_wrapper header_to_include:/path/to/cpp.pb.h /path/to/protoc \ |
+ # [protoc args] |
+ if len(argv) < 3: |
+ return 1 |
+ |
+ # Parse the first argument: |
+ combined_wrapper_arg = argv[1] |
+ if combined_wrapper_arg.count(':') > 1: |
+ return 1 |
+ (extra_header, generated_header) = combined_wrapper_arg.split(':') |
+ if not generated_header: |
+ return 1 |
+ |
+ # Run what is hopefully protoc. |
+ try: |
+ ret = subprocess.call(argv[2:]) |
+ if ret != 0: |
+ return ret |
+ except: |
+ return 1 |
+ |
+ # protoc succeeded, check to see if the generated cpp header needs editing. |
+ if not extra_header: |
+ return 0 |
+ return modifyHeader(generated_header, extra_header) |
+ |
+ |
+if __name__ == '__main__': |
+ sys.exit(main(sys.argv)) |
Property changes on: tools/protoc_wrapper/protoc_wrapper.py |
___________________________________________________________________ |
Added: svn:executable |
+ * |
Added: svn:eol-style |
+ LF |