-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_expression.py
More file actions
195 lines (167 loc) · 5.9 KB
/
replace_expression.py
File metadata and controls
195 lines (167 loc) · 5.9 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import shutil
import fnmatch
import traceback
def do_replace(
file_path,
file_dir,
replace_callback,
num_iterations=1,
replace_callback_all_lines=None,
simulate=False,
):
with open(
file_path, "r"
) as fp: # encoding="ascii", errors="surrogateescape") as fp:
lines = fp.read().splitlines()
new_lines = lines
found = False
if replace_callback:
for iteration in range(num_iterations):
new_lines = []
for line_orig in lines:
line = line_orig.rstrip()
# process line
line = replace_callback(line, file_dir, iteration)
# line was removed
if line is None:
found = True
if simulate:
print("\t" + line_orig + " -> " + "removed")
else:
# line has changed
if line != line_orig.rstrip():
found = True
if simulate:
print("\t" + line_orig + " -> " + line)
new_lines.append(line)
else:
new_lines.append(line_orig)
# nothing todo
if not found:
return
lines = new_lines
if replace_callback_all_lines:
new_lines, changed = replace_callback_all_lines(new_lines, file_path)
if changed:
found = True
if not simulate and found:
try:
tmp_file = file_path + ".bak"
with open(tmp_file, "w") as fp:
for line in new_lines:
print(line, file=fp)
shutil.move(tmp_file, file_path)
except:
print("\t" + "Skipped %s" % file_path)
def skip_file(name, exlude_patterns):
if exlude_patterns is None:
return False
for pattern in exlude_patterns:
if len(fnmatch.filter([name], pattern)) != 0 or pattern in name:
print("[INFO] skipping %s because of pattern %s" % (name, pattern))
return True
return False
def listdir(path, exlude_patterns=[]):
"""
recursively walk directory to specified depth
:param path: (str) path to list files from
:yields: (str) filename, including path
"""
for filename in os.listdir(path):
if skip_file(filename, exlude_patterns):
continue
yield os.path.join(path, filename)
def walk(path=".", depth=None, exlude_patterns=[], verbose=False):
"""
recursively walk directory to specified depth
:param path: (str) the base path to start walking from
:param depth: (None or int) max. recursive depth, None = no limit
:yields: (str) filename, including path
"""
if depth and depth == 1:
for filename in listdir(path, exlude_patterns):
yield filename
else:
top_pathlen = len(path) + len(os.path.sep)
for dirpath, dirnames, filenames in os.walk(path):
if skip_file(dirpath, exlude_patterns):
continue
if verbose:
print("[INFO] processing %s" % (dirpath))
dirlevel = dirpath[top_pathlen:].count(os.path.sep)
if depth and dirlevel >= depth:
dirnames[:] = []
else:
for filename in filenames:
yield os.path.join(dirpath, filename)
def process_all_files(
root_dir,
replace_callback,
num_iterations=1,
replace_callback_all_lines=None,
simulate=False,
exlude_patterns=[],
max_depth=10,
):
def process_file(file_path):
try:
do_replace(
file_path,
os.path.dirname(file_path),
replace_callback=replace_callback,
num_iterations=num_iterations,
replace_callback_all_lines=replace_callback_all_lines,
simulate=simulate,
)
except UnicodeDecodeError:
print("\n[ERROR] UnicodeDecodeError in %s:\n" % file_path)
except Exception:
print("[ERROR] %s:\n\n" % file_path, traceback.format_exc())
if os.path.isfile(root_dir) and not os.path.isdir(root_dir):
process_file(root_dir)
for file_path in walk(
root_dir, depth=max_depth, exlude_patterns=exlude_patterns, verbose=simulate
):
if os.path.isfile(file_path):
if (
file_path.endswith(".cpp")
or file_path.endswith(".cxx")
or file_path.endswith(".h")
or file_path.endswith(".hpp")
or file_path.endswith(".inl")
):
process_file(file_path)
def main(replace_callback, num_iterations=1, replace_callback_all_lines=None):
import argparse
parser = argparse.ArgumentParser("")
parser.add_argument(
"-source",
dest="source",
help="path to source directory",
)
parser.add_argument(
"-simulate",
action="store_true",
help="only show changes without modifying files",
)
parser.add_argument(
"-depth", dest="depth", default=10, help="max depth for recursion"
)
parser.add_argument(
"-e",
"-exlude-pattern",
dest="exlude_patterns",
action="append",
help="pattern to exclude specific folder(s) with Unix shell-style wildcards (see fnmatch)",
)
args = parser.parse_args()
process_all_files(
args.source,
replace_callback=replace_callback,
num_iterations=num_iterations,
replace_callback_all_lines=replace_callback_all_lines,
simulate=args.simulate,
exlude_patterns=args.exlude_patterns,
max_depth=int(args.depth),
)