|
| 1 | +#!/bin/bash |
| 2 | +# Copyright The OpenTelemetry Authors |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# Regenerate python code from opamp protos in |
| 17 | +# https://github.com/open-telemetry/opamp-spec |
| 18 | +# |
| 19 | +# To use, update OPAMP_SPEC_REPO_BRANCH_OR_COMMIT variable below to a commit hash or |
| 20 | +# tag in opentelemtry-proto repo that you want to build off of. Then, just run |
| 21 | +# this script to update the proto files. Commit the changes as well as any |
| 22 | +# fixes needed in the OTLP exporter. |
| 23 | +# |
| 24 | +# Optional envars: |
| 25 | +# OPAMP_SPEC_REPO_DIR - the path to an existing checkout of the opamp-spec repo |
| 26 | + |
| 27 | +# Pinned commit/branch/tag for the current version used in the opamp python package. |
| 28 | +OPAMP_SPEC_REPO_BRANCH_OR_COMMIT="v0.12.0" |
| 29 | + |
| 30 | +set -e |
| 31 | + |
| 32 | +OPAMP_SPEC_REPO_DIR=${OPAMP_SPEC_REPO_DIR:-"/tmp/opamp-spec"} |
| 33 | +# root of opentelemetry-python repo |
| 34 | +repo_root="$(git rev-parse --show-toplevel)" |
| 35 | +venv_dir="/tmp/opamp_proto_codegen_venv" |
| 36 | +proto_output_dir="$repo_root/src/opentelemetry/_opamp/proto" |
| 37 | + |
| 38 | +# run on exit even if crash |
| 39 | +cleanup() { |
| 40 | + echo "Deleting $venv_dir" |
| 41 | + rm -rf $venv_dir |
| 42 | +} |
| 43 | +trap cleanup EXIT |
| 44 | + |
| 45 | +echo "Creating temporary virtualenv at $venv_dir using $(python3 --version)" |
| 46 | +python3 -m venv $venv_dir |
| 47 | +source $venv_dir/bin/activate |
| 48 | +python -m pip install \ |
| 49 | + -c $repo_root/opamp-gen-requirements.txt \ |
| 50 | + grpcio-tools mypy-protobuf |
| 51 | +echo 'python -m grpc_tools.protoc --version' |
| 52 | +python -m grpc_tools.protoc --version |
| 53 | + |
| 54 | +# Clone the proto repo if it doesn't exist |
| 55 | +if [ ! -d "$OPAMP_SPEC_REPO_DIR" ]; then |
| 56 | + git clone https://github.com/open-telemetry/opamp-spec.git $OPAMP_SPEC_REPO_DIR |
| 57 | +fi |
| 58 | + |
| 59 | +# Pull in changes and switch to requested branch |
| 60 | +( |
| 61 | + cd $OPAMP_SPEC_REPO_DIR |
| 62 | + git fetch --all |
| 63 | + git checkout $OPAMP_SPEC_REPO_BRANCH_OR_COMMIT |
| 64 | + # pull if OPAMP_SPEC_BRANCH_OR_COMMIT is not a detached head |
| 65 | + git symbolic-ref -q HEAD && git pull --ff-only || true |
| 66 | +) |
| 67 | + |
| 68 | +cd $proto_output_dir |
| 69 | + |
| 70 | +# clean up old generated code |
| 71 | +find . -regex ".*_pb2.*\.pyi?" -exec rm {} + |
| 72 | + |
| 73 | +# generate proto code for all protos |
| 74 | +all_protos=$(find $OPAMP_SPEC_REPO_DIR/ -name "*.proto") |
| 75 | +python -m grpc_tools.protoc \ |
| 76 | + -I $OPAMP_SPEC_REPO_DIR/proto \ |
| 77 | + --python_out=. \ |
| 78 | + --mypy_out=. \ |
| 79 | + $all_protos |
| 80 | + |
| 81 | +sed -i -e 's/import anyvalue_pb2 as anyvalue__pb2/from . import anyvalue_pb2 as anyvalue__pb2/' opamp_pb2.py |
0 commit comments