|
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
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 | |
4 # found in the LICENSE file. | |
5 | |
6 """A simple wrapper for protoc to add includes in generated cpp headers.""" | |
7 | |
8 import subprocess | |
9 import sys | |
10 | |
11 PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n' | |
12 | |
13 def modifyHeader(header_file, extra_header): | |
Evan Martin
2012/09/11 21:12:26
ModifyHeader
Lei Zhang
2012/09/11 21:51:25
Done.
| |
14 """Adds |extra_header| to |header_file|. Returns 0 on success. | |
15 | |
16 |extra_header| is the name of the header file to include. | |
17 |header_file| is a generated protobuf cpp header. | |
18 """ | |
19 try: | |
20 include_point_found = False | |
21 header_contents = [] | |
22 with open(header_file) as f: | |
23 for line in f: | |
24 header_contents.append(line) | |
25 if line == PROTOC_INCLUDE_POINT: | |
26 extra_header_msg = '#include "%s"' % extra_header | |
27 header_contents.append(extra_header_msg) | |
28 include_point_found = True; | |
29 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.
| |
30 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.
| |
31 return 1 | |
32 | |
33 if not include_point_found: | |
34 return 1 | |
35 | |
36 try: | |
37 with open(header_file, 'wb') as f: | |
38 f.write('\n'.join(header_contents)) | |
39 f.close() | |
Evan Martin
2012/09/11 21:12:26
close not needed here too
Lei Zhang
2012/09/11 21:51:25
Done.
| |
40 except: | |
41 return 1 | |
42 return 0 | |
43 | |
44 | |
45 def main(argv): | |
46 # Expected to be called as: | |
47 # protoc_wrapper header_to_include:/path/to/cpp.pb.h /path/to/protoc \ | |
48 # [protoc args] | |
49 if len(argv) < 3: | |
50 return 1 | |
51 | |
52 # Parse the first argument: | |
53 combined_wrapper_arg = argv[1] | |
54 if combined_wrapper_arg.count(':') > 1: | |
55 return 1 | |
56 (extra_header, generated_header) = combined_wrapper_arg.split(':') | |
57 if not generated_header: | |
58 return 1 | |
59 | |
60 # Run what is hopefully protoc. | |
61 try: | |
62 ret = subprocess.call(argv[2:]) | |
63 if ret != 0: | |
64 return ret | |
65 except: | |
66 return 1 | |
67 | |
68 # protoc succeeded, check to see if the generated cpp header needs editing. | |
69 if not extra_header: | |
70 return 0 | |
71 return modifyHeader(generated_header, extra_header) | |
72 | |
73 | |
74 if __name__ == '__main__': | |
75 sys.exit(main(sys.argv)) | |
OLD | NEW |