-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_python_grpc_client.sh
executable file
·69 lines (50 loc) · 1.67 KB
/
generate_python_grpc_client.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
set -e
GITHUB_REPO_URL="https://github.com/project-kessel/inventory-api"
PROTO_FOLDER="api/kessel/inventory/v1beta1"
TEMP_DIR=$(mktemp -d)
VENV_DIR="$TEMP_DIR/venv"
OUTPUT_DIR=$(pwd)
function cleanup {
echo "Cleaning up temporary files..."
rm -rf "$TEMP_DIR"
echo "Done."
}
trap cleanup EXIT
cd "$TEMP_DIR"
echo "Initializing an empty repository and setting sparse-checkout for $PROTO_FOLDER"
git init
git remote add origin "$GITHUB_REPO_URL"
git config core.sparseCheckout true
echo "$PROTO_FOLDER/*" >> .git/info/sparse-checkout
echo "third_party/*" >> .git/info/sparse-checkout
git pull origin main
if command -v python3 &>/dev/null; then
PYTHON_CMD=python3
elif command -v python &>/dev/null; then
PYTHON_CMD=python
else
echo "Python is not installed. Please install Python and try again."
exit 1
fi
echo "Using Python command: $PYTHON_CMD"
echo "Creating Python virtual environment..."
$PYTHON_CMD -m venv "$VENV_DIR"
source "$VENV_DIR/bin/activate"
echo "Installing required packages..."
pip install grpcio-tools
echo "Generating Python client from proto files..."
$PYTHON_CMD -m grpc_tools.protoc \
-I"$TEMP_DIR/third_party/" \
-Ikessel/inventory/v1beta1="$TEMP_DIR/$PROTO_FOLDER" \
--python_out="$OUTPUT_DIR"/src \
--pyi_out="$OUTPUT_DIR"/src \
--grpc_python_out="$OUTPUT_DIR"/src \
"$TEMP_DIR/$PROTO_FOLDER"/*.proto \
"$TEMP_DIR/third_party/validate/"validate.proto
echo "Python client generated in $TEMP_DIR"
echo "Deactivating and removing the virtual environment..."
deactivate
rm -rf "$VENV_DIR"
find "$TEMP_DIR" -name "*.py"
echo "Script completed successfully."