Its been a while since I wrote any new hook scripts so I thought I would refresh my knowledge before talking about them at phpconference.co.uk. I also love playing around with python – it’s straight forward, pretty and gives good ‘sad puppy’ feedback when you’ve missed a closing bracket.
So I came up with this beast to turn svn log output into a dictionary. You can then do what you like with it: output to a file, turn into html, stream to a feed, whatever.
from subprocess import check_output repo_cmd = "svn log file:///svnrepro/tests/deleteFile/trunk" output = check_output(repo_cmd, shell=True) demarker = "--------------------------------------------------------\n" contents = output.split(demarker) ignore_these = ["[hotfix]", "[merge]"] log_book = list() for section in contents: lines = section.split("\n\n") if len(lines) < 2 : continue commit_info = lines message = lines.strip() ignore = False for prefix in ignore_these: if message.startswith(prefix): ignore = True break if ignore == True: continue info = commit_info.split("|") section = dict() section["revision"] = info.strip().replace("r", "") section["user"] = info.strip() section["date"] = info.strip() length = info.strip().split(" ") section["messageLength"] = length section["message"] = message log_book.append(section) print log_book
What can I say, I like understanding things at a first principles level.
Of course then I remembered a cool flag which turns a predetermined text output into something much more meaningful.
repo_cmd = "svn log --xml file:///svnrepro/tests/deleteFile/trunk"
xml.etree.ElementTree is the built in python way of handling xml and, again, it’s a bit hand cranked and leads to many unnecessary lines of code. So why re-invent the wheel?
“Has someone already done this?” is rapidly becoming my first question, and yes, someone has!
This is the second module library I tried, and is one that can actually deal with lists of elements – essential for svn log output. If you are used to handling things in JSON, it’s really easy:
from subprocess import check_output import json import xmltodict # https://github.com/martinblech/xmltodict repo_cmd = "svn log --xml file:///svnrepro/tests/deleteFile/trunk" output = check_output(repo_cmd, shell=True) print json.dumps(xmltodict.parse(output))
I have to admit, a little piece of me feels like I’m no longer writing ‘real’ code and that there’s nothing left unsolved or uncoded. The rest of me is doing a happy dance – now we are freed up to use technology to solve the real issues – save time, collaborate well and communicate effectively to make lives better!