/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/multiparent.py

  • Committer: Jelmer Vernooij
  • Date: 2018-05-07 15:27:39 UTC
  • mto: This revision was merged to the branch mainline in revision 6958.
  • Revision ID: jelmer@jelmer.uk-20180507152739-fuv9z9r0yzi7ln3t
Specify source in .coveragerc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
from __future__ import absolute_import
 
18
 
 
19
import errno
 
20
import os
 
21
 
 
22
from .lazy_import import lazy_import
 
23
 
 
24
lazy_import(globals(), """
 
25
import gzip
 
26
import itertools
 
27
 
 
28
from breezy import (
 
29
    bencode,
 
30
    errors,
 
31
    patiencediff,
 
32
    ui,
 
33
    )
 
34
""")
 
35
from .sixish import (
 
36
    BytesIO,
 
37
    range,
 
38
    )
 
39
 
 
40
 
 
41
def topo_iter_keys(vf, keys=None):
 
42
    if keys is None:
 
43
        keys = vf.keys()
 
44
    parents = vf.get_parent_map(keys)
 
45
    return _topo_iter(parents, keys)
 
46
 
 
47
def topo_iter(vf, versions=None):
 
48
    if versions is None:
 
49
        versions = vf.versions()
 
50
    parents = vf.get_parent_map(versions)
 
51
    return _topo_iter(parents, versions)
 
52
 
 
53
def _topo_iter(parents, versions):
 
54
    seen = set()
 
55
    descendants = {}
 
56
    def pending_parents(version):
 
57
        if parents[version] is None:
 
58
            return []
 
59
        return [v for v in parents[version] if v in versions and
 
60
                v not in seen]
 
61
    for version_id in versions:
 
62
        if parents[version_id] is None:
 
63
            # parentless
 
64
            continue
 
65
        for parent_id in parents[version_id]:
 
66
            descendants.setdefault(parent_id, []).append(version_id)
 
67
    cur = [v for v in versions if len(pending_parents(v)) == 0]
 
68
    while len(cur) > 0:
 
69
        next = []
 
70
        for version_id in cur:
 
71
            if version_id in seen:
 
72
                continue
 
73
            if len(pending_parents(version_id)) != 0:
 
74
                continue
 
75
            next.extend(descendants.get(version_id, []))
 
76
            yield version_id
 
77
            seen.add(version_id)
 
78
        cur = next
 
79
 
 
80
 
 
81
class MultiParent(object):
 
82
    """A multi-parent diff"""
 
83
 
 
84
    __slots__ = ['hunks']
 
85
 
 
86
    def __init__(self, hunks=None):
 
87
        if hunks is not None:
 
88
            self.hunks = hunks
 
89
        else:
 
90
            self.hunks = []
 
91
 
 
92
    def __repr__(self):
 
93
        return "MultiParent(%r)" % self.hunks
 
94
 
 
95
    def __eq__(self, other):
 
96
        if self.__class__ is not other.__class__:
 
97
            return False
 
98
        return (self.hunks == other.hunks)
 
99
 
 
100
    @staticmethod
 
101
    def from_lines(text, parents=(), left_blocks=None):
 
102
        """Produce a MultiParent from a list of lines and parents"""
 
103
        def compare(parent):
 
104
            matcher = patiencediff.PatienceSequenceMatcher(None, parent,
 
105
                                                           text)
 
106
            return matcher.get_matching_blocks()
 
107
        if len(parents) > 0:
 
108
            if left_blocks is None:
 
109
                left_blocks = compare(parents[0])
 
110
            parent_comparisons = [left_blocks] + [compare(p) for p in
 
111
                                                  parents[1:]]
 
112
        else:
 
113
            parent_comparisons = []
 
114
        cur_line = 0
 
115
        new_text = NewText([])
 
116
        parent_text = []
 
117
        block_iter = [iter(i) for i in parent_comparisons]
 
118
        diff = MultiParent([])
 
119
        def next_block(p):
 
120
            try:
 
121
                return next(block_iter[p])
 
122
            except StopIteration:
 
123
                return None
 
124
        cur_block = [next_block(p) for p, i in enumerate(block_iter)]
 
125
        while cur_line < len(text):
 
126
            best_match = None
 
127
            for p, block in enumerate(cur_block):
 
128
                if block is None:
 
129
                    continue
 
130
                i, j, n = block
 
131
                while j + n <= cur_line:
 
132
                    block = cur_block[p] = next_block(p)
 
133
                    if block is None:
 
134
                        break
 
135
                    i, j, n = block
 
136
                if block is None:
 
137
                    continue
 
138
                if j > cur_line:
 
139
                    continue
 
140
                offset = cur_line - j
 
141
                i += offset
 
142
                j = cur_line
 
143
                n -= offset
 
144
                if n == 0:
 
145
                    continue
 
146
                if best_match is None or n > best_match.num_lines:
 
147
                    best_match = ParentText(p, i, j, n)
 
148
            if best_match is None:
 
149
                new_text.lines.append(text[cur_line])
 
150
                cur_line += 1
 
151
            else:
 
152
                if len(new_text.lines) > 0:
 
153
                    diff.hunks.append(new_text)
 
154
                    new_text = NewText([])
 
155
                diff.hunks.append(best_match)
 
156
                cur_line += best_match.num_lines
 
157
        if len(new_text.lines) > 0:
 
158
            diff.hunks.append(new_text)
 
159
        return diff
 
160
 
 
161
    def get_matching_blocks(self, parent, parent_len):
 
162
        for hunk in self.hunks:
 
163
            if not isinstance(hunk, ParentText) or hunk.parent != parent:
 
164
                continue
 
165
            yield (hunk.parent_pos, hunk.child_pos, hunk.num_lines)
 
166
        yield parent_len, self.num_lines(), 0
 
167
 
 
168
    def to_lines(self, parents=()):
 
169
        """Contruct a fulltext from this diff and its parents"""
 
170
        mpvf = MultiMemoryVersionedFile()
 
171
        for num, parent in enumerate(parents):
 
172
            mpvf.add_version(BytesIO(parent).readlines(), num, [])
 
173
        mpvf.add_diff(self, 'a', list(range(len(parents))))
 
174
        return mpvf.get_line_list(['a'])[0]
 
175
 
 
176
    @classmethod
 
177
    def from_texts(cls, text, parents=()):
 
178
        """Produce a MultiParent from a text and list of parent text"""
 
179
        return cls.from_lines(BytesIO(text).readlines(),
 
180
                              [BytesIO(p).readlines() for p in parents])
 
181
 
 
182
    def to_patch(self):
 
183
        """Yield text lines for a patch"""
 
184
        for hunk in self.hunks:
 
185
            for line in hunk.to_patch():
 
186
                yield line
 
187
 
 
188
    def patch_len(self):
 
189
        return len(''.join(self.to_patch()))
 
190
 
 
191
    def zipped_patch_len(self):
 
192
        return len(gzip_string(self.to_patch()))
 
193
 
 
194
    @classmethod
 
195
    def from_patch(cls, text):
 
196
        """Create a MultiParent from its string form"""
 
197
        return cls._from_patch(BytesIO(text))
 
198
 
 
199
    @staticmethod
 
200
    def _from_patch(lines):
 
201
        """This is private because it is essential to split lines on \n only"""
 
202
        line_iter = iter(lines)
 
203
        hunks = []
 
204
        cur_line = None
 
205
        while(True):
 
206
            try:
 
207
                cur_line = next(line_iter)
 
208
            except StopIteration:
 
209
                break
 
210
            if cur_line[0] == 'i':
 
211
                num_lines = int(cur_line.split(' ')[1])
 
212
                hunk_lines = [next(line_iter) for _ in range(num_lines)]
 
213
                hunk_lines[-1] = hunk_lines[-1][:-1]
 
214
                hunks.append(NewText(hunk_lines))
 
215
            elif cur_line[0] == '\n':
 
216
                hunks[-1].lines[-1] += '\n'
 
217
            else:
 
218
                if not (cur_line[0] == 'c'):
 
219
                    raise AssertionError(cur_line[0])
 
220
                parent, parent_pos, child_pos, num_lines =\
 
221
                    [int(v) for v in cur_line.split(' ')[1:]]
 
222
                hunks.append(ParentText(parent, parent_pos, child_pos,
 
223
                                        num_lines))
 
224
        return MultiParent(hunks)
 
225
 
 
226
    def range_iterator(self):
 
227
        """Iterate through the hunks, with range indicated
 
228
 
 
229
        kind is "new" or "parent".
 
230
        for "new", data is a list of lines.
 
231
        for "parent", data is (parent, parent_start, parent_end)
 
232
        :return: a generator of (start, end, kind, data)
 
233
        """
 
234
        start = 0
 
235
        for hunk in self.hunks:
 
236
            if isinstance(hunk, NewText):
 
237
                kind = 'new'
 
238
                end = start + len(hunk.lines)
 
239
                data = hunk.lines
 
240
            else:
 
241
                kind = 'parent'
 
242
                start = hunk.child_pos
 
243
                end = start + hunk.num_lines
 
244
                data = (hunk.parent, hunk.parent_pos, hunk.parent_pos +
 
245
                        hunk.num_lines)
 
246
            yield start, end, kind, data
 
247
            start = end
 
248
 
 
249
    def num_lines(self):
 
250
        """The number of lines in the output text"""
 
251
        extra_n = 0
 
252
        for hunk in reversed(self.hunks):
 
253
            if isinstance(hunk, ParentText):
 
254
               return hunk.child_pos + hunk.num_lines + extra_n
 
255
            extra_n += len(hunk.lines)
 
256
        return extra_n
 
257
 
 
258
    def is_snapshot(self):
 
259
        """Return true of this hunk is effectively a fulltext"""
 
260
        if len(self.hunks) != 1:
 
261
            return False
 
262
        return (isinstance(self.hunks[0], NewText))
 
263
 
 
264
 
 
265
class NewText(object):
 
266
    """The contents of text that is introduced by this text"""
 
267
 
 
268
    __slots__ = ['lines']
 
269
 
 
270
    def __init__(self, lines):
 
271
        self.lines = lines
 
272
 
 
273
    def __eq__(self, other):
 
274
        if self.__class__ is not other.__class__:
 
275
            return False
 
276
        return (other.lines == self.lines)
 
277
 
 
278
    def __repr__(self):
 
279
        return 'NewText(%r)' % self.lines
 
280
 
 
281
    def to_patch(self):
 
282
        yield 'i %d\n' % len(self.lines)
 
283
        for line in self.lines:
 
284
            yield line
 
285
        yield '\n'
 
286
 
 
287
 
 
288
class ParentText(object):
 
289
    """A reference to text present in a parent text"""
 
290
 
 
291
    __slots__ = ['parent', 'parent_pos', 'child_pos', 'num_lines']
 
292
 
 
293
    def __init__(self, parent, parent_pos, child_pos, num_lines):
 
294
        self.parent = parent
 
295
        self.parent_pos = parent_pos
 
296
        self.child_pos = child_pos
 
297
        self.num_lines = num_lines
 
298
 
 
299
    def _as_dict(self):
 
300
        return dict(parent=self.parent, parent_pos=self.parent_pos,
 
301
                    child_pos=self.child_pos, num_lines=self.num_lines)
 
302
 
 
303
    def __repr__(self):
 
304
        return ('ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'
 
305
                ' %(num_lines)r)' % self._as_dict())
 
306
 
 
307
    def __eq__(self, other):
 
308
        if self.__class__ is not other.__class__:
 
309
            return False
 
310
        return self._as_dict() == other._as_dict()
 
311
 
 
312
    def to_patch(self):
 
313
        yield ('c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'
 
314
               % self._as_dict())
 
315
 
 
316
 
 
317
class BaseVersionedFile(object):
 
318
    """Pseudo-VersionedFile skeleton for MultiParent"""
 
319
 
 
320
    def __init__(self, snapshot_interval=25, max_snapshots=None):
 
321
        self._lines = {}
 
322
        self._parents = {}
 
323
        self._snapshots = set()
 
324
        self.snapshot_interval = snapshot_interval
 
325
        self.max_snapshots = max_snapshots
 
326
 
 
327
    def versions(self):
 
328
        return iter(self._parents)
 
329
 
 
330
    def has_version(self, version):
 
331
        return version in self._parents
 
332
 
 
333
    def do_snapshot(self, version_id, parent_ids):
 
334
        """Determine whether to perform a snapshot for this version"""
 
335
        if self.snapshot_interval is None:
 
336
            return False
 
337
        if self.max_snapshots is not None and\
 
338
            len(self._snapshots) == self.max_snapshots:
 
339
            return False
 
340
        if len(parent_ids) == 0:
 
341
            return True
 
342
        for ignored in range(self.snapshot_interval):
 
343
            if len(parent_ids) == 0:
 
344
                return False
 
345
            version_ids = parent_ids
 
346
            parent_ids = []
 
347
            for version_id in version_ids:
 
348
                if version_id not in self._snapshots:
 
349
                    parent_ids.extend(self._parents[version_id])
 
350
        else:
 
351
            return True
 
352
 
 
353
    def add_version(self, lines, version_id, parent_ids,
 
354
                    force_snapshot=None, single_parent=False):
 
355
        """Add a version to the versionedfile
 
356
 
 
357
        :param lines: The list of lines to add.  Must be split on '\n'.
 
358
        :param version_id: The version_id of the version to add
 
359
        :param force_snapshot: If true, force this version to be added as a
 
360
            snapshot version.  If false, force this version to be added as a
 
361
            diff.  If none, determine this automatically.
 
362
        :param single_parent: If true, use a single parent, rather than
 
363
            multiple parents.
 
364
        """
 
365
        if force_snapshot is None:
 
366
            do_snapshot = self.do_snapshot(version_id, parent_ids)
 
367
        else:
 
368
            do_snapshot = force_snapshot
 
369
        if do_snapshot:
 
370
            self._snapshots.add(version_id)
 
371
            diff = MultiParent([NewText(lines)])
 
372
        else:
 
373
            if single_parent:
 
374
                parent_lines = self.get_line_list(parent_ids[:1])
 
375
            else:
 
376
                parent_lines = self.get_line_list(parent_ids)
 
377
            diff = MultiParent.from_lines(lines, parent_lines)
 
378
            if diff.is_snapshot():
 
379
                self._snapshots.add(version_id)
 
380
        self.add_diff(diff, version_id, parent_ids)
 
381
        self._lines[version_id] = lines
 
382
 
 
383
    def get_parents(self, version_id):
 
384
        return self._parents[version_id]
 
385
 
 
386
    def make_snapshot(self, version_id):
 
387
        snapdiff = MultiParent([NewText(self.cache_version(version_id))])
 
388
        self.add_diff(snapdiff, version_id, self._parents[version_id])
 
389
        self._snapshots.add(version_id)
 
390
 
 
391
    def import_versionedfile(self, vf, snapshots, no_cache=True,
 
392
                             single_parent=False, verify=False):
 
393
        """Import all revisions of a versionedfile
 
394
 
 
395
        :param vf: The versionedfile to import
 
396
        :param snapshots: If provided, the revisions to make snapshots of.
 
397
            Otherwise, this will be auto-determined
 
398
        :param no_cache: If true, clear the cache after every add.
 
399
        :param single_parent: If true, omit all but one parent text, (but
 
400
            retain parent metadata).
 
401
        """
 
402
        if not (no_cache or not verify):
 
403
            raise ValueError()
 
404
        revisions = set(vf.versions())
 
405
        total = len(revisions)
 
406
        with ui.ui_factory.nested_progress_bar() as pb:
 
407
            while len(revisions) > 0:
 
408
                added = set()
 
409
                for revision in revisions:
 
410
                    parents = vf.get_parents(revision)
 
411
                    if [p for p in parents if p not in self._parents] != []:
 
412
                        continue
 
413
                    lines = [a + ' ' + l for a, l in
 
414
                             vf.annotate(revision)]
 
415
                    if snapshots is None:
 
416
                        force_snapshot = None
 
417
                    else:
 
418
                        force_snapshot = (revision in snapshots)
 
419
                    self.add_version(lines, revision, parents, force_snapshot,
 
420
                                     single_parent)
 
421
                    added.add(revision)
 
422
                    if no_cache:
 
423
                        self.clear_cache()
 
424
                        vf.clear_cache()
 
425
                        if verify:
 
426
                            if not (lines == self.get_line_list([revision])[0]):
 
427
                                raise AssertionError()
 
428
                            self.clear_cache()
 
429
                    pb.update(gettext('Importing revisions'),
 
430
                              (total - len(revisions)) + len(added), total)
 
431
                revisions = [r for r in revisions if r not in added]
 
432
 
 
433
    def select_snapshots(self, vf):
 
434
        """Determine which versions to add as snapshots"""
 
435
        build_ancestors = {}
 
436
        descendants = {}
 
437
        snapshots = set()
 
438
        for version_id in topo_iter(vf):
 
439
            potential_build_ancestors = set(vf.get_parents(version_id))
 
440
            parents = vf.get_parents(version_id)
 
441
            if len(parents) == 0:
 
442
                snapshots.add(version_id)
 
443
                build_ancestors[version_id] = set()
 
444
            else:
 
445
                for parent in vf.get_parents(version_id):
 
446
                    potential_build_ancestors.update(build_ancestors[parent])
 
447
                if len(potential_build_ancestors) > self.snapshot_interval:
 
448
                    snapshots.add(version_id)
 
449
                    build_ancestors[version_id] = set()
 
450
                else:
 
451
                    build_ancestors[version_id] = potential_build_ancestors
 
452
        return snapshots
 
453
 
 
454
    def select_by_size(self, num):
 
455
        """Select snapshots for minimum output size"""
 
456
        num -= len(self._snapshots)
 
457
        new_snapshots = self.get_size_ranking()[-num:]
 
458
        return [v for n, v in new_snapshots]
 
459
 
 
460
    def get_size_ranking(self):
 
461
        """Get versions ranked by size"""
 
462
        versions = []
 
463
        new_snapshots = set()
 
464
        for version_id in self.versions():
 
465
            if version_id in self._snapshots:
 
466
                continue
 
467
            diff_len = self.get_diff(version_id).patch_len()
 
468
            snapshot_len = MultiParent([NewText(
 
469
                self.cache_version(version_id))]).patch_len()
 
470
            versions.append((snapshot_len - diff_len, version_id))
 
471
        versions.sort()
 
472
        return versions
 
473
 
 
474
    def import_diffs(self, vf):
 
475
        """Import the diffs from another pseudo-versionedfile"""
 
476
        for version_id in vf.versions():
 
477
            self.add_diff(vf.get_diff(version_id), version_id,
 
478
                          vf._parents[version_id])
 
479
 
 
480
    def get_build_ranking(self):
 
481
        """Return revisions sorted by how much they reduce build complexity"""
 
482
        could_avoid = {}
 
483
        referenced_by = {}
 
484
        for version_id in topo_iter(self):
 
485
            could_avoid[version_id] = set()
 
486
            if version_id not in self._snapshots:
 
487
                for parent_id in self._parents[version_id]:
 
488
                    could_avoid[version_id].update(could_avoid[parent_id])
 
489
                could_avoid[version_id].update(self._parents)
 
490
                could_avoid[version_id].discard(version_id)
 
491
            for avoid_id in could_avoid[version_id]:
 
492
                referenced_by.setdefault(avoid_id, set()).add(version_id)
 
493
        available_versions = list(self.versions())
 
494
        ranking = []
 
495
        while len(available_versions) > 0:
 
496
            available_versions.sort(key=lambda x:
 
497
                len(could_avoid[x]) *
 
498
                len(referenced_by.get(x, [])))
 
499
            selected = available_versions.pop()
 
500
            ranking.append(selected)
 
501
            for version_id in referenced_by[selected]:
 
502
                could_avoid[version_id].difference_update(
 
503
                    could_avoid[selected])
 
504
            for version_id in could_avoid[selected]:
 
505
                referenced_by[version_id].difference_update(
 
506
                    referenced_by[selected]
 
507
                )
 
508
        return ranking
 
509
 
 
510
    def clear_cache(self):
 
511
        self._lines.clear()
 
512
 
 
513
    def get_line_list(self, version_ids):
 
514
        return [self.cache_version(v) for v in version_ids]
 
515
 
 
516
    def cache_version(self, version_id):
 
517
        try:
 
518
            return self._lines[version_id]
 
519
        except KeyError:
 
520
            pass
 
521
        diff = self.get_diff(version_id)
 
522
        lines = []
 
523
        reconstructor = _Reconstructor(self, self._lines, self._parents)
 
524
        reconstructor.reconstruct_version(lines, version_id)
 
525
        self._lines[version_id] = lines
 
526
        return lines
 
527
 
 
528
 
 
529
class MultiMemoryVersionedFile(BaseVersionedFile):
 
530
    """Memory-backed pseudo-versionedfile"""
 
531
 
 
532
    def __init__(self, snapshot_interval=25, max_snapshots=None):
 
533
        BaseVersionedFile.__init__(self, snapshot_interval, max_snapshots)
 
534
        self._diffs = {}
 
535
 
 
536
    def add_diff(self, diff, version_id, parent_ids):
 
537
        self._diffs[version_id] = diff
 
538
        self._parents[version_id] = parent_ids
 
539
 
 
540
    def get_diff(self, version_id):
 
541
        try:
 
542
            return self._diffs[version_id]
 
543
        except KeyError:
 
544
            raise errors.RevisionNotPresent(version_id, self)
 
545
 
 
546
    def destroy(self):
 
547
        self._diffs = {}
 
548
 
 
549
 
 
550
class MultiVersionedFile(BaseVersionedFile):
 
551
    """Disk-backed pseudo-versionedfile"""
 
552
 
 
553
    def __init__(self, filename, snapshot_interval=25, max_snapshots=None):
 
554
        BaseVersionedFile.__init__(self, snapshot_interval, max_snapshots)
 
555
        self._filename = filename
 
556
        self._diff_offset = {}
 
557
 
 
558
    def get_diff(self, version_id):
 
559
        start, count = self._diff_offset[version_id]
 
560
        infile = open(self._filename + '.mpknit', 'rb')
 
561
        try:
 
562
            infile.seek(start)
 
563
            sio = BytesIO(infile.read(count))
 
564
        finally:
 
565
            infile.close()
 
566
        zip_file = gzip.GzipFile(None, mode='rb', fileobj=sio)
 
567
        try:
 
568
            file_version_id = zip_file.readline()
 
569
            content = zip_file.read()
 
570
            return MultiParent.from_patch(content)
 
571
        finally:
 
572
            zip_file.close()
 
573
 
 
574
    def add_diff(self, diff, version_id, parent_ids):
 
575
        outfile = open(self._filename + '.mpknit', 'ab')
 
576
        try:
 
577
            outfile.seek(0, 2)      # workaround for windows bug:
 
578
                                    # .tell() for files opened in 'ab' mode
 
579
                                    # before any write returns 0
 
580
            start = outfile.tell()
 
581
            try:
 
582
                zipfile = gzip.GzipFile(None, mode='ab', fileobj=outfile)
 
583
                zipfile.writelines(itertools.chain(
 
584
                    ['version %s\n' % version_id], diff.to_patch()))
 
585
            finally:
 
586
                zipfile.close()
 
587
            end = outfile.tell()
 
588
        finally:
 
589
            outfile.close()
 
590
        self._diff_offset[version_id] = (start, end-start)
 
591
        self._parents[version_id] = parent_ids
 
592
 
 
593
    def destroy(self):
 
594
        try:
 
595
            os.unlink(self._filename + '.mpknit')
 
596
        except OSError as e:
 
597
            if e.errno != errno.ENOENT:
 
598
                raise
 
599
        try:
 
600
            os.unlink(self._filename + '.mpidx')
 
601
        except OSError as e:
 
602
            if e.errno != errno.ENOENT:
 
603
                raise
 
604
 
 
605
    def save(self):
 
606
        open(self._filename + '.mpidx', 'wb').write(bencode.bencode(
 
607
            (self._parents, list(self._snapshots), self._diff_offset)))
 
608
 
 
609
    def load(self):
 
610
        self._parents, snapshots, self._diff_offset = bencode.bdecode(
 
611
            open(self._filename + '.mpidx', 'rb').read())
 
612
        self._snapshots = set(snapshots)
 
613
 
 
614
 
 
615
class _Reconstructor(object):
 
616
    """Build a text from the diffs, ancestry graph and cached lines"""
 
617
 
 
618
    def __init__(self, diffs, lines, parents):
 
619
        self.diffs = diffs
 
620
        self.lines = lines
 
621
        self.parents = parents
 
622
        self.cursor = {}
 
623
 
 
624
    def reconstruct(self, lines, parent_text, version_id):
 
625
        """Append the lines referred to by a ParentText to lines"""
 
626
        parent_id = self.parents[version_id][parent_text.parent]
 
627
        end = parent_text.parent_pos + parent_text.num_lines
 
628
        return self._reconstruct(lines, parent_id, parent_text.parent_pos,
 
629
                                 end)
 
630
 
 
631
    def _reconstruct(self, lines, req_version_id, req_start, req_end):
 
632
        """Append lines for the requested version_id range"""
 
633
        # stack of pending range requests
 
634
        if req_start == req_end:
 
635
            return
 
636
        pending_reqs = [(req_version_id, req_start, req_end)]
 
637
        while len(pending_reqs) > 0:
 
638
            req_version_id, req_start, req_end = pending_reqs.pop()
 
639
            # lazily allocate cursors for versions
 
640
            if req_version_id in self.lines:
 
641
                lines.extend(self.lines[req_version_id][req_start:req_end])
 
642
                continue
 
643
            try:
 
644
                start, end, kind, data, iterator = self.cursor[req_version_id]
 
645
            except KeyError:
 
646
                iterator = self.diffs.get_diff(req_version_id).range_iterator()
 
647
                start, end, kind, data = next(iterator)
 
648
            if start > req_start:
 
649
                iterator = self.diffs.get_diff(req_version_id).range_iterator()
 
650
                start, end, kind, data = next(iterator)
 
651
 
 
652
            # find the first hunk relevant to the request
 
653
            while end <= req_start:
 
654
                start, end, kind, data = next(iterator)
 
655
            self.cursor[req_version_id] = start, end, kind, data, iterator
 
656
            # if the hunk can't satisfy the whole request, split it in two,
 
657
            # and leave the second half for later.
 
658
            if req_end > end:
 
659
                pending_reqs.append((req_version_id, end, req_end))
 
660
                req_end = end
 
661
            if kind == 'new':
 
662
                lines.extend(data[req_start - start: (req_end - start)])
 
663
            else:
 
664
                # If the hunk is a ParentText, rewrite it as a range request
 
665
                # for the parent, and make it the next pending request.
 
666
                parent, parent_start, parent_end = data
 
667
                new_version_id = self.parents[req_version_id][parent]
 
668
                new_start = parent_start + req_start - start
 
669
                new_end = parent_end + req_end - end
 
670
                pending_reqs.append((new_version_id, new_start, new_end))
 
671
 
 
672
    def reconstruct_version(self, lines, version_id):
 
673
        length = self.diffs.get_diff(version_id).num_lines()
 
674
        return self._reconstruct(lines, version_id, 0, length)
 
675
 
 
676
 
 
677
def gzip_string(lines):
 
678
    sio = BytesIO()
 
679
    data_file = gzip.GzipFile(None, mode='wb', fileobj=sio)
 
680
    data_file.writelines(lines)
 
681
    data_file.close()
 
682
    return sio.getvalue()