-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfindreplace.py
48 lines (40 loc) · 1.43 KB
/
findreplace.py
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
# expects 3 args
# python findreplace.py <path> <find string> <replace string>
import os
import fileinput
import sys
allowed_extensions = [ ".h", ".cpp", ".m", ".mm", ".pbxproj", ".pch", ".plist" ]
forbidden_dirs = [ ".git", ".svn" ]
path, search_str, replace_str = sys.argv[1:4]
print "Replacing " + search_str + " with " + replace_str
def findReplaceFilename(path, filename):
name = filename.replace(search_str, replace_str)
if name != filename:
os.rename(os.path.join(path, filename), os.path.join(path, name))
return True
return False
def findReplaceContents(path):
f = open(path, "r")
lines = f.readlines(99999999)
f.close()
f = open(path, "w")
f.seek(0, os.SEEK_SET)
for line in lines:
replaced = line.replace(search_str, replace_str)
if replaced != line:
print "Replaced line in " + str(path)
print replaced
f.write(replaced)
for root, dirs, files in os.walk(path):
for filename in files:
if filename[filename.rfind("."):] not in allowed_extensions:
continue
findReplaceContents(os.path.join(root, filename))
findReplaceFilename(root, filename)
for dirname in dirs:
if dirname in forbidden_dirs:
dirs.remove(dirname)
continue
if findReplaceFilename(root, dirname):
dirs.remove(dirname)
dirs.append(dirname.replace(search_str, replace_str))