1
1
# Copyright (C) 2005 by Canonical Development 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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
A store that keeps the full text of every version.
17
"""A store that keeps the full text of every version.
20
19
This store keeps uncompressed versions of the full text. It does not
21
20
do any sort of delta compression.
26
24
import bzrlib.store
27
from bzrlib.store import hash_prefix
28
25
from bzrlib.trace import mutter
29
from bzrlib.errors import BzrError, FileExists
26
from bzrlib.errors import BzrError, NoSuchFile, FileExists
31
29
from cStringIO import StringIO
32
from stat import ST_SIZE
35
32
class TextStore(bzrlib.store.TransportStore):
42
39
Files are stored uncompressed, with no delta compression.
45
def __init__(self, transport, prefixed=False):
46
super(TextStore, self).__init__(transport)
47
self._prefixed = prefixed
49
def _check_fileid(self, fileid):
50
if not isinstance(fileid, basestring):
51
raise TypeError('Fileids should be a string type: %s %r' % (type(fileid), fileid))
52
if '\\' in fileid or '/' in fileid:
53
raise ValueError("invalid store id %r" % fileid)
55
def _relpath(self, fileid):
56
self._check_fileid(fileid)
58
return hash_prefix(fileid) + fileid
62
def add(self, f, fileid):
63
"""Add contents of a file into the store.
65
f -- A file-like object, or string
67
mutter("add store entry %r" % (fileid))
42
def _add_compressed(self, fn, f):
43
from cStringIO import StringIO
44
from bzrlib.osutils import pumpfile
46
if isinstance(f, basestring):
69
fn = self._relpath(fileid)
70
if self._transport.has(fn):
71
raise BzrError("store %r already contains id %r" % (self._transport.base, fileid))
75
self._transport.mkdir(hash_prefix(fileid))
79
self._transport.put(fn, f)
81
def __contains__(self, fileid):
83
fn = self._relpath(fileid)
84
return self._transport.has(fn)
86
def has(self, fileids, pb=None):
87
"""Return True/False for each entry in fileids.
89
:param fileids: A List or generator yielding file ids.
90
:return: A generator or list returning True/False for each entry.
92
relpaths = (self._relpath(fid) for fid in fileids)
93
return self._transport.has_multi(relpaths, pb=pb)
95
def get(self, fileids, permit_failure=False, pb=None):
96
"""Return a set of files, one for each requested entry.
98
TODO: Write some tests to make sure that permit_failure is
101
TODO: What should the exception be for a missing file?
102
KeyError, or NoSuchFile?
105
# This next code gets a bit hairy because it can allow
106
# to not request a file which doesn't seem to exist.
107
# Also, the same fileid may be requested twice, so we
108
# can't just build up a map.
109
rel_paths = [self._relpath(fid) for fid in fileids]
114
for path, has in zip(rel_paths,
115
self._transport.has_multi(rel_paths)):
117
existing_paths.append(path)
118
is_requested.append(True)
120
is_requested.append(False)
121
#mutter('Retrieving %s out of %s' % (existing_paths, rel_paths))
123
#mutter('Retrieving all %s' % (rel_paths, ))
124
existing_paths = rel_paths
125
is_requested = [True for x in rel_paths]
128
for f in self._transport.get_multi(existing_paths, pb=pb):
129
assert count < len(is_requested)
130
while not is_requested[count]:
136
while count < len(is_requested):
141
for relpath, st in self._iter_relpaths():
142
yield os.path.basename(relpath)
145
return len(list(self._iter_relpath()))
147
def total_size(self):
148
"""Return (count, bytes)
150
This is the (compressed) size stored on disk, not the size of
154
for relpath, st in self._iter_relpaths():
161
class ScratchTextStore(TextStore):
162
"""Self-destructing test subclass of TextStore.
164
The Store only exists for the lifetime of the Python object.
165
Obviously you should not put anything precious in it.
168
from transport import transport
169
super(ScratchTextStore, self).__init__(transport(tempfile.mkdtemp()))
172
self._transport.delete_multi(self._transport.list_dir('.'))
173
os.rmdir(self._transport.base)
174
mutter("%r destroyed" % self)
50
gf = gzip.GzipFile(mode='wb', fileobj=sio)
51
# if pumpfile handles files that don't fit in ram,
52
# so will this function
56
self._try_put(fn, sio)
58
def _add(self, fn, f):
60
self._add_compressed(fn, f)
64
def _try_put(self, fn, f):
66
self._transport.put_file(fn, f, mode=self._file_mode)
68
if not self._prefixed:
71
self._transport.mkdir(os.path.dirname(fn), mode=self._dir_mode)
74
self._transport.put_file(fn, f, mode=self._file_mode)
77
if fn.endswith('.gz'):
78
return self._get_compressed(fn)
80
return self._transport.get(fn)
82
def _copy_one(self, fileid, suffix, other, pb):
83
# TODO: Once the copy_to interface is improved to allow a source
84
# and destination targets, then we can always do the copy
85
# as long as other is a TextStore
86
if not (isinstance(other, TextStore)
87
and other._prefixed == self._prefixed):
88
return super(TextStore, self)._copy_one(fileid, suffix, other, pb)
90
mutter('_copy_one: %r, %r', fileid, suffix)
91
path = other._get_name(fileid, suffix)
93
raise KeyError(fileid + '-' + str(suffix))
96
result = other._transport.copy_to([path], self._transport,
99
if not self._prefixed:
102
self._transport.mkdir(self.hash_prefix(fileid)[:-1], mode=self._dir_mode)
105
result = other._transport.copy_to([path], self._transport,
106
mode=self._file_mode)
109
raise BzrError('Unable to copy file: %r' % (path,))
111
def _get_compressed(self, filename):
112
"""Returns a file reading from a particular entry."""
113
f = self._transport.get(filename)
114
# gzip.GzipFile.read() requires a tell() function
115
# but some transports return objects that cannot seek
116
# so buffer them in a StringIO instead
117
if getattr(f, 'tell', None) is not None:
118
return gzip.GzipFile(mode='rb', fileobj=f)
120
from cStringIO import StringIO
121
sio = StringIO(f.read())
122
return gzip.GzipFile(mode='rb', fileobj=sio)