| OLD | NEW |
| 1 #!/usr/bin/env python |
| 1 # Protocol Buffers - Google's data interchange format | 2 # Protocol Buffers - Google's data interchange format |
| 2 # Copyright 2008 Google Inc. All rights reserved. | 3 # Copyright 2008 Google Inc. All rights reserved. |
| 3 # https://developers.google.com/protocol-buffers/ | 4 # https://developers.google.com/protocol-buffers/ |
| 4 # | 5 # |
| 5 # Redistribution and use in source and binary forms, with or without | 6 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are | 7 # modification, are permitted provided that the following conditions are |
| 7 # met: | 8 # met: |
| 8 # | 9 # |
| 9 # * Redistributions of source code must retain the above copyright | 10 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. | 11 # notice, this list of conditions and the following disclaimer. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 31 |
| 31 """Protocol message implementation hooks for C++ implementation. | 32 """Script to update a failure list file to add/remove failures. |
| 32 | 33 |
| 33 Contains helper functions used to create protocol message classes from | 34 This is sort of like comm(1), except it recognizes comments and ignores them. |
| 34 Descriptor objects at runtime backed by the protocol buffer C++ API. | |
| 35 """ | 35 """ |
| 36 | 36 |
| 37 __author__ = 'tibell@google.com (Johan Tibell)' | 37 import argparse |
| 38 import fileinput |
| 38 | 39 |
| 39 from google.protobuf.pyext import _message | 40 parser = argparse.ArgumentParser( |
| 41 description='Adds/removes failures from the failure list.') |
| 42 parser.add_argument('filename', type=str, help='failure list file to update') |
| 43 parser.add_argument('--add', dest='add_list', action='append') |
| 44 parser.add_argument('--remove', dest='remove_list', action='append') |
| 40 | 45 |
| 46 args = parser.parse_args() |
| 41 | 47 |
| 42 class GeneratedProtocolMessageType(_message.MessageMeta): | 48 add_set = set() |
| 49 remove_set = set() |
| 43 | 50 |
| 44 """Metaclass for protocol message classes created at runtime from Descriptors. | 51 for add_file in (args.add_list or []): |
| 52 with open(add_file) as f: |
| 53 for line in f: |
| 54 add_set.add(line) |
| 45 | 55 |
| 46 The protocol compiler currently uses this metaclass to create protocol | 56 for remove_file in (args.remove_list or []): |
| 47 message classes at runtime. Clients can also manually create their own | 57 with open(remove_file) as f: |
| 48 classes at runtime, as in this example: | 58 for line in f: |
| 59 if line in add_set: |
| 60 raise "Asked to both add and remove test: " + line |
| 61 remove_set.add(line.strip()) |
| 49 | 62 |
| 50 mydescriptor = Descriptor(.....) | 63 add_list = sorted(add_set, reverse=True) |
| 51 class MyProtoClass(Message): | |
| 52 __metaclass__ = GeneratedProtocolMessageType | |
| 53 DESCRIPTOR = mydescriptor | |
| 54 myproto_instance = MyProtoClass() | |
| 55 myproto.foo_field = 23 | |
| 56 ... | |
| 57 | 64 |
| 58 The above example will not work for nested types. If you wish to include them, | 65 existing_list = file(args.filename).read() |
| 59 use reflection.MakeClass() instead of manually instantiating the class in | |
| 60 order to create the appropriate class structure. | |
| 61 """ | |
| 62 | 66 |
| 63 # Must be consistent with the protocol-compiler code in | 67 with open(args.filename, "w") as f: |
| 64 # proto2/compiler/internal/generator.*. | 68 for line in existing_list.splitlines(True): |
| 65 _DESCRIPTOR_KEY = 'DESCRIPTOR' | 69 test = line.split("#")[0].strip() |
| 70 while len(add_list) > 0 and test > add_list[-1]: |
| 71 f.write(add_list.pop()) |
| 72 if test not in remove_set: |
| 73 f.write(line) |
| OLD | NEW |