~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-03-25 00:02:51 UTC
  • mfrom: (5106.1.1 version-bump)
  • Revision ID: pqm@pqm.ubuntu.com-20100325000251-bwsv5c5d3l9x3lnn
(Jelmer) Bump API version for 2.2.0.

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):
86
84
        return self.lock_url == other.lock_url and self.details == other.details
87
85
 
88
86
    def __repr__(self):
89
 
        return '%s(%s, %s)' % (self.__class__.__name__,
 
87
        return '%s(%s%s)' % (self.__class__.__name__,
90
88
                             self.lock_url, self.details)
91
89
 
92
90
 
93
 
class LogicalLockResult(object):
94
 
    """The result of a lock_read/lock_write/lock_tree_write call on lockables.
95
 
 
96
 
    :ivar unlock: A callable which will unlock the lock.
97
 
    """
98
 
 
99
 
    def __init__(self, unlock):
100
 
        self.unlock = unlock
101
 
 
102
 
    def __repr__(self):
103
 
        return "LogicalLockResult(%s)" % (self.unlock)
104
 
 
105
 
 
106
 
 
107
91
def cant_unlock_not_held(locked_object):
108
92
    """An attempt to unlock failed because the object was not locked.
109
93
 
173
157
            self.f.close()
174
158
            self.f = None
175
159
 
 
160
    def __del__(self):
 
161
        if self.f:
 
162
            from warnings import warn
 
163
            warn("lock on %r not released" % self.f)
 
164
            self.unlock()
 
165
 
176
166
    def unlock(self):
177
167
        raise NotImplementedError()
178
168
 
537
527
    locked the same way), and -Drelock is set, then this will trace.note a
538
528
    message about it.
539
529
    """
540
 
 
 
530
    
541
531
    _prev_lock = None
542
532
 
543
533
    def _note_lock(self, lock_type):
546
536
                type_name = 'read'
547
537
            else:
548
538
                type_name = 'write'
549
 
            trace.note(gettext('{0!r} was {1} locked again'), self, type_name)
 
539
            trace.note('%r was %s locked again', self, type_name)
550
540
        self._prev_lock = lock_type
551
541
 
552
 
@contextlib.contextmanager
553
 
def write_locked(lockable):
554
 
    lockable.lock_write()
555
 
    try:
556
 
        yield lockable
557
 
    finally:
558
 
        lockable.unlock()