Monitor your SVN Repositories with a simple Python script

I have to work with a lot of SVN repositories that do only have a terrible EMail notification. So, it would be nice to have a script that quickly collects the updates of all your repositories by listening you all the commit messages of all new commits.

This can be done using the following simple Python script:

svncheck.py

# This script has some functions to check the svn revision and print the logs since the last update (it updates on
# its way).
# In the base version of this script, the list 'svn_folders' is run through. However you can also print the change log
# for the current working directory with 'printLog(getRevisionDifference())'

import os

# TODO: Enter the paths to your repos here
svn_folders = ["/home/USER/Repos/Repo1", "/home/USER/Repos/Repo2"]

# parses the current local revision
def getRevision():
    stdin, stdout = os.popen2("svn log -l 1")
    stdin.close()
    lines = stdout.readlines()
    if len(lines) == 1:
        return None
    return int(lines[1].split(" ")[0].replace('r',''))


# gets the svn up to date
def update():
    stdin, stdout = os.popen2("svn up")
    stdin.close()
    stdout.readlines() # for waiting for termination


# updates the svn and returns the revision change
def getRevisionDifference():
    revOld = getRevision()
    if revOld is None:
        print 'Not a working directory. Please check your configuration.'
        return
    update()
    revNew = getRevision()
    return revNew - revOld


# prints the log for the last n changes
def printLog(lastN):
    if lastN == 0:
        print "No changes."
        return
    os.system("svn log -l " + str(lastN))


# execute
for svn_folder in svn_folders:
    print svn_folder
    os.chdir(svn_folder)
    printLog(getRevisionDifference()

Simply save it as a .py-file and make it executable with ‘chmod +x svncheck.py’. Afterwards you can execute it anytime with ‘python svncheck.py‘ (actually on most systems you don’t need the ‘python‘)

The script will go to each repository-folder and for each of these folders:
1. Check the current local revision
2. Update the repository
3. Check the new revision
4. Print the log for the new revisions

Note: This does not automatically send you notifications but only displays changes if executed manually. However, you can quite easily pipe

os.system("svn log -l " + str(lastN)

into some notification program and let the script be executed automatically via a cronjob.

Advertisement