Dave Aaron Smith
Dave Aaron Smith

Reputation: 4567

Determining the Bazaar version number from Python without calling bzr

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]

Upvotes: 1

Views: 284

Answers (2)

Andrew
Andrew

Reputation: 13191

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.

Upvotes: 4

James
James

Reputation: 25543

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

Upvotes: 2

Related Questions