Show More
@@ -1,134 +1,144 b'' | |||
|
1 | 1 | #!/usr/bin/python |
|
2 | 2 | import difflib, struct, mmap |
|
3 | 3 | |
|
4 | 4 | devzero = file("/dev/zero") |
|
5 | 5 | |
|
6 | 6 | def unidiff(a, ad, b, bd, fn): |
|
7 | 7 | if not a and not b: return "" |
|
8 | 8 | a = a.splitlines(1) |
|
9 | 9 | b = b.splitlines(1) |
|
10 | 10 | l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn, ad, bd)) |
|
11 | 11 | return "".join(l) |
|
12 | 12 | |
|
13 | 13 | def textdiff(a, b): |
|
14 | 14 | return diff(a.splitlines(1), b.splitlines(1)) |
|
15 | 15 | |
|
16 | 16 | def sortdiff(a, b): |
|
17 | 17 | la = lb = 0 |
|
18 | 18 | |
|
19 | 19 | while 1: |
|
20 | 20 | if la >= len(a) or lb >= len(b): break |
|
21 | 21 | if b[lb] < a[la]: |
|
22 | 22 | si = lb |
|
23 | 23 | while lb < len(b) and b[lb] < a[la] : lb += 1 |
|
24 | 24 | yield "insert", la, la, si, lb |
|
25 | 25 | elif a[la] < b[lb]: |
|
26 | 26 | si = la |
|
27 | 27 | while la < len(a) and a[la] < b[lb]: la += 1 |
|
28 | 28 | yield "delete", si, la, lb, lb |
|
29 | 29 | else: |
|
30 | 30 | la += 1 |
|
31 | 31 | lb += 1 |
|
32 | 32 | |
|
33 | 33 | if lb < len(b): |
|
34 | 34 | yield "insert", la, la, lb, len(b) |
|
35 | 35 | |
|
36 | 36 | if la < len(a): |
|
37 | 37 | yield "delete", la, len(a), lb, lb |
|
38 | 38 | |
|
39 | 39 | def diff(a, b, sorted=0): |
|
40 | 40 | bin = [] |
|
41 | 41 | p = [0] |
|
42 | 42 | for i in a: p.append(p[-1] + len(i)) |
|
43 | 43 | |
|
44 | 44 | if sorted: |
|
45 | 45 | d = sortdiff(a, b) |
|
46 | 46 | else: |
|
47 | 47 | d = difflib.SequenceMatcher(None, a, b).get_opcodes() |
|
48 | 48 | |
|
49 | 49 | for o, m, n, s, t in d: |
|
50 | 50 | if o == 'equal': continue |
|
51 | 51 | s = "".join(b[s:t]) |
|
52 | 52 | bin.append(struct.pack(">lll", p[m], p[n], len(s)) + s) |
|
53 | 53 | |
|
54 | 54 | return "".join(bin) |
|
55 | 55 | |
|
56 | def patchtext(bin): | |
|
57 | pos = 0 | |
|
58 | t = [] | |
|
59 | while pos < len(bin): | |
|
60 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) | |
|
61 | pos += 12 | |
|
62 | t.append(bin[pos:pos + l]) | |
|
63 | pos += l | |
|
64 | return "".join(t) | |
|
65 | ||
|
56 | 66 | # This attempts to apply a series of patches in time proportional to |
|
57 | 67 | # the total size of the patches, rather than patches * len(text). This |
|
58 | 68 | # means rather than shuffling strings around, we shuffle around |
|
59 | 69 | # pointers to fragments with fragment lists. |
|
60 | 70 | # |
|
61 | 71 | # When the fragment lists get too long, we collapse them. To do this |
|
62 | 72 | # efficiently, we do all our operations inside a buffer created by |
|
63 | 73 | # mmap and simply use memmove. This avoids creating a bunch of large |
|
64 | 74 | # temporary string buffers. |
|
65 | 75 | |
|
66 | 76 | def patches(a, bins): |
|
67 | 77 | if not bins: return a |
|
68 | 78 | |
|
69 | 79 | plens = [len(x) for x in bins] |
|
70 | 80 | pl = sum(plens) |
|
71 | 81 | bl = len(a) + pl |
|
72 | 82 | tl = bl + bl + pl # enough for the patches and two working texts |
|
73 | 83 | b1, b2 = 0, bl |
|
74 | 84 | |
|
75 | 85 | if not tl: return a |
|
76 | 86 | |
|
77 | 87 | m = mmap.mmap(devzero.fileno(), tl, mmap.MAP_PRIVATE) |
|
78 | 88 | |
|
79 | 89 | # load our original text |
|
80 | 90 | m.write(a) |
|
81 | 91 | frags = [(len(a), b1)] |
|
82 | 92 | |
|
83 | 93 | # copy all the patches into our segment so we can memmove from them |
|
84 | 94 | pos = b2 + bl |
|
85 | 95 | m.seek(pos) |
|
86 | 96 | for p in bins: m.write(p) |
|
87 | 97 | |
|
88 | 98 | def pull(dst, src, l): # pull l bytes from src |
|
89 | 99 | while l: |
|
90 | 100 | f = src.pop(0) |
|
91 | 101 | if f[0] > l: # do we need to split? |
|
92 | 102 | src.insert(0, (f[0] - l, f[1] + l)) |
|
93 | 103 | dst.append((l, f[1])) |
|
94 | 104 | return |
|
95 | 105 | dst.append(f) |
|
96 | 106 | l -= f[0] |
|
97 | 107 | |
|
98 | 108 | def collect(buf, list): |
|
99 | 109 | start = buf |
|
100 | 110 | for l, p in list: |
|
101 | 111 | m.move(buf, p, l) |
|
102 | 112 | buf += l |
|
103 | 113 | return (buf - start, start) |
|
104 | 114 | |
|
105 | 115 | for plen in plens: |
|
106 | 116 | # if our list gets too long, execute it |
|
107 | 117 | if len(frags) > 128: |
|
108 | 118 | b2, b1 = b1, b2 |
|
109 | 119 | frags = [collect(b1, frags)] |
|
110 | 120 | |
|
111 | 121 | new = [] |
|
112 | 122 | end = pos + plen |
|
113 | 123 | last = 0 |
|
114 | 124 | while pos < end: |
|
115 | 125 | p1, p2, l = struct.unpack(">lll", m[pos:pos + 12]) |
|
116 | 126 | pull(new, frags, p1 - last) # what didn't change |
|
117 | 127 | pull([], frags, p2 - p1) # what got deleted |
|
118 | 128 | new.append((l, pos + 12)) # what got added |
|
119 | 129 | pos += l + 12 |
|
120 | 130 | last = p2 |
|
121 | 131 | frags = new + frags # what was left at the end |
|
122 | 132 | |
|
123 | 133 | t = collect(b2, frags) |
|
124 | 134 | |
|
125 | 135 | return m[t[1]:t[1] + t[0]] |
|
126 | 136 | |
|
127 | 137 | def patch(a, bin): |
|
128 | 138 | return patches(a, [bin]) |
|
129 | 139 | |
|
130 | 140 | try: |
|
131 | 141 | import mpatch |
|
132 | 142 | patches = mpatch.patches |
|
133 | 143 | except: |
|
134 | 144 | pass |
General Comments 0
You need to be logged in to leave comments.
Login now