OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 """Converts protocol buffer definitions to ones supported by the Nano library. | 6 """Converts protocol buffer definitions to ones supported by the Nano library. |
7 | 7 |
8 Note: Java files generated from the output of this script should only be used | 8 Note: Java files generated from the output of this script should only be used |
9 in tests. | 9 in tests. |
10 | 10 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 with open(os.path.join(options.output_dir, file_name), 'w') as new_file: | 45 with open(os.path.join(options.output_dir, file_name), 'w') as new_file: |
46 new_file.write(new_contents) | 46 new_file.write(new_contents) |
47 | 47 |
48 | 48 |
49 def ConvertProtoFileContents(contents): | 49 def ConvertProtoFileContents(contents): |
50 """Returns a Nano-compatible version of contents. | 50 """Returns a Nano-compatible version of contents. |
51 | 51 |
52 Args: | 52 Args: |
53 contents: The contents of a protocol buffer definition file. | 53 contents: The contents of a protocol buffer definition file. |
54 """ | 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 | 55 # 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 | 56 # after the syntax declaration, so look for the declaration and place the |
62 # options immediately after it. | 57 # options immediately after it. |
63 # TODO(pvalenzuela): Set Java options via proto compiler flags instead of | 58 # TODO(pvalenzuela): Set Java options via proto compiler flags instead of |
64 # modifying the files here. | 59 # modifying the files here. |
65 syntax_regex = re.compile(r'^\s*syntax\s*=.*;', re.MULTILINE) | 60 syntax_regex = re.compile(r'^\s*syntax\s*=.*;', re.MULTILINE) |
66 syntax_end = syntax_regex.search(pruned_contents).end() | 61 syntax_end = syntax_regex.search(contents).end() |
67 java_options = ( | 62 java_options = ( |
68 'option java_multiple_files = true; ' | 63 'option java_multiple_files = true; ' |
69 'option java_package = "org.chromium.components.sync.protocol";') | 64 'option java_package = "org.chromium.components.sync.protocol";') |
70 | 65 |
71 contents_to_join = (pruned_contents[:syntax_end], java_options, | 66 contents_to_join = (contents[:syntax_end], java_options, |
72 pruned_contents[syntax_end:]) | 67 contents[syntax_end:]) |
73 return ''.join(contents_to_join) | 68 return ''.join(contents_to_join) |
74 | 69 |
75 | 70 |
76 if __name__ == '__main__': | 71 if __name__ == '__main__': |
77 sys.exit(main()) | 72 sys.exit(main()) |
OLD | NEW |