#!/usr/bin/python # Transfoms Compact into your compiler # @author Joao Vieira import sys, os, string, itertools, fileinput if len(sys.argv) < 3: print >> sys.stderr, "usage: rename " sys.exit(1) if not os.path.isdir(sys.argv[1]): print >> sys.stderr, "oops,", sys.argv[1], "is not a directory." sys.exit(2) for root, dirs, files in os.walk(sys.argv[1]): for path in itertools.chain(files, dirs): old, new = os.path.join(root, path), os.path.join(root, path.replace('Compact', sys.argv[2])) if not os.path.exists(new): os.rename(old, new) if os.path.isfile(new): for line in fileinput.input(new, inplace = 1): #TODO(vieira@yubo.be): Avoid triplication of variables. """ this is ugly but regular expressions are slow, so we will avoid them.""" COMPACT, Compact, compact = 0, 0, 0 COMPACT, Compact, compact = string.find(line, "COMPACT"), string.find(line, "Compact"), string.find(line, "compact") if COMPACT >= 0: line = line.replace("COMPACT", sys.argv[2].upper()) if Compact >= 0: line = line.replace("Compact", sys.argv[2]) if compact >= 0: line = line.replace("compact", sys.argv[2].lower()) sys.stdout.write(line)