blob: 453d97abaa5d34af85289133ea8440ec294c1e6d [file] [log] [blame]
Scott Bakerbb7edfd2019-12-13 12:18:56 -08001# 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
18from __future__ import print_function
19
20import subprocess
21import sys
22
23
24def 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
33def 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)
51 if first:
52 first = False
53 call_protoc(part, msgName, protoFileName, includeDir, first)
54 in_bytes = sys.stdin.buffer.read(1)
55
56 # there is likely one trailing message still to print
57 if buf:
58 call_protoc(buf, msgName, protoFileName, includeDir, first)
59
60 print ("]")
61
62
63if __name__ == "__main__":
64 main()