OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright 2015 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 """Converts protocol buffer definitions to ones supported by the Nano library. | |
7 | |
8 Note: Java files generated from the output of this script should only be used | |
9 in tests. | |
10 | |
11 Chromium uses the Java Protocol Buffers Nano runtime library. This library, | |
12 compared to the standard proto compilter in C++, is limited. Most notably, the | |
13 retain_unknown_fields option is not supported. | |
14 | |
15 This script also adds Java-specific configs to enable usage in Java. This script | |
16 is Sync-specific, so a Sync package name is used. | |
17 """ | |
18 | |
19 import optparse | |
20 import os | |
21 import re | |
22 import sys | |
23 | |
24 | |
25 def main(): | |
26 parser = optparse.OptionParser( | |
27 usage='Usage: %prog [options...] original_proto_files') | |
28 parser.add_option('-o', '--output_dir', dest='output_dir', | |
29 help='Where to put the modified proto files') | |
30 | |
31 options, args = parser.parse_args() | |
32 if not options.output_dir: | |
33 print 'output_dir not specified.' | |
34 return 1 | |
35 | |
36 # Map of original base file names to their contents. | |
37 original_proto_contents = {} | |
38 for original_proto_file in args: | |
39 with open(original_proto_file, 'r') as original_file: | |
40 base_name = os.path.basename(original_proto_file) | |
41 original_proto_contents[base_name] = original_file.read() | |
42 | |
43 for file_name, original_contents in original_proto_contents.iteritems(): | |
44 new_contents = ConvertProtoFileContents(original_contents) | |
45 with open(os.path.join(options.output_dir, file_name), 'w') as new_file: | |
46 new_file.write(new_contents) | |
47 | |
48 | |
49 def ConvertProtoFileContents(contents): | |
50 """Returns a Nano-compatible version of contents. | |
51 | |
52 Args: | |
53 contents: The contents of a protocol buffer definition file. | |
54 """ | |
55 # Remove the retain_unknown_fields option. | |
56 incompatible_option_regex = re.compile( | |
57 r'^\s*option\s+retain_unknown_fields\s*=.*;', re.MULTILINE) | |
58 pruned_contents = incompatible_option_regex.sub('', contents) | |
59 | |
60 # Add the java_multiple_files and java_package options. Options must be set | |
61 # after the syntax declaration, so look for the declaration and place the | |
62 # options immediately after it. | |
63 # TODO(pvalenzuela): Set Java options via proto compiler flags instead of | |
64 # modifying the files here. | |
65 syntax_regex = re.compile(r'^\s*syntax\s*=.*;', re.MULTILINE) | |
66 syntax_end = syntax_regex.search(pruned_contents).end() | |
67 java_options = ('option java_multiple_files = true; ' | |
68 'option java_package = "org.chromium.sync.protocol";') | |
69 | |
70 contents_to_join = (pruned_contents[:syntax_end], java_options, | |
71 pruned_contents[syntax_end:]) | |
72 return ''.join(contents_to_join) | |
73 | |
74 | |
75 if __name__ == '__main__': | |
76 sys.exit(main()) | |
OLD | NEW |