Skip to content

ProtocolBuffers:BuildScript

protoc 를 사용하기 위한 스크립트들 모음.

go

필요 라이브러리:

  • github.com/golang/protobuf
  • github.com/joho/godotenv
  • google.golang.org/grpc
  • google.golang.org/protobuf

protoc 실행 bash 스크립트:

#!/usr/bin/env bash

ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" || exit; pwd)

if ! command -v protoc &> /dev/null; then
    echo "Not found protoc command" 1>&2
    exit 1
fi

OUT_DIR="$ROOT_DIR/pkg/pb"
mkdir -p "$OUT_DIR"

echo "Compiling all Kubernetes API proto files..."

# Find all .proto files and compile them
PROTO_FILES=$(find "$ROOT_DIR/proto/k8s" -name "*.proto" 2>/dev/null)

if [ -z "$PROTO_FILES" ]; then
    echo "No proto files found in $ROOT_DIR/proto/k8s" 1>&2
    exit 1
fi

protoc \
    --go_out="$OUT_DIR" \
    --go_opt=paths=source_relative \
    --go-grpc_out="$OUT_DIR" \
    --go-grpc_opt=paths=source_relative \
    --proto_path="$ROOT_DIR/proto" \
    $PROTO_FILES

if [ $? -eq 0 ]; then
    echo "Proto compilation complete!"
    echo "Generated code location: $OUT_DIR"
else
    echo "Proto compilation failed!" 1>&2
    exit 1
fi

python

설치:

pip install grpcio-tools mypy-protobuf types-protobuf

protoc 실행 bash 스크립트:

#!/usr/bin/env bash

ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" || exit; pwd)

function print_error
{
    # shellcheck disable=SC2145
    echo -e "\033[31m$@\033[0m" 1>&2
}

function print_message
{
    # shellcheck disable=SC2145
    echo -e "\033[32m$@\033[0m"
}

function on_interrupt_trap
{
    print_error "An interrupt signal was detected."
    exit 1
}

trap on_interrupt_trap INT

PYTHON="$ROOT_DIR/python"
PROTOS_DIR="${ROOT_DIR}/cvp/protos"
PROTOC_VERSION=$("$PYTHON" -m grpc_tools.protoc --version | sed "s/libprotoc //g")
PYTHON_BIN_DIR=$("$PYTHON" -c "import sysconfig; print(sysconfig.get_path('scripts'))")
PROTOC_GEN_MYPY="${PYTHON_BIN_DIR}/protoc-gen-mypy"
mapfile -t PROTO_FILES < <(find "$PROTOS_DIR" -mindepth 1 -maxdepth 1 -name '*.proto')

if [[ -z "$PROTOC_VERSION" ]]; then
    print_error "The protoc version is unknown"
    print_error "Please install the 'grpcio-tools' package"
    exit 1
fi

if [[ ! -x "$PROTOC_GEN_MYPY" ]]; then
    print_error "Not found protoc-gen-mypy executable"
    print_error "Please install the 'mypy-protobuf' package"
    exit 1
fi

if [[ "${#PROTO_FILES[@]}" -eq 0 ]]; then
    print_error "Not found *.proto files"
    exit 1
fi

ARGS=(
    "--plugin=protoc-gen-mypy=${PYTHON_BIN_DIR}/protoc-gen-mypy"
    "--proto_path=${ROOT_DIR}"
    "--python_out=${ROOT_DIR}"
    "--mypy_out=${ROOT_DIR}"
    "--grpc_python_out=${ROOT_DIR}"
    "${PROTO_FILES[@]}"
)

echo "grpc_tools.protoc version: $PROTOC_VERSION"
echo "protoc-gen-mypy plugin path: $PROTOC_GEN_MYPY"
echo "total proto files: ${#PROTO_FILES[@]}"

print_message "grpc_tools.protoc ${ARGS[*]}"
"$PYTHON" -m grpc_tools.protoc "${ARGS[@]}"

black 의 예외 처리: python -m black "--exclude=(.*_pb2(_grpc)?\.py(i)?$|/\.git|/\.venv)" ...

flake8 예외 처리:

[flake8]
exclude =
    */.git,
    */.venv,
    *_pb2.py,
    *_pb2_grpc.py

isort 예외 처리:

[isort]
skip_glob =
    *_pb2.py
    *_pb2.pyi
    *_pb2_grpc.py

pytest-cov 예외 처리:

[run]
omit =
    */__init__.py
    */__main__.py
    *_pb2.py
    *_pb2_grpc.py

See also