tech.rommelhok.com

yet another one of those technology blogs

Automating subversion with pysvn

Being that Subversion is a wonderful tool for keeping track of changes in a set of files (well, d’uh), I figured I’d start using it as a backup system for my files.

For which it works quite admirably, thank you very much.

The only thing it’s not quite so good at is dealing with new files, files that have been moved and files that have been deleted. Not without a bit of help, anyway.

The solution to this problem turns out to be to use the pysvn python module (if there’s a perl interface to Subversion, I haven’t found it - then again, I didn’t look for it very hard either and in any event pysvn is made by the same people who make Subversion itself which I guess makes it official).

It took less than an hour from deciding to automate management of my local svn checkout directory to a quick google for a perl or python module to installing pysvn and skimming the manual and finally, the result:

#!/usr/bin/env python
"""
Simple svn script. It automatically adds new files (automatically seems to delete missing ones as well).
Password info unfortunately goes hardcoded into a variable and it leaves auth/ directories all over
the place, but that doesn't bother me
"""

import sys
import os
import time

import pysvn

dirname = '/path/to/checkoutdir'
user = 'yourlogin'
passwd = 'yourpass'

def get_login( realm, username, may_save ):
    return True, user, passwd, 1

client = pysvn.Client(dirname)
client.callback_get_login = get_login
client.update(dirname)
for item in client.status(dirname):
    if(item.text_status == pysvn.wc_status_kind.unversioned):
        client.add(item.path)
        print 'adding', item.path
msg = time.strftime('%Y%m%d')
client.checkin(dirname, msg)

Tags: ,

3 Responses to “Automating subversion with pysvn”

  1. Priyadarshan Says:

    Thank you, as a writer, that is a very interesting script for me. I am not fluent in Python; do you need a different version of the script in case you want to automatically commit different directories?

    I share with you a keen interest in Lisp. Did you find anything similar to this script in Lisp?

    Thank you again

  2. matthijs Says:

    Hi Priyadarshan,

    With a little effort the script could be modified for multiple repositories but as it is now, it only does one. Sorry about that.

    As far as Lisp is concerned, judging from the title of your site you probably know more about that than I do.

  3. Richard Says:

    Have you tried Mercurial?

    It’s on of those fancy DVCS, but if you play with it you will find quite similar to Subversion command-wise.

    Quick example of what you want to do:

    cd my_dir
    hg init #creates mercurial repo inside this dir

    hg addremove #adds all files under dir to backup
    hg commit #initial commit

    now to make the backup dir

    hg clone my_dir backup_dir

    So everytime you want to do a backup just execute:

    cd my_dir
    hg addremove #adds new files and remove deleted files
    hg commit -m “new backup”
    cd backup_dir
    hg pull #pulls commit from my_dir’s repo

    If you like this take a look at Mercurial’s tutorial

Leave a Reply