~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 08:15:14 UTC
  • mto: This revision was merged to the branch mainline in revision 5498.
  • Revision ID: andrew.bennetts@canonical.com-20101008081514-dviqzrdfwyzsqbz2
Split NEWS into per-release doc/en/release-notes/bzr-*.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
 
17
18
"""Locking using OS file locks or file existence.
18
19
 
19
20
Note: This method of locking is generally deprecated in favour of LockDir, but
33
34
unlock() method.
34
35
"""
35
36
 
36
 
from __future__ import absolute_import
37
 
 
38
 
import contextlib
39
37
import errno
40
38
import os
41
39
import sys
47
45
    osutils,
48
46
    trace,
49
47
    )
50
 
from bzrlib.hooks import Hooks
51
 
from bzrlib.i18n import gettext
 
48
from bzrlib.hooks import HookPoint, Hooks
 
49
 
52
50
 
53
51
class LockHooks(Hooks):
54
52
 
55
53
    def __init__(self):
56
 
        Hooks.__init__(self, "bzrlib.lock", "Lock.hooks")
57
 
        self.add_hook('lock_acquired',
58
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
59
 
            "acquired.", (1, 8))
60
 
        self.add_hook('lock_released',
61
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
62
 
            "released.", (1, 8))
63
 
        self.add_hook('lock_broken',
64
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
65
 
            "broken.", (1, 15))
 
54
        Hooks.__init__(self)
 
55
        self.create_hook(HookPoint('lock_acquired',
 
56
            "Called with a bzrlib.lock.LockResult when a physical lock is "
 
57
            "acquired.", (1, 8), None))
 
58
        self.create_hook(HookPoint('lock_released',
 
59
            "Called with a bzrlib.lock.LockResult when a physical lock is "
 
60
            "released.", (1, 8), None))
 
61
        self.create_hook(HookPoint('lock_broken',
 
62
            "Called with a bzrlib.lock.LockResult when a physical lock is "
 
63
            "broken.", (1, 15), None))
66
64
 
67
65
 
68
66
class Lock(object):
173
171
            self.f.close()
174
172
            self.f = None
175
173
 
 
174
    def __del__(self):
 
175
        if self.f:
 
176
            from warnings import warn
 
177
            warn("lock on %r not released" % self.f)
 
178
            self.unlock()
 
179
 
176
180
    def unlock(self):
177
181
        raise NotImplementedError()
178
182
 
537
541
    locked the same way), and -Drelock is set, then this will trace.note a
538
542
    message about it.
539
543
    """
540
 
 
 
544
    
541
545
    _prev_lock = None
542
546
 
543
547
    def _note_lock(self, lock_type):
546
550
                type_name = 'read'
547
551
            else:
548
552
                type_name = 'write'
549
 
            trace.note(gettext('{0!r} was {1} locked again'), self, type_name)
 
553
            trace.note('%r was %s locked again', self, type_name)
550
554
        self._prev_lock = lock_type
551
555
 
552
 
@contextlib.contextmanager
553
 
def write_locked(lockable):
554
 
    lockable.lock_write()
555
 
    try:
556
 
        yield lockable
557
 
    finally:
558
 
        lockable.unlock()