tags:

views:

443

answers:

3

I'm writing a utility in Python that will attach changed files in Subversion to an email and send it when a subset of folders that are under source control in SVN have been changed. I am using the pysvn library to access the repository.

I have a copy of the files on my local file system and I do an update to check if the files have changed since the last time the utility was run.

I am at the point where I am translating the path names in SVN to the path names on my local copy.

Currently I have written the following to do the job:

def formatPaths(self, paths):
    newPaths = list()
    for path in paths:
        path = path[len(self.basePath):]
        path = path.replace("/", "\\")
        newPaths.append(path)
    return newPaths

self.basePath would be something like "/trunk/project1" and I'm looking to just get the relative path of a subset of folders (I.e. folder1 under "/trunk/project1").

Is this a good way to solve this problem or is there some magical function I missed?

A: 

Hm... That would do it:

baselen = len(self.basePath)
for path in paths:
    path = path[baselen:].replace("/", "\\")
    newPaths.append(path)
return newPaths

If you like, you can do it like this:

baselen = len(self.basePath)
return (path[baselen:].replace("/", "\\") for path in paths)

Not calculating baselen in every loop iteration is also good practice.

Tomalak
str.lstrip([chars]) does not accept an integer.
gimel
Narf. That's stupid. :-D (I'll correct it.)
Tomalak
+2  A: 

Stay with the slice operator, but do not change the loop variable inside the loop. for fun, try the generator expression (or keep the listcomp).

baselen = len(self.basePath)
return (path[baselen:].replace("/", "\\") for path in paths)

Edit: `lstrip()' is not relevant here. From the manual:

str.lstrip([chars])

Return a copy of the string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on.

gimel
A: 

Your specific solution to the path name copy is reasonable, but your general solution to the entire problem could be improved.

I would easy_install anyvc, a library developed for the PIDA IDE which is a uniform python interface into version control systems, and use it instead:

from anyvc import Subversion
vc = Subversion('/trunk')

modified = [f.relpath for f in vc.list() if f.state != 'clean']

for f in modified:
    print f.relpath # the relative path of the file to the source root

Additionally, I would probably attach a diff to an email rather than the actual file. But I guess that's your choice.

Ali A
I thought about doing a diff too, but it is out of my hands unfortunately.
Jesse Dearing