~ubuntu-branches/ubuntu/oneiric/kdesdk/oneiric-updates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#! /usr/bin/env python
#
# Copyright David Faure <faure@kde.org>, License LGPL v2
#
# This script renames files (sources and headers), and take care of
# - doing the svn command to rename the file
# - updating Makefile.am
# - updating #include lines (for headers and for .moc)
#
# The input is a file with a syntax like "kwframe KWFrame", one renaming on each line.
# "oldName newName" will rename oldName.h to newName.h and oldName.{cc,cpp} to newName.cpp
#
import sys, os

buildFile = "Makefile.am"
if not os.path.exists( buildFile ):
	buildFile = "CMakeLists.txt"
	if not os.path.exists( buildFile ):
		buildFile = ""

MakefileAmContents = ""
if buildFile:
	MakefileAmContents = file(buildFile, "r").read()
origMakefileAmContents = MakefileAmContents

def renameFile( oldFileName, newFileName ):
	global MakefileAmContents
	if oldFileName != newFileName and os.path.exists( oldFileName ):
		print oldFileName + " -> " + newFileName
		if os.path.exists( newFileName ):
			print "Warning, erasing " + newFileName
			os.unlink( newFileName )  # could be due to a former run
		#os.rename( oldFileName, newFileName )
		os.system( 'svn move --force ' + oldFileName + ' ' + newFileName )
		MakefileAmContents = MakefileAmContents.replace( oldFileName, newFileName )
	
def renameHeader( oldFileName, newFileName ):
	global MakefileAmContents
	renameFile( oldFileName, newFileName )
	MakefileAmContents = MakefileAmContents.replace( oldFileName.replace('.h','.skel'), newFileName.replace('.h','.skel') )
	# And now rename includes
	#  ... in source files
	files = os.popen('find . -name \'*[cph]\' | xargs grep -l '+oldFileName).readlines()
	for f in files:
		data = file(f.strip(),"r").read()
		data = data.replace( '#include <' + oldFileName + '>', '#include <' + newFileName + '>' )
		data = data.replace( '#include "' + oldFileName + '"', '#include "' + newFileName + '"' )
		data = data.replace( '#include "' + oldFileName.replace('.h','.moc') + '"', '#include "' + newFileName.replace('.h','.moc') + '"' )
		file(f.strip(),"w").write( data )
	#  ... in ui files
	uifiles = os.popen('find . -name \'*.ui\' | xargs grep -l '+oldFileName).readlines()
	for f in uifiles:
		data = file(f.strip(),"r").read()
		data = data.replace( '>' + oldFileName + '</header', '>' + newFileName + '</header>' )
		file(f.strip(),"w").write( data )
	#  ... and in kcfg files
	kcfgfiles = os.popen('find . -name \'*.kcfg\' | xargs grep -l '+oldFileName).readlines()
	for f in kcfgfiles:
		data = file(f.strip(),"r").read()
		data = data.replace( '>' + oldFileName + '</include', '>' + newFileName + '</include>' )
		file(f.strip(),"w").write( data )

def help():
	print "Usage: %s filename"
	print "where the file <filename> contains the definition of the files to rename."
	sys.exit()

# Main

if len(sys.argv) < 2:
	help()

replaceFileName = sys.argv[1];

for line in file(replaceFileName, "r").readlines():
	words = line.split()
	oldBaseName = words[0]
	newBaseName = words[1]
	renameHeader( oldBaseName + '.h', newBaseName + '.h' )
	if os.path.exists( oldBaseName + '.cc' ):
		renameFile( oldBaseName + '.cc', newBaseName + '.cpp' )
	elif os.path.exists( oldBaseName + '.cpp' ):
		 renameFile( oldBaseName + '.cpp', newBaseName + '.cpp' )
	

# Write Makefile.am back
if buildFile and (origMakefileAmContents != MakefileAmContents):
	file(buildFile,"w").write( MakefileAmContents )