-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreplace_line.py
47 lines (39 loc) · 1.3 KB
/
replace_line.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from os.path import abspath, dirname, basename, join
from os import walk
FROM_STRING = """
<script src="${js.ctrl_render_item}"></script>
<script src="${js.ctrl_popreply}"></script>
"""
TO_STRING = """
zzzzcvs
zzzzcvs
"""
def run():
from_string = FROM_STRING.strip()
to_string = TO_STRING.strip()
for from_s, to_s in zip(filter(bool,FROM_STRING.split('\n')), filter(bool,TO_STRING.split('\n'))):
replace(from_s.strip(), to_s.strip())
def replace(from_string, to_string):
from_string = from_string.strip()
to_string = to_string.strip()
file = abspath(__file__)
for dirpath, dirnames, filenames in walk(dirname(file)):
dirbase = basename(dirpath)
if dirbase.startswith('.'):
continue
for filename in filenames:
suffix = filename.rsplit('.', 1)[-1]
if suffix not in ('py', 'htm', 'txt', 'conf', 'css', 'h', 'template', 'js'):
continue
path = join(dirpath, filename)
if path == file:
continue
with open(path) as f:
content = f.read()
t = content.replace(from_string, to_string)
if t != content:
with open(path, 'wb') as f:
f.write(t)
run()