views:

1639

answers:

7

I have a python project under SVN, and I'm wanting to display the version number when it is run. Is there any way of doing this (such as automatically running a short script on commit which could update a version file, or querying an SVN repository in Python?)

+1  A: 

The file hooks/pre-commit in your repository is a script or program which will be executed before a successful commit. Simply make this a script which updates your file with the proper version number as it is being committed (the revision number is passed to your script as it's run). Here's a tutorial with an example of writing a pre-commit hook: http://wordaligned.org/articles/a-subversion-pre-commit-hook

Eli Courtwright
Ouch. Letting the pre-commit hook modify files is generally a bad idea. The version in the repo will be different from the version in the working copy, and this may lead to some confusion. The committer has to revert to an earlier version of the file, and then get the new one, probably resulting in some kind of conflict, as svn has stored the original version in the text-base of the working copy.
sunny256
I agree that this is dangerous and not usually a desired behavior, but if you limit yourself appropriately, then I don't see the problem. For example, if you have a "svn_version.txt" file that contains nothing but the current revision number, then it would be perfectly safe; your local copy would always have the version which you've checked out and the repository would always have the latest (often the same as your working copy).
Eli Courtwright
+1  A: 

Check out pysvn. it exposes extensions in Python for Subversion runtime functionality.

Konstantinos
+1 for pysvn, but it's best to avoid linking to "this"
Rich Seller
+3  A: 

There's a snippet of code in Django that allows you to do this, I'd recommend looking at it. http://code.djangoproject.com/browser/django/trunk/django/utils/version.py

David
This function only works when you're running from within a subversion checkout. If this is what the OP wants, then problem solved. However, I suspect he may want the script to have contained within itself or a config file or something its subversion revision, even if the script is no longer under subversion, in which case that link won't be useful.
Eli Courtwright
Yeah, this is great fallback option, but having the version number also under source control would be better. Thanks! +1
Smashery
+2  A: 

I do this by simply running a small script when building my project. The script just calls svn info in combination with sed to get out the bare revision data and injects that number into the revision.txt file.

The Hudson build server makes it even easier as it sets the SVN revision number as an environment variable right before invoking your Build scripts.

zedoo
We do that as well, works nicely
orip
+7  A: 

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];
}
Ciaran McNulty
This is the right answer! Also, remember to set the property 'keywords=Revision' on each file you want to do this to.
Drew Hall
Drew - I think if enable-auto-props=true is set, then the default keywords are all set?
Ciaran McNulty
+1 That's awesome! In my case, though, is there a way to have SVN edit the revision number if it's a different file that was changed in this revision? For instance, if I have the revision number keyword in version.py, but I don't change version.py in revision nnn, it won't update it, will it? If not, is there a way to use this method in that way?
Smashery
@Smashery I'm afraid you'd have to write your own commit hook to do that I suspect - you could use the svn:keywords property though by setting your own keyword and letting SVN do the substitution for you.
Ciaran McNulty
That will only work on the file(s) being committed. We handle it in the build script, easy to do.
orip
+1  A: 

I find the when it is run section slightly ambiguous - when it is run from where ? From an SVN checkout or from released code ? All the solutions work on injecting the SVN version somehow into the Python source or somewhere in the repository in a separate version file.

The version file is changed on fresh checkouts, so clean checkouts need to be done before distribution. So my question of where the program is being run remains.

whatnick
A: 

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
Will
or ''.join(c for c in "$LastChangedRevision: 1546 $" if c.isdigit()) if you don't like filter.
Tal Weiss