#!/usr/bin/python ############################################################################ # copyright : (C) 2001-2004 Marko Grönroos # # email : magi@iki.fi # ############################################################################ # # # This program is free software; you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation; either version 2 of the License, or # # (at your option) any later version. # # # ############################################################################ import os,sys,re ################################################################################ # Print error message ################################################################################ def printError(): print "Usage: rsed [] '' '' ''\n\n" \ " Replaces a string with a new string from all files that match \n" \ " the given file name pattern in and below the current working \n" \ " directory, recursively.\n" print " The string is a regular expression to be searched, and \n" \ " the string is a replacement that can contain match group \n" \ " results indicated with a backslash and the number of the group.\n" print " Options: -c Do not recurse into subdirectories." print " -d Root directory for replacing." print " -q Quiet.\n" print " Examples: rsed 'foo' 'bar' '*.h'" print " rsed 'f[io]+' 'bar' '*.h'" print " rsed 'f([io]+)' 'bar\1' '*.h'\n" sys.exit() ################################################################################ # Handle command line arguments ################################################################################ # Default option values rootDirectory = "." # Root directory for find flagQuiet = 0 # Be quiet; do not print anything. findOptions = "" # Miscellaneous find options if len(sys.argv) < 4: print "error: too few (%d) arguments.\n" % (len (sys.argv)-1) printError () # Parse optinal parameters moreOptions = 1 while moreOptions: # -c: Do not recurse. if sys.argv[1] == '-c': flagOnlyCurrent = 1 findOptions = findOptions + " -maxdepth 1 " del sys.argv[1] # -d: Root directory elif sys.argv[1] == '-d': if len (sys.argv) < 3: print "error: -d requires an argument.\n" printError () rootDirectory = sys.argv[2] del sys.argv[1:3] # -q: Quiet elif sys.argv[1] == '-q': flagQuiet = 1 del sys.argv[1] else: moreOptions = 0 if len(sys.argv) != 4: print "error: wrong number (%d) of mandatory arguments.\n" % (len (sys.argv)) printError () # Retrieve mandatory parameters replaced = sys.argv[1] replacing = sys.argv[2] include = "-name '%s' " % (sys.argv[3]) ################################################################################ # Find files and replace ################################################################################ findCommand = "find %s -type f %s %s -print" % \ (rootDirectory, include, findOptions) # Find files to be processed filesp = os.popen (findCommand, "r") filenames = map (lambda x: x[:-1], filesp.readlines()) filesp.close () # Replace occurrences in each matching file for filename in filenames: # To avoid using a temporary file, we first read the file # in and then write it. # Read file in filein = open (filename, "r") lines = map (lambda x: x[:-1], filein.readlines()) filein.close() # Replace occurrences replCount = 0 for i in xrange(len(lines)): if re.search (replaced, lines[i]): lines[i] = re.sub (replaced, replacing, lines[i]) replCount = replCount + 1 # Write file only if some changes were made. if replCount > 0: # Write the file fileout = open (filename, "w") for line in lines: fileout.write (line+"\n") fileout.close() if not flagQuiet: print "%s: %d replaced" % (filename, replCount)