Skip to content

Instantly share code, notes, and snippets.

@leonmax
Created June 13, 2020 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonmax/abb9538cd9c0574a6202e0ef4e96d93d to your computer and use it in GitHub Desktop.
Save leonmax/abb9538cd9c0574a6202e0ef4e96d93d to your computer and use it in GitHub Desktop.
generate `*_pb*.py` with bazel and copy to project
#!/usr/bin/env bash
function info () {
printf "\r \033[00;34m>>>\033[0m %s \n" "$1"
}
function remove_old() {
info "Remove all previous generated files!"
local VERBOSE="$1"
if [ -z "$VERBOSE" ]
then
find . -iname "*_pb2*.py" -delete;
else
find . -iname "*_pb2*.py" -exec rm "${VERBOSE}" -f {} \;
fi
}
function bazel_gen() {
TARGET_LABEL=$1
info "Run bazel target!"
bazel build "${TARGET_LABEL}"
}
function list_py() {
local TAR_PATH="$1"
tar tf "${TAR_PATH}" \
| grep -ve "\.\." \
| grep _pb2
}
function extract_py() {
local TAR_PATH="$1"
local VERBOSE="$2"
info "Extracting protobuf generated python files from ${TAR_PATH}"
list_py "${TAR_PATH}" | xargs tar "${VERBOSE}" -xf "${TAR_PATH}"
}
function reduce_tar() {
local TAR_PATH="$1"
local REDUCED_TAR_PATH="$2"
local VERBOSE="$3"
local MANIFEST_FILE="bazel-bin/manifest.txt"
info "Reduced tar with only protobuf generated python files from ${TAR_PATH}"
list_py "${TAR_PATH}" > ${MANIFEST_FILE}
< ${MANIFEST_FILE} xargs tar "${VERBOSE}" -xf "${TAR_PATH}"
< ${MANIFEST_FILE} xargs tar "${VERBOSE}" -rf "${REDUCED_TAR_PATH}"
}
function usage() {
cat <<-EOF
./gen_pb.sh [options]
This small script did the following:
* Remove all existing *.pb2.py or *.pb2_grpc.py files
* Bazel build the ":proto_pkg" target
* Extract all *.pb2.py or *.pb2_grpc.py to the current directory.
In addition:
* when --reduce is defined, pack all the extracted files into a tar, which reduce the tar size significantly
* when --extract is defined, skip the first two steps and extract directly from provided tar
Note that --reduce and --extract are mutually exclusive
Options:
-h --help:
Show this help message
-v --verbose:
List removed files and extracted files when true
-r --reduce REDUCED_TAR_PATH:
Extract and save the protos to that TAR_PATH to reduce the size
-e --extract SOURCE_TAR_PATH:
No removal or bazel will be triggered, only extract from that TAR_PATH
EOF
}
PROGRAM="${0##*/}"
GETOPT=$(command -v gnu-getopt || command -v getopt)
function main() {
local VERBOSE=""
local EXTRACT_ONLY=false
local REDUCE_ONLY=false
local TAR_PATH=""
local REDUCED_TAR_PATH=""
local PACKAGE_NAME=""
local TARGET_NAME="proto_pkg"
opts="$($GETOPT -o hve:r: -l help,verbose,extract:,reduce: -n "$PROGRAM" -- "$@")";
eval set -- "$opts"
while true;
do
case $1 in
-h | --help)
usage; exit 0;;
-v | --verbose)
VERBOSE="-v"
shift;;
-e | --extract)
EXTRACT_ONLY=true
shift;
TAR_PATH=$1
shift;;
-r | --reduce)
REDUCE_ONLY=true
shift;
REDUCED_TAR_PATH=$1
shift;;
--)
shift;
break;;
esac
done
[ "$#" -gt 1 ] && usage && exit 1
if [ -z "${TAR_PATH}" ]
then
[[ -z "${PACKAGE_NAME}" ]] \
&& TAR_PATH="bazel-bin/${TARGET_NAME}.tar" \
|| TAR_PATH="bazel-bin/${PACKAGE_NAME}/${TARGET_NAME}.tar"
fi
if [ "${EXTRACT_ONLY}" != "true" ];
then
remove_old ${VERBOSE}
bazel_gen "//${PACKAGE_NAME}:${TARGET_NAME}"
fi
if [ "${REDUCE_ONLY}" == "true" ];
then
reduce_tar ${TAR_PATH} "${REDUCED_TAR_PATH}" ${VERBOSE}
else
extract_py ${TAR_PATH} ${VERBOSE}
fi
info "You are all set!"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment