From 98d00f0d217afa9c241e9a43bd7a715c6b7d8fdd Mon Sep 17 00:00:00 2001 From: boukeversteegh Date: Sun, 5 Jul 2020 12:13:59 +0200 Subject: [PATCH] Supports running plugin.py standalone by reading from a dump-file, so its possible to debug it. --- betterproto/plugin.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/betterproto/plugin.py b/betterproto/plugin.py index 9f4df64..27788af 100755 --- a/betterproto/plugin.py +++ b/betterproto/plugin.py @@ -386,6 +386,10 @@ def main(): request = plugin.CodeGeneratorRequest() request.ParseFromString(data) + dump_file = os.getenv("DUMP_FILE") + if dump_file: + dump_request(dump_file, request) + # Create response response = plugin.CodeGeneratorResponse() @@ -399,5 +403,16 @@ def main(): sys.stdout.buffer.write(output) +def dump_request(dump_file: str, request: CodeGeneratorRequest): + """ + For developers: Supports running plugin.py standalone so its possible to debug it. + Run protoc (or generate.py) with DUMP_FILE="yourfile.bin" to write the request to a file. + Then run plugin.py from your IDE in debugging mode, and redirect stdin to the file. + """ + with open(str(dump_file), "wb") as fh: + sys.stderr.write(f"\033[31mWriting: {dump_file}\033[0m\n") + fh.write(request.SerializeToString()) + + if __name__ == "__main__": main()