views:

74

answers:

3

I have a project that used to be under SVN, but now I'm moving it to Mercurial, so I want to clear out all the .svn files.

It's a little too big to do it by hand, so I decided to write a python script to do it. But it isn't working.

def cleandir(dir_path):
    print "Cleaning %s\n" % dir_path
    toDelete = []
    files = os.listdir(dir_path)
    for filedir in files:
        print "considering %s" % filedir
        # continue
        if filedir == '.' or filedir == '..':
            print "skipping %s" % filedir
            continue
        path = dir_path + os.sep + filedir
        if os.path.isdir(path):
            cleandir(path)
        else:
            print "not dir: %s" % path

        if 'svn' in filedir:
            toDelete.append(path)

    print "Files to be deleted:"
    for candidate in toDelete:
        print candidate
    print "Delete all? [y|n]"

    choice = raw_input()

    if choice == 'y':
        for filedir in toDelete:
            if os.path.isdir(filedir):
                os.rmdir(filedir)
            else:
                os.unlink(filedir)

    exit()

if __name__ == "__main__":
    cleandir(dir)

The print statements show that it's only "considering" the filedirs whose names start with ".". However, if I uncomment the continue statement, all the filedirs are "considered". Why is this?

Or is there some other utility that already exists to recursively de-SVN-ify a directory tree?

+4  A: 

At the very least you should not re-invent the wheel for recursive directory traversal and just use os.walk.

scrible
Oh thanks I didn't know that function existed.
Rosarch
That solved the problem, but I don't understand why.
Rosarch
+2  A: 

This doesn't answer your Python question, but there's an easier way to do what you want: svn export will write your tree of files some place new without all the .svn directories.

Ned Batchelder
Nice. What if I've already deleted some of the svn files by hand and/or otherwise horribly mangled the directory structure?
Rosarch
@Rosarch: You can export directly from an SVN url, eg. `svn export svn://server/repo/trunk myproject`
Greg Hewgill
+1  A: 

The reason 'continue' shows all the folders being considered is because exit() is being called at the end of the function. The call to exit() will exit Python after the first directory is searched. If you change it to 'return' your function will be called recursively.

Also os.listdir does not return '.' or '..' so you don't need a check or any continue statements.

:-)

Davy

daftspaniel