matt wilkie
matt wilkie

Reputation: 18084

Have program report bazaar revision number when run

I have a python program under bazaar version control. How can I get the program to display the current branch revision number and date/build-date when run? The program is exported, not executed from a working copy tree.

There is a svn version of this question at Getting SVN revision number into a program automatically

Upvotes: 3

Views: 306

Answers (3)

matt wilkie
matt wilkie

Reputation: 18084

Ok, I get it, adding a build step, or more precisely something to the export process in this case, to stamp some revision info is the only way to go (as I've been told before in the context of Mercurial. I guess I'm thick and a slow learner :-)

So building on both jcollado and wal-o-mat's answsers with some stuff I learned elsewhere, here is the approach I'll use:

At export time generate current revision info (in command shell, I could get wal-o-mat's subprocess to work on on of my computers but not the another):

bzr version-info --python > bzr_version.py

At run-time read and report from the file generated above:

import bzr_version
d = bzr_version.version_info
build = d.get('revno','<unknown revno>')
date  = d.get('build_date','<unknown build date>')

Upvotes: 1

wal-o-mat
wal-o-mat

Reputation: 7344

An easy way (assuming you're inside a bzr directory):

import subprocess    
revno = int(subprocess.check_output(["bzr", "revno"]))

edit

As you assumption is that you move the file elsewhere, maybe it's a solution to use the post commit hook provided by bzr and invoke a script that searches for a special string format (like "REVNO = 123") and replaces that with the new revision number?

Upvotes: 1

jcollado
jcollado

Reputation: 40414

If you don't have the branch (that's what I understand from the question), I don't know if there's any standard solution for that. What I usually do is use a template that is populated with values at build time (in my case debian package generation) so that a file like version.py.tmpl becomes version.py. That module is imported from other modules in the application and that's the way they get the revision number once the software is installed.

By the way, to get the revision number using bzrlib you can do the following:

from bzrlib.branch import Branch
branch = Branch.open('/path/to/branch')
print branch.revno()

Upvotes: 3

Related Questions