はじめに
特許アルゴリズムの実装をPyPIに登録しました(Cython+ライブラリ構成でPyPI登録に苦労した話:特許実装をpipから使えるようにしました)が、自前環境(Windows)以外での検証をしておらず、検証しておこうと思い、GitHubActionsで簡単なCI/CDを構築しました。
手順は他の方がまとめていると思うので、詰まったところだけ備忘録として残したいと思います。
簡単な手順
GitHubリポジトリ内の決められた名前のフォルダ(「.github/workflows/」)に、設定を記したymlファイルを追加して、アップロードして、記載した内容の処理が実行される感じです。
UIからボタンを押して実行や、tagをpushしたときに実行などを選ぶことができます。
- yaml例
name: CI Test
on:
workflow_dispatch:
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ['3.10', '3.11', '3.12']
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install numpy
pip install build pytest cython
pip install -e .
- name: Run tests
run: |
pytest
- name: Build package
run: |
python -m build
今回行った事
テストやデプロイをGitHubのUIから手動で実行するような形にしました。
そして、簡単なテストを実行し、対象OSは、windows、linux、mac、pythonバージョンは、3.10、3.11、3.12の3バージョンの確認を行っています。
詰まったところ
- cython+フォルダ構成が複雑なので、bedcmm.patternのpackageが読み込めない旨の警告が消えませんでした
Package 'bedcmm.pattern' is absent from the packages configuration. !! ********************************************************************************
############################ # Package would be ignored # ############################
Python recognizes 'bedcmm.pattern' as an importable package[^1], but it is absent from setuptools' packages configuration. This leads to an ambiguous overall configuration. If you want to distribute this package, please make sure that 'bedcmm.pattern' is explicitly added to the packages configuration field. Alternatively, you can also rely on setuptools' discovery methods (for example by using find_namespace_packages(...)/find_namespace: instead of find_packages(...)/find:).
You can read more about "package discovery" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/package_discovery.html
If you don't want 'bedcmm.pattern' to be distributed and are already explicitly excluding 'bedcmm.pattern' via find_namespace_packages(...)/find_namespace or find_packages(...)/find, you can try to use exclude_package_data, or include-package-data=False in combination with a more fine grained package-data configuration. You can read more about "package data files" on setuptools documentation page: - https://setuptools.pypa.io/en/latest/userguide/datafiles.html
[^1]: For Python, any directory (with suitable naming) can be imported, even if it does not contain any .py files. On the other hand, currently there is no concept of package data directory, all directories are treated like packages. ******************************************************************************** !! check.warn(importable)
setup.pyの記載をシンプルにして、include_package_data=Falseを追記したら解決しました。
ライブラリの自動抽出がうまくいっていないのを無効化して、直接指定しているイメージです。
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
from Cython.Build import cythonize
import numpy
ext_modules = cythonize([
Extension(
"bedcmm.pattern.cy_impl", # bedcmm/pattern/cy_impl.pyx
["bedcmm/pattern/cy_impl.pyx"],
include_dirs=[numpy.get_include()],
language="c"
)
], compiler_directives={'language_level': "3"})
setup(
packages=["bedcmm", "bedcmm.pattern", "bedcmm.communication"],
include_package_data=False,
ext_modules=ext_modules,
)
- pyproject.tomlの記載
SetuptoolsDeprecationWarning: `project.license` as a TOML table is deprecated
!!
********************************************************************************
Please use a simple string containing a SPDX expression for `project.license`. You can also use `project.license-files`. (Both options available on setuptools>=77.0.0).
By 2027-Feb-18, you need to update your project and remove deprecated calls
or your builds will no longer be supported.
See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
********************************************************************************
!!
corresp(dist, value, root_dir)
tomlのlicense = {text = "Custom"}を修正しました。
tomlの記載方法の指定ができたようです。
license = "LicenseRef-Proprietary"
license-files = ["LICENSE"]
toml全体
[build-system]
requires = [
"setuptools",
"wheel",
"Cython",
"numpy",
]
build-backend = "setuptools.build_meta"
[tool.setuptools]
packages = ["bedcmm","bedcmm.pattern","bedcmm.communication"]
[tool.setuptools.exclude-package-data]
"*" = ["communication_key_info/*","sample_data/*","tests/*"]
[project]
name = "bedcmm"
version = "2.43"
description = "Pattern Extraction and Periodicity analysis,and communication multiple method library based on BEDCMM"
readme = "README.md"
requires-python = ">=3.10"
license = "LicenseRef-Proprietary"
license-files = ["LICENSE"]
authors = [
{name = "WATARU YASUHARA"}
]
dependencies = [
"numpy",
]
[project.optional-dependencies]
dev = [
"pytest",
]
[project.urls]
Homepage = "https://github.com/YASUHARA-Wataru/bedcmm"
Repository = "https://github.com/YASUHARA-Wataru/bedcmm"
Issues = "https://github.com/YASUHARA-Wataru/bedcmm/issues"
まとめ
GitHubActionsを使って、CI/CD環境を構築しました。
それにより、Windows、Linux、macそれぞれの環境で、pythonの3.10、3.11、3.12でのテストを行い、無事に動く事の確認をしました。
ライブラリ構成による警告や、tomlの設定の更新などの警告を取り除くのに、つまずき少し時間を要した。
GitHub Actions を導入したことで、クリーン環境で継続的に build/test を確認できるようになりました。
おわりに
やはりクリーンに、pipコマンドで入れられるようにするのは大変ですね。
Windowsでpython3.11以外は、Wheelファイルを作成しておらず、Cython使っているので、pipを使用してもCのコンパイラの環境はPCに必要な状況となっています。
研究、検証用のライブラリなので、ここまでの対応で良いかと思っています。
pip install bedcmmやpip install bedcmmPitchで導入できるので、良かったら試してください~。
GitHub