1
# Copyright (C) 2007-2011 Canonical Ltd
1
# Copyright (C) 2007 Canonical Ltd
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
13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
from bzrlib.lazy_import import lazy_import
19
19
lazy_import(globals(), """
24
23
from StringIO import StringIO
26
25
from bzrlib import (
31
from bzrlib.util import bencode
33
from bzrlib.tuned_gzip import GzipFile
35
36
def topo_iter_keys(vf, keys=None):
282
279
class ParentText(object):
283
280
"""A reference to text present in a parent text"""
285
__slots__ = ['parent', 'parent_pos', 'child_pos', 'num_lines']
287
282
def __init__(self, parent, parent_pos, child_pos, num_lines):
288
283
self.parent = parent
289
284
self.parent_pos = parent_pos
290
285
self.child_pos = child_pos
291
286
self.num_lines = num_lines
294
return dict(parent=self.parent, parent_pos=self.parent_pos,
295
child_pos=self.child_pos, num_lines=self.num_lines)
297
288
def __repr__(self):
298
return ('ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'
299
' %(num_lines)r)' % self._as_dict())
289
return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
290
' %(num_lines)r)' % self.__dict__
301
292
def __eq__(self, other):
302
if self.__class__ is not other.__class__:
293
if self.__class__ != other.__class__:
304
return self._as_dict() == other._as_dict()
295
return (self.__dict__ == other.__dict__)
306
297
def to_patch(self):
307
yield ('c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'
298
yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\
311
302
class BaseVersionedFile(object):
325
316
return version in self._parents
327
318
def do_snapshot(self, version_id, parent_ids):
328
"""Determine whether to perform a snapshot for this version"""
319
"""Determine whether to perform a a snapshot for this version"""
329
320
if self.snapshot_interval is None:
331
322
if self.max_snapshots is not None and\
560
551
sio = StringIO(infile.read(count))
563
zip_file = gzip.GzipFile(None, mode='rb', fileobj=sio)
554
zip_file = GzipFile(None, mode='rb', fileobj=sio)
565
556
file_version_id = zip_file.readline()
566
content = zip_file.read()
567
return MultiParent.from_patch(content)
557
return MultiParent.from_patch(zip_file.read())
576
566
# before any write returns 0
577
567
start = outfile.tell()
579
zipfile = gzip.GzipFile(None, mode='ab', fileobj=outfile)
569
zipfile = GzipFile(None, mode='ab', fileobj=outfile)
580
570
zipfile.writelines(itertools.chain(
581
571
['version %s\n' % version_id], diff.to_patch()))
674
664
def gzip_string(lines):
676
data_file = gzip.GzipFile(None, mode='wb', fileobj=sio)
666
data_file = GzipFile(None, mode='wb', fileobj=sio)
677
667
data_file.writelines(lines)
678
668
data_file.close()
679
669
return sio.getvalue()