24
24
import os, tempfile, types, osutils, gzip, errno
25
25
from stat import ST_SIZE
26
26
from StringIO import StringIO
27
from bzrlib.errors import BzrError
28
from bzrlib.trace import mutter
27
from trace import mutter
31
29
######################################################################
93
91
p = self._path(fileid)
94
92
if os.access(p, os.F_OK) or os.access(p + '.gz', os.F_OK):
93
from bzrlib.errors import bailout
95
94
raise BzrError("store %r already contains id %r" % (self._basedir, fileid))
114
def copy_multi(self, other, ids, permit_failure=False):
113
def copy_multi(self, other, ids):
115
114
"""Copy texts for ids from other into self.
117
If an id is present in self, it is skipped.
119
Returns (count_copied, failed), where failed is a collection of ids
120
that could not be copied.
116
If an id is present in self, it is skipped. A count of copied
117
ids is returned, which may be less than len(ids).
122
pb = bzrlib.ui.ui_factory.progress_bar()
119
from bzrlib.progress import ProgressBar
124
121
pb.update('preparing to copy')
125
122
to_copy = [id for id in ids if id not in self]
126
123
if isinstance(other, ImmutableStore):
127
124
return self.copy_multi_immutable(other, to_copy, pb)
130
126
for id in to_copy:
132
128
pb.update('copy', count, len(to_copy))
133
if not permit_failure:
134
self.add(other[id], id)
143
if not permit_failure:
144
assert count == len(to_copy)
129
self.add(other[id], id)
130
assert count == len(to_copy)
148
def copy_multi_immutable(self, other, to_copy, pb, permit_failure=False):
135
def copy_multi_immutable(self, other, to_copy, pb):
149
136
from shutil import copyfile
152
138
for id in to_copy:
153
139
p = self._path(id)
154
140
other_p = other._path(id)
156
142
copyfile(other_p, p)
157
143
except IOError, e:
158
144
if e.errno == errno.ENOENT:
159
if not permit_failure:
160
copyfile(other_p+".gz", p+".gz")
163
copyfile(other_p+".gz", p+".gz")
165
if e.errno == errno.ENOENT:
145
copyfile(other_p+".gz", p+".gz")
195
172
def __len__(self):
196
173
return len(os.listdir(self._basedir))
199
175
def __getitem__(self, fileid):
200
176
"""Returns a file reading from a particular entry."""
201
177
p = self._path(fileid)
203
179
return gzip.GzipFile(p + '.gz', 'rb')
204
180
except IOError, e:
205
if e.errno != errno.ENOENT:
211
if e.errno != errno.ENOENT:
214
raise IndexError(fileid)
181
if e.errno == errno.ENOENT:
217
186
def total_size(self):
218
187
"""Return (count, bytes)