Skip to content

Commit

Permalink
Generate Tekton Pipeline Python SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
jinchihe committed Aug 14, 2020
1 parent e6f8baa commit 585619d
Show file tree
Hide file tree
Showing 255 changed files with 27,316 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ dist/
coverage/

# Kodata
kodata
kodata

#sdk
sdk/hack/swagger-codegen-cli.jar
55 changes: 55 additions & 0 deletions sdk/hack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

# Readme for Generating Tekton Pipeline SDK

The guide shows how to generate the openapi model and swagger.json file from Tekton Pipeline types using `openapi-gen` and generate Tekton Pipeline Python SDK Client for the Python object models using `swagger-codegen`. Also show how to upload the Tekton Pipeline SDK to Pypi.

## Generate openapi spec and swagger file.

Download `tektoncd/pipeline` repository, and execute the below script to generate openapi spec and swagger file.

```
./hack/update-openapigen.sh
```
After executing, the `openapi_generated.go` and `swagger.json` are generated and stored under `pkg/apis/pipeline/v1beta1/`.

And then copy the `pkg/apis/pipeline/v1beta1/swagger.json` to the `sdk/hack` in this repo. If not copy, the `sdk-gen.sh` will download from github directly.

## Generate Tekton Pipeline Python SDK

Execute the script `/sdk/hack/sdk-gen.sh` to install swagger-codegen and generate Tekton Pipeline Python SDK, or you can install customized swagger-codegen and generate SDK manually following the [guide](https://github.com/swagger-api/swagger-codegen#getting-started) of swagger-codegen.

```
./sdk/hack/sdk-gen.sh
```
After the script execution, the Tekton Pipeline Python SDK is generated in the `./sdk/python` directory. Some files such as [README](../python/README.md) and documents need to be merged manually after the script execution.

## (Optional) Refresh Python SDK in the Pypi

Navigate to `sdk/python/tekton` directory.

1. Install `twine`:

```bash
pip install twine
```

2. Update the Tekton Pipeline Python SDK version in the [setup.py](../python/setup.py).

3. Create some distributions in the normal way:

```bash
python setup.py sdist bdist_wheel
```

4. Upload with twine to [Test PyPI](https://packaging.python.org/guides/using-testpypi/) and verify things look right. `Twine` will automatically prompt for your username and password:
```bash
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
username: ...
password:
...
```

5. Upload to [PyPI](https://pypi.org/project/tekton-pipeline/):
```bash
twine upload dist/*
```
14 changes: 14 additions & 0 deletions sdk/hack/boilerplate.python.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2020 The Tekton Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

74 changes: 74 additions & 0 deletions sdk/hack/sdk-gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env bash

# Copyright 2020 The Tekton Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset

SWAGGER_JAR_URL="http://search.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.6/swagger-codegen-cli-2.4.6.jar"
SWAGGER_CODEGEN_JAR="sdk/hack/swagger-codegen-cli.jar"
SWAGGER_CODEGEN_CONF="sdk/hack/swagger_config.json"
SWAGGER_CODEGEN_FILE="sdk/hack/swagger.json"
SWAGGER_CODEGEN_SOURCE="https://github.com/tektoncd/pipeline/tree/master/pkg/apis/pipeline/v1beta1/swagger.json"
SDK_OUTPUT_PATH="./sdk/python"

echo "Check the swagger.json file ..."
if [ ! -f ${SWAGGER_CODEGEN_FILE} ]
then
wget -o ${SWAGGER_CODEGEN_FILE} ${SWAGGER_CODEGEN_SOURCE}
fi

echo "Downloading the swagger-codegen JAR package ..."
if [ ! -f ${SWAGGER_CODEGEN_JAR} ]
then
wget -O ${SWAGGER_CODEGEN_JAR} ${SWAGGER_JAR_URL}
fi

echo "Generating Python SDK for Tekton Pipeline ..."
java -jar ${SWAGGER_CODEGEN_JAR} generate -i ${SWAGGER_CODEGEN_FILE} -l python -o ${SDK_OUTPUT_PATH} -c ${SWAGGER_CODEGEN_CONF}

echo "Adding Python boilerplate message ..."
for i in $(find ./sdk/python -name *.py)
do
if ! grep -q Copyright $i
then
cat sdk/hack/boilerplate.python.txt $i >$i.new && mv $i.new $i
fi
done

echo "Replace Kubernetes document link ..."
MAPPING_LIST=`grep V1 ${SWAGGER_CODEGEN_CONF} |awk -F '"' '{print $2}'`
K8S_URL='https://github.com/kubernetes-client/python/blob/master/kubernetes/docs'
for map in ${MAPPING_LIST}
do
find ./sdk/python -type f -exec sed -i "s%(${map}.md)%(${K8S_URL}/${map}.md)%g" {} \;
done

echo "Update some specify files ..."
git checkout ${SDK_OUTPUT_PATH}/setup.py
# Better to merge README file munally.
#git checkout ${SDK_OUTPUT_PATH}/README.md

if ! grep -q "TektonClient" ${SDK_OUTPUT_PATH}/tekton/__init__.py
then
echo "from tekton.api.tekton_client import TektonClient" >> ${SDK_OUTPUT_PATH}/tekton/__init__.py
fi

if ! grep -q "constants" ${SDK_OUTPUT_PATH}/tekton/__init__.py
then
echo "from tekton.constants import constants" >> ${SDK_OUTPUT_PATH}/tekton/__init__.py
fi

echo "Tekton Pipeline Python SDK is generated successfully to folder ${SDK_OUTPUT_PATH}/."
Loading

0 comments on commit 585619d

Please sign in to comment.