Listing 2. bts_merge.py

#!/usr/bin/env python

import os, MySQLdb

TRUNK_WC = "/path/to/working_copy_of_trunk/"
TRUNK_URL = "svn+ssh://vcs-server/project/trunk/"
BRANCH_URL = "svn+ssh://vcs-server/project/branch/"

def initDB():
    # connect to database, return connection cursor
    connection = MySQLdb.connect(host='dbhost',
                                 db='dbname',
                                 user='user',
                                 passwd='password')
    cursor = connection.cursor()
    return connection, cursor

def listUpdatedFiles(cursor):
    # return updated file paths and BTS ids.
    cursor.execute("""SELECT changedfiles
                   FROM BugTable
                   WHERE status =
                   'ready for production'""")
    fileList = cursor.fetchall()
    cursor.execute("""SELECT bugID
                   FROM BugTable
                   WHERE status =
                   'ready_for_production'""")
    idList = cursor.fetchall()
    return fileList, idList

def mergeUpdatedFiles(fileList):
    # merge branch changes into the trunk.
    for fileName in fileList:
        cmd = 'svn merge %s/%s %s/%s'%(BRANCH_URL,
                                       fileName,
                                       TRUNK_URL,
                                       fileName)
        for line in os.popen4(cmd)[1].readlines():
            print line

def updateBTSStatus(idList, cursor):
    # update BTS ids to 'in_production' status.
    for ID in idList:
        cursor.execute("""UPDATE BugTable
                       SET status = 'in_production'
                       WHERE bugID = %s""" % ID)

def stopDB(connection, cursor):
    # close the database connection
    cursor.close()
    connection.close()

if __name__=="__main__":
    os.chdir(TRUNK_WC)
    connection, cursor = initDB()
    fileList, idList = listUpdatedFiles(cursor)
    mergeUpdatedFiles(fileList)
    updateBTSStatus(idList, cursor)
    stopDB(connection, cursor)


