views:

62

answers:

2

I have a django (Python) project that needs to know what version its code is on in Bazaar for deployment purposes. This is a web application, so I don't want to do this because it fires off a new subprocess and that's not going to scale.

import subprocess
subprocess.Popen(["bzr", "revno"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

Is there a way to parse Bazaar repositories to calculate the version number? Bazaar itself is written in Python and contains this code for calculating the revno, which makes me think it isn't exactly trivial.

rh = self.revision_history()
revno = len(rh)

Edit: Final fix

from bzrlib.branch import BzrBranch
branch = BzrBranch.open_containing('.')[0]
revno = len(branch.revision_history())

Edit: Final fix but for real this time

from bzrlib.branch import BzrBranch
branch = BzrBranch.open_containing('.')[0]
revno = branch.last_revision_info()[0]
+2  A: 

Do it once and cache the result (in a DB/file, if need be)? I doubt the version is going to change that much.

Autopulated
+4  A: 

You can use Bazaar's bzrlib API to get information about any given Bazaar repository.

>>> from bzrlib.branch import BzrBranch
>>> branch =  BzrBranch.open('.')
>>> branch.last_revision_info()

More examples are available here.

Andrew
`branch.last_revision_info()` returns tuple `(revno, revision_id)`. If you need just revno then `revno = branch.last_revision_info()[0]`
bialix