Search code examples
pythonsetuptoolspython-wheel

Set Python wheel version tag dynamically


I'm trying to integrate building of a wheel file into a Bamboo plan. Ultimately, I'd like to tie part of the version tag of the .whl file to the Bamboo build number in someway, i.e. the pre-release for version 0 would be 0.dev1, 0.dev2, 0.dev3 for successive builds.

The old egg format used to allow the --tag_build option, which would allow you to specify a tag that is appended to the version parameter defined in the setup function in the setup.py file. The bdist_wheel command apparently doesn't have an equivalent option.

This dashed my hopes of running setup.py from a script, using the Bamboo build number variable. I'm looking for any other suggestions other than either converting the build script to Powershell, or generating setup.py on the fly each build.


Solution

  • The version tag in the wheel filename is just the package version number, defined by setup.py, and setup.py is a Python script with all the power of Python available to it. Thus, setup.py can simply set the version parameter of the setup() function based on the bamboo_buildNumber environment variable:

    import os
    
    version = whatever_the_version_would_be_otherwise
    try:
        version += '.dev' + os.environ['bamboo_buildNumber']
    except KeyError:  # bamboo_buildNumber isn't defined, so we're not running in Bamboo
        pass
    
    setup(
        version = version,
        ...
    )