Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for python #41

Merged
merged 11 commits into from
May 1, 2020
23 changes: 20 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ dist: trusty
language: java
jdk: oraclejdk8
node_js: 8
addons:
apt:
packages:
- python3.5
python: 3.5
before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
Expand All @@ -13,15 +18,26 @@ cache:
env:
- GEN_LIBRARY=typescript-node
- GEN_LIBRARY=java
install: npm install @openapitools/[email protected] [email protected] -g
- GEN_LIBRARY=python
install:
- npm install @openapitools/[email protected] [email protected] -g
- pyenv install --list
- pyenv global 3.6.3
- pip3 install --upgrade pip
- pip3 install -r requirements.txt
- pip3 install -r test-requirements.txt
- pip3 install pytest pyyaml twine wheel
before_script:
- pyenv --version ; pyenv versions
- python --version ; python3 --version ; python3.5 --version
- pip3 --version ; pip3 list
script:
- bash download-and-patch.sh
- bash generate.sh $GEN_LIBRARY build
deploy:
- provider: script
skip_cleanup: true
script:
- bash generate.sh $GEN_LIBRARY publish
script: bash generate.sh $GEN_LIBRARY publish
on:
branch: master
- provider: script
Expand All @@ -43,3 +59,4 @@ before_install:
echo "Signing artifacts"
openssl aes-256-cbc -K $encrypted_bb47ec883ff6_key -iv $encrypted_bb47ec883ff6_iv -in symbol-sdk-java.gpg.enc -out symbol-sdk-java.gpg -d
fi
- pyenv versions ; python --version ; python3 --version ; python3.5 --version
20 changes: 19 additions & 1 deletion download-and-patch.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ for value in Amount BlockDuration Difficulty Height Importance Score Timestamp R
cp tmp.yml openapi3-any-of-patch.yml
done

sed -i '/anyOf: ''/d' openapi3-any-of-patch.yml
PATCHFILE="openapi3-any-of-patch.yml"
SED_ARGS_ANYOF_PATCH='/anyOf: ''/d'

case $(uname | tr '[:upper:]' '[:lower:]') in
linux*)
echo "Patch anyof using sed on Linux"
sed -i "$SED_ARGS_ANYOF_PATCH" "$PATCHFILE"
;;
darwin*)
echo "Patch anyof using sed on OSX"
sed -i '' -e "$SED_ARGS_ANYOF_PATCH" "$PATCHFILE"
;;
msys*)
echo "This patch script does not run on Windows"
;;
*)
echo "This patch script does not run on $(uname)"
;;
esac

rm tmp.yml
132 changes: 128 additions & 4 deletions generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ if [ "$1" == "-h" ]; then
echo " * vertx: it generates vertx java version"
echo " * okhttp-gson: it generates okhttp-gson java version"
echo " * typescript-node: it generates typescript-node javascript version"
echo " * python: it generates python version"
echo "[operation] is optional. Possible values: "
echo " * no value | unknown value: It generates and builds the libraries."
echo " * publish | master: It generates, builds, and publish the libraries and documentation to npm, maven repos and/or github pages. "
echo " * release: It generates, builds, and publish the libraries and documentation to npm, maven repos and/or github pages updating the version to a release."
echo " * publish | master: It generates, builds, and publish the libraries and documentation to npm, pypi, maven repos and/or github pages. "
echo " * release: It generates, builds, and publish the libraries and documentation to npm, pypi, maven repos and/or github pages updating the version to a release."
exit 0
fi
LIBRARY_ARG="$1"
OPERATION_ARG="$2"

arg1Values=['all','java','jersey2','vertx','okhttp-gson','typescript-node']
arg1Values=['all','java','jersey2','vertx','okhttp-gson','typescript-node','python']

if [[ " ${arg1Values[*]} " != *"$LIBRARY_ARG"* || "" == "$LIBRARY_ARG" ]]; then
echo "Usage: $(basename $0) [library] [operation]"
Expand Down Expand Up @@ -69,7 +70,7 @@ export JAVA_OPTS="-Dlog.level=error"

buildJava() {
OPERATION="$1"
echo "Build Java runnnig operation $OPERATION"
echo "Build Java running operation $OPERATION"
echo "./gradlew install"
./gradlew install
if [[ $OPERATION == "publish" || $OPERATION == "release" ]]; then
Expand Down Expand Up @@ -144,13 +145,132 @@ generateJavascript() {
return 0
}

generatePython() {
LIBRARY="$1"
OPERATION="$2"
ARTIFACT_ID="symbol-openapi-$LIBRARY-client"
PACKAGE_NAME="symbol_openapi_client"
LICENSE_INFO="Apache-2.0"
INFO_NAME="nemtech"
INFO_EMAIL="[email protected]"
# Prerelease and snapshot must follow PEP 440 to upload to PyPI.
PRERELEASE_VERSION="a1"
SNAPSHOT_DATETIME=".$(date -u +'%Y%m%d.%H%M%S')" # UTC time for snapshots
# Set the full package version
PACKAGE_VERSION="${VERSION}${SNAPSHOT_DATETIME}${PRERELEASE_VERSION}" # snapshot version
if [[ $OPERATION == "publish" ]]; then
PACKAGE_VERSION="${VERSION}${PRERELEASE_VERSION}" # prerelease version
elif [[ $OPERATION == "release" ]]; then
PACKAGE_VERSION="${VERSION}" # release version
fi

# Patch openapi yaml for python test generator
PY_INPUT=openapi3-python-patch.yml
cp $INPUT $PY_INPUT
echo "python_openapi3_patch.sh $PY_INPUT $INPUT"
bash python_openapi3_patch.sh $PY_INPUT $INPUT
echo "The command \"bash python_openapi3_patch.sh \$PY_INPUT \$INPUT\" exited with $?."

# Generate the python openapi library
echo "Generating $LIBRARY"
rm -rf "$BUILD_DIR/$ARTIFACT_ID"
openapi-generator generate -g "$LIBRARY" \
-o "$BUILD_DIR/$ARTIFACT_ID" \
-t "$LIBRARY-templates/" \
-i "$PY_INPUT" \
-p "projectName=$ARTIFACT_ID" \
-p "packageName=$PACKAGE_NAME" \
-p "packageVersion=$PACKAGE_VERSION" \
--additional-properties="licenseInfo=$LICENSE_INFO" \
--additional-properties="infoName=$INFO_NAME" \
--additional-properties="infoEmail=$INFO_EMAIL" \
--additional-properties="snapshot=$SNAPSHOT" \
--type-mappings=x-number-string=int
# Build, test, publish/release
buildPython "$BUILD_DIR" "$ARTIFACT_ID" "$PACKAGE_VERSION" "$OPERATION"
rm $PY_INPUT
return 0
}

buildPython() {
BUILD_DIR="$1"
ARTIFACT_ID="$2"
PACKAGE_VERSION="$3"
OPERATION="$4"

# Go to artifact build dir
ORIGINAL_DIR="$(pwd)" # we'll return to this directory after the build
cd "$BUILD_DIR/$ARTIFACT_ID"
echo "Build Python running operation '$OPERATION'"

# Build
echo "python3 setup.py sdist bdist_wheel build"
PYTHONPATH=".:${PYTHONPATH}" python3 setup.py sdist bdist_wheel build

# Patch openapi generated test files
# TEST_DIR="$ORIGINAL_DIR/build/$ARTIFACT_ID/test"
# echo "bash $ORIGINAL_DIR/python_test_files_patch.sh $TEST_DIR"
# bash "$ORIGINAL_DIR/python_test_files_patch.sh" "$TEST_DIR"
# echo "The command \"bash \$ORIGINAL_DIR/python_test_files_patch.sh \$TEST_DIR\" exited with $?."

# Tests
# Commented out pytest as the openapi generated test files have many errors that can't be easily patched.
# echo "python3 -m pytest -v --color=yes --showlocals --maxfail=100"
# PYTHONPATH=".:${PYTHONPATH}" python3 -m pytest -v --color=yes --showlocals --maxfail=100

# Publish/Release
UPLOAD=false # default to disable upload to artifact repo
if [[ $OPERATION == "publish" ]]|| [[ $OPERATION == "release" ]]; then
UPLOAD=true
REPO="pypi"
echo "Enabled upload to $REPO"
fi
if [[ $OPERATION == "test" ]] || { [[ -n ${TEST_PYPI_USER} ]] && [[ -n ${TEST_PYPI_PASS} ]]; }; then
UPLOAD=true
REPO="testpypi"
REPO_URL="https://test.pypi.org/legacy/"
echo "Enabled upload to $REPO url $REPO_URL"
fi

if [[ $UPLOAD == true ]]; then
# Log intention
if [[ $OPERATION == "release" ]]; then
echo "Releasing python artifact[$ARTIFACT_ID $PACKAGE_VERSION] to $REPO"
else
echo "Publishing python artifact[$ARTIFACT_ID $PACKAGE_VERSION] to $REPO"
fi
# Upload
if [[ $REPO == "pypi" ]]; then
if [[ -n ${PYPI_USER} ]] && [[ -n ${PYPI_PASS} ]]; then
echo "PYPI_USER and PYPI_PASS already set: Uploading to PyPI"
PYTHONPATH=".:${PYTHONPATH}" python3 -m twine upload -u "$PYPI_USER" -p "$PYPI_PASS" dist/*
else
echo "PYPI_USER and/or PYPI_PASS not set: Cancelled upload to PyPI"
fi
else
if [[ -n ${TEST_PYPI_USER} ]] && [[ -n ${TEST_PYPI_PASS} ]]; then
echo "TEST_PYPI_USER and TEST_PYPI_PASS already set: Uploading to Test PyPI"
PYTHONPATH=".:${PYTHONPATH}" python3 -m twine upload --repository-url "$REPO_URL" -u "$TEST_PYPI_USER" -p "$TEST_PYPI_PASS" dist/*
elif [[ $OPERATION == "test" ]]; then
echo "OPERATION=test -> Initiate manual upload"
PYTHONPATH=".:${PYTHONPATH}" python3 -m twine upload --repository "$REPO" dist/*
fi
fi
else
echo "REPO=${REPO:-N/A} ARTIFACT_ID=$ARTIFACT_ID PACKAGE_VERSION=$PACKAGE_VERSION"
fi

cd "$ORIGINAL_DIR" # return to original directory
}

if [[ $LIBRARY_ARG == "all" ]]; then
echo "Generating $LIBRARY_ARG and running operation $OPERATION_ARG"
generateJava "jersey2" "build"
generateJava "vertx" "build"
generateJava "okhttp-gson" "build"
buildJava "$OPERATION_ARG" "build"
generateJavascript "typescript-node" "$OPERATION_ARG"
generatePython "python" "$OPERATION_ARG"
fi

if [[ $LIBRARY_ARG == "java" ]]; then
Expand All @@ -176,3 +296,7 @@ fi
if [[ $LIBRARY_ARG == "typescript-node" ]]; then
generateJavascript "$LIBRARY_ARG" "$OPERATION_ARG"
fi

if [[ $LIBRARY_ARG == "python" ]]; then
generatePython "$LIBRARY_ARG" "$OPERATION_ARG"
fi
61 changes: 61 additions & 0 deletions python-templates/README.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# {{{projectName}}}
{{#appDescriptionWithNewLines}}
{{{appDescriptionWithNewLines}}}
{{/appDescriptionWithNewLines}}

This Python package is automatically generated by [Symbol OpenAPI Generator](https://github.com/nemtech/symbol-openapi-generator):

- API version: {{appVersion}}
- Package version: {{packageVersion}}
{{^hideGenerationTimestamp}}
- Build date: {{generatedDate}}
{{/hideGenerationTimestamp}}
- Build package: {{generatorClass}}
{{#infoUrl}}
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
{{/infoUrl}}

If you want to regenerate the client, check the [symbol-openapi-generator](https://github.com/nemtech/symbol-openapi-generator) library page.

## Requirements.

Python 3.4+

Recommended: Python 3.7+

The recommended is Python 3.7+ for compatibility with Python Catbuffer library generated by [catbuffer-generators](https://github.com/nemtech/catbuffer-generators)

## Installation & Usage
### pip install

If the python package is hosted on a repository, you can install directly using:

```sh
pip install git+https://{{gitHost}}/{{{gitUserId}}}/{{{gitRepoId}}}.git
```
(you may need to run `pip` with root permission: `sudo pip install git+https://{{gitHost}}/{{{gitUserId}}}/{{{gitRepoId}}}.git`)

Then import the package:
```python
import {{{packageName}}}
```

### Setuptools

Install via [Setuptools](http://pypi.python.org/pypi/setuptools).

```sh
python setup.py install --user
```
(or `sudo python setup.py install` to install the package for all users)

Then import the package:
```python
import {{{packageName}}}
```

## Getting Started

Please follow the [installation procedure](#installation--usage) and then run the following:

{{> common_README }}
58 changes: 58 additions & 0 deletions python-templates/git_push.sh.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/sh
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
#
# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update" "gitlab.com"

git_user_id=$1
git_repo_id=$2
release_note=$3
git_host=$4

if [ "$git_host" = "" ]; then
git_host="{{{gitHost}}}"
echo "[INFO] No command line input provided. Set \$git_host to $git_host"
fi

if [ "$git_user_id" = "" ]; then
git_user_id="{{{gitUserId}}}"
echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id"
fi

if [ "$git_repo_id" = "" ]; then
git_repo_id="{{{gitRepoId}}}"
echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id"
fi

if [ "$release_note" = "" ]; then
release_note="{{{releaseNote}}}"
echo "[INFO] No command line input provided. Set \$release_note to $release_note"
fi

# Initialize the local directory as a Git repository
git init

# Adds the files in the local repository and stages them for commit.
git add .

# Commits the tracked changes and prepares them to be pushed to a remote repository.
git commit -m "$release_note"

# Sets the new remote
git_remote=`git remote`
if [ "$git_remote" = "" ]; then # git remote not defined

if [ "$GIT_TOKEN" = "" ]; then
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
git remote add origin https://${git_host}/${git_user_id}/${git_repo_id}.git
else
git remote add origin https://${git_user_id}:${GIT_TOKEN}@${git_host}/${git_user_id}/${git_repo_id}.git
fi

fi

git pull --allow-unrelated-histories origin master

# Pushes (Forces) the changes in the local repository up to the remote repository
echo "Git pushing to https://${git_host}/${git_user_id}/${git_repo_id}.git"
git push origin master 2>&1 | grep -v 'To https'

20 changes: 20 additions & 0 deletions python-templates/licenseInfo.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
****************************************************************************
Copyright (c) 2016-present,
Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp. All rights reserved.

This file is part of Catapult.

Catapult is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Catapult is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Catapult. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************

Loading