-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmassive-rename.py
75 lines (57 loc) · 1.81 KB
/
massive-rename.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
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
import os, re, sys
import getopt
debug = True
def usage(progName):
print "usage: %s [--help] [--stdin] [--count] <search> [<replace>]" % progName
print
def treat_file(fname, countOnly, toSearch, toReplace):
content = open(fname, "r").read()
outputContent = ''
count = 0
startAt = 0
while True:
pos = content.find(toSearch, startAt)
if pos < 0:
break
count += 1
if not countOnly:
outputContent += content[startAt:pos] + toReplace
startAt = pos + len(toSearch)
if not countOnly and count:
outputContent += content[startAt:]
open(fname, "w").write(outputContent)
if debug and count:
print "%s: %d" % (fname, count)
return count
if __name__ == '__main__':
countOnly = False
source = None
try:
opts, args = getopt.getopt(sys.argv[1:], "", ["help", "stdin", "count"])
except getopt.GetoptError as err:
print str(err)
usage(sys.argv[0])
sys.exit(2)
for option, arg in opts:
if option in ("--help", "-h"):
usage(sys.argv[0])
sys.exit(0)
elif option in ("--count"):
countOnly = True
elif option in ("--stdin"):
source = sys.stdin.readlines()
if not source:
source = os.popen("git ls-files").readlines()
requiredArgs = 2
if countOnly:
requiredArgs = 1
if len(args) < requiredArgs:
print "missing arguments"
usage(sys.argv[0])
sys.exit(2)
modified = 0
for l in source:
l = l.strip('\n')
modified += treat_file(l, countOnly, args[0], countOnly and args[0] or args[1])
if countOnly:
print "got %d occurences" % modified