Skip to content

Commit

Permalink
chore: create a python version of tendermint runtime
Browse files Browse the repository at this point in the history
Signed-off-by: Kostas Christopoulos <k.christopoulos@rocketfueldev.com>
  • Loading branch information
kchrist-rocketfueldev committed Nov 8, 2023
1 parent d48997f commit bb2452a
Show file tree
Hide file tree
Showing 15 changed files with 1,286 additions and 19 deletions.
70 changes: 51 additions & 19 deletions compile-proto.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
#!/bin/bash

print_help() {
echo "Usage: $0 -p|--proto-path <proto_file_location> -o|--output <output_location>"
echo "Usage: $0 -p|--proto-path <proto_file_location> -o|--output <output_location> -l|--language <target_language>"
echo
echo "Options:"
echo " -p, --proto-path <proto_file_location> Specify the path to the .proto file."
echo " -o, --output <output_location> Specify the output directory for the generated files."
echo " -l, --language <target_language> Specify the target language (typescript or python)."
echo " -h, --help Display this help message."
}

while [[ "$#" -gt 0 ]]; do
case $1 in
-p|--proto-path) proto_file_location="$2"; shift; ;;
-o|--output) output_location="$2"; shift; ;;
-l|--language) target_language="$2"; shift; ;;
-h|--help) print_help; exit 0 ;;
*) echo "Unknown parameter passed: $1"; print_help; exit 1 ;;
esac
shift
done

# Check if required arguments are provided
if [ -z "$proto_file_location" ] || [ -z "$output_location" ]; then
echo "Error: Both -p or --proto-path and -o or --output options are required."
if [ -z "$proto_file_location" ] || [ -z "$output_location" ] || [ -z "$target_language" ]; then
echo "Error: -p or --proto-path, -o or --output, and -l or --language options are required."
print_help
exit 1
fi
Expand All @@ -31,29 +33,59 @@ if [ ! -d "$output_location" ]; then
mkdir -p "$output_location"
fi

# Define the dependencies and download them if necessary
PROTOC_GEN_TS_PROTO="./node_modules/.bin/protoc-gen-ts_proto"
# Define the dependencies and download them if necessary based on the selected target language
proto_path_dir=$(dirname "$proto_file_location")
if [ "$target_language" == "typescript" ]; then
PROTOC_GEN_TS_PROTO="./node_modules/.bin/protoc-gen-ts_proto"
PROTOC_CMD="protoc --proto_path='$proto_path_dir' --plugin='$PROTOC_GEN_TS_PROTO' --ts_proto_opt=snakeToCamel=false --ts_proto_opt=outputServices=grpc-js --ts_proto_out='$output_location' '$proto_file_location'"
elif [ "$target_language" == "python" ]; then
PROTOC_CMD="python3 -m grpc_tools.protoc -I $proto_path_dir --python_out='$output_location' --grpc_python_out='$output_location' '$proto_file_location'"
else
echo "Error: Unsupported target language. Supported languages are 'typescript' and 'python'."
exit 1
fi

# Try to install the package, handle errors
if [ ! -f "$PROTOC_GEN_TS_PROTO" ]; then
echo "Downloading protoc-gen-ts_proto..."
# Check if the protoc plugin is installed and install if it does not exist
if ! command -v "$PROTOC_GEN_TS_PROTO" &> /dev/null; then
echo "$PROTOC_GEN_TS_PROTO is not installed. Attempting to install..."
if yarn install; then
echo "ProtoC-gen-ts_proto installed successfully."
echo "$PROTOC_GEN_TS_PROTO installed successfully."
else
echo "Error: Failed to download protoc-gen-ts_proto. Make sure you have Node.js and Yarn installed."
echo "Error: Failed to download $PROTOC_GEN_TS_PROTO. Make sure you have Node.js and Yarn installed."
exit 1
fi
else
echo "$PROTOC_GEN_TS_PROTO is already installed."
fi

# Run protoc with the specified options
proto_path_dir=$(dirname "$proto_file_location")
if protoc --proto_path="$proto_path_dir" \
--plugin="$PROTOC_GEN_TS_PROTO" \
--ts_proto_opt=snakeToCamel=false \
--ts_proto_opt=outputServices=grpc-js \
--ts_proto_out="$output_location" "$proto_file_location"; then
echo "Proto compilation completed."
# Check if the protoc plugin is installed and install it if it does not exist
if ! command -v "$PROTOC_GEN_GRPC_PYTHON" &> /dev/null; then
echo "$PROTOC_GEN_GRPC_PYTHON is not installed. Attempting to install..."
if pip3 install grpcio-tools; then
echo "$PROTOC_GEN_GRPC_PYTHON installed successfully."

# Automatically find and add the directory containing protoc-gen-grpc_python to the PATH
INSTALL_DIR=$(pip3 show grpcio-tools | grep "Location" | cut -d ' ' -f 2)
if [ -n "$INSTALL_DIR" ]; then
export PATH="$INSTALL_DIR:$PATH"
echo "$PROTOC_GEN_GRPC_PYTHON is added to the PATH."
else
echo "Error: Could not determine the installation directory of $PROTOC_GEN_GRPC_PYTHON."
exit 1
fi
else
echo "Error: Failed to install $PROTOC_GEN_GRPC_PYTHON. Make sure you have pip and grpcio-tools installed."
exit 1
fi
else
echo "$PROTOC_GEN_GRPC_PYTHON is already installed."
fi

# Run protoc with the specified options based on the selected target language
echo $PROTOC_CMD
if eval "$PROTOC_CMD"; then
echo "Proto compilation for $target_language completed."
else
echo "Error: Proto compilation failed."
echo "Error: Proto compilation for $target_language failed."
exit 1
fi
9 changes: 9 additions & 0 deletions docker-integrations/tendermint-python/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
**/node_modules/
**/dist
**/out
.git
npm-debug.log
.coverage
.coverage.*
.env
.aws
7 changes: 7 additions & 0 deletions docker-integrations/tendermint-python/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[flake8]
max-line-length = 88
ignore = E501,E402,E203
select = W504
exclude = venv,src/protos
filename = *.py
enable-extensions = E731
192 changes: 192 additions & 0 deletions docker-integrations/tendermint-python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# Created by https://www.toptal.com/developers/gitignore/api/node

# Edit at https://www.toptal.com/developers/gitignore?templates=node

### Node

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
\*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
\*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

\*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

\*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.cache
.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

.cache/

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*
.DS_Store/

### Node Patch

# Serverless Webpack directories

.webpack/

# Optional stylelint cache

# SvelteKit build / generate output

.svelte-kit
.idea
/common/types/client/
/common/types/lcd/

# End of https://www.toptal.com/developers/gitignore/api/node

/venv/
8 changes: 8 additions & 0 deletions docker-integrations/tendermint-python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.0.1]

**Note:** TODO UPDATE BEFORE MERGING
17 changes: 17 additions & 0 deletions docker-integrations/tendermint-python/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Use the official Python image as the base image
FROM python:3.11

# Set the working directory inside the container
WORKDIR /app

# Copy the Python code and protobuf files to the container
COPY ./ ./

# Install the required Python packages
RUN pip install --no-cache-dir -r requirements.txt

# Expose the gRPC port
EXPOSE 50051

# Run the Python script
CMD ["python", "src/main.py"]
Loading

0 comments on commit bb2452a

Please sign in to comment.