Tuesday, April 25, 2017

packaging - How to keep version number in a single place in a python project with setup.py and debian/control



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 debian/changelog

  • a recipe in launchpad adds the revision number of the development branch when the source package is built.

  • a function in setup.py parses the first line of debian/changelog to retrieve the version number and pass it to setup so that setup --version works 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.py module 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.


  • In setup.py




    I've seen a project that subclasses distutils.command.install_data.install_data to 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:





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.




A tool/command is used to generate a version.py module 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.




You can, of course, do that at build time, rather that at source-package-creation time.



version.py:




# Overwritten during Debian packaging build
package_version="unknown"


debian/rules:



override_dh_auto_build:
dpkg-parsechangelog | sed -rne 's,^Version: (.*),package_version="\1", p' > version.py
dh_auto_build



debian/clean:



version.py

No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...