For a python project I'm working on, I'd like to:
- Keep a version string in a single place.
- Be able get the version string at runtime for logging purposes
- Get a default value when launching the code from the source branch.
- Get a value that depends on the bzr revision number of the branch that was used to build the source package.
- Make the project packages available in a launchpad PPA using a recipe.
In the way the code works right now:
- the version number is in
- a recipe in launchpad adds the revision number of the development branch when the source package is built.
- a function in
setup.pyparses the first line of
debian/changelogto retrieve the version number and pass it to
setup --versionworks fine (without the revision number, but that could be fixed).
However, the version number isn't available at run time. What would be a good way to do that and meet the requirements above?
Some solutions I've used/seen in the past are based on generating a
version.py module at build time from a template:
Using a external tool
A tool/command is used to generate a
version.pymodule with the version number. However, this doesn't work well with launchpad recipes because the ability to run arbitrary commands in a recipe is disabled in launchpad.
I've seen a project that subclasses
distutils.command.install_data.install_datato perform some string substitutions on the fly. However the code isn't very readable, so I'd prefer to use some other strategy.
Besides this, I've also used
python-apt to just query the apt cache for the installed package version to get the version string back. However, this doesn't work to display a default value when the code is launched from the source branch unless the package isn't installed or some path checks code are added which isn't very clean from my point of view.
I've already read other questions in SO like:
- How can I get the version defined in setup.py (setuptools) in my package?
- Automatic version number both in setup.py (setuptools) AND source code?
but they don't really address the problem of dealing with with packaging and version control at the same time.
Please let me know what could be a reasonable approach to do this and provide a link to an example project that uses that approach if possible.