/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
711 by Martin Pool
- store docs
17
"""
18
Stores are the main data-storage mechanism for Bazaar-NG.
1 by mbp at sourcefrog
import from baz patch-364
19
20
A store is a simple write-once container indexed by a universally
711 by Martin Pool
- store docs
21
unique ID.
22
"""
1 by mbp at sourcefrog
import from baz patch-364
23
127 by mbp at sourcefrog
- store support for retrieving compressed files
24
import os, tempfile, types, osutils, gzip, errno
81 by mbp at sourcefrog
show space usage for various stores in the info command
25
from stat import ST_SIZE
1 by mbp at sourcefrog
import from baz patch-364
26
from StringIO import StringIO
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
27
from bzrlib.errors import BzrError
1104 by Martin Pool
- Add a simple UIFactory
28
from bzrlib.trace import mutter
29
import bzrlib.ui
1092.2.2 by Robert Collins
move RemoteStore to store.py
30
from bzrlib.remotebranch import get_url
1 by mbp at sourcefrog
import from baz patch-364
31
32
######################################################################
33
# stores
34
35
class StoreError(Exception):
36
    pass
37
38
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
39
class Store(object):
40
    """An abstract store that holds files indexed by unique names.
1 by mbp at sourcefrog
import from baz patch-364
41
42
    Files can be added, but not modified once they are in.  Typically
43
    the hash is used as the name, or something else known to be unique,
44
    such as a UUID.
45
46
    >>> st = ImmutableScratchStore()
47
48
    >>> st.add(StringIO('hello'), 'aa')
49
    >>> 'aa' in st
50
    True
51
    >>> 'foo' in st
52
    False
53
54
    You are not allowed to add an id that is already present.
55
56
    Entries can be retrieved as files, which may then be read.
57
58
    >>> st.add(StringIO('goodbye'), '123123')
59
    >>> st['123123'].read()
60
    'goodbye'
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
61
    """
62
63
    def total_size(self):
64
        """Return (count, bytes)
65
66
        This is the (compressed) size stored on disk, not the size of
67
        the content."""
68
        total = 0
69
        count = 0
70
        for fid in self:
71
            count += 1
72
            total += self._item_size(fid)
73
        return count, total
74
75
76
class ImmutableStore(Store):
77
    """Store that stores files on disk.
1 by mbp at sourcefrog
import from baz patch-364
78
254 by Martin Pool
- Doc cleanups from Magnus Therning
79
    TODO: Atomic add by writing to a temporary file and renaming.
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
80
    TODO: Guard against the same thing being stored twice, compressed and
81
          uncompressed during copy_multi_immutable - the window is for a
82
          matching store with some crack code that lets it offer a 
83
          non gz FOO and then a fz FOO.
1 by mbp at sourcefrog
import from baz patch-364
84
711 by Martin Pool
- store docs
85
    In bzr 0.0.5 and earlier, files within the store were marked
86
    readonly on disk.  This is no longer done but existing stores need
87
    to be accomodated.
1 by mbp at sourcefrog
import from baz patch-364
88
    """
89
90
    def __init__(self, basedir):
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
91
        super(ImmutableStore, self).__init__()
1 by mbp at sourcefrog
import from baz patch-364
92
        self._basedir = basedir
93
1185.1.1 by Martin Pool
- cleanup Store._path
94
    def _path(self, entry_id):
95
        if not isinstance(entry_id, basestring):
96
            raise TypeError(type(entry_id))
97
        if '\\' in entry_id or '/' in entry_id:
98
            raise ValueError("invalid store id %r" % entry_id)
99
        return os.path.join(self._basedir, entry_id)
1 by mbp at sourcefrog
import from baz patch-364
100
101
    def __repr__(self):
102
        return "%s(%r)" % (self.__class__.__name__, self._basedir)
103
129 by mbp at sourcefrog
Store.add defaults to adding gzipped files
104
    def add(self, f, fileid, compressed=True):
1 by mbp at sourcefrog
import from baz patch-364
105
        """Add contents of a file into the store.
106
254 by Martin Pool
- Doc cleanups from Magnus Therning
107
        f -- An open file, or file-like object."""
716 by Martin Pool
- write into store using AtomicFile
108
        # FIXME: Only works on files that will fit in memory
109
        
110
        from bzrlib.atomicfile import AtomicFile
111
        
1 by mbp at sourcefrog
import from baz patch-364
112
        mutter("add store entry %r" % (fileid))
113
        if isinstance(f, types.StringTypes):
114
            content = f
115
        else:
116
            content = f.read()
716 by Martin Pool
- write into store using AtomicFile
117
            
129 by mbp at sourcefrog
Store.add defaults to adding gzipped files
118
        p = self._path(fileid)
119
        if os.access(p, os.F_OK) or os.access(p + '.gz', os.F_OK):
694 by Martin Pool
- weed out all remaining calls to bailout() and remove the function
120
            raise BzrError("store %r already contains id %r" % (self._basedir, fileid))
129 by mbp at sourcefrog
Store.add defaults to adding gzipped files
121
716 by Martin Pool
- write into store using AtomicFile
122
        fn = p
129 by mbp at sourcefrog
Store.add defaults to adding gzipped files
123
        if compressed:
716 by Martin Pool
- write into store using AtomicFile
124
            fn = fn + '.gz'
129 by mbp at sourcefrog
Store.add defaults to adding gzipped files
125
            
716 by Martin Pool
- write into store using AtomicFile
126
        af = AtomicFile(fn, 'wb')
127
        try:
128
            if compressed:
129
                gf = gzip.GzipFile(mode='wb', fileobj=af)
130
                gf.write(content)
131
                gf.close()
132
            else:
133
                af.write(content)
134
            af.commit()
135
        finally:
136
            af.close()
1 by mbp at sourcefrog
import from baz patch-364
137
670 by Martin Pool
- Show progress while branching
138
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
139
    def copy_multi(self, other, ids, permit_failure=False):
626 by Martin Pool
- add Store.copy_multi for use in pulling changes into a branch
140
        """Copy texts for ids from other into self.
141
1116 by Martin Pool
- fix a few errors in new merge code
142
        If an id is present in self, it is skipped.
143
144
        Returns (count_copied, failed), where failed is a collection of ids
145
        that could not be copied.
626 by Martin Pool
- add Store.copy_multi for use in pulling changes into a branch
146
        """
1104 by Martin Pool
- Add a simple UIFactory
147
        pb = bzrlib.ui.ui_factory.progress_bar()
148
        
670 by Martin Pool
- Show progress while branching
149
        pb.update('preparing to copy')
150
        to_copy = [id for id in ids if id not in self]
790 by Martin Pool
Merge from aaron:
151
        if isinstance(other, ImmutableStore):
152
            return self.copy_multi_immutable(other, to_copy, pb)
626 by Martin Pool
- add Store.copy_multi for use in pulling changes into a branch
153
        count = 0
974.2.7 by aaron.bentley at utoronto
Merged from bzr.24
154
        failed = set()
670 by Martin Pool
- Show progress while branching
155
        for id in to_copy:
156
            count += 1
157
            pb.update('copy', count, len(to_copy))
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
158
            if not permit_failure:
159
                self.add(other[id], id)
160
            else:
161
                try:
162
                    entry = other[id]
163
                except IndexError:
974.2.7 by aaron.bentley at utoronto
Merged from bzr.24
164
                    failed.add(id)
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
165
                    continue
166
                self.add(entry, id)
167
                
974.2.7 by aaron.bentley at utoronto
Merged from bzr.24
168
        if not permit_failure:
169
            assert count == len(to_copy)
670 by Martin Pool
- Show progress while branching
170
        pb.clear()
974.2.7 by aaron.bentley at utoronto
Merged from bzr.24
171
        return count, failed
172
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
173
    def copy_multi_immutable(self, other, to_copy, pb, permit_failure=False):
790 by Martin Pool
Merge from aaron:
174
        from shutil import copyfile
175
        count = 0
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
176
        failed = set()
790 by Martin Pool
Merge from aaron:
177
        for id in to_copy:
178
            p = self._path(id)
179
            other_p = other._path(id)
180
            try:
181
                copyfile(other_p, p)
182
            except IOError, e:
183
                if e.errno == errno.ENOENT:
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
184
                    if not permit_failure:
185
                        copyfile(other_p+".gz", p+".gz")
186
                    else:
187
                        try:
188
                            copyfile(other_p+".gz", p+".gz")
189
                        except IOError, e:
190
                            if e.errno == errno.ENOENT:
191
                                failed.add(id)
192
                            else:
193
                                raise
790 by Martin Pool
Merge from aaron:
194
                else:
195
                    raise
196
            
197
            count += 1
198
            pb.update('copy', count, len(to_copy))
199
        assert count == len(to_copy)
200
        pb.clear()
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
201
        return count, failed
1 by mbp at sourcefrog
import from baz patch-364
202
203
    def __contains__(self, fileid):
204
        """"""
128 by mbp at sourcefrog
More support for compressed files in stores
205
        p = self._path(fileid)
206
        return (os.access(p, os.R_OK)
207
                or os.access(p + '.gz', os.R_OK))
1 by mbp at sourcefrog
import from baz patch-364
208
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
209
    def _item_size(self, fid):
210
        p = self._path(fid)
211
        try:
212
            return os.stat(p)[ST_SIZE]
213
        except OSError:
214
            return os.stat(p + '.gz')[ST_SIZE]
1 by mbp at sourcefrog
import from baz patch-364
215
216
    def __iter__(self):
128 by mbp at sourcefrog
More support for compressed files in stores
217
        for f in os.listdir(self._basedir):
218
            if f[-3:] == '.gz':
219
                # TODO: case-insensitive?
220
                yield f[:-3]
221
            else:
222
                yield f
1 by mbp at sourcefrog
import from baz patch-364
223
80 by mbp at sourcefrog
show_info: Show number of entries in the branch stores
224
    def __len__(self):
225
        return len(os.listdir(self._basedir))
226
1 by mbp at sourcefrog
import from baz patch-364
227
    def __getitem__(self, fileid):
228
        """Returns a file reading from a particular entry."""
127 by mbp at sourcefrog
- store support for retrieving compressed files
229
        p = self._path(fileid)
230
        try:
231
            return gzip.GzipFile(p + '.gz', 'rb')
232
        except IOError, e:
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
233
            if e.errno != errno.ENOENT:
234
                raise
235
236
        try:
237
            return file(p, 'rb')
238
        except IOError, e:
239
            if e.errno != errno.ENOENT:
240
                raise
241
242
        raise IndexError(fileid)
243
1 by mbp at sourcefrog
import from baz patch-364
244
245
class ImmutableScratchStore(ImmutableStore):
246
    """Self-destructing test subclass of ImmutableStore.
247
248
    The Store only exists for the lifetime of the Python object.
711 by Martin Pool
- store docs
249
 Obviously you should not put anything precious in it.
1 by mbp at sourcefrog
import from baz patch-364
250
    """
251
    def __init__(self):
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
252
        super(ImmutableScratchStore, self).__init__(tempfile.mkdtemp())
1 by mbp at sourcefrog
import from baz patch-364
253
254
    def __del__(self):
130 by mbp at sourcefrog
- fixup checks on retrieved files to cope with compression,
255
        for f in os.listdir(self._basedir):
163 by mbp at sourcefrog
merge win32 portability fixes
256
            fpath = os.path.join(self._basedir, f)
257
            # needed on windows, and maybe some other filesystems
258
            os.chmod(fpath, 0600)
259
            os.remove(fpath)
130 by mbp at sourcefrog
- fixup checks on retrieved files to cope with compression,
260
        os.rmdir(self._basedir)
261
        mutter("%r destroyed" % self)
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
262
263
264
class ImmutableMemoryStore(Store):
265
    """A memory only store."""
266
267
    def __init__(self):
268
        super(ImmutableMemoryStore, self).__init__()
269
        self._contents = {}
270
271
    def add(self, stream, fileid, compressed=True):
272
        if self._contents.has_key(fileid):
273
            raise StoreError("fileid %s already in the store" % fileid)
274
        self._contents[fileid] = stream.read()
275
276
    def __getitem__(self, fileid):
277
        """Returns a file reading from a particular entry."""
278
        if not self._contents.has_key(fileid):
279
            raise IndexError
280
        return StringIO(self._contents[fileid])
281
282
    def _item_size(self, fileid):
283
        return len(self._contents[fileid])
284
285
    def __iter__(self):
286
        return iter(self._contents.keys())
1092.2.2 by Robert Collins
move RemoteStore to store.py
287
288
289
class RemoteStore(object):
290
291
    def __init__(self, baseurl):
292
        self._baseurl = baseurl
293
294
    def _path(self, name):
295
        if '/' in name:
296
            raise ValueError('invalid store id', name)
297
        return self._baseurl + '/' + name
298
        
299
    def __getitem__(self, fileid):
300
        p = self._path(fileid)
301
        try:
302
            return get_url(p, compressed=True)
303
        except:
304
            raise KeyError(fileid)
1092.2.3 by Robert Collins
move CachedStore into store.py
305
306
307
class CachedStore:
308
    """A store that caches data locally, to avoid repeated downloads.
309
    The precacache method should be used to avoid server round-trips for
310
    every piece of data.
311
    """
312
313
    def __init__(self, store, cache_dir):
314
        self.source_store = store
315
        self.cache_store = ImmutableStore(cache_dir)
316
317
    def __getitem__(self, id):
318
        mutter("Cache add %s" % id)
319
        if id not in self.cache_store:
320
            self.cache_store.add(self.source_store[id], id)
321
        return self.cache_store[id]
322
323
    def prefetch(self, ids):
324
        """Copy a series of ids into the cache, before they are used.
325
        For remote stores that support pipelining or async downloads, this can
326
        increase speed considerably.
327
        """
328
        mutter("Prefetch of ids %s" % ",".join(ids))
329
        self.cache_store.copy_multi(self.source_store, ids)