I’m writing out data from python programs and it is important to have an audit trail. I decided that I would write out the SVN revision number of the code generating the reports, into the reports.
Getting the svn revision number of a file is fairly straight forward in python:
os.popen('svn info %s | grep "Last Changed Rev" ' % fName, "r").readline().replace("Last Changed Rev:","")
that acutally returns a string with the revision number in it.
Thats a bit of code though, and I didn’t want it sprinkled, duplicated, all over my different python files
so I put it in my util library
in python, grabbing the name of the file you are working from is fairly straightforward:
__file__
however, since I was writing this in my utility library, I didn’t care about that file’s name, I wanted the name of the calling file.
I figured that out
fName = sys._getframe(1).f_code.co_filename
this gets the name of the file 1 call up the call stack
combining these I ended up with
def getSvnVersionOfFile(fName = sys._getframe(1).f_code.co_filename):
return os.popen('svn info %s | grep "Last Changed Rev" ' % Name, "r").readline().replace("Last Changed Rev:","")