Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

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
share|improve this answer
    
Thanks a lot. Running a command in debian/rules is indeed a nice strategy that works fine. –  jcollado Apr 11 '13 at 17:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.