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
18
17
"""Locking using OS file locks or file existence.
20
19
Note: This method of locking is generally deprecated in favour of LockDir, but
36
from __future__ import absolute_import
48
from bzrlib.hooks import HookPoint, Hooks
50
from brzlib.hooks import Hooks
51
from brzlib.i18n import gettext
51
53
class LockHooks(Hooks):
53
55
def __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))
56
Hooks.__init__(self, "brzlib.lock", "Lock.hooks")
57
self.add_hook('lock_acquired',
58
"Called with a brzlib.lock.LockResult when a physical lock is "
60
self.add_hook('lock_released',
61
"Called with a brzlib.lock.LockResult when a physical lock is "
63
self.add_hook('lock_broken',
64
"Called with a brzlib.lock.LockResult when a physical lock is "
66
68
class Lock(object):
88
90
self.lock_url, self.details)
93
class LogicalLockResult(object):
94
"""The result of a lock_read/lock_write/lock_tree_write call on lockables.
96
:ivar unlock: A callable which will unlock the lock.
99
def __init__(self, unlock):
103
return "LogicalLockResult(%s)" % (self.unlock)
91
107
def cant_unlock_not_held(locked_object):
92
108
"""An attempt to unlock failed because the object was not locked.
536
546
type_name = 'read'
538
548
type_name = 'write'
539
trace.note('%r was %s locked again', self, type_name)
549
trace.note(gettext('{0!r} was {1} locked again'), self, type_name)
540
550
self._prev_lock = lock_type
552
@contextlib.contextmanager
553
def write_locked(lockable):
554
lockable.lock_write()