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