Warning, /sdk/kde-dev-scripts/rename_source_files is written in an unsupported language. File is not indexed.
0001 #! /usr/bin/env python 0002 # 0003 # Copyright David Faure <faure@kde.org>, License LGPL v2 0004 # 0005 # This script renames files (sources and headers), and take care of 0006 # - doing the svn command to rename the file 0007 # - updating Makefile.am 0008 # - updating #include lines (for headers and for .moc) 0009 # 0010 # The input is a file with a syntax like "kwframe KWFrame", one renaming on each line. 0011 # "oldName newName" will rename oldName.h to newName.h and oldName.{cc,cpp} to newName.cpp 0012 # 0013 import sys, os 0014 0015 buildFile = "Makefile.am" 0016 if not os.path.exists( buildFile ): 0017 buildFile = "CMakeLists.txt" 0018 if not os.path.exists( buildFile ): 0019 buildFile = "" 0020 0021 MakefileAmContents = "" 0022 if buildFile: 0023 MakefileAmContents = file(buildFile, "r").read() 0024 origMakefileAmContents = MakefileAmContents 0025 0026 def renameFile( oldFileName, newFileName ): 0027 global MakefileAmContents 0028 if oldFileName != newFileName and os.path.exists( oldFileName ): 0029 print oldFileName + " -> " + newFileName 0030 if os.path.exists( newFileName ): 0031 print "Warning, erasing " + newFileName 0032 os.unlink( newFileName ) # could be due to a former run 0033 #os.rename( oldFileName, newFileName ) 0034 os.system( 'svn move --force ' + oldFileName + ' ' + newFileName ) 0035 MakefileAmContents = MakefileAmContents.replace( oldFileName, newFileName ) 0036 0037 def renameHeader( oldFileName, newFileName ): 0038 global MakefileAmContents 0039 renameFile( oldFileName, newFileName ) 0040 MakefileAmContents = MakefileAmContents.replace( oldFileName.replace('.h','.skel'), newFileName.replace('.h','.skel') ) 0041 # And now rename includes 0042 # ... in source files 0043 files = os.popen('find . -name \'*[cph]\' | xargs grep -l '+oldFileName).readlines() 0044 for f in files: 0045 data = file(f.strip(),"r").read() 0046 data = data.replace( '#include <' + oldFileName + '>', '#include <' + newFileName + '>' ) 0047 data = data.replace( '#include "' + oldFileName + '"', '#include "' + newFileName + '"' ) 0048 data = data.replace( '#include "' + oldFileName.replace('.h','.moc') + '"', '#include "' + newFileName.replace('.h','.moc') + '"' ) 0049 file(f.strip(),"w").write( data ) 0050 # ... in ui files 0051 uifiles = os.popen('find . -name \'*.ui\' | xargs grep -l '+oldFileName).readlines() 0052 for f in uifiles: 0053 data = file(f.strip(),"r").read() 0054 data = data.replace( '>' + oldFileName + '</header', '>' + newFileName + '</header>' ) 0055 file(f.strip(),"w").write( data ) 0056 # ... and in kcfg files 0057 kcfgfiles = os.popen('find . -name \'*.kcfg\' | xargs grep -l '+oldFileName).readlines() 0058 for f in kcfgfiles: 0059 data = file(f.strip(),"r").read() 0060 data = data.replace( '>' + oldFileName + '</include', '>' + newFileName + '</include>' ) 0061 file(f.strip(),"w").write( data ) 0062 0063 def help(): 0064 print "Usage: %s filename" 0065 print "where the file <filename> contains the definition of the files to rename." 0066 sys.exit() 0067 0068 # Main 0069 0070 if len(sys.argv) < 2: 0071 help() 0072 0073 replaceFileName = sys.argv[1]; 0074 0075 for line in file(replaceFileName, "r").readlines(): 0076 words = line.split() 0077 oldBaseName = words[0] 0078 newBaseName = words[1] 0079 renameHeader( oldBaseName + '.h', newBaseName + '.h' ) 0080 if os.path.exists( oldBaseName + '.cc' ): 0081 renameFile( oldBaseName + '.cc', newBaseName + '.cpp' ) 0082 elif os.path.exists( oldBaseName + '.cpp' ): 0083 renameFile( oldBaseName + '.cpp', newBaseName + '.cpp' ) 0084 0085 0086 # Write Makefile.am back 0087 if buildFile and (origMakefileAmContents != MakefileAmContents): 0088 file(buildFile,"w").write( MakefileAmContents )