~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store.py

  • Committer: Martin Pool
  • Date: 2005-06-17 07:28:53 UTC
  • Revision ID: mbp@sourcefrog.net-20050617072853-a815535b877b1ac7
- weed out all remaining calls to bailout() and remove the function

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env python
2
 
# -*- coding: UTF-8 -*-
 
1
 
3
2
 
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
35
34
    pass
36
35
 
37
36
 
38
 
class ImmutableStore:
 
37
class ImmutableStore(object):
39
38
    """Store that holds files indexed by unique names.
40
39
 
41
40
    Files can be added, but not modified once they are in.  Typically
58
57
    >>> st['123123'].read()
59
58
    'goodbye'
60
59
 
61
 
    :todo: Atomic add by writing to a temporary file and renaming.
 
60
    TODO: Atomic add by writing to a temporary file and renaming.
62
61
 
63
 
    :todo: Perhaps automatically transform to/from XML in a method?
 
62
    TODO: Perhaps automatically transform to/from XML in a method?
64
63
           Would just need to tell the constructor what class to
65
64
           use...
66
65
 
67
 
    :todo: Even within a simple disk store like this, we could
 
66
    TODO: Even within a simple disk store like this, we could
68
67
           gzip the files.  But since many are less than one disk
69
68
           block, that might not help a lot.
70
69
 
75
74
        self._basedir = basedir
76
75
 
77
76
    def _path(self, id):
 
77
        assert '/' not in id
78
78
        return os.path.join(self._basedir, id)
79
79
 
80
80
    def __repr__(self):
83
83
    def add(self, f, fileid, compressed=True):
84
84
        """Add contents of a file into the store.
85
85
 
86
 
        :param f: An open file, or file-like object."""
 
86
        f -- An open file, or file-like object."""
87
87
        # FIXME: Only works on smallish files
88
88
        # TODO: Can be optimized by copying at the same time as
89
89
        # computing the sum.
95
95
 
96
96
        p = self._path(fileid)
97
97
        if os.access(p, os.F_OK) or os.access(p + '.gz', os.F_OK):
98
 
            bailout("store %r already contains id %r" % (self._basedir, fileid))
 
98
            raise BzrError("store %r already contains id %r" % (self._basedir, fileid))
99
99
 
100
100
        if compressed:
101
101
            f = gzip.GzipFile(p + '.gz', 'wb')
108
108
        f.close()
109
109
 
110
110
 
 
111
    def copy_multi(self, other, ids):
 
112
        """Copy texts for ids from other into self.
 
113
 
 
114
        If an id is present in self, it is skipped.  A count of copied
 
115
        ids is returned, which may be less than len(ids).
 
116
        """
 
117
        from bzrlib.progress import ProgressBar
 
118
        pb = ProgressBar()
 
119
        pb.update('preparing to copy')
 
120
        to_copy = [id for id in ids if id not in self]
 
121
        count = 0
 
122
        for id in to_copy:
 
123
            count += 1
 
124
            pb.update('copy', count, len(to_copy))
 
125
            self.add(other[id], id)
 
126
        assert count == len(to_copy)
 
127
        pb.clear()
 
128
        return count
 
129
    
 
130
 
111
131
    def __contains__(self, fileid):
112
132
        """"""
113
133
        p = self._path(fileid)
169
189
 
170
190
    def __del__(self):
171
191
        for f in os.listdir(self._basedir):
172
 
            os.remove(os.path.join(self._basedir, f))
 
192
            fpath = os.path.join(self._basedir, f)
 
193
            # needed on windows, and maybe some other filesystems
 
194
            os.chmod(fpath, 0600)
 
195
            os.remove(fpath)
173
196
        os.rmdir(self._basedir)
174
197
        mutter("%r destroyed" % self)