diff --git a/scripts/generate_proto_stubs.sh b/scripts/generate_proto_stubs.sh index 7d7a606da..89edfc8b8 100755 --- a/scripts/generate_proto_stubs.sh +++ b/scripts/generate_proto_stubs.sh @@ -1,8 +1,10 @@ -#!/bin/bash +#!/usr/bin/env bash # Some of the proto .pyi stubs in stubs/protobuf/ # are autogenerated using the mypy-protobuf project on the # latest `.proto` files shipped with protoc. -# + +set -ex -o pipefail + # When run, this script will autogenerate the _pb2.pyi stubs to # stubs/protobuf. It should be run any time there's # a meaningful update to either PROTOBUF_VERSION or MYPY_PROTOBUF_VERSION, @@ -12,45 +14,43 @@ PROTOBUF_VERSION=3.18.1 MYPY_PROTOBUF_VERSION=v3.0.0 -set -ex - if uname -a | grep Darwin; then # brew install coreutils wget PLAT=osx else PLAT=linux fi -REPO_ROOT=$(realpath $(dirname "${BASH_SOURCE[0]}")/..) -TMP_DIR=$(mktemp -d) -PYTHON_PROTOBUF_FILENAME=protobuf-python-${PROTOBUF_VERSION}.zip -PROTOC_FILENAME=protoc-${PROTOBUF_VERSION}-${PLAT}-x86_64.zip -PROTOC_URL=https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_FILENAME} -PYTHON_PROTOBUF_URL=https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION}/${PYTHON_PROTOBUF_FILENAME} +REPO_ROOT="$(realpath "$(dirname "${BASH_SOURCE[0]}")"/..)" +TMP_DIR="$(mktemp -d)" +PYTHON_PROTOBUF_FILENAME="protobuf-python-${PROTOBUF_VERSION}.zip" +PROTOC_FILENAME="protoc-${PROTOBUF_VERSION}-${PLAT}-x86_64.zip" +PROTOC_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION}/$PROTOC_FILENAME" +PYTHON_PROTOBUF_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION}/$PYTHON_PROTOBUF_FILENAME" -cd $TMP_DIR +cd "$TMP_DIR" echo "Working in $TMP_DIR" # Install protoc -wget $PROTOC_URL +wget "$PROTOC_URL" mkdir protoc_install -unzip $PROTOC_FILENAME -d protoc_install +unzip "$PROTOC_FILENAME" -d protoc_install # Fetch protoc-python (which contains all the .proto files) -wget $PYTHON_PROTOBUF_URL -unzip $PYTHON_PROTOBUF_FILENAME -PYTHON_PROTOBUF_DIR=protobuf-$PROTOBUF_VERSION +wget "$PYTHON_PROTOBUF_URL" +unzip "$PYTHON_PROTOBUF_FILENAME" +PYTHON_PROTOBUF_DIR="protobuf-$PROTOBUF_VERSION" # Prepare virtualenv VENV=venv -python3 -m venv $VENV -source $VENV/bin/activate -pip install -r $REPO_ROOT/requirements-tests.txt # for black and isort +python3 -m venv "$VENV" +source "$VENV/bin/activate" +pip install -r "$REPO_ROOT/requirements-tests.txt" # for black and isort # Install mypy-protobuf -pip install git+https://github.com/dropbox/mypy-protobuf@${MYPY_PROTOBUF_VERSION} +pip install "git+https://github.com/dropbox/mypy-protobuf@$MYPY_PROTOBUF_VERSION" # Remove existing pyi -find $REPO_ROOT/stubs/protobuf/ -name "*_pb2.pyi" -delete +find "$REPO_ROOT/stubs/protobuf/" -name '*_pb2.pyi' -delete # Roughly reproduce the subset of .proto files on the public interface as described # by find_package_modules in the protobuf setup.py. @@ -69,9 +69,9 @@ PROTO_FILES=$(grep "generate_proto.*google" $PYTHON_PROTOBUF_DIR/python/setup.py ) # And regenerate! -protoc_install/bin/protoc --proto_path=$PYTHON_PROTOBUF_DIR/src --mypy_out=$REPO_ROOT/stubs/protobuf $PROTO_FILES +protoc_install/bin/protoc --proto_path="$PYTHON_PROTOBUF_DIR/src" --mypy_out="$REPO_ROOT/stubs/protobuf" $PROTO_FILES -isort $REPO_ROOT/stubs/protobuf -black $REPO_ROOT/stubs/protobuf +isort "$REPO_ROOT/stubs/protobuf" +black "$REPO_ROOT/stubs/protobuf" -sed -i="" "s/mypy-protobuf [^\"]*/mypy-protobuf ${MYPY_PROTOBUF_VERSION}/" $REPO_ROOT/stubs/protobuf/METADATA.toml +sed -i="" "s/mypy-protobuf [^\"]*/mypy-protobuf ${MYPY_PROTOBUF_VERSION}/" "$REPO_ROOT/stubs/protobuf/METADATA.toml"