-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-sed-replace.py
60 lines (54 loc) · 1.76 KB
/
build-sed-replace.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
import re, glob
replacements = {}
regexp_replacements = {}
def init():
quotes = [re.compile('^"([^"]+)" "([^"]+)"$'), re.compile('^"([^"]+)" ([^ "]+)$'),
re.compile('^([^ "]+) "([^"]+)"$'), re.compile('^([^ "]+) ([^ "]+)$')]
for filePath in glob.glob("./replacements/*.txt"):
file = open(filePath, "r")
for line in file.readlines():
line = line.strip()
if line.startswith('#'):
continue
i = 0
found = False
while True:
r = quotes[i].match(line)
if r:
src = r.group(1)
trg = r.group(2)
replacements[re.escape(src)] = trg
found = True
break
i = i + 1
if i == 4:
break
if not found:
print ("Error in line: ", line, "(", filePath, ")")
quotesRegexp = re.compile('^"(.+)" "(.+)" "(.+)"$')
for filePath in glob.glob("./replacements/*.regexp"):
file = open(filePath, "r")
for line in file.readlines():
line = line.strip()
if line.startswith('#'):
continue
if "#" in line:
line = line.split("#")[0].strip()
r = quotesRegexp.match(line)
if r:
src = r.group(1)
trg = r.group(2)
regexp_replacements[src] = trg
else:
print ("Error in line: ", line, "(", filePath, ")")
init()
output = open("replace-all.sh", "w")
output.write("path=$1\n")
for src in replacements:
trg = replacements[src]
output.write('echo "Searching: ' + src + ' in file ${path}"\n')
output.write(u'sed -i "s/\\b'+src+'\\b/'+trg+'/g" ${path}\n')
for src in regexp_replacements:
trg = regexp_replacements[src]
output.write('echo "Searching: ' + src + ' in file ${path}"\n')
output.write(u'sed -i -E "s/'+src+'/'+trg+'/g" ${path}\n')