从git标签获取版本(通过pbr)

时间:2018-01-22 14:23:58

标签: python versioning

我使用pbr进行包装。它从git标签获取版本并将其应用于setup.py

现在我也希望在包中提供该版本。例如,要具有__version__属性。我可以使用pbr库吗?

还有另一个库:versioneer也从git标签中提取版本,但这会增加额外的要求。我希望从pbr

获得此功能

3 个答案:

答案 0 :(得分:1)

如果您可以将pbr作为软件包的运行时依赖项,那么可以使用pbr.version中的VersionInfo类:

from pbr.version import VersionInfo

package_name='my_package'
info = VersionInfo(package_name)

print(info.version_string())

(另请参见How to load package version into __version__ variable if you are using pbr?

答案 1 :(得分:1)

请考虑使用setuptools_scm,它会从git或hg标记中提取版本,或生成适当的开发者发布版本(例如hgvs-1.2.5.dev6+hb5d989061852.d20181124)。该版本与硬编码版本一样被写入包元数据中。不需要非标准的运行时支持。

尽管我已经在许多项目中使用了setuptools_scm,但是我没有使用PBR。我很好奇,并完成了这个简单的演示:

snafu$ head setup.py setup.cfg pbrversiontest/*.py
==> setup.py <==
from setuptools import setup

setup(
    setup_requires=[
        "pbr",
        "setuptools_scm"
    ],
    pbr=True,
    use_scm_version=True,
)

==> setup.cfg <==
[metadata]
name = pbrversiontest
summary = test whether we can use pbr and setuptools_scm

[files]
packages =
    pbrversiontest

==> pbrversiontest/__init__.py <==
# This is straight from setuptools_scm README
from pkg_resources import get_distribution, DistributionNotFound
try:
    __version__ = get_distribution(__name__).version
except DistributionNotFound:
    # package is not installed
    pass

==> pbrversiontest/__main__.py <==
# this isn't required -- only for the demo
import pbrversiontest

print("version is " + pbrversiontest.__version__)

在开发目录中,您可能会像这样进行会话:

snafu$ python3.6 -mvenv venv/3.6
snafu$ source venv/3.6/bin/activate
(3.6) snafu$ git tag 0.1.2
(3.6) snafu$ pip install -e .
(3.6) snafu$ python -m pbrversiontest 
version is 0.1.2
(3.6) snafu$ pip install wheel
(3.6) snafu$ python setup.py bdist_wheel
...
creating 'dist/pbrversiontest-0.1.2-py3-none-any.whl' and adding 'build/bdist.linux-x86_64/wheel' to it
...
(3.6) snafu$ unzip -l dist/pbrversiontest-0.1.2-py3-none-any.whl 
Archive:  dist/pbrversiontest-0.1.2-py3-none-any.whl
  Length      Date    Time    Name
---------  ---------- -----   ----
      192  2018-11-25 05:26   pbrversiontest/__init__.py
       73  2018-11-25 05:46   pbrversiontest/__main__.py
       33  2018-11-25 06:06   pbrversiontest-0.1.2.dist-info/AUTHORS
      217  2018-11-25 06:06   pbrversiontest-0.1.2.dist-info/METADATA
       92  2018-11-25 06:06   pbrversiontest-0.1.2.dist-info/WHEEL
       47  2018-11-25 06:06   pbrversiontest-0.1.2.dist-info/pbr.json
       15  2018-11-25 06:06   pbrversiontest-0.1.2.dist-info/top_level.txt
      675  2018-11-25 06:06   pbrversiontest-0.1.2.dist-info/RECORD
---------                     -------
     1344                     8 files

答案 2 :(得分:1)

setuptoolspbr正确设置后,可以通过以下几种方法进行操作:

import pkg_resources  # part of setuptools

# I don't like this one because the version method is hidden
v1 = pkg_resources.require("my_package_name")[0].version
print('v1 {}'.format(v1))

# This is my favorite - the output without .version is just a longer string with
# both the package name, a space, and the version string
v2 = pkg_resources.get_distribution('my_package_name').version
print('v2 {}'.format(v2))

from pbr.version import VersionInfo

# This one seems to be slower, and with pyinstaller makes the exe a lot bigger
v3 = VersionInfo('my_package_name').release_string()
print('v3 {}'.format(v3))

# Update, new option as of Python 3.8 (credit: sinoroc)
# In Python 3.8, importlib.metadata is part of the stdlib,
# which removes run-time dependencies on `pbr` or `setuptools`
import importlib.metadata

__version__ = importlib.metadata.version('my_package_name')

如果要从命令行使用它,可以执行以下操作:

py setup.py --version

或者,即使始终将软件包安装在本地,也可以从脚本中运行setup.py脚本:

from subprocess import Popen, PIPE

(output, err) = Popen('py setup.py --version'.split(' '),
                      stdout=PIPE, stderr=PIPE, text=True).communicate()
if err: print('ERROR: "{}"'.format(err))
else: print('setup.py --version = {}'.format(output))

注意:有关使用子进程启动和读取stdout等的更多详细信息,请参见this answer,尤其是在Python的较早版本(3.7之前)上。

然后您可以像这样将__version__添加到您的软件包__init__.py中:

__all__ = (
    '__version__',
    'my_package_name'
)

# Or substitute a different method and assign the result to __version__
import pkg_resources  # part of setuptools

__version__ = pkg_resources.get_distribution("my_package_name").version

一些其他问答环节可能会帮助您进行设置以及有关如何更新版本和其他信息的详细信息,特别是如果从您的Git存储库中获取信息(来自标签,作者和ChangeLog信息的版本):