~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2005-05-31 08:10:44 UTC
  • Revision ID: mbp@sourcefrog.net-20050531081044-0f6d28e39b8e19de
- replace Branch.lock(mode) with separate lock_read and lock_write 
  methods

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
    """Method decorator for functions run with the branch locked."""
51
51
    def d(self, *a, **k):
52
52
        # called with self set to the branch
53
 
        self.lock('w')
 
53
        self.lock_write()
54
54
        try:
55
55
            return method(self, *a, **k)
56
56
        finally:
60
60
 
61
61
def with_readlock(method):
62
62
    def d(self, *a, **k):
63
 
        self.lock('r')
 
63
        self.lock_read()
64
64
        try:
65
65
            return method(self, *a, **k)
66
66
        finally:
200
200
            self.unlock()
201
201
 
202
202
 
203
 
    def lock(self, mode):
204
 
        if self._lock_mode:
205
 
            if mode == 'w' and cur_lm == 'r':
206
 
                raise BzrError("can't upgrade to a write lock")
 
203
 
 
204
    def lock_write(self):
 
205
        if self._lock_mode:
 
206
            if self._lock_mode != 'w':
 
207
                from errors import LockError
 
208
                raise LockError("can't upgrade to a write lock from %r" %
 
209
                                self._lock_mode)
 
210
            self._lock_count += 1
 
211
        else:
 
212
            from bzrlib.lock import lock, LOCK_EX
 
213
 
 
214
            lock(self._lockfile, LOCK_EX)
 
215
            self._lock_mode = 'w'
 
216
            self._lock_count = 1
 
217
 
 
218
 
 
219
 
 
220
    def lock_read(self):
 
221
        if self._lock_mode:
 
222
            assert self._lock_mode in ('r', 'w'), \
 
223
                   "invalid lock mode %r" % self._lock_mode
 
224
            self._lock_count += 1
 
225
        else:
 
226
            from bzrlib.lock import lock, LOCK_SH
 
227
 
 
228
            lock(self._lockfile, LOCK_SH)
 
229
            self._lock_mode = 'r'
 
230
            self._lock_count = 1
 
231
                        
 
232
 
207
233
            
208
 
            assert self._lock_count >= 1
209
 
            self._lock_count += 1
210
 
        else:
211
 
            from bzrlib.lock import lock, LOCK_SH, LOCK_EX
212
 
            if mode == 'r':
213
 
                m = LOCK_SH
214
 
            elif mode == 'w':
215
 
                m = LOCK_EX
216
 
            else:
217
 
                raise ValueError('invalid lock mode %r' % mode)
218
 
 
219
 
            lock(self._lockfile, m)
220
 
            self._lock_mode = mode
221
 
            self._lock_count = 1
222
 
 
223
 
 
224
234
    def unlock(self):
225
235
        if not self._lock_mode:
226
 
            raise BzrError('branch %r is not locked' % (self))
 
236
            from errors import LockError
 
237
            raise LockError('branch %r is not locked' % (self))
227
238
 
228
239
        if self._lock_count > 1:
229
240
            self._lock_count -= 1
794
805
    def destroy(self):
795
806
        """Destroy the test branch, removing the scratch directory."""
796
807
        try:
797
 
            mutter("delete ScratchBranch %s" % self.base)
798
 
            shutil.rmtree(self.base)
 
808
            if self.base:
 
809
                mutter("delete ScratchBranch %s" % self.base)
 
810
                shutil.rmtree(self.base)
799
811
        except OSError, e:
800
812
            # Work around for shutil.rmtree failing on Windows when
801
813
            # readonly files are encountered