Archive for May, 2009

What Does This Mean?

Monday, May 18th, 2009

Do you know what this means?

htons

No Cheating via Google! Click for the answer … (more…)

Removing SVN Bindings with Python

Saturday, May 16th, 2009

I had an SVN repository on my website that I was using to store a personal project of mine.  I wanted to move the files into a different SVN repository on another website. Doing this required that I first remove all of the .SVN folders in my project. It’s got a bunch of folders, so going through manually would have been a big pain. Besides, as a self-respecting geek, I’d definitely prefer spending twice as long writing a script to automate a boring process over just doing it manually. My script, which is based upon this script here, generates a batch file that you run to clean the directories out. I would have done it directly from python, but I kept getting ‘this directory could not be found’ exceptions, probably becuase of the spaces in my folder names.

The script is below, for your edification. It only works on windows, but it could easily be modified to run on a UNIX based system. It is presented without any warranty, express or implied, not even that of suitability for a specific purpose.

import os
import sys
 
def findFileGenerator(rootDirectory, acceptanceFunction):
  for aCurrentDirectoryItem in [ os.path.join(rootDirectory, x) for x in os.listdir(rootDirectory) ]:
    if os.path.isdir(aCurrentDirectoryItem):
		if acceptanceFunction(aCurrentDirectoryItem):
			yield aCurrentDirectoryItem
		for aSubdirectoryItem in findFileGenerator(aCurrentDirectoryItem, acceptanceFunction):
			yield aSubdirectoryItem
 
def acceptSVNChildren(theFileOrDir):
	return theFileOrDir.find('.svn') != -1
 
def removeSVN(rootdir):
	"""recursively removes the SVN bindings from the directory specified in the argument"""
 
	for dude in findFileGenerator(rootdir,acceptSVNChildren):
		if os.path.isdir(dude):
			print "rmdir /S /Q", '"'+dude+'"'
 
def main():
	"""Removes the SVN bindings from the commandline dir specified in the argument.
	If no argument is specified, the root directory is used."""
 
	dirToClean = os.getcwd()
	if(len(sys.argv) == 2):
		dirToClean = os.path.join(os.getcwd(),sys.argv[1])
	elif (len(sys.argv) > 2):
		print "Usage: remove_svn.py <dirToClean>"
 
	removeSVN(dirToClean)
 
if __name__ == "__main__" : main()