~yade-dev/yade/0.80

« back to all changes in this revision

Viewing changes to scripts/rename-class.py

  • Committer: Anton Gladky
  • Date: 2012-05-02 21:50:42 UTC
  • Revision ID: gladky.anton@gmail.com-20120502215042-v1fa9r65usqe7kfk
0.80.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
import os,re,sys,os.path
 
3
 
 
4
if not os.path.exists('SConstruct'): raise 'Must be run from the top-level source directory'
 
5
oldClass,newClass=sys.argv[1],sys.argv[2]
 
6
replCnt,moveCnt,moveDirCnt=0,0,0
 
7
 
 
8
for root, dirs, files in os.walk('.'):
 
9
        for name in files:
 
10
                if os.path.isdir(root+'/'+name): continue
 
11
                if not name.endswith('pp') and name!='SConscript': continue
 
12
                modified=False; new=[]; fullName=root+'/'+name
 
13
                if fullName.startswith('./lib/') or fullName.startswith('./extra'): continue
 
14
                for l in open(fullName):
 
15
                        nl,cnt=re.subn(r'\b'+oldClass+r'\b',newClass,l)
 
16
                        if cnt>0:
 
17
                                new.append(nl); modified=True; replCnt+=1
 
18
                        else: new.append(l)
 
19
                # write back the file if it was modified
 
20
                if modified:
 
21
                        print 'Updated: ',root+'/'+name
 
22
                        f=open(root+'/'+name,'w')
 
23
                        for l in new: f.write(l)
 
24
                        f.close()
 
25
                # try to replace the class within the filename, move using bzr if it has changed
 
26
                newName,cnt=re.subn(r'\b'+oldClass+r'\b',newClass,name)
 
27
                if cnt>0:
 
28
                        newFullName=root+'/'+newName
 
29
                        os.system('bzr mv '+fullName+' '+newFullName)
 
30
                        moveCnt+=1
 
31
 
 
32
# walk directories and change them with bzr if they match exactly (rarely useful)
 
33
for root, dirs, files in os.walk('.'):
 
34
        for d in dirs:
 
35
                if d==oldClass:
 
36
                        os.system('bzr mv %s/%s %s/%s'%(root,oldClass,root,newClass))
 
37
                        moveDirCnt+=1
 
38
 
 
39
print "Replaced %d occurences, moved %d files and %d directories"%(replCnt,moveCnt,moveDirCnt)
 
40
print "Update python scripts (if wanted) by running: perl -pi -e 's/\\b%s\\b/%s/g' `ls **/*.py **/*.rst |grep -v py/system.py`"%(oldClass,newClass)
 
41
import time,pwd,socket
 
42
# update python deprecation records
 
43
if replCnt+moveCnt+moveDirCnt==0:
 
44
        print "No replaces, not updating py/system.py deprecated names map."
 
45
if True:
 
46
        new=[]
 
47
        for l in open('py/system.py'):
 
48
                if 'END_RENAMED_CLASSES_LIST' in l: new+="\t'%s':'%s', # %s, %s@%s\n"%(oldClass,newClass,time.asctime(),pwd.getpwuid(os.getuid())[0],socket.gethostname())
 
49
                new+=l
 
50
        f=open('py/system.py','w')
 
51
        for l in new: f.write(l)
 
52
        f.close()
 
53
 
 
54
 
 
55