Automatic version number both in setup.py (setuptools) AND source code?

前端 未结 7 1368
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 11:21

SITUATION:

I have a python library, which is controlled by git, and bundled with distutils/setuptools. And I want to automatically generate version

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 11:47

    As was mentioned in another answer, this is related to the release process and not to the development process, as such it is not a git issue in itself, but more how is your release work process.

    A very simple variant is to use this:

    python setup.py egg_info -b ".`date '+%Y%m%d'`git`git rev-parse --short HEAD`" build sdist
    

    The portion between the quotes is up for customization, however I tried to follow the typical Fedora/RedHat package names.

    Of note, even if egg_info implies relation to .egg, actually it's used through the toolchain, for example for bdist_wheel as well and has to be specified in the beginning.

    In general, your pre-release and post-release versions should live outside setup.py or any type of import version.py. The topic about versioning and egg_info is covered in detail here.

    Example:

    • v1.3.4dev.20200813gitabcdef0
    • The v1.3.4 is in setup.py or any other variation you would like
    • The dev and 20200813gitabcdef0 is generated during the build process (example above)
    • None of the files generated during build are checked in git (usually in .gitignore they are filtered by default); sometimes there is a separate "deployment" repository, or similar, completely separate from the source one

    A more complex way would be to have your release work process encoded in a Makefile which is outside the scope of this question, however a good source of inspiration can be found here and here. You will find good correspondeces between Makefile targets and setup.py commands.

提交回复
热议问题