Getting SVN revision number into a program automatically Getting SVN revision number into a program automatically python python

Getting SVN revision number into a program automatically


I'm not sure about the Python specifics, but if put the string $Revision$ into your file somewhere and you have enable-auto-props=true in your SVN config, it'll get rewritten to something like $Revision: 144$. You could then parse this in your script.

There are a number of property keywords you can use in this way.

This won't have any overhead, e.g. querying the SVN repo, because the string is hard-coded into your file on commit or update.

I'm not sure how you'd parse this in Python but in PHP I'd do:

$revString = '$Revision: 144$';if(preg_match('/: ([0-9]+)\$/', $revString, $matches) {    echo 'Revision is ' . $matches[1];}


Similar to, but a little more pythonic than the PHP answer; put this in your module's __init__.py:

__version__ = filter(str.isdigit, "$Revision: 13 $")

and make sure you add the Revision property:

svn propset svn:keywords Revision __init__.py


Or you can do like this:

import re,subprocesssvn_info = subprocess.check_output("svn info")print (re.search(ur"Revision:\s\d+", svn_info)).group()

it prints "Revision: 2874" in my project

Or like this:

print (subprocess.check_output("svnversion")).split(":")[0]

it prints "2874" in my project