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

feat: add cython implementation #10

Merged
merged 5 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 93 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: actions/setup-python@v4
with:
python-version: "3.9"
- uses: pre-commit/action@v2.0.3
python-version: "3.10"
- uses: pre-commit/action@v3.0.0

# Make sure commit messages follow the conventional commits convention:
# https://www.conventionalcommits.org
Expand All @@ -29,30 +29,40 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: wagoid/commitlint-github-action@v4.1.11
- uses: wagoid/commitlint-github-action@v5.3.1

test:
strategy:
fail-fast: false
matrix:
python-version:
- "3.9"
- "3.10"
- "3.11"
os:
- ubuntu-latest
- macOS-latest
extension:
- "skip_cython"
- "use_cython"
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- uses: snok/install-poetry@v1
- uses: snok/install-poetry@v1.3.3
- name: Install Dependencies
run: poetry install
run: |
if [ "${{ matrix.extension }}" = "skip_cython" ]; then
SKIP_CYTHON=1 poetry install --only=main,dev
else
poetry install --only=main,dev
fi
shell: bash
- name: Test with Pytest
run: poetry run pytest --cov-report=xml
shell: bash
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
Expand All @@ -79,7 +89,81 @@ jobs:
# - Create GitHub release
# - Publish to PyPI
- name: Python Semantic Release
uses: relekang/python-semantic-release@v7.32.2
uses: relekang/python-semantic-release@v7.33.2
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
pypi_token: ${{ secrets.PYPI_TOKEN }}

build_wheels:
needs: [release]

name: Build wheels on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- macos-11
- ubuntu-20.04
- windows-2022

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
ref: "main"

# Used to host cibuildwheel
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"

- name: Install python-semantic-release
run: python3 -m pip install python-semantic-release

- name: Get Release Tag
id: release_tag
shell: bash
run: |
echo "::set-output name=newest_release_tag::$(semantic-release print-version --current)"

- uses: actions/checkout@v3
with:
ref: "v${{ steps.release_tag.outputs.newest_release_tag }}"
fetch-depth: 0

- name: Install cibuildwheel
run: python -m pip install cibuildwheel==2.12.0

- name: Build wheels
run: python -m cibuildwheel --output-dir wheelhouse
# to supply options, put them in 'env', like:
env:
CIBW_SKIP: cp36-* cp37-* cp38-* cp39-* pp36-* pp37-* pp38-* pp39-*
CIBW_BEFORE_ALL_LINUX: apt-get install -y gcc || yum install -y gcc || apk add gcc
CIBW_BUILD_VERBOSITY: 3
REQUIRE_CYTHON: 1

- uses: actions/upload-artifact@v3
with:
path: ./wheelhouse/*.whl

upload_pypi:
needs: [build_wheels]
runs-on: ubuntu-latest
environment: release

steps:
- uses: actions/download-artifact@v3
with:
# unpacks default artifact into dist/
# if `name: artifact` is omitted, the action will create extra parent dir
name: artifact
path: dist

- uses: pypa/gh-action-pypi-publish@v1.5.0
with:
user: __token__
password: ${{ secrets.PYPI_TOKEN }}

# To test: repository_url: https://test.pypi.org/legacy/
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ repos:
- id: pyupgrade
args: [--py37-plus]
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/psf/black
Expand Down
48 changes: 48 additions & 0 deletions build_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Build optional cython modules."""

import os
from distutils.command.build_ext import build_ext
from os.path import join
from typing import Any

try:
from setuptools import Extension
except ImportError:
from distutils.core import Extension

utils_module = Extension(
"bluetooth_data_tools._utils_impl",
[
join("src", "bluetooth_data_tools", "_utils_impl.pyx"),
],
language="c++",
)


class BuildExt(build_ext):
def build_extensions(self) -> None:
try:
super().build_extensions()
except Exception: # nosec
pass


def build(setup_kwargs: Any) -> None:
if os.environ.get("SKIP_CYTHON", False):
return
try:
from Cython.Build import cythonize

setup_kwargs.update(
dict(
ext_modules=cythonize(
[utils_module, "src/bluetooth_data_tools/gap.py"],
compiler_directives={"language_level": "3"}, # Python 3
),
cmdclass=dict(build_ext=BuildExt),
)
)
except Exception:
if os.environ.get("REQUIRE_CYTHON"):
raise
pass
Loading