-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path__init__.py
306 lines (270 loc) · 10.3 KB
/
__init__.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
Main git-search-replace module
"""
from optparse import OptionParser
import subprocess
import sys
import re
import tempfile
import os
import bisect
import fnmatch
DEFAULT_SEPARATOR = '///'
def run_subprocess(cmd):
pipe = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output = pipe.communicate()[0]
return output
def error(s):
print("git-search-replace: error: " + s)
sys.exit(-1)
class Expression(object):
def __init__(self, fromexpr, toexpr, big_g):
self.fromexpr = fromexpr
self.toexpr = toexpr
self.big_g = big_g
def underscore_to_titlecase(name):
l = []
for p in name.split('_'):
if p:
p = p[:1].upper() + p[1:]
l.append(p)
return ''.join(l)
def titlecase_to_underscore(name):
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()
class GitSearchReplace(object):
"""Main class"""
def __init__(self, separator=None, diff=None, fix=None, renames=None, filters=None, expressions=None):
self.separator = separator
self.diff = diff
self.fix = fix
self.renames = renames
self.filters = filters
self.expressions_str = expressions
self.expressions = []
self.stage = None
BIG_G_REGEX = re.compile(r"[\]G[{][^}]*[}]")
def calc_big_g(self, big_g_expr):
"""Transform the special interpolated \G{<python>}"""
parts = []
prefix = r'\G{'
oparts = big_g_expr.split(prefix)
parts = [oparts[0]]
for part in oparts[1:]:
if '}' in part:
x = part.find('}')
parts.append(prefix + part[:x+1])
parts.append(part[x+1:])
else:
parts.append(part)
def replacer_func(G):
def m(i):
return G.groups(0)[i]
gen = []
dotslash = '/'
if self.stage == 'content':
dotslash = '.'
namespace = dict(
G=G,
m=m,
underscore_to_titlecase=underscore_to_titlecase,
titlecase_to_underscore=titlecase_to_underscore,
dotslash=dotslash,
)
for part in parts:
if part.startswith(r'\G{'):
gen.append(eval(part[3:-1:], namespace))
else:
gen.append(part)
return ''.join(gen)
return replacer_func
def compile_expressions(self):
if not self.expressions_str:
error("no FROM-TO expressions specified")
return
expressions = []
if self.separator is None:
if len(self.expressions_str) % 2 != 0:
error("FROM-TO expressions not paired")
pairs = list(zip(self.expressions_str[::2], self.expressions_str[1::2]))
else:
pairs = [expr.split(self.separator, 1) for expr in self.expressions_str]
for fromexpr, toexpr in pairs:
big_g = None
if self.BIG_G_REGEX.search(toexpr):
big_g = self.calc_big_g(toexpr)
from_regex = re.compile(fromexpr)
expressions.append(Expression(from_regex, toexpr, big_g))
self.expressions = expressions
def sub(self, expr, content, stage):
self.stage = stage
if expr.big_g:
return expr.fromexpr.sub(expr.big_g, content)
return expr.fromexpr.sub(expr.toexpr, content)
def search_replace_in_files(self):
filenames = str(run_subprocess(["git", "ls-files"]), 'utf-8').splitlines()
filtered_filenames = []
for filename in filenames:
excluded = False
for (mode, pattern) in self.filters:
if fnmatch.fnmatch(filename, pattern):
excluded = mode == 'exclude'
continue
if excluded:
continue
filtered_filenames.append(filename)
for filename in filtered_filenames:
if not os.path.isfile(filename):
continue
with open(filename) as fileobj:
try:
filedata = fileobj.read()
except UnicodeDecodeError:
continue
if self.diff or self.fix:
self.show_file(filename, filedata)
else:
self.show_lines_grep_like(filename, filedata)
if self.renames:
for filename in filtered_filenames:
for expr in self.expressions:
new_filename = filename
new_filename = self.sub(expr, new_filename, 'filename')
if new_filename != filename:
print()
print("rename-src-file: %s" % (filename, ))
print("rename-dst-file: %s" % (new_filename, ))
if self.fix:
dirname = os.path.dirname(new_filename)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
cmd = ["git", "mv", filename, new_filename]
run_subprocess(cmd)
def show_file(self, filename, filedata):
new_filedata = filedata
for expr in self.expressions:
new_filedata = self.sub(expr, new_filedata, 'content')
if new_filedata != filedata:
self.act_on_possible_modification(filename, new_filedata)
def show_lines_grep_like(self, filename, filedata):
new_filedata = filedata
expr_id = 0
shown_lines = []
for expr in self.expressions:
lines = []
line_pos = []
pos = 0
for line in new_filedata.split("\n"):
lines.append(line)
line_pos.append(pos)
pos += len(line) + 1
matches = expr.fromexpr.finditer(new_filedata)
for match in matches:
line_nr = bisect.bisect(line_pos, match.start())
shown_lines.append('%s:%d:%s:%s' % (
filename, line_nr, expr_id*'_',
lines[line_nr - 1]))
new_filedata = self.sub(expr, new_filedata, 'content')
expr_id += 1
shown_lines.sort()
for line in shown_lines:
print(line)
def act_on_possible_modification(self, filename, new_filedata):
if self.diff:
print("diff -urN a/%s b/%s" % (filename, filename))
self.show_diff(filename, new_filedata)
if self.fix:
fileobj = open(filename, "w")
fileobj.write(new_filedata)
fileobj.close()
def show_diff(self, filename, new_filedata):
fileobj = None
tempf = tempfile.mktemp()
try:
fileobj = open(tempf, "w")
fileobj.write(new_filedata)
fileobj.close()
diff = str(run_subprocess(["diff", "-urN", filename, tempf]), 'utf8')
minus_matched = False
plus_matched = False
for line in diff.splitlines():
if not minus_matched:
minus_matched = True
match = re.match("^--- ([^ ]+) (.*)$", line)
if match:
print("--- a/%s %s" % (filename,
match.groups(0)[1], ))
continue
if not plus_matched:
plus_matched = True
match = re.match("^[+][+][+] ([^ ]+) (.*)$", line)
if match:
print("+++ b/%s %s" % (filename,
match.groups(0)[1], ))
continue
print(line)
finally:
os.unlink(tempf)
def run(self):
self.compile_expressions()
self.search_replace_in_files()
def add_filter(option, opt_str, value, parser, mode):
if not hasattr(parser.values, 'filters'):
parser.values.filters = []
parser.values.filters.append((mode, value))
def main():
"""Main function"""
parser = OptionParser(usage=
"usage: %prog [options] (FROM-SEPARATOR-TO...)\n"
" %prog [options] -p FROM1 TO1 FROM2 TO2 ...")
parser.add_option(
"-s", "--separator", dest="separator", default=None,
help="The separator string the separates FROM and TO regexes. %s by default,"
" if -p is not specified" % (DEFAULT_SEPARATOR, ),
metavar="STRING")
parser.add_option(
"-p", "--pair-arguments",
action="store_true", dest="pair_args", default=False,
help="Use argument pairs for FROM and TO regexes. "
"Useful with shell expansion. E.g: colo{,u}r")
parser.add_option("-f", "--fix",
action="store_true", dest="fix", default=False,
help="Perform changes in-place")
parser.add_option("-d", "--diff",
action="store_true", dest="diff", default=False,
help="Use 'diff' util to show differences")
parser.add_option("-e", "--exclude",
action="callback", callback=add_filter,
callback_args=('exclude', ),
type="string",
metavar="PATTERN",
help="Exclude files matching the provided globbing "
"pattern (can be specified more than once)")
parser.add_option("-i", "--include",
action="callback", callback=add_filter,
callback_args=('include', ),
type="string",
metavar="PATTERN",
help="Include files matching the provided globbing "
"pattern (can be specified more than once)")
parser.add_option("--no-renames",
action="store_false", dest="renames", default=True,
help="Don't perform renames")
(options, args) = parser.parse_args()
filters = getattr(options, 'filters', [])
if len(filters) >= 1:
if filters[0][0] == 'include':
filters = [('exclude', '**')] + filters
if options.pair_args:
assert options.separator is None
sep = None
else:
sep = options.separator or DEFAULT_SEPARATOR
gsr = GitSearchReplace(
separator=sep,
diff=options.diff,
fix=options.fix,
renames=options.renames,
filters=filters,
expressions=args)
gsr.run()
__all__ = ["main"]