/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/store.py

  • Committer: Martin Pool
  • Date: 2005-06-20 04:24:35 UTC
  • Revision ID: mbp@sourcefrog.net-20050620042435-7c315b5a93001b89
- add jk's patchwork client

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
        """Add contents of a file into the store.
79
79
 
80
80
        f -- An open file, or file-like object."""
81
 
        # FIXME: Only works on files that will fit in memory
82
 
        
83
 
        from bzrlib.atomicfile import AtomicFile
84
 
        
 
81
        # FIXME: Only works on smallish files
 
82
        # TODO: Can be optimized by copying at the same time as
 
83
        # computing the sum.
85
84
        mutter("add store entry %r" % (fileid))
86
85
        if isinstance(f, types.StringTypes):
87
86
            content = f
88
87
        else:
89
88
            content = f.read()
90
 
            
 
89
 
91
90
        p = self._path(fileid)
92
91
        if os.access(p, os.F_OK) or os.access(p + '.gz', os.F_OK):
93
 
            from bzrlib.errors import bailout
94
92
            raise BzrError("store %r already contains id %r" % (self._basedir, fileid))
95
93
 
96
 
        fn = p
97
94
        if compressed:
98
 
            fn = fn + '.gz'
 
95
            f = gzip.GzipFile(p + '.gz', 'wb')
 
96
        else:
 
97
            f = file(p, 'wb')
99
98
            
100
 
        af = AtomicFile(fn, 'wb')
101
 
        try:
102
 
            if compressed:
103
 
                gf = gzip.GzipFile(mode='wb', fileobj=af)
104
 
                gf.write(content)
105
 
                gf.close()
106
 
            else:
107
 
                af.write(content)
108
 
            af.commit()
109
 
        finally:
110
 
            af.close()
 
99
        f.write(content)
 
100
        f.close()
111
101
 
112
102
 
113
103
    def copy_multi(self, other, ids):
120
110
        pb = ProgressBar()
121
111
        pb.update('preparing to copy')
122
112
        to_copy = [id for id in ids if id not in self]
123
 
        if isinstance(other, ImmutableStore):
124
 
            return self.copy_multi_immutable(other, to_copy, pb)
125
113
        count = 0
126
114
        for id in to_copy:
127
115
            count += 1
130
118
        assert count == len(to_copy)
131
119
        pb.clear()
132
120
        return count
133
 
 
134
 
 
135
 
    def copy_multi_immutable(self, other, to_copy, pb):
136
 
        from shutil import copyfile
137
 
        count = 0
138
 
        for id in to_copy:
139
 
            p = self._path(id)
140
 
            other_p = other._path(id)
141
 
            try:
142
 
                copyfile(other_p, p)
143
 
            except IOError, e:
144
 
                if e.errno == errno.ENOENT:
145
 
                    copyfile(other_p+".gz", p+".gz")
146
 
                else:
147
 
                    raise
148
 
            
149
 
            count += 1
150
 
            pb.update('copy', count, len(to_copy))
151
 
        assert count == len(to_copy)
152
 
        pb.clear()
153
 
        return count
154
121
    
155
122
 
156
123
    def __contains__(self, fileid):
172
139
    def __len__(self):
173
140
        return len(os.listdir(self._basedir))
174
141
 
175
 
 
176
142
    def __getitem__(self, fileid):
177
143
        """Returns a file reading from a particular entry."""
178
144
        p = self._path(fileid)
179
145
        try:
180
146
            return gzip.GzipFile(p + '.gz', 'rb')
181
147
        except IOError, e:
182
 
            if e.errno != errno.ENOENT:
183
 
                raise
184
 
 
185
 
        try:
186
 
            return file(p, 'rb')
187
 
        except IOError, e:
188
 
            if e.errno != errno.ENOENT:
189
 
                raise
190
 
 
191
 
        raise IndexError(fileid)
192
 
 
 
148
            if e.errno == errno.ENOENT:
 
149
                return file(p, 'rb')
 
150
            else:
 
151
                raise e
193
152
 
194
153
    def total_size(self):
195
154
        """Return (count, bytes)