pulumi/sdk/proto/generate.sh
Sean Gillespie c2b2f3b117
Initial Python 3 port of the Python SDK (#1563)
* Machine-assisted Python 3 port

* Hack around protoc python imports

* Regenerate gRPC and protobuf

* CR: Bash code cleanup
2018-06-26 11:14:03 -07:00

66 lines
2.8 KiB
Bash
Executable file

#!/bin/bash
# This script regenerates all Protobuf/gRPC client files.
#
# For now, it must be run manually, and the results are checked into source control. Eventually we might choose to
# automate this process as part of the overall build so that it's less manual and hence error prone.
#
# To run this script, the following pre-requisites are necessary:
#
# 1) Install the latest Protobuf compiler from https://github.com/google/protobuf/releases.
# 2) Add the `protoc` binary to your PATH (so that it can be found below).
# 3) Install the Golang Protobuf compiler by running this command from your Go workspace (also on your PATH):
# go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
# 4) Install the Node.js gRPC SDK, which includes the gRPC Node.js compiler plugin
# npm install -g grpc-tools
# and add the `grpc_tools_node_protoc_plugin` binary to your PATH.a
# 5) Install the Python gRPC SDK, which includes the gRPC Python compiler plugin
# python -m pip install grpcio grpcio-tools
#
# The results are checked into bin/; at this moment, they need to be copied to their final destinations manually.
set -e
PROTOC=$(which protoc || { >&2 echo "error: Protobuf compiler (protoc) not found on PATH"; exit 1; })
echo Generating Protobuf/gRPC SDK files:
echo -e "\tVERSION: $(protoc --version)"
echo -e "Generated by version $(protoc --version) of protoc" > ./grpc_version.txt
GO_PULUMIRPC=./go
GO_PROTOFLAGS="plugins=grpc"
echo -e "\tGo: $GO_PULUMIRPC [$GO_PROTOFLAGS]"
mkdir -p $GO_PULUMIRPC
$PROTOC --go_out=$GO_PROTOFLAGS:$GO_PULUMIRPC *.proto
JS_PULUMIRPC=../nodejs/proto/
JS_PROTOFLAGS="import_style=commonjs,binary"
echo -e "\tJS: $JS_PULUMIRPC [$JS_PROTOFLAGS]"
mkdir -p $JS_PULUMIRPC
$PROTOC --js_out=$JS_PROTOFLAGS:$JS_PULUMIRPC --grpc_out=$JS_PULUMIRPC --plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` *.proto
PY_PULUMIRPC=../python/lib/pulumi/runtime/proto/
echo -e "\tPython: $PY_PULUMIRPC"
mkdir -p $PY_PULUMIRPC
function on_exit() {
rm -rf "$TEMP_DIR"
}
# Protoc for Python has a bug where, if your proto files are all in the same directory relative
# to one another, imports of said proto files will produce imports that don't work using Python 3.
#
# Since our proto files are all in the same directory, this little bit of sed rewrites the broken
# imports that protoc produces, of the form
# import foo_pb2 as bar
# to the form
# from . import foo_pb2 as bar
# This form is semantically equivalent and is accepted by both Python 2 and Python 3.
TEMP_DIR="$(mktemp -d)"
trap on_exit EXIT
echo -e "\tPython temp dir: $TEMP_DIR"
python -m grpc_tools.protoc -I./ --python_out="$TEMP_DIR" --grpc_python_out="$TEMP_DIR" *.proto
sed -i '.old' "s/^import \([^ ]*\)_pb2 as \([^ ]*\)$/from . import \1_pb2 as \2/" "$TEMP_DIR"/*.py
cp "$TEMP_DIR"/*.py "$PY_PULUMIRPC"
echo Done.