Scott Baker | bb7edfd | 2019-12-13 12:18:56 -0800 | [diff] [blame] | 1 | # Copyright 2019-present the original author or authors. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | # Separate the incoming stream of messages from kafkacat and call |
| 16 | # protoc on each message. |
| 17 | |
| 18 | from __future__ import print_function |
| 19 | |
| 20 | import subprocess |
| 21 | import sys |
| 22 | |
| 23 | |
| 24 | def call_protoc(buf, msgName, protoFileName, includeDir, first): |
| 25 | process = subprocess.Popen(["protoc", "--decode="+msgName, protoFileName, "-I", includeDir], stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 26 | process.stdin.write(buf) |
| 27 | if not first: |
| 28 | print(",") |
| 29 | print(process.communicate()[0].decode("utf-8")) |
| 30 | process.stdin.close() |
| 31 | |
| 32 | |
| 33 | def main(): |
| 34 | if len(sys.argv) != 4: |
| 35 | print(sys.stderr, "syntax: callprotoc.py <msgname> <protofilename> <includedir>", file=sys.stderr) |
| 36 | sys.exit(-1) |
| 37 | |
| 38 | msgName = sys.argv[1] |
| 39 | protoFileName = sys.argv[2] |
| 40 | includeDir = sys.argv[3] |
| 41 | |
| 42 | print ("[") |
| 43 | |
| 44 | buf = b"" |
| 45 | first = True |
| 46 | in_bytes = sys.stdin.buffer.read(1) |
| 47 | while in_bytes: |
| 48 | buf = buf + in_bytes |
| 49 | while b"===VOLTHA-DELIM===" in buf: |
| 50 | (part, buf) = buf.split(b"===VOLTHA-DELIM===", 1) |
Scott Baker | bb7edfd | 2019-12-13 12:18:56 -0800 | [diff] [blame] | 51 | call_protoc(part, msgName, protoFileName, includeDir, first) |
Scott Baker | 659c644 | 2019-12-16 13:35:45 -0800 | [diff] [blame] | 52 | first = False |
Scott Baker | bb7edfd | 2019-12-13 12:18:56 -0800 | [diff] [blame] | 53 | in_bytes = sys.stdin.buffer.read(1) |
| 54 | |
| 55 | # there is likely one trailing message still to print |
| 56 | if buf: |
| 57 | call_protoc(buf, msgName, protoFileName, includeDir, first) |
| 58 | |
| 59 | print ("]") |
| 60 | |
| 61 | |
| 62 | if __name__ == "__main__": |
| 63 | main() |