-
Notifications
You must be signed in to change notification settings - Fork 10
/
udiff
executable file
·282 lines (250 loc) · 9.31 KB
/
udiff
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
#!/usr/bin/env python
# vim: set ts=8 sw=4 sts=4 et ai:
#
# udiff.py: find minor differences in large files
# (microdiff, or unified-diff *grin*)
#
# Use this instead of regular diff(1) which is prone to choke with a
# "memory exhausted" error on large files. This can be useful when
# comparing database dumps before and after an operation: lots of
# invariant INSERT statements and only a handful more or less caused
# by the operation.
#
# Walter Doekes, 2011, OSSO B.V.
# http://wjd.nu/notes/2011#diff-memory-exhausted-udiff
# License: Public Domain
BUFFER_LINES = 10000
class LineByLineReader(object):
'''A file reader that reads a file line by line which can look up if
a similar line exists a bit down the road. The maximum number of
lines to look forward to is defined by max_lookahead.'''
def __init__(self, file, max_lookahead=BUFFER_LINES):
super(LineByLineReader, self).__init__()
self.file = file
self.max_lookahead = max_lookahead
self.lineno = 0
self.buffer = [] # list of (lineno, line) tuples
self.bigbuffer = b''
def _fill(self):
'''Add another line to the internal buffer. Raises
StopIteration on EOF.'''
if self.bigbuffer is None:
raise StopIteration()
eof = False
while True:
more = self.file.read(8192)
if not more:
eof = True
break
if b'\n' in more:
self.bigbuffer += more
break
self.bigbuffer += more
items = self.bigbuffer.split(b'\n')
for item in items[0:-1]:
self.lineno += 1
self.buffer.append((self.lineno, item))
if eof:
if items[-1]:
# Missing LF at eof..
self.lineno += 1
self.buffer.append((self.lineno, items[-1]))
self.bigbuffer = None
if not self.buffer:
raise StopIteration()
else:
self.bigbuffer = items[-1]
def _pop(self):
'''Pops a line from the internal buffer.'''
ret = self.buffer[0]
self.buffer.pop(0)
return ret
def has_next(self):
'''Returns whether there are more lines or not.'''
if not self.buffer:
try:
self._fill()
except StopIteration:
return False
return True
def push_back(self, linedata):
'''Push a line back into the buffer.'''
self.buffer.insert(0, linedata)
def next(self):
'''Serve the next line or None if we're at EOF.'''
if not self.buffer:
try:
self._fill()
except StopIteration:
return None
return self._pop()
def find(self, line):
'''Returns amount of lines to skip if line is found or None.'''
i = 0
while True:
for i, buf in enumerate(self.buffer[i:], i):
if buf[1] == line:
return i
if len(self.buffer) >= self.max_lookahead:
break
try:
self._fill()
except StopIteration:
break
return None
def __repr__(self):
return 'LineByLineReader(%s)' % (self.file.name,)
def filediff(file1, file2):
max_context = 3
reader1 = LineByLineReader(file1)
reader2 = LineByLineReader(file2)
# How many lines of context we still need to print:
post_context_needed = 0
mode = 'skip' # 'skip', 'diff', 'context'
has_heading = False # any heading at all
context = []
lineno1 = lineno2 = -1
while reader1.has_next() and reader2.has_next():
lineno1, line1 = reader1.next()
lineno2, line2 = reader2.next()
# Ah, lines are the same.
if line1 == line2:
# Are we at the end of a diff?
if mode == 'diff':
mode = 'context'
post_context_needed = max_context
# If we need post-diff-context, yield that
if mode == 'context':
if post_context_needed:
yield b' %s\n' % (line1,)
post_context_needed -= 1
else:
mode = 'skip'
# Assert we have context for the next diff
if mode == 'skip':
context.append(line1)
while len(context) > max_context:
context.pop(0)
# Lines are different.
else:
# Dump sub-heading and context if not done yet
if mode == 'skip':
if not has_heading or len(context) == max_context:
yield b'@@ -%d,? +%d,? @@\n' % (
lineno1 - len(context), lineno2 - len(context))
has_heading = True
while context:
yield b' %s\n' % (context.pop(0),)
mode = 'diff'
# Attempt to find a match a bit later on
lines2 = reader2.find(line1)
lines1 = reader1.find(line2)
if lines1 is not None or lines2 is not None:
# Okay.. we've found something here
if lines1 is not None and (
lines2 is None or lines1 < lines2):
line, reader, lines, sign = line1, reader1, lines1, b'-'
reader2.push_back((lineno2, line2))
elif lines2 is not None and (
lines1 is None or lines2 < lines1):
line, reader, lines, sign = line2, reader2, lines2, b'+'
reader1.push_back((lineno1, line1))
else:
# Both differences are "smallest", 1 goes first
line, reader, lines, sign = line1, reader1, lines1, b'-'
reader2.push_back((lineno2, line2))
yield b'%s%s\n' % (sign, line)
while lines:
yield b'%s%s\n' % (sign, reader.next()[1])
lines -= 1
else:
yield b'-%s\n' % (line1,)
yield b'+%s\n' % (line2,)
# Dump sub-heading and context if not done yet
if mode == 'skip' and (reader1.has_next() or reader2.has_next()):
# Only print heading if !has_heading or the context is fresh
if not has_heading or len(context) == max_context:
# Handle the special case when one of the files is empty
if lineno1 == -1 and reader1.has_next():
lineno1 = 0
if lineno2 == -1 and reader2.has_next():
lineno2 = 0
yield b'@@ -%d,? +%d,? @@\n' % (
lineno1 - len(context) + 1, lineno2 - len(context) + 1)
has_heading = True
while context:
yield b' %s\n' % (context.pop(0),)
while reader1.has_next():
yield b'-%s\n' % (reader1.next()[1],)
while reader2.has_next():
yield b'+%s\n' % (reader2.next()[1],)
def fixheadings(iterator):
buf = []
blank = left = right = 0
for line in iterator:
if line.startswith(b'@@'):
if buf:
yield (
buf[0].replace(b'?', b'%u' % (blank + left), 1)
.replace(b'?', b'%u' % (blank + right), 1))
for i in buf[1:]:
yield i
buf = []
blank = left = right = 0
elif line.startswith(b'-'):
left += 1
elif line.startswith(b'+'):
right += 1
else:
blank += 1
buf.append(line)
# Safety feature: don't overrun memory to fix the numbers.
# Flush the whole thing and exit.
if len(buf) > BUFFER_LINES:
for i in buf:
yield i
for i in iterator:
yield i
return
if buf:
yield (
buf[0].replace(b'?', b'%u' % (blank + left), 1)
.replace(b'?', b'%u' % (blank + right), 1))
for i in buf[1:]:
yield i
def diff(args, stdout, stderr):
if len(args) != 2:
stderr.write(b'diff: need two arguments\n')
return 1
file1 = open(args[0], 'rb')
file2 = open(args[1], 'rb')
generator = fixheadings(filediff(file1, file2))
# Print header before first output
for output in generator:
import datetime
import os
mtime, mdate, nsec = [None, None], [None, None], [None, None]
for i in range(2):
mtime[i] = os.stat(args[i]).st_mtime
mdate[i] = datetime.datetime.fromtimestamp(mtime[i])
nsec[i] = '%09d' % (int((mtime[i] - int(mtime[i])) * 1000000000),)
fmt = '%Y-%m-%d %H:%M:%S'
tz = '+0000' # for another rainy day
stdout.write(('--- %s\t%s.%s %s\n' % (
args[0], mdate[0].strftime(fmt), nsec[0], tz)).encode('utf-8'))
stdout.write(('+++ %s\t%s.%s %s\n' % (
args[1], mdate[1].strftime(fmt), nsec[1], tz)).encode('utf-8'))
stdout.write(output)
break
# Print other output
for output in generator:
stdout.write(output)
pass
if __name__ == '__main__':
import sys
if hasattr(sys.stdout, 'buffer'):
ret = diff(sys.argv[1:], sys.stdout.buffer, sys.stderr.buffer)
else:
ret = diff(sys.argv[1:], sys.stdout, sys.stderr) # py2
if ret != 0:
sys.exit(ret)