#!/usr/bin/env bash
set -e
if ! command -v openapi-generator-cli &> /dev/null; then
echo "Error: openapi-generator-cli not found."
exit 1
fi
SPEC_PATH=$1
OUTPUT_DIR=$2
if [ ! -f "$SPEC_PATH" ]; then
echo "Error: OpenAPI specification file not found at $SPEC_PATH"
exit 1
fi
generate_sdk() {
mkdir -p "$OUTPUT_DIR"
echo "Generating Python SDK from $SPEC_PATH to $OUTPUT_DIR..."
# Download LICENSE file from System Initiative repo
echo "Downloading LICENSE file..."
curl -s "https://raw.githubusercontent.com/systeminit/si/main/LICENSE" > "$OUTPUT_DIR/LICENSE"
cat > config.json << EOL
{
"packageName": "system_initiative_api_client",
"projectName": "system-initiative-api-client",
"packageVersion": "1.0.0",
"packageUrl": "https://github.com/systeminit/si",
"author": "System Initiative",
"authorEmail": "support@systeminit.com",
"developerName": "System Initiative",
"developerEmail": "info@systeminit.com",
"developerOrganization": "System Initiative",
"developerOrganizationUrl": "https://systeminit.com",
"artifactDescription": "Python SDK for the System Initiative Public API",
"artifactUrl": "https://github.com/systeminit/si",
"hideGenerationTimestamp": true,
"licenseId": "Apache-2.0",
"licenseName": "Apache License 2.0",
"licenseUrl": "https://www.apache.org/licenses/LICENSE-2.0.html"
}
EOL
# Extract package data from config.json before it's removed
PACKAGE_NAME=$(grep -o '"packageName": "[^"]*' config.json | cut -d'"' -f4)
PACKAGE_VERSION=$(grep -o '"packageVersion": "[^"]*' config.json | cut -d'"' -f4)
PACKAGE_URL=$(grep -o '"packageUrl": "[^"]*' config.json | cut -d'"' -f4)
AUTHOR=$(grep -o '"author": "[^"]*' config.json | cut -d'"' -f4)
AUTHOR_EMAIL=$(grep -o '"authorEmail": "[^"]*' config.json | cut -d'"' -f4)
ARTIFACT_DESCRIPTION=$(grep -o '"artifactDescription": "[^"]*' config.json | cut -d'"' -f4)
LICENSE_NAME=$(grep -o '"licenseName": "[^"]*' config.json | cut -d'"' -f4)
LICENSE_ID=$(grep -o '"licenseId": "[^"]*' config.json | cut -d'"' -f4)
LICENSE_URL=$(grep -o '"licenseUrl": "[^"]*' config.json | cut -d'"' -f4)
DEVELOPER_NAME=$(grep -o '"developerName": "[^"]*' config.json | cut -d'"' -f4)
DEVELOPER_EMAIL=$(grep -o '"developerEmail": "[^"]*' config.json | cut -d'"' -f4)
DEVELOPER_ORG=$(grep -o '"developerOrganization": "[^"]*' config.json | cut -d'"' -f4)
DEVELOPER_ORG_URL=$(grep -o '"developerOrganizationUrl": "[^"]*' config.json | cut -d'"' -f4)
openapi-generator-cli generate \
-i "$SPEC_PATH" \
-g python \
-o "$OUTPUT_DIR" \
-c config.json \
--skip-operation-example \
--skip-validate-spec \
--additional-properties=licenseId=Apache-2.0
rm config.json
find "$OUTPUT_DIR" -name ".openapi-generator" -type d -exec rm -rf {} +
find "$OUTPUT_DIR" -name "docs" -type d -exec rm -rf {} +
find "$OUTPUT_DIR" -name ".github" -type d -exec rm -rf {} +
find "$OUTPUT_DIR" -name ".gitignore" -type f -delete
find "$OUTPUT_DIR" -name ".travis.yml" -type f -delete
find "$OUTPUT_DIR" -name ".gitlab-ci.yml" -type f -delete
find "$OUTPUT_DIR" -name "git_push.sh" -type f -delete
find "$OUTPUT_DIR" -name ".openapi-generator-ignore" -type f -delete
# Customize setup.py with all metadata from config.json
SETUP_PY="$OUTPUT_DIR/setup.py"
if [ -f "$SETUP_PY" ]; then
echo "Updating setup.py with all metadata..."
# Create a complete setup.py - more reliable than sed for complex modifications
TMP_SETUP_PY="$OUTPUT_DIR/setup.py.new"
# Extract REQUIRES section from original setup.py
REQUIRES_SECTION=$(grep -A 10 "REQUIRES = \[" "$SETUP_PY" | sed -n '/REQUIRES/,/\]/p')
cat > "$TMP_SETUP_PY" << EOF
# coding: utf-8
"""
System Initiative API
The API Server for interacting with a System Initiative workspace
The version of the OpenAPI document: $PACKAGE_VERSION
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
""" # noqa: E501
from setuptools import setup, find_packages # noqa: H301
# To install the library, run the following
#
# python setup.py install
#
# prerequisite: setuptools
# http://pypi.python.org/pypi/setuptools
NAME = "$PACKAGE_NAME"
VERSION = "$PACKAGE_VERSION"
PYTHON_REQUIRES = ">= 3.8"
$REQUIRES_SECTION
setup(
name=NAME,
version=VERSION,
description="$ARTIFACT_DESCRIPTION",
author="$AUTHOR",
author_email="$AUTHOR_EMAIL",
url="$PACKAGE_URL",
install_requires=REQUIRES,
packages=find_packages(exclude=["test", "tests"]),
include_package_data=True,
python_requires=PYTHON_REQUIRES,
license="$LICENSE_ID",
long_description="""$ARTIFACT_DESCRIPTION""",
long_description_content_type='text/markdown',
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
],
project_urls={
"Source": "$PACKAGE_URL",
"Documentation": "https://docs.systeminit.com/reference/public-api",
"Bug Tracker": "$PACKAGE_URL/issues",
},
package_data={"$PACKAGE_NAME": ["py.typed"]},
)
EOF
# Replace the original with our modified version
mv "$TMP_SETUP_PY" "$SETUP_PY"
echo "setup.py updated with all package metadata"
fi
# Also create/update pyproject.toml
PYPROJECT_TOML="$OUTPUT_DIR/pyproject.toml"
echo "Creating pyproject.toml with metadata..."
cat > "$PYPROJECT_TOML" << EOF
[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "${PACKAGE_NAME}"
version = "${PACKAGE_VERSION}"
description = "${ARTIFACT_DESCRIPTION}"
authors = [
{name = "${AUTHOR}", email = "${AUTHOR_EMAIL}"}
]
readme = "README.md"
requires-python = ">=3.8"
license = "${LICENSE_ID}"
license-files = ["LICENSE"]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Operating System :: OS Independent",
]
dependencies = [
"urllib3 >= 2.1.0, < 3.0.0",
"python-dateutil >= 2.8.2",
"pydantic >= 2",
"typing-extensions >= 4.7.1",
]
[project.urls]
"Homepage" = "${PACKAGE_URL}"
"Bug Tracker" = "${PACKAGE_URL}/issues"
"Documentation" = "https://docs.systeminit.com/reference/public-api"
"Source Code" = "${PACKAGE_URL}"
[tool.setuptools]
package-data = {"${PACKAGE_NAME}" = ["py.typed"]}
[tool.setuptools.packages.find]
exclude = ["test", "tests"]
EOF
echo "Created pyproject.toml with all package metadata"
# Update README.md with better documentation
README_MD="$OUTPUT_DIR/README.md"
if [ -f "$README_MD" ]; then
echo "Updating README.md with improved content..."
cat > "$README_MD" << EOF
# ${PACKAGE_NAME}
${ARTIFACT_DESCRIPTION}
## Installation
You can install the package via pip:
\`\`\`bash
pip install ${PACKAGE_NAME}
\`\`\`
## Requirements
Python >=3.8
## Usage
Please refer to the [documentation](${PACKAGE_URL}) for more information.
### Authentication
This API uses BASIC authentication.
\`\`\`python
import ${PACKAGE_NAME}
from ${PACKAGE_NAME}.api_client import ApiClient
from ${PACKAGE_NAME}.configuration import Configuration
# Configure API key authorization
api_token = os.environ.get('SI_API_TOKEN')
api_client = system_initiative_api_client.ApiClient(configuration)
api_client.default_headers['Authorization'] = f"Bearer {api_token}"
change_sets_api = ChangeSetsApi(api_client)
workspace_id = os.environ.get("SI_WORKSPACE_ID")
def print_response(response, title="Response"):
if hasattr(response, "to_dict"):
response_dict = response.to_dict()
print(json.dumps(response_dict, indent=2, default=str))
# Example API client usage
list_response = change_sets_api.list_change_sets(workspace_id=workspace_id)
print_response(list_response, "List Change Sets Response")
\`\`\`
## License
[${LICENSE_NAME}](${LICENSE_URL})
## Author Information
- **${AUTHOR}** - ${AUTHOR_EMAIL}
- **${DEVELOPER_NAME}** - ${DEVELOPER_EMAIL}
- **Organization**: ${DEVELOPER_ORG} - ${DEVELOPER_ORG_URL}
## Development
For development, clone this repository and install in development mode:
\`\`\`bash
git clone ${PACKAGE_URL}
cd generated-sdks/python
pip install -e .
\`\`\`
EOF
echo "README.md updated with improved content"
fi
echo "SDK generation successful! SDK files available at: $OUTPUT_DIR"
echo "To install the SDK, run:"
echo " cd $OUTPUT_DIR"
echo " pip install -e ."
echo "Generation complete"
exit
}
echo "Generating Python SDK"
generate_sdk
exit 0