~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-09-04 02:59:56 UTC
  • mfrom: (1172)
  • mto: (1185.3.4)
  • mto: This revision was merged to the branch mainline in revision 1178.
  • Revision ID: aaron.bentley@utoronto.ca-20050904025956-776ba4f07de97700
Merged mpool's latest changes (~0.0.7)

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import os, tempfile, types, osutils, gzip, errno
25
25
from stat import ST_SIZE
26
26
from StringIO import StringIO
27
 
from trace import mutter
 
27
from bzrlib.errors import BzrError
 
28
from bzrlib.trace import mutter
 
29
import bzrlib.ui
28
30
 
29
31
######################################################################
30
32
# stores
109
111
            af.close()
110
112
 
111
113
 
112
 
    def copy_multi(self, other, ids):
 
114
    def copy_multi(self, other, ids, permit_failure=False):
113
115
        """Copy texts for ids from other into self.
114
116
 
115
 
        If an id is present in self, it is skipped.  A count of copied
116
 
        ids is returned, which may be less than len(ids).
 
117
        If an id is present in self, it is skipped.
 
118
 
 
119
        Returns (count_copied, failed), where failed is a collection of ids
 
120
        that could not be copied.
117
121
        """
118
 
        from bzrlib.progress import ProgressBar
119
 
        pb = ProgressBar()
 
122
        pb = bzrlib.ui.ui_factory.progress_bar()
 
123
        
120
124
        pb.update('preparing to copy')
121
125
        to_copy = [id for id in ids if id not in self]
122
 
        count = 0
123
 
        for id in to_copy:
124
 
            count += 1
125
 
            pb.update('copy', count, len(to_copy))
126
 
            self.add(other[id], id)
 
126
        if isinstance(other, ImmutableStore):
 
127
            return self.copy_multi_immutable(other, to_copy, pb)
 
128
        count = 0
 
129
        failed = set()
 
130
        for id in to_copy:
 
131
            count += 1
 
132
            pb.update('copy', count, len(to_copy))
 
133
            if not permit_failure:
 
134
                self.add(other[id], id)
 
135
            else:
 
136
                try:
 
137
                    entry = other[id]
 
138
                except IndexError:
 
139
                    failed.add(id)
 
140
                    continue
 
141
                self.add(entry, id)
 
142
                
 
143
        if not permit_failure:
 
144
            assert count == len(to_copy)
 
145
        pb.clear()
 
146
        return count, failed
 
147
 
 
148
    def copy_multi_immutable(self, other, to_copy, pb, permit_failure=False):
 
149
        from shutil import copyfile
 
150
        count = 0
 
151
        failed = set()
 
152
        for id in to_copy:
 
153
            p = self._path(id)
 
154
            other_p = other._path(id)
 
155
            try:
 
156
                copyfile(other_p, p)
 
157
            except IOError, e:
 
158
                if e.errno == errno.ENOENT:
 
159
                    if not permit_failure:
 
160
                        copyfile(other_p+".gz", p+".gz")
 
161
                    else:
 
162
                        try:
 
163
                            copyfile(other_p+".gz", p+".gz")
 
164
                        except IOError, e:
 
165
                            if e.errno == errno.ENOENT:
 
166
                                failed.add(id)
 
167
                            else:
 
168
                                raise
 
169
                else:
 
170
                    raise
 
171
            
 
172
            count += 1
 
173
            pb.update('copy', count, len(to_copy))
127
174
        assert count == len(to_copy)
128
175
        pb.clear()
129
 
        return count
 
176
        return count, failed
130
177
    
131
178
 
132
179
    def __contains__(self, fileid):
148
195
    def __len__(self):
149
196
        return len(os.listdir(self._basedir))
150
197
 
 
198
 
151
199
    def __getitem__(self, fileid):
152
200
        """Returns a file reading from a particular entry."""
153
201
        p = self._path(fileid)
154
202
        try:
155
203
            return gzip.GzipFile(p + '.gz', 'rb')
156
204
        except IOError, e:
157
 
            if e.errno == errno.ENOENT:
158
 
                return file(p, 'rb')
159
 
            else:
160
 
                raise e
 
205
            if e.errno != errno.ENOENT:
 
206
                raise
 
207
 
 
208
        try:
 
209
            return file(p, 'rb')
 
210
        except IOError, e:
 
211
            if e.errno != errno.ENOENT:
 
212
                raise
 
213
 
 
214
        raise IndexError(fileid)
 
215
 
161
216
 
162
217
    def total_size(self):
163
218
        """Return (count, bytes)