Skip to content

Commit

Permalink
FEAT: add ansys-api-template
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgepiloto committed Jun 21, 2023
1 parent 945b9db commit 58903af
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 3 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/templates_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
- main
workflow_dispatch:

env:
MAIN_PYTHON_VERSION: '3.8'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Expand Down Expand Up @@ -47,6 +50,9 @@ jobs:
- {name: "pyansys-advanced-poetry", template: "pyansys-advanced", build-system: "poetry", outdir: "pyansys_a1/pyproduct-library"}
- {name: "pyansys-advanced-setuptools", template: "pyansys-advanced", build-system: "setuptools", outdir: "pyansys_a2/pyproduct-library"}

# All ansys API templates
- {name: "ansys-api", template: "ansys-api", build-system: "setuptools", outdir: "ansys_api_s0/pyproduct-library"}

# # All pyace template tests
- {name: "pyace-pkg", template: "pyace", build-system: "setuptools", outdir: "pyace_set0/project"}
- {name: "pyace-flask", template: "pyace-flask", build-system: "setuptools", outdir: "pyace_fla2/project"}
Expand Down Expand Up @@ -75,23 +81,23 @@ jobs:
tox -e ${{ matrix.toxenv }}-${{ matrix.toxextra }}-template
- name: Move baked project to repo again
if: matrix.python-version == '3.8' && github.event_name == 'push'
if: matrix.python-version == env.MAIN_PYTHON_VERSION && github.event_name == 'push'
run: |
mv "output/test_template_python_${{ matrix.cfg.outdir }}" baked_template
# GitHub Apps are not allowed to deal with .github workflows
if [ -d "baked_template/.github" ]; then mv baked_template/.github baked_template/.github_demo; fi
ls -a baked_template
- name: Create demo branch
if: matrix.python-version == '3.8' && github.event_name == 'push'
if: matrix.python-version == env.MAIN_PYTHON_VERSION && github.event_name == 'push'
uses: peterjgrainger/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
branch: "demo/${{ matrix.cfg.name }}"

- name: Publish demo branch
if: matrix.python-version == '3.8' && github.event_name == 'push'
if: matrix.python-version == env.MAIN_PYTHON_VERSION && github.event_name == 'push'
uses: s0/git-publish-subdir-action@develop
env:
REPO: self
Expand Down
1 change: 1 addition & 0 deletions src/ansys/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"pybasic": "Create a basic Python Package.",
"pyansys": "Create a PyAnsys Python Package project.",
"pyansys-advanced": "Create an advanced PyAnsys Python Package project.",
"ansys-api": "Create a new gRPC API definition.",
"pyansys-openapi_client": "Create an OpenAPI Client Package project.",
"pyace": "Create a Python project for any method developers.",
"pyace-flask": "Create a Flask project initialized for any developer.",
Expand Down
6 changes: 6 additions & 0 deletions src/ansys/templates/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def pyansys_advanced():
create_project("pyansys-advanced")


@new.command()
def pyansys_advanced():
"""Create a new gRPC API definition."""
create_project("ansys-api")


@new.command()
def pyansys_openapi_client():
"""Create an OpenAPI Client Package project."""
Expand Down
76 changes: 76 additions & 0 deletions src/ansys/templates/python/ansys_api/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: GitHub CI

# run only on main branch. This avoids duplicated actions on PRs
on:
pull_request:
push:
tags:
- "*"
branches:
- main

jobs:
build:
name: Build package
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: "3.7"
- name: Install build requirements
run: |
pip install -U pip
pip install build
- name: Build
run: python -m build
- name: Install
run: pip install dist/*.whl
- name: Test import
run: |
mkdir tmp
cd tmp
python -c "import {{ cookiecutter._dot_package_name }}; print('Sucessfully imported {{ cookiecutter._dot_package_name }}')"
python -c "from {{ cookiecutter._dot_package_name }} import __version__; print(__version__)"
- name: Upload packages
uses: actions/upload-artifact@v2
with:
name: {{ cookiecutter.project_slug }}-packages
path: dist/
retention-days: 7

Release:
if: github.event_name == 'push' && contains(github.ref, 'refs/tags')
needs: [build]
runs-on: ubuntu-latest
steps:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.10

- uses: actions/download-artifact@v2

- name: Display structure of downloaded files
run: ls -R

# uncomment the following section to permit upload to public PyPI

# - name: Upload to Public PyPi
# run: |
# pip install twine
# twine upload --skip-existing ./**/*.whl
# twine upload --skip-existing ./**/*.tar.gz
# env:
# TWINE_USERNAME: __token__
# TWINE_PASSWORD: {% raw %}${{ secrets.PYPI_TOKEN }} {% endraw %}

- name: Release
uses: softprops/action-gh-release@v1
with:
generate_release_notes: true
files: |
./**/*.whl
./**/*.tar.gz
./**/*.pdf
63 changes: 63 additions & 0 deletions src/ansys/templates/python/ansys_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
### {{ cookiecutter.project_slug }} gRPC Interface Package

This Python package contains the auto-generated gRPC Python interface files for
{{ cookiecutter.product_name }}.


#### Installation

Provided that these wheels have been published to public PyPI, they can be
installed with:

```
pip install {{ cookiecutter.project_slug | lower }}
```

Otherwise, see the


#### Build

To build the gRPC packages, run:

```
pip install build
python -m build
```

This will create both the source distribution containing just the protofiles
along with the wheel containing the protofiles and build Python interface
files.

Note that the interface files are identical regardless of the version of Python
used to generate them, but the last pre-built wheel for ``grpcio~=1.17`` was
Python 3.7, so to improve your build time, use Python 3.7 when building the
wheel.


#### Manual Deployment

After building the packages, manually deploy them with:

```
pip install twine
twine upload dist/*
```

Note that this is automatically done through CI/CD.


#### Automatic Deployment

This repository contains GitHub CI/CD that enables the automatic building of
source and wheel packages for these gRPC Python interface files. By default,
these are built on PRs, the main branch, and on tags when pushing. Artifacts
are uploaded for each PR.

To publicly release wheels to PyPI, ensure your branch is up-to-date and then
push tags. For example, for the version ``v0.5.0``.

```bash
git tag v0.5.0
git push --tags
```
3 changes: 3 additions & 0 deletions src/ansys/templates/python/ansys_api/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools >= 42.0.0", "wheel", "ansys_tools_protoc_helper"{% for mod in cookiecutter.proto_dependencies['modules'] %}, "{{ mod }}"{% endfor %}]
build-backend = "setuptools.build_meta:__legacy__"
50 changes: 50 additions & 0 deletions src/ansys/templates/python/ansys_api/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Installation file for the {{ cookiecutter.project_slug }} package"""

import os
from datetime import datetime

import setuptools

from ansys.tools.protoc_helper import CMDCLASS_OVERRIDE

# Get the long description from the README file
HERE = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(HERE, "README.md"), encoding="utf-8") as f:
long_description = f.read()

product = "{{ cookiecutter.product_name | slugify(separator='_') }}"
library = "{{ cookiecutter.library_name | slugify(separator='_') }}"
package_info = ["ansys", "api", product, library, "v{{ cookiecutter.api_version }}"]
with open(os.path.join(HERE, "src", "ansys", "api", product, library, "VERSION"), encoding="utf-8") as f:
version = f.read().strip()

package_name = "{{ cookiecutter.project_slug | lower }}"
dot_package_name = '.'.join(filter(None, package_info))

description = f"Autogenerated python gRPC interface package for {package_name}, built on {datetime.now().strftime('%H:%M:%S on %d %B %Y')}"

if __name__ == "__main__":
setuptools.setup(
name=package_name,
version=version,
author="ANSYS, Inc.",
author_email='[email protected]',
description=description,
long_description=long_description,
long_description_content_type='text/markdown',
url=f"https://github.com/ansys/{package_name}",
license="MIT",
python_requires=">=3.7",
install_requires=["grpcio~=1.17", "protobuf~=3.19"{% for mod in cookiecutter.proto_dependencies['modules'] %}, "{{ mod }}"{% endfor %}],
package_dir = {"": "src"},
packages=setuptools.find_namespace_packages("src", include=("ansys.*",)),
package_data={
"": ["*.proto", "*.pyi", "py.typed", "VERSION"],
},
entry_points={
"ansys.tools.protoc_helper.proto_provider": [
f"{dot_package_name}={dot_package_name}"
],
},
cmdclass=CMDCLASS_OVERRIDE
)

0 comments on commit 58903af

Please sign in to comment.