/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
1
from bzrlib.lazy_import import lazy_import
2
3
lazy_import(globals(), """
0.9.31 by Aaron Bentley
Allow selecting MemoryVersionedFile from commandline
4
import errno
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
5
import itertools
0.9.31 by Aaron Bentley
Allow selecting MemoryVersionedFile from commandline
6
import os
0.9.25 by Aaron Bentley
More messy hacking
7
from StringIO import StringIO
0.9.19 by Aaron Bentley
More tweakage
8
0.9.25 by Aaron Bentley
More messy hacking
9
from bzrlib import (
10
    patiencediff,
11
    trace,
12
    ui,
13
    )
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
14
from bzrlib.util import bencode
15
""")
0.9.25 by Aaron Bentley
More messy hacking
16
from bzrlib.tuned_gzip import GzipFile
0.9.3 by Aaron Bentley
Get three-parent comparisions under test
17
0.9.33 by Aaron Bentley
Enable caching commandline param
18
0.9.26 by Aaron Bentley
Move topological iteration into an iterator
19
def topo_iter(vf):
20
    seen = set()
21
    descendants = {}
22
    for version_id in vf.versions():
23
        for parent_id in vf.get_parents(version_id):
24
            descendants.setdefault(parent_id, []).append(version_id)
25
    cur = [v for v in vf.versions() if len(vf.get_parents(v)) == 0]
26
    while len(cur) > 0:
27
        next = []
28
        for version_id in cur:
29
            if version_id in seen:
30
                continue
31
            parents = vf.get_parents(version_id)
32
            if not seen.issuperset(parents):
33
                continue
34
            next.extend(descendants.get(version_id, []))
35
            yield version_id
36
            seen.add(version_id)
37
        cur = next
38
39
0.9.1 by Aaron Bentley
Get trivial case passing
40
class MultiParent(object):
41
0.9.2 by Aaron Bentley
Get single-parent comparison working
42
    def __init__(self, hunks=None):
43
        if hunks is not None:
44
            self.hunks = hunks
45
        else:
46
            self.hunks = []
47
48
    def __repr__(self):
49
        return "MultiParent(%r)" % self.hunks
50
51
    def __eq__(self, other):
52
        if self.__class__ is not other.__class__:
53
            return False
54
        return (self.hunks == other.hunks)
0.9.1 by Aaron Bentley
Get trivial case passing
55
56
    @staticmethod
57
    def from_lines(text, parents=()):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
58
        """Produce a MultiParent from a list of lines and parents"""
0.9.2 by Aaron Bentley
Get single-parent comparison working
59
        def compare(parent):
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
60
            matcher = patiencediff.PatienceSequenceMatcher(None, parent,
61
                                                           text)
62
            return matcher.get_matching_blocks()
0.9.2 by Aaron Bentley
Get single-parent comparison working
63
        parent_comparisons = [compare(p) for p in parents]
64
        cur_line = 0
65
        new_text = NewText([])
66
        parent_text = []
67
        block_iter = [iter(i) for i in parent_comparisons]
68
        diff = MultiParent([])
69
        def next_block(p):
70
            try:
71
                return block_iter[p].next()
72
            except StopIteration:
73
                return None
74
        cur_block = [next_block(p) for p, i in enumerate(block_iter)]
75
        while cur_line < len(text):
76
            best_match = None
77
            for p, block in enumerate(cur_block):
78
                if block is None:
79
                    continue
80
                i, j, n = block
81
                while j + n < cur_line:
82
                    block = cur_block[p] = next_block(p)
83
                    if block is None:
84
                        break
85
                    i, j, n = block
86
                if block is None:
87
                    continue
88
                if j > cur_line:
89
                    continue
90
                offset = cur_line - j
91
                i += offset
92
                j = cur_line
93
                n -= offset
94
                if n == 0:
95
                    continue
96
                if best_match is None or n > best_match.num_lines:
97
                    best_match = ParentText(p, i, j, n)
98
            if best_match is None:
99
                new_text.lines.append(text[cur_line])
100
                cur_line += 1
101
            else:
102
                if len(new_text.lines) > 0:
103
                    diff.hunks.append(new_text)
104
                    new_text = NewText([])
105
                diff.hunks.append(best_match)
106
                cur_line += best_match.num_lines
107
        if len(new_text.lines) > 0:
108
            diff.hunks.append(new_text)
0.9.1 by Aaron Bentley
Get trivial case passing
109
        return diff
110
111
    @classmethod
112
    def from_texts(cls, text, parents=()):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
113
        """Produce a MultiParent from a text and list of parent text"""
0.9.1 by Aaron Bentley
Get trivial case passing
114
        return cls.from_lines(text.splitlines(True),
115
                              [p.splitlines(True) for p in parents])
116
0.9.4 by Aaron Bentley
Start supporting serialization
117
    def to_patch(self):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
118
        """Yield text lines for a patch"""
0.9.4 by Aaron Bentley
Start supporting serialization
119
        for hunk in self.hunks:
120
            for line in hunk.to_patch():
121
                yield line
122
0.9.25 by Aaron Bentley
More messy hacking
123
    def patch_len(self):
124
        return len(''.join(self.to_patch()))
125
126
    def zipped_patch_len(self):
127
        return len(gzip_string(self.to_patch()))
128
0.9.18 by Aaron Bentley
Implement from_patch
129
    @staticmethod
130
    def from_patch(lines):
0.9.19 by Aaron Bentley
More tweakage
131
        """Produce a MultiParent from a sequence of lines"""
0.9.18 by Aaron Bentley
Implement from_patch
132
        line_iter = iter(lines)
133
        hunks = []
134
        cur_line = None
135
        while(True):
136
            try:
137
                cur_line = line_iter.next()
138
            except StopIteration:
139
                break
140
            if cur_line[0] == 'i':
141
                num_lines = int(cur_line.split(' ')[1])
142
                hunk_lines = [line_iter.next() for x in xrange(num_lines)]
143
                hunk_lines[-1] = hunk_lines[-1][:-1]
144
                hunks.append(NewText(hunk_lines))
145
            elif cur_line[0] == '\n':
146
                hunks[-1].lines[-1] += '\n'
147
            else:
148
                assert cur_line[0] == 'c', cur_line[0]
149
                parent, parent_pos, child_pos, num_lines =\
150
                    [int(v) for v in cur_line.split(' ')[1:]]
151
                hunks.append(ParentText(parent, parent_pos, child_pos,
152
                                        num_lines))
153
        return MultiParent(hunks)
154
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
155
    def range_iterator(self):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
156
        """Iterate through the hunks, with range indicated
157
158
        kind is "new" or "parent".
159
        for "new", data is a list of lines.
160
        for "parent", data is (parent, parent_start, parent_end)
161
        :return: a generator of (start, end, kind, data)
162
        """
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
163
        start = 0
164
        for hunk in self.hunks:
165
            if isinstance(hunk, NewText):
166
                kind = 'new'
167
                end = start + len(hunk.lines)
168
                data = hunk.lines
169
            else:
170
                kind = 'parent'
171
                start = hunk.child_pos
172
                end = start + hunk.num_lines
173
                data = (hunk.parent, hunk.parent_pos, hunk.parent_pos +
174
                        hunk.num_lines)
175
            yield start, end, kind, data
176
            start = end
177
0.9.11 by Aaron Bentley
Implement reconstruct_version, handle all hunks through that
178
    def num_lines(self):
179
        extra_n = 0
180
        for hunk in reversed(self.hunks):
181
            if isinstance(hunk, ParentText):
182
               return hunk.child_pos + hunk.num_lines + extra_n
183
            extra_n += len(hunk.lines)
184
        return extra_n
185
0.9.25 by Aaron Bentley
More messy hacking
186
    def is_snapshot(self):
187
        if len(self.hunks) != 1:
188
            return False
189
        return (isinstance(self.hunks[0], NewText))
190
0.9.1 by Aaron Bentley
Get trivial case passing
191
192
class NewText(object):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
193
    """The contents of text that is introduced by this text"""
0.9.1 by Aaron Bentley
Get trivial case passing
194
195
    def __init__(self, lines):
196
        self.lines = lines
197
198
    def __eq__(self, other):
199
        if self.__class__ is not other.__class__:
200
            return False
201
        return (other.lines == self.lines)
0.9.2 by Aaron Bentley
Get single-parent comparison working
202
203
    def __repr__(self):
204
        return 'NewText(%r)' % self.lines
205
0.9.4 by Aaron Bentley
Start supporting serialization
206
    def to_patch(self):
207
        yield 'i %d\n' % len(self.lines)
208
        for line in self.lines:
209
            yield line
210
        yield '\n'
211
0.9.2 by Aaron Bentley
Get single-parent comparison working
212
213
class ParentText(object):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
214
    """A reference to text present in a parent text"""
0.9.2 by Aaron Bentley
Get single-parent comparison working
215
216
    def __init__(self, parent, parent_pos, child_pos, num_lines):
217
        self.parent = parent
218
        self.parent_pos = parent_pos
219
        self.child_pos = child_pos
220
        self.num_lines = num_lines
221
222
    def __repr__(self):
223
        return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
224
            ' %(num_lines)r)' % self.__dict__
225
226
    def __eq__(self, other):
227
        if self.__class__ != other.__class__:
228
            return False
229
        return (self.__dict__ == other.__dict__)
0.9.4 by Aaron Bentley
Start supporting serialization
230
231
    def to_patch(self):
232
        yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\
233
            % self.__dict__
0.9.8 by Aaron Bentley
get add_version working
234
235
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
236
class BaseVersionedFile(object):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
237
    """VersionedFile skeleton for MultiParent"""
0.9.8 by Aaron Bentley
get add_version working
238
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
239
    def __init__(self, snapshot_interval=25, max_snapshots=None):
0.9.8 by Aaron Bentley
get add_version working
240
        self._lines = {}
241
        self._parents = {}
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
242
        self._snapshots = set()
0.9.12 by Aaron Bentley
Make benchmarks for mp
243
        self.snapshot_interval = snapshot_interval
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
244
        self.max_snapshots = max_snapshots
0.9.12 by Aaron Bentley
Make benchmarks for mp
245
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
246
    def versions(self):
247
        return iter(self._parents)
248
0.9.12 by Aaron Bentley
Make benchmarks for mp
249
    def do_snapshot(self, version_id, parent_ids):
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
250
        if self.snapshot_interval is None:
251
            return False
252
        if self.max_snapshots is not None and\
253
            len(self._snapshots) == self.max_snapshots:
0.9.14 by Aaron Bentley
Temporarily force snapshots to 44
254
            return False
0.9.12 by Aaron Bentley
Make benchmarks for mp
255
        if len(parent_ids) == 0:
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
256
            return True
257
        for ignored in xrange(self.snapshot_interval):
0.9.12 by Aaron Bentley
Make benchmarks for mp
258
            if len(parent_ids) == 0:
259
                return False
0.9.17 by Aaron Bentley
Dynamically select snapshots based on all parents
260
            version_ids = parent_ids
261
            parent_ids = []
262
            for version_id in version_ids:
263
                if version_id not in self._snapshots:
264
                    parent_ids.extend(self._parents[version_id])
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
265
        else:
266
            return True
0.9.8 by Aaron Bentley
get add_version working
267
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
268
    def add_version(self, lines, version_id, parent_ids,
0.9.20 by Aaron Bentley
Convert to a plugin
269
                    force_snapshot=None, single_parent=False):
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
270
        if force_snapshot is None:
271
            do_snapshot = self.do_snapshot(version_id, parent_ids)
272
        else:
273
            do_snapshot = force_snapshot
274
        if do_snapshot:
275
            self._snapshots.add(version_id)
0.9.12 by Aaron Bentley
Make benchmarks for mp
276
            diff = MultiParent([NewText(lines)])
277
        else:
0.9.20 by Aaron Bentley
Convert to a plugin
278
            if single_parent:
279
                parent_lines = self.get_line_list(parent_ids[:1])
280
            else:
281
                parent_lines = self.get_line_list(parent_ids)
0.9.12 by Aaron Bentley
Make benchmarks for mp
282
            diff = MultiParent.from_lines(lines, parent_lines)
0.9.25 by Aaron Bentley
More messy hacking
283
            if diff.is_snapshot():
284
                self._snapshots.add(version_id)
0.9.8 by Aaron Bentley
get add_version working
285
        self.add_diff(diff, version_id, parent_ids)
286
        self._lines[version_id] = lines
287
0.9.35 by Aaron Bentley
Add build ranking
288
    def get_parents(self, version_id):
289
        return self._parents[version_id]
290
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
291
    def make_snapshot(self, version_id):
292
        snapdiff = MultiParent([NewText(self.cache_version(version_id))])
293
        self._snapshots.add(version_id)
294
0.9.20 by Aaron Bentley
Convert to a plugin
295
    def import_versionedfile(self, vf, snapshots, no_cache=True,
0.9.22 by Aaron Bentley
Fix restoration bug
296
                             single_parent=False, verify=False):
0.9.20 by Aaron Bentley
Convert to a plugin
297
        """Import all revisions of a versionedfile
298
299
        :param vf: The versionedfile to import
300
        :param snapshots: If provided, the revisions to make snapshots of.
301
            Otherwise, this will be auto-determined
302
        :param no_cache: If true, clear the cache after every add.
303
        :param single_parent: If true, omit all but one parent text, (but
304
            retain parent metadata).
305
        """
0.9.22 by Aaron Bentley
Fix restoration bug
306
        assert no_cache or not verify
0.9.19 by Aaron Bentley
More tweakage
307
        revisions = set(vf.versions())
308
        total = len(revisions)
0.9.20 by Aaron Bentley
Convert to a plugin
309
        pb = ui.ui_factory.nested_progress_bar()
310
        try:
311
            while len(revisions) > 0:
312
                added = set()
313
                for revision in revisions:
314
                    parents = vf.get_parents(revision)
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
315
                    if [p for p in parents if p not in self._parents] != []:
0.9.20 by Aaron Bentley
Convert to a plugin
316
                        continue
317
                    lines = [a + ' ' + l for a, l in
318
                             vf.annotate_iter(revision)]
0.9.21 by Aaron Bentley
finish converting ft_ to snapshots
319
                    if snapshots is None:
0.9.20 by Aaron Bentley
Convert to a plugin
320
                        force_snapshot = None
321
                    else:
0.9.21 by Aaron Bentley
finish converting ft_ to snapshots
322
                        force_snapshot = (revision in snapshots)
0.9.20 by Aaron Bentley
Convert to a plugin
323
                    self.add_version(lines, revision, parents, force_snapshot,
324
                                     single_parent)
325
                    added.add(revision)
326
                    if no_cache:
327
                        self.clear_cache()
0.9.25 by Aaron Bentley
More messy hacking
328
                        vf.clear_cache()
0.9.22 by Aaron Bentley
Fix restoration bug
329
                        if verify:
330
                            assert lines == self.get_line_list([revision])[0]
331
                            self.clear_cache()
0.9.20 by Aaron Bentley
Convert to a plugin
332
                    pb.update('Importing revisions',
333
                              (total - len(revisions)) + len(added), total)
334
                revisions = [r for r in revisions if r not in added]
335
        finally:
336
            pb.finished()
0.9.19 by Aaron Bentley
More tweakage
337
0.9.23 by Aaron Bentley
handle snapshots all at once
338
    def select_snapshots(self, vf):
0.9.28 by Aaron Bentley
Update snapshot-picking to use sets of ancestors
339
        build_ancestors = {}
0.9.23 by Aaron Bentley
handle snapshots all at once
340
        descendants = {}
341
        snapshots = set()
0.9.26 by Aaron Bentley
Move topological iteration into an iterator
342
        for version_id in topo_iter(vf):
0.9.28 by Aaron Bentley
Update snapshot-picking to use sets of ancestors
343
            potential_build_ancestors = set(vf.get_parents(version_id))
344
            parents = vf.get_parents(version_id)
345
            if len(parents) == 0:
0.9.26 by Aaron Bentley
Move topological iteration into an iterator
346
                snapshots.add(version_id)
0.9.28 by Aaron Bentley
Update snapshot-picking to use sets of ancestors
347
                build_ancestors[version_id] = set()
0.9.26 by Aaron Bentley
Move topological iteration into an iterator
348
            else:
0.9.28 by Aaron Bentley
Update snapshot-picking to use sets of ancestors
349
                for parent in vf.get_parents(version_id):
350
                    potential_build_ancestors.update(build_ancestors[parent])
351
                if len(potential_build_ancestors) > self.snapshot_interval:
352
                    snapshots.add(version_id)
353
                    build_ancestors[version_id] = set()
0.9.23 by Aaron Bentley
handle snapshots all at once
354
                else:
0.9.28 by Aaron Bentley
Update snapshot-picking to use sets of ancestors
355
                    build_ancestors[version_id] = potential_build_ancestors
0.9.23 by Aaron Bentley
handle snapshots all at once
356
        return snapshots
357
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
358
    def select_by_size(self, num):
0.9.35 by Aaron Bentley
Add build ranking
359
        """Select snapshots for minimum output size"""
360
        num -= len(self._snapshots)
361
        return get_size_ranking()[:num]
362
363
    def get_size_ranking(self):
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
364
        versions = []
365
        new_snapshots = set()
366
        for version_id in self.versions():
367
            if version_id in self._snapshots:
368
                continue
369
            diff_len = self.get_diff(version_id).patch_len()
370
            snapshot_len = MultiParent([NewText(
371
                self.cache_version(version_id))]).patch_len()
372
            versions.append((diff_len - snapshot_len, version_id))
373
        versions.sort()
374
        return [v for n, v in versions[:num]]
0.9.23 by Aaron Bentley
handle snapshots all at once
375
0.9.35 by Aaron Bentley
Add build ranking
376
    def get_build_ranking(self):
377
        could_avoid = {}
378
        referenced_by = {}
379
        for version_id in topo_iter(self):
380
            could_avoid[version_id] = set()
381
            if version_id not in self._snapshots:
382
                for parent_id in self._parents[version_id]:
383
                    could_avoid[version_id].update(could_avoid[parent_id])
384
                could_avoid[version_id].update(self._parents)
385
                could_avoid[version_id].discard(version_id)
386
            for avoid_id in could_avoid[version_id]:
387
                referenced_by.setdefault(avoid_id, set()).add(version_id)
388
        available_versions = list(self.versions())
389
        ranking = []
390
        while len(available_versions) > 0:
391
            available_versions.sort(key=lambda x:
392
                len(could_avoid[x]) *
393
                len(referenced_by.get(x, [])))
394
            selected = available_versions.pop()
395
            ranking.append(selected)
396
            for version_id in referenced_by[selected]:
397
                could_avoid[version_id].difference_update(
398
                    could_avoid[selected])
399
            for version_id in could_avoid[selected]:
400
                referenced_by[version_id].difference_update(
401
                    referenced_by[selected]
402
                )
403
        return ranking
404
0.9.8 by Aaron Bentley
get add_version working
405
    def clear_cache(self):
406
        self._lines.clear()
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
407
408
    def get_line_list(self, version_ids):
409
        return [self.cache_version(v) for v in version_ids]
410
411
    def cache_version(self, version_id):
412
        try:
413
            return self._lines[version_id]
414
        except KeyError:
415
            pass
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
416
        diff = self.get_diff(version_id)
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
417
        lines = []
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
418
        reconstructor = _Reconstructor(self, self._lines,
0.9.17 by Aaron Bentley
Dynamically select snapshots based on all parents
419
                                       self._parents)
0.9.11 by Aaron Bentley
Implement reconstruct_version, handle all hunks through that
420
        reconstructor.reconstruct_version(lines, version_id)
0.9.33 by Aaron Bentley
Enable caching commandline param
421
        self._lines[version_id] = lines
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
422
        return lines
423
0.9.33 by Aaron Bentley
Enable caching commandline param
424
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
425
class MultiMemoryVersionedFile(BaseVersionedFile):
426
427
    def __init__(self, snapshot_interval=25, max_snapshots=None):
428
        BaseVersionedFile.__init__(self, snapshot_interval, max_snapshots)
429
        self._diffs = {}
430
431
    def add_diff(self, diff, version_id, parent_ids):
432
        self._diffs[version_id] = diff
433
        self._parents[version_id] = parent_ids
434
435
    def get_diff(self, version_id):
436
        return self._diffs[version_id]
437
0.9.31 by Aaron Bentley
Allow selecting MemoryVersionedFile from commandline
438
    def destroy(self):
439
        self._diffs = {}
440
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
441
442
class MultiVersionedFile(BaseVersionedFile):
443
444
    def __init__(self, filename, snapshot_interval=25, max_snapshots=None):
445
        BaseVersionedFile.__init__(self, snapshot_interval, max_snapshots)
446
        self._filename = filename
447
        self._diff_offset = {}
448
449
    def get_diff(self, version_id):
450
        start, count = self._diff_offset[version_id]
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
451
        infile = open(self._filename + '.mpknit', 'rb')
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
452
        try:
453
            infile.seek(start)
454
            sio = StringIO(infile.read(count))
455
        finally:
456
            infile.close()
457
        zip_file = GzipFile(None, mode='rb', fileobj=sio)
458
        try:
459
            file_version_id = zip_file.readline()
460
            return MultiParent.from_patch(zip_file.readlines())
461
        finally:
462
            zip_file.close()
463
464
    def add_diff(self, diff, version_id, parent_ids):
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
465
        outfile = open(self._filename + '.mpknit', 'ab')
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
466
        try:
467
            start = outfile.tell()
468
            try:
469
                zipfile = GzipFile(None, mode='ab', fileobj=outfile)
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
470
                zipfile.writelines(itertools.chain(
471
                    ['version %s\n' % version_id], diff.to_patch()))
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
472
            finally:
473
                zipfile.close()
474
            end = outfile.tell()
475
        finally:
476
            outfile.close()
477
        self._diff_offset[version_id] = (start, end-start)
478
        self._parents[version_id] = parent_ids
479
0.9.31 by Aaron Bentley
Allow selecting MemoryVersionedFile from commandline
480
    def destroy(self):
481
        try:
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
482
            os.unlink(self._filename + '.mpknit')
483
        except OSError, e:
484
            if e.errno != errno.ENOENT:
485
                raise
486
        try:
487
            os.unlink(self._filename + '.mpidx')
488
        except OSError, e:
489
            if e.errno != errno.ENOENT:
490
                raise
491
492
    def save(self):
493
        open(self._filename + '.mpidx', 'wb').write(bencode.bencode(
494
            (self._parents, list(self._snapshots), self._diff_offset)))
495
496
    def load(self):
497
        self._parents, snapshots, self._diff_offset = bencode.bdecode(
498
            open(self._filename + '.mpidx', 'rb').read())
499
        self._snapshots = set(snapshots)
0.9.31 by Aaron Bentley
Allow selecting MemoryVersionedFile from commandline
500
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
501
502
class _Reconstructor(object):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
503
    """Build a text from the diffs, ancestry graph and cached lines"""
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
504
505
    def __init__(self, diffs, lines, parents):
506
        self.diffs = diffs
507
        self.lines = lines
508
        self.parents = parents
509
        self.cursor = {}
510
511
    def reconstruct(self, lines, parent_text, version_id):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
512
        """Append the lines referred to by a ParentText to lines"""
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
513
        parent_id = self.parents[version_id][parent_text.parent]
514
        end = parent_text.parent_pos + parent_text.num_lines
0.9.17 by Aaron Bentley
Dynamically select snapshots based on all parents
515
        return self._reconstruct(lines, parent_id, parent_text.parent_pos,
516
                                 end)
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
517
518
    def _reconstruct(self, lines, req_version_id, req_start, req_end):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
519
        """Append lines for the requested version_id range"""
520
        # stack of pending range requests
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
521
        pending_reqs = [(req_version_id, req_start, req_end)]
522
        while len(pending_reqs) > 0:
523
            req_version_id, req_start, req_end = pending_reqs.pop()
0.9.10 by Aaron Bentley
Text reconstruction seems to work
524
            # lazily allocate cursors for versions
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
525
            try:
526
                start, end, kind, data, iterator = self.cursor[req_version_id]
527
            except KeyError:
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
528
                iterator = self.diffs.get_diff(req_version_id).range_iterator()
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
529
                start, end, kind, data = iterator.next()
0.9.22 by Aaron Bentley
Fix restoration bug
530
            if start > req_start:
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
531
                iterator = self.diffs.get_diff(req_version_id).range_iterator()
0.9.22 by Aaron Bentley
Fix restoration bug
532
                start, end, kind, data = iterator.next()
533
0.9.10 by Aaron Bentley
Text reconstruction seems to work
534
            # find the first hunk relevant to the request
535
            while end <= req_start:
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
536
                start, end, kind, data = iterator.next()
537
            self.cursor[req_version_id] = start, end, kind, data, iterator
0.9.10 by Aaron Bentley
Text reconstruction seems to work
538
            # if the hunk can't satisfy the whole request, split it in two,
539
            # and leave the second half for later.
540
            if req_end > end:
541
                pending_reqs.append((req_version_id, end, req_end))
542
                req_end = end
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
543
            if kind == 'new':
544
                lines.extend(data[req_start - start: (req_end - start)])
545
            else:
0.9.10 by Aaron Bentley
Text reconstruction seems to work
546
                # If the hunk is a ParentText, rewrite it as a range request
547
                # for the parent, and make it the next pending request.
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
548
                parent, parent_start, parent_end = data
0.9.10 by Aaron Bentley
Text reconstruction seems to work
549
                new_version_id = self.parents[req_version_id][parent]
550
                new_start = parent_start + req_start - start
551
                new_end = parent_end + req_end - end
552
                pending_reqs.append((new_version_id, new_start, new_end))
0.9.11 by Aaron Bentley
Implement reconstruct_version, handle all hunks through that
553
554
    def reconstruct_version(self, lines, version_id):
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
555
        length = self.diffs.get_diff(version_id).num_lines()
0.9.11 by Aaron Bentley
Implement reconstruct_version, handle all hunks through that
556
        return self._reconstruct(lines, version_id, 0, length)
0.9.25 by Aaron Bentley
More messy hacking
557
558
def gzip_string(lines):
559
    sio = StringIO()
560
    data_file = GzipFile(None, mode='wb', fileobj=sio)
561
    data_file.writelines(lines)
562
    data_file.close()
563
    return sio.getvalue()