Last active
May 6, 2020 11:42
-
-
Save ashokpant/def7caeda47a8ce80c75fde7847b3f28 to your computer and use it in GitHub Desktop.
Generate protos and rpcs for python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Author: Ashok Kumar Pant | |
# Repo and script file structure | |
# ================================ | |
# proto_gen_py_and_install.sh | |
# corona_protos/ | |
# corona_py_gen/ | |
# -->coronagen/ | |
# ---->pb/ | |
# ---->rpc/ | |
# Install protogen library: pip install grpcio-tools | |
PROTO_REPO=corona_protos | |
GEN_REPO=corona_py_gen | |
GEN_REPO_PACKAGE=coronagen | |
echo "Proto repo: "${PROTO_REPO} | |
echo "Gen repo: "${GEN_REPO} | |
echo "Gen repo package: "${GEN_REPO_PACKAGE} | |
BUILD_DIR=${PROTO_REPO}/build/gen_py | |
#cd ${PROTO_REPO} || exit | |
# shellcheck disable=SC2086 | |
if [ -d ${BUILD_REPO} ]; then | |
rm -r ${BUILD_DIR:?}/ | |
fi | |
mkdir -p ${BUILD_DIR}/pb && mkdir -p ${BUILD_DIR}/rpc | |
echo "Generating proto/rpc..." | |
python -m grpc_tools.protoc -I ${PROTO_REPO}/ --python_out=${BUILD_DIR}/pb --grpc_python_out=${BUILD_DIR}/rpc ${PROTO_REPO}/*.proto | |
echo "Coping generated proto/rpc to destination repo..." | |
# shellcheck disable=SC2115 | |
rm -r ${GEN_REPO}/${GEN_REPO_PACKAGE}/* | |
cp -ri ${BUILD_DIR}/* ${GEN_REPO}/${GEN_REPO_PACKAGE}/ | |
touch ${GEN_REPO}/${GEN_REPO_PACKAGE}/pb/__init__.py | |
touch ${GEN_REPO}/${GEN_REPO_PACKAGE}/rpc/__init__.py | |
touch ${GEN_REPO}/${GEN_REPO_PACKAGE}/__init__.py | |
# Hack for relative import | |
sed -i -E 's/^(import*.*_pb2)/from '${GEN_REPO_PACKAGE}.pb' \1/' ${GEN_REPO}/${GEN_REPO_PACKAGE}/pb/*.py | |
sed -i -E 's/^(import*.*_pb2)/from '${GEN_REPO_PACKAGE}.pb' \1/' ${GEN_REPO}/${GEN_REPO_PACKAGE}/rpc/*.py | |
echo "Removing build dir..." | |
rm -rf ${BUILD_DIR} | |
echo "Installing..." | |
cd ${GEN_REPO}/ && python setup.py install |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment