/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
711 by Martin Pool
- store docs
1
# Copyright (C) 2005 by Canonical Development Ltd
1 by mbp at sourcefrog
import from baz patch-364
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
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
16
1374 by Martin Pool
todo
17
# TODO: Could remember a bias towards whether a particular store is typically
18
# compressed or not.
19
711 by Martin Pool
- store docs
20
"""
21
Stores are the main data-storage mechanism for Bazaar-NG.
1 by mbp at sourcefrog
import from baz patch-364
22
23
A store is a simple write-once container indexed by a universally
711 by Martin Pool
- store docs
24
unique ID.
25
"""
1 by mbp at sourcefrog
import from baz patch-364
26
1442.1.51 by Robert Collins
teach iter about suffixes
27
import os
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
28
from cStringIO import StringIO
1479 by Robert Collins
More quoting at the transport layer bugfixes.
29
import urllib
1429 by Robert Collins
merge in niemeyers prefixed-store patch
30
from zlib import adler32
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
31
1442.1.44 by Robert Collins
Many transport related tweaks:
32
import bzrlib
1433 by Robert Collins
merge in and make incremental Gustavo Niemeyers nested log patch, and remove all bare exceptions in store and transport packages.
33
import bzrlib.errors as errors
1393.2.3 by John Arbash Meinel
Fixing typos, updating stores, getting tests to pass.
34
from bzrlib.errors import BzrError, UnlistableStore, TransportNotPossible
1104 by Martin Pool
- Add a simple UIFactory
35
from bzrlib.trace import mutter
1442.1.44 by Robert Collins
Many transport related tweaks:
36
import bzrlib.transport as transport
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
37
from bzrlib.transport.local import LocalTransport
1 by mbp at sourcefrog
import from baz patch-364
38
39
######################################################################
40
# stores
41
42
class StoreError(Exception):
43
    pass
44
45
1185.11.1 by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet.
46
class Store(object):
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
47
    """This class represents the abstract storage layout for saving information.
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
48
    
1 by mbp at sourcefrog
import from baz patch-364
49
    Files can be added, but not modified once they are in.  Typically
50
    the hash is used as the name, or something else known to be unique,
51
    such as a UUID.
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
52
    """
53
54
    def __len__(self):
55
        raise NotImplementedError('Children should define their length')
56
1442.1.50 by Robert Collins
test get with suffixes
57
    def get(self, file_id, suffix=None):
58
        """Returns a file reading from a particular entry.
59
        
60
        If suffix is present, retrieve the named suffix for file_id.
61
        """
62
        raise NotImplementedError
1442.1.35 by Robert Collins
convert all users of __getitem__ into TransportStores to use .get instead
63
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
64
    def __getitem__(self, fileid):
1442.1.35 by Robert Collins
convert all users of __getitem__ into TransportStores to use .get instead
65
        """DEPRECATED. Please use .get(file_id) instead."""
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
66
        raise NotImplementedError
67
1442.1.45 by Robert Collins
replace __contains__ calls in stores with has_id
68
    #def __contains__(self, fileid):
69
    #    """Deprecated, please use has_id"""
70
    #    raise NotImplementedError
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
71
72
    def __iter__(self):
73
        raise NotImplementedError
74
907.1.43 by John Arbash Meinel
Restoring compatibility for Storage.add(file, fileid), it is a little arbitrary, and compatibility is better
75
    def add(self, f, fileid):
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
76
        """Add a file object f to the store accessible from the given fileid"""
1185.11.1 by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet.
77
        raise NotImplementedError('Children of Store must define their method of adding entries.')
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
78
1442.1.47 by Robert Collins
test for has with suffixed files
79
    def has_id(self, file_id, suffix=None):
80
        """Return True or false for the presence of file_id in the store.
81
        
82
        suffix, if present, is a per file suffix, i.e. for digital signature 
83
        data."""
1442.1.45 by Robert Collins
replace __contains__ calls in stores with has_id
84
        raise NotImplementedError
907.1.36 by John Arbash Meinel
Moving the multi-get functionality higher up into the Branch class.
85
1400.1.1 by Robert Collins
implement a basic test for the ui branch command from http servers
86
    def listable(self):
87
        """Return True if this store is able to be listed."""
88
        return hasattr(self, "__iter__")
89
1185.11.1 by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet.
90
    def copy_multi(self, other, ids, pb=None, permit_failure=False):
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
91
        """Copy texts for ids from other into self.
92
93
        If an id is present in self, it is skipped.  A count of copied
94
        ids is returned, which may be less than len(ids).
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
95
1185.11.1 by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet.
96
        :param other: Another Store object
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
97
        :param ids: A list of entry ids to be copied
1185.11.1 by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet.
98
        :param pb: A ProgressBar object, if none is given, the default will be created.
99
        :param permit_failure: Allow missing entries to be ignored
100
        :return: (n_copied, [failed]) The number of entries copied successfully,
101
            followed by a list of entries which could not be copied (because they
102
            were missing)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
103
        """
1185.11.1 by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet.
104
        if pb is None:
105
            pb = bzrlib.ui.ui_factory.progress_bar()
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
106
        pb.update('preparing to copy')
974.2.7 by aaron.bentley at utoronto
Merged from bzr.24
107
        failed = set()
1442.1.53 by Robert Collins
Unroll the multiple-copy logic enough to remove the duplicate iteration and yet retain the optimised gzip->gzip copy.
108
        count = 0
109
        ids = list(ids) # get the list for showing a length.
110
        for fileid in ids:
111
            count += 1
112
            if self.has_id(fileid):
113
                continue
114
            try:
1442.1.54 by Robert Collins
Teach store.copy_all about fileid suffixes
115
                self._copy_one(fileid, None, other, pb)
116
                for suffix in self._suffixes:
117
                    try:
118
                        self._copy_one(fileid, suffix, other, pb)
119
                    except KeyError:
120
                        pass
1442.1.53 by Robert Collins
Unroll the multiple-copy logic enough to remove the duplicate iteration and yet retain the optimised gzip->gzip copy.
121
                pb.update('copy', count, len(ids))
122
            except KeyError:
123
                if permit_failure:
124
                    failed.add(fileid)
125
                else:
126
                    raise
127
        assert count == len(ids)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
128
        pb.clear()
1442.1.53 by Robert Collins
Unroll the multiple-copy logic enough to remove the duplicate iteration and yet retain the optimised gzip->gzip copy.
129
        return count, failed
130
1442.1.54 by Robert Collins
Teach store.copy_all about fileid suffixes
131
    def _copy_one(self, fileid, suffix, other, pb):
1442.1.53 by Robert Collins
Unroll the multiple-copy logic enough to remove the duplicate iteration and yet retain the optimised gzip->gzip copy.
132
        """Most generic copy-one object routine.
133
        
134
        Subclasses can override this to provide an optimised
135
        copy between their own instances. Such overriden routines
136
        should call this if they have no optimised facility for a 
137
        specific 'other'.
138
        """
1442.1.54 by Robert Collins
Teach store.copy_all about fileid suffixes
139
        f = other.get(fileid, suffix)
140
        self.add(f, fileid, suffix)
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
141
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
142
143
class TransportStore(Store):
144
    """A TransportStore is a Store superclass for Stores that use Transports."""
145
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
146
    def add(self, f, fileid, suffix=None):
1442.1.28 by Robert Collins
pull up core TransportStore.add from TextStore.add and CompressedTextStore.add
147
        """Add contents of a file into the store.
148
149
        f -- A file-like object, or string
150
        """
151
        mutter("add store entry %r" % (fileid))
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
152
        
153
        if suffix is not None:
154
            fn = self._relpath(fileid, [suffix])
155
        else:
156
            fn = self._relpath(fileid)
1442.1.28 by Robert Collins
pull up core TransportStore.add from TextStore.add and CompressedTextStore.add
157
        if self._transport.has(fn):
158
            raise BzrError("store %r already contains id %r" % (self._transport.base, fileid))
159
160
        if self._prefixed:
161
            try:
1442.1.47 by Robert Collins
test for has with suffixed files
162
                self._transport.mkdir(hash_prefix(fileid)[:-1])
1442.1.28 by Robert Collins
pull up core TransportStore.add from TextStore.add and CompressedTextStore.add
163
            except errors.FileExists:
164
                pass
165
166
        self._add(fn, f)
167
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
168
    def _check_fileid(self, fileid):
169
        if not isinstance(fileid, basestring):
170
            raise TypeError('Fileids should be a string type: %s %r' % (type(fileid), fileid))
171
        if '\\' in fileid or '/' in fileid:
172
            raise ValueError("invalid store id %r" % fileid)
173
1442.1.47 by Robert Collins
test for has with suffixed files
174
    def has_id(self, fileid, suffix=None):
175
        """See Store.has_id."""
176
        if suffix is not None:
177
            fn = self._relpath(fileid, [suffix])
178
        else:
179
            fn = self._relpath(fileid)
1442.1.38 by Robert Collins
unify __contains__ for TransportStore classes
180
        return self._transport.has(fn)
181
1442.1.36 by Robert Collins
convert get() in TextStore and CompressedTextStore into a template method
182
    def _get(self, filename):
183
        """Return an vanilla file stream for clients to read from.
184
185
        This is the body of a template method on 'get', and should be 
186
        implemented by subclasses.
187
        """
188
        raise NotImplementedError
189
1442.1.50 by Robert Collins
test get with suffixes
190
    def get(self, fileid, suffix=None):
191
        """See Store.get()."""
1442.1.54 by Robert Collins
Teach store.copy_all about fileid suffixes
192
        if suffix is None or suffix == 'gz':
1442.1.50 by Robert Collins
test get with suffixes
193
            fn = self._relpath(fileid)
194
        else:
195
            fn = self._relpath(fileid, [suffix])
1433 by Robert Collins
merge in and make incremental Gustavo Niemeyers nested log patch, and remove all bare exceptions in store and transport packages.
196
        try:
1442.1.36 by Robert Collins
convert get() in TextStore and CompressedTextStore into a template method
197
            return self._get(fn)
1433 by Robert Collins
merge in and make incremental Gustavo Niemeyers nested log patch, and remove all bare exceptions in store and transport packages.
198
        except errors.NoSuchFile:
199
            raise KeyError(fileid)
200
1442.1.44 by Robert Collins
Many transport related tweaks:
201
    def __init__(self, a_transport, prefixed=False):
202
        assert isinstance(a_transport, transport.Transport)
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
203
        super(TransportStore, self).__init__()
1442.1.44 by Robert Collins
Many transport related tweaks:
204
        self._transport = a_transport
1442.1.25 by Robert Collins
Test TransportStore._relpath for simple cases: pull up _prefixed attribute as a result.
205
        self._prefixed = prefixed
1442.1.54 by Robert Collins
Teach store.copy_all about fileid suffixes
206
        # conflating the .gz extension and user suffixes was a mistake.
207
        # RBC 20051017 - TODO SOON, separate them again.
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
208
        self._suffixes = set()
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
209
1479 by Robert Collins
More quoting at the transport layer bugfixes.
210
    def _iter_files_recursive(self):
211
        """Iterate through the files in the transport."""
212
        for quoted_relpath in self._transport.iter_files_recursive():
213
            yield urllib.unquote(quoted_relpath)
214
1442.1.51 by Robert Collins
teach iter about suffixes
215
    def __iter__(self):
1479 by Robert Collins
More quoting at the transport layer bugfixes.
216
        for relpath in self._iter_files_recursive():
1442.1.51 by Robert Collins
teach iter about suffixes
217
            # worst case is one of each suffix.
218
            name = os.path.basename(relpath)
219
            if name.endswith('.gz'):
220
                name = name[:-3]
221
            skip = False
222
            for count in range(len(self._suffixes)):
223
                for suffix in self._suffixes:
224
                    if name.endswith('.' + suffix):
225
                        skip = True
226
            if not skip:
227
                yield name
228
1442.1.40 by Robert Collins
unify __len__() implementations for TransportStore classes
229
    def __len__(self):
1442.1.50 by Robert Collins
test get with suffixes
230
        return len(list(self.__iter__()))
1442.1.40 by Robert Collins
unify __len__() implementations for TransportStore classes
231
1442.1.26 by Robert Collins
Pull up _relpath with gz suffix for CompressedTextStore into TransportStore
232
    def _relpath(self, fileid, suffixes=[]):
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
233
        self._check_fileid(fileid)
1442.1.27 by Robert Collins
Check that file suffixes in TransportStore are also valid
234
        for suffix in suffixes:
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
235
            if not suffix in self._suffixes:
236
                raise ValueError("Unregistered suffix %r" % suffix)
1442.1.27 by Robert Collins
Check that file suffixes in TransportStore are also valid
237
            self._check_fileid(suffix)
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
238
        if self._prefixed:
1442.1.26 by Robert Collins
Pull up _relpath with gz suffix for CompressedTextStore into TransportStore
239
            path = [hash_prefix(fileid) + fileid]
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
240
        else:
1442.1.26 by Robert Collins
Pull up _relpath with gz suffix for CompressedTextStore into TransportStore
241
            path = [fileid]
242
        path.extend(suffixes)
1469 by Robert Collins
Change Transport.* to work with URL's.
243
        return transport.urlescape('.'.join(path))
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
244
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
245
    def __repr__(self):
246
        if self._transport is None:
247
            return "%s(None)" % (self.__class__.__name__)
248
        else:
249
            return "%s(%r)" % (self.__class__.__name__, self._transport.base)
250
251
    __str__ = __repr__
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
252
1400.1.1 by Robert Collins
implement a basic test for the ui branch command from http servers
253
    def listable(self):
254
        """Return True if this store is able to be listed."""
255
        return self._transport.listable()
256
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
257
    def register_suffix(self, suffix):
258
        """Register a suffix as being expected in this store."""
259
        self._check_fileid(suffix)
260
        self._suffixes.add(suffix)
261
1442.1.37 by Robert Collins
pull up total_size into TransportStore
262
    def total_size(self):
263
        """Return (count, bytes)
264
265
        This is the (compressed) size stored on disk, not the size of
266
        the content."""
267
        total = 0
268
        count = 0
1442.1.44 by Robert Collins
Many transport related tweaks:
269
        for relpath in self._transport.iter_files_recursive():
1442.1.37 by Robert Collins
pull up total_size into TransportStore
270
            count += 1
1442.1.44 by Robert Collins
Many transport related tweaks:
271
            total += self._transport.stat(relpath).st_size
1442.1.37 by Robert Collins
pull up total_size into TransportStore
272
                
273
        return count, total
274
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
275
1442.1.44 by Robert Collins
Many transport related tweaks:
276
def ImmutableMemoryStore():
277
    return bzrlib.store.text.TextStore(transport.memory.MemoryTransport())
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
278
        
279
280
class CachedStore(Store):
1092.2.3 by Robert Collins
move CachedStore into store.py
281
    """A store that caches data locally, to avoid repeated downloads.
282
    The precacache method should be used to avoid server round-trips for
283
    every piece of data.
284
    """
285
286
    def __init__(self, store, cache_dir):
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
287
        super(CachedStore, self).__init__()
1092.2.3 by Robert Collins
move CachedStore into store.py
288
        self.source_store = store
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
289
        # This clones the source store type with a locally bound
290
        # transport. FIXME: it assumes a constructor is == cloning.
291
        # clonable store - it might be nicer to actually have a clone()
292
        # or something. RBC 20051003
293
        self.cache_store = store.__class__(LocalTransport(cache_dir))
1092.2.3 by Robert Collins
move CachedStore into store.py
294
1442.1.35 by Robert Collins
convert all users of __getitem__ into TransportStores to use .get instead
295
    def get(self, id):
1092.2.3 by Robert Collins
move CachedStore into store.py
296
        mutter("Cache add %s" % id)
297
        if id not in self.cache_store:
1442.1.35 by Robert Collins
convert all users of __getitem__ into TransportStores to use .get instead
298
            self.cache_store.add(self.source_store.get(id), id)
299
        return self.cache_store.get(id)
1092.2.3 by Robert Collins
move CachedStore into store.py
300
1442.1.47 by Robert Collins
test for has with suffixed files
301
    def has_id(self, fileid, suffix=None):
302
        """See Store.has_id."""
303
        if self.cache_store.has_id(fileid, suffix):
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
304
            return True
1442.1.47 by Robert Collins
test for has with suffixed files
305
        if self.source_store.has_id(fileid, suffix):
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
306
            # We could copy at this time
307
            return True
308
        return False
309
1092.2.19 by Robert Collins
update with integration
310
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
311
def copy_all(store_from, store_to):
312
    """Copy all ids from one store to another."""
1393.1.14 by Martin Pool
doc
313
    # TODO: Optional progress indicator
1400.1.1 by Robert Collins
implement a basic test for the ui branch command from http servers
314
    if not store_from.listable():
315
        raise UnlistableStore(store_from)
316
    ids = [f for f in store_from]
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
317
    store_to.copy_multi(store_from, ids)
1393.2.1 by John Arbash Meinel
Merged in split-storage-2 branch. Need to cleanup a little bit more still.
318
1429 by Robert Collins
merge in niemeyers prefixed-store patch
319
def hash_prefix(file_id):
320
    return "%02x/" % (adler32(file_id) & 0xff)
321