2011/04/09

Python command line script to get merge sets by author/authors

Here is a little Python script I wrote to get SVN merge sets by author. I decided it would be a good idea to learn Python instead of using Bash and the script below is my first crack at the new language.
#! /usr/bin/env python
# Get all outstanding svn mergeinfo and print version sets with comments.
# Optionally you can also filter by authors. P.S. this is my first python
# script any suggestions are welcome.
# info@rcpeters.com
import commands
import re
from xml.dom.minidom import parse, parseString
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-a", "--author", dest="author", default='.*',
help="Source svn directory.")
parser.add_option("-s", "--source", dest='source', default='https://svn.enloop.com/repos/enloop/trunk/workspace',
help="Take .")
parser.add_option("-t", "--target", dest="target", default='/root/workspace',
help="Target svn directory.")
(options, args) = parser.parse_args()

re = re.compile(options.author.replace(',','$|')+'$')
src=options.source
trg=options.target

mergeinfo = commands.getoutput('svn mergeinfo --show-revs eligible '+src+' '+trg)

if len(mergeinfo)!=0:
revs =[]
for i in mergeinfo.split('\n'):
revXmlStr = commands.getoutput('svn log --xml -'+i+' '+src);
#print revXmlStr
xmlDoc = parseString(revXmlStr)
for logXml in xmlDoc.childNodes:
authStr = logXml.getElementsByTagName('author')[0].childNodes[0].data
if re.match(authStr) != None:
if len(logXml.getElementsByTagName('msg')[0].childNodes) > 0:
msgStr = logXml.getElementsByTagName('msg')[0].childNodes[0].data
rev = int(i.replace('r',''))
revs.append(rev)
print '-r'+str(rev-1)+':'+str(rev)+' '+authStr
for j in msgStr.split('\n'):
if len(revs) > 0:
revStr=''
curSeqStart=int(revs[0])-1
for i in range(len(revs)):
if i !=0:
if revs[i-1]+1!=revs[i]:
revStr += '-r'+str(curSeqStart)+':'+str(revs[i-1])+' '
curSeqStart=int(revs[i])-1
if i == len(revs)-1:
revStr +='-r'+str(curSeqStart)+':'+str(revs[i])+' '
print 'svn merge '+revStr+'--dry-run '+src+' '+trg
else:
print 'No merges available'
print '\t'+j
print