6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PyPIに登録するパッケージバージョンをGitから取得する

Last updated at Posted at 2017-01-26

概要

PyPI にパッケージを登録する場合,setup.py で呼び出す setup 関数に version 引数を渡す必要がある.
このバージョン情報を今までは手作業で編集していたのだが,Gitのタグから引っ張ってこれたので備忘録として記しておく.

setuptools_scm

setuptools_scm はバージョン管理システムから適当に情報を引っ張ってきて,setup.py を設定してくれるライブラリである.
これを使えば,タグ情報を元にバージョン番号を算出し設定できる.
また,トラッキングされているパッケージデータを自動的にパッケージへ追加する機能もある.

setuptools_scm を使用してバージョン番号を設定する場合, setup.py の setup 関数に次のようなパラメータを渡す.

setup.py
from setuptools import setup

setup(
    use_scm_version=True,
    setup_requires=[
        "setuptools_scm"
    ],
    ... # 他の項目は省略
)

これで,python setup.py sdistとする時に適切にバージョン番号が計算される.
バージョン番号の計算方法は次のルールに従っている.

  • 最新コミットにバージョンタグが付いており,更新されたファイルがない場合: そのバージョンタグ
  • 最新コミットにバージョンタグが付いており,更新されたファイルがある場合: バージョンタグ+現在の日付
  • バージョンタグ付きコミット以降にコミットがあり,更新されたファイルがない場合: 次のバージョン.dev(何コミット離れているか)+n(ハッシュ)
  • バージョンタグ付きコミット以降にコミットがあり,更新されたファイルがある場合: 次のバージョン.dev(何コミット離れているか)+n(ハッシュ).日付

より詳しい情報はマニュアルを参照のこと.

パッケージデータの追加

トラッキングされているパッケージデータを自動で追加する場合, setup 関数に include_package_data=True を追加する.
つまり,

setup.py
from setuptools import setup

setup(
    use_scm_version=True,
    include_package_data=True,
    setup_requires=[
        "setuptools_scm"
    ],
    ... # 他の項目は省略
)

とする.
なお,トラッキングしているけれどパッケージには含めたくないデータファイルがある場合,
exclude_package_data で指定できる.詳細はマニュアルを参照.

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?