/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2017-06-02 11:26:27 UTC
  • mfrom: (6621.27.5 1089352-sni-support)
  • Revision ID: breezy.the.bot@gmail.com-20170602112627-jbvjcm9czx7gt3gb
Add SNI support.

Merged from https://code.launchpad.net/~jelmer/brz/sni-support/+merge/324979

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from bzrlib.lazy_import import lazy_import
 
17
from __future__ import absolute_import
 
18
 
 
19
import errno
 
20
import os
 
21
 
 
22
from .lazy_import import lazy_import
18
23
 
19
24
lazy_import(globals(), """
20
 
import errno
 
25
import gzip
21
26
import itertools
22
 
import os
23
 
from StringIO import StringIO
24
27
 
25
 
from bzrlib import (
 
28
from breezy import (
 
29
    bencode,
26
30
    errors,
27
31
    patiencediff,
28
 
    trace,
29
32
    ui,
30
33
    )
31
 
from bzrlib import bencode
32
34
""")
33
 
from bzrlib.tuned_gzip import GzipFile
 
35
from .sixish import (
 
36
    BytesIO,
 
37
    )
34
38
 
35
39
 
36
40
def topo_iter_keys(vf, keys=None):
76
80
class MultiParent(object):
77
81
    """A multi-parent diff"""
78
82
 
 
83
    __slots__ = ['hunks']
 
84
 
79
85
    def __init__(self, hunks=None):
80
86
        if hunks is not None:
81
87
            self.hunks = hunks
111
117
        diff = MultiParent([])
112
118
        def next_block(p):
113
119
            try:
114
 
                return block_iter[p].next()
 
120
                return next(block_iter[p])
115
121
            except StopIteration:
116
122
                return None
117
123
        cur_block = [next_block(p) for p, i in enumerate(block_iter)]
162
168
        """Contruct a fulltext from this diff and its parents"""
163
169
        mpvf = MultiMemoryVersionedFile()
164
170
        for num, parent in enumerate(parents):
165
 
            mpvf.add_version(StringIO(parent).readlines(), num, [])
 
171
            mpvf.add_version(BytesIO(parent).readlines(), num, [])
166
172
        mpvf.add_diff(self, 'a', range(len(parents)))
167
173
        return mpvf.get_line_list(['a'])[0]
168
174
 
169
175
    @classmethod
170
176
    def from_texts(cls, text, parents=()):
171
177
        """Produce a MultiParent from a text and list of parent text"""
172
 
        return cls.from_lines(StringIO(text).readlines(),
173
 
                              [StringIO(p).readlines() for p in parents])
 
178
        return cls.from_lines(BytesIO(text).readlines(),
 
179
                              [BytesIO(p).readlines() for p in parents])
174
180
 
175
181
    def to_patch(self):
176
182
        """Yield text lines for a patch"""
187
193
    @classmethod
188
194
    def from_patch(cls, text):
189
195
        """Create a MultiParent from its string form"""
190
 
        return cls._from_patch(StringIO(text))
 
196
        return cls._from_patch(BytesIO(text))
191
197
 
192
198
    @staticmethod
193
199
    def _from_patch(lines):
197
203
        cur_line = None
198
204
        while(True):
199
205
            try:
200
 
                cur_line = line_iter.next()
 
206
                cur_line = next(line_iter)
201
207
            except StopIteration:
202
208
                break
203
209
            if cur_line[0] == 'i':
204
210
                num_lines = int(cur_line.split(' ')[1])
205
 
                hunk_lines = [line_iter.next() for x in xrange(num_lines)]
 
211
                hunk_lines = [next(line_iter) for x in xrange(num_lines)]
206
212
                hunk_lines[-1] = hunk_lines[-1][:-1]
207
213
                hunks.append(NewText(hunk_lines))
208
214
            elif cur_line[0] == '\n':
258
264
class NewText(object):
259
265
    """The contents of text that is introduced by this text"""
260
266
 
 
267
    __slots__ = ['lines']
 
268
 
261
269
    def __init__(self, lines):
262
270
        self.lines = lines
263
271
 
279
287
class ParentText(object):
280
288
    """A reference to text present in a parent text"""
281
289
 
 
290
    __slots__ = ['parent', 'parent_pos', 'child_pos', 'num_lines']
 
291
 
282
292
    def __init__(self, parent, parent_pos, child_pos, num_lines):
283
293
        self.parent = parent
284
294
        self.parent_pos = parent_pos
285
295
        self.child_pos = child_pos
286
296
        self.num_lines = num_lines
287
297
 
 
298
    def _as_dict(self):
 
299
        return dict(parent=self.parent, parent_pos=self.parent_pos,
 
300
                    child_pos=self.child_pos, num_lines=self.num_lines)
 
301
 
288
302
    def __repr__(self):
289
 
        return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
290
 
            ' %(num_lines)r)' % self.__dict__
 
303
        return ('ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'
 
304
                ' %(num_lines)r)' % self._as_dict())
291
305
 
292
306
    def __eq__(self, other):
293
307
        if self.__class__ is not other.__class__:
294
308
            return False
295
 
        return (self.__dict__ == other.__dict__)
 
309
        return self._as_dict() == other._as_dict()
296
310
 
297
311
    def to_patch(self):
298
 
        yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\
299
 
            % self.__dict__
 
312
        yield ('c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'
 
313
               % self._as_dict())
300
314
 
301
315
 
302
316
class BaseVersionedFile(object):
412
426
                            if not (lines == self.get_line_list([revision])[0]):
413
427
                                raise AssertionError()
414
428
                            self.clear_cache()
415
 
                    pb.update('Importing revisions',
 
429
                    pb.update(gettext('Importing revisions'),
416
430
                              (total - len(revisions)) + len(added), total)
417
431
                revisions = [r for r in revisions if r not in added]
418
432
        finally:
548
562
        infile = open(self._filename + '.mpknit', 'rb')
549
563
        try:
550
564
            infile.seek(start)
551
 
            sio = StringIO(infile.read(count))
 
565
            sio = BytesIO(infile.read(count))
552
566
        finally:
553
567
            infile.close()
554
 
        zip_file = GzipFile(None, mode='rb', fileobj=sio)
 
568
        zip_file = gzip.GzipFile(None, mode='rb', fileobj=sio)
555
569
        try:
556
570
            file_version_id = zip_file.readline()
557
 
            return MultiParent.from_patch(zip_file.read())
 
571
            content = zip_file.read()
 
572
            return MultiParent.from_patch(content)
558
573
        finally:
559
574
            zip_file.close()
560
575
 
566
581
                                    # before any write returns 0
567
582
            start = outfile.tell()
568
583
            try:
569
 
                zipfile = GzipFile(None, mode='ab', fileobj=outfile)
 
584
                zipfile = gzip.GzipFile(None, mode='ab', fileobj=outfile)
570
585
                zipfile.writelines(itertools.chain(
571
586
                    ['version %s\n' % version_id], diff.to_patch()))
572
587
            finally:
580
595
    def destroy(self):
581
596
        try:
582
597
            os.unlink(self._filename + '.mpknit')
583
 
        except OSError, e:
 
598
        except OSError as e:
584
599
            if e.errno != errno.ENOENT:
585
600
                raise
586
601
        try:
587
602
            os.unlink(self._filename + '.mpidx')
588
 
        except OSError, e:
 
603
        except OSError as e:
589
604
            if e.errno != errno.ENOENT:
590
605
                raise
591
606
 
631
646
                start, end, kind, data, iterator = self.cursor[req_version_id]
632
647
            except KeyError:
633
648
                iterator = self.diffs.get_diff(req_version_id).range_iterator()
634
 
                start, end, kind, data = iterator.next()
 
649
                start, end, kind, data = next(iterator)
635
650
            if start > req_start:
636
651
                iterator = self.diffs.get_diff(req_version_id).range_iterator()
637
 
                start, end, kind, data = iterator.next()
 
652
                start, end, kind, data = next(iterator)
638
653
 
639
654
            # find the first hunk relevant to the request
640
655
            while end <= req_start:
641
 
                start, end, kind, data = iterator.next()
 
656
                start, end, kind, data = next(iterator)
642
657
            self.cursor[req_version_id] = start, end, kind, data, iterator
643
658
            # if the hunk can't satisfy the whole request, split it in two,
644
659
            # and leave the second half for later.
662
677
 
663
678
 
664
679
def gzip_string(lines):
665
 
    sio = StringIO()
666
 
    data_file = GzipFile(None, mode='wb', fileobj=sio)
 
680
    sio = BytesIO()
 
681
    data_file = gzip.GzipFile(None, mode='wb', fileobj=sio)
667
682
    data_file.writelines(lines)
668
683
    data_file.close()
669
684
    return sio.getvalue()