Sunday, July 24, 2005

An interesting Python function for working with Subversion

This is a function I wrote for working with SVN repositories. It works like os.walk.


import pysvn

"""
Utilities for working with Subversion repositories

"""


def walk (targeturl):
"""
Walk through an subversion repository.

"""
dirStack = []
theEnd = 0
client = pysvn.Client()
client.callback_get_login = prompt
client.callback_notify = notify
client.callback_get_log_message = logmessage

ls = client.ls(targeturl, recurse = False)
files = []
dirs = []
for item in ls:
if item["kind"] == pysvn.node_kind.dir:
dirStack.append(item)
dirs.append(endOfPath(item["name"]))
if item["kind"] == pysvn.node_kind.file:
files.append(item["name"])
yield (targeturl, dirs, files)

while len(dirStack):
files = []
dirs = []
base = dirStack[theEnd]
del dirStack[theEnd]

currentDir = base["name"]
ls = client.ls(currentDir, recurse = False)
for item in ls:
if item["kind"] == pysvn.node_kind.dir:
name = endOfPath(item["name"])
dirs.append(name)
dirStack.append(item)
if item["kind"] == pysvn.node_kind.file:
name = endOfPath(item["name"])
files.append(name)

yield (currentDir, dirs, files)



return

No comments: