Recently, I needed an way to report the version of the application I'm working on to the users of the application.
I wanted to avoid manually updating a settings file or some similar configuration file that the application could read and runtime and report the version. This would be a manual step that would eventually be forgotten at some point, or overlooked.
I tag each of my deployments in SVN, so I figured there must be some way to associate the tagname (which represented the version) to the version that's displayed to the users. When you run the 'svn info' command, it does display the working directory's URL path to the svn repo. I decided to ask around for suggestions, as I didn't necessarily want to roll new code to parse that URL path if some useful utility existed.
Luckily, someone did recommend a library that provided an svn binding for python. I explored pysvn and found it provided exactly what I needed. The documentation for pysvn is outstanding. It gave all the examples and descriptions that I needed to easily write a function to retrieve the tag name from the working directory's svn URL.
My first step was to install the pysvn bindings. Unfortunately, I didn't see it available in PyPI, so I had to download and install it (thankfully on Ubuntu, it's a simple sudo apt-get python-svn call, as described here).
I used the pysvn libraries to retrieve the working directory's svn URL, the builtin urlparse library to parse the path from the full url, and finally the posixpath library to get the 'basename' which is the tagname.
Here's my final code for doing so:
import pysvn import urlparse, posixpath URLPARSE_PATH_INDEX = 2 def find_basepath_name(): # initialize the pysvn Client object, which contains all the functions for # working with the svn repo, including checkout, add, and status client = pysvn.Client() # grab the info from the current working directory entry = client.info('.') # parse the results of the URL to which the working directory is associated url_details = urlparse.urlparse(entry.url) # grab the 'path' component of the parsed url path = url_details[URLPARSE_PATH_INDEX] # use the posixpath module to correctly parse out the basename basename = posixpath.basename(path) return basename if __name__ == '__main__': basename = find_basepath_name() print basename