~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

(vila) Make all transport put_bytes() raises TypeError when given unicode
 strings rather than bytes (Vincent Ladeuil)

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
 
 
18
17
"""Locking using OS file locks or file existence.
19
18
 
20
19
Note: This method of locking is generally deprecated in favour of LockDir, but
34
33
unlock() method.
35
34
"""
36
35
 
 
36
from __future__ import absolute_import
 
37
 
 
38
import contextlib
37
39
import errno
38
40
import os
39
41
import sys
45
47
    osutils,
46
48
    trace,
47
49
    )
48
 
from bzrlib.hooks import HookPoint, Hooks
49
 
 
 
50
from bzrlib.hooks import Hooks
 
51
from bzrlib.i18n import gettext
50
52
 
51
53
class LockHooks(Hooks):
52
54
 
53
55
    def __init__(self):
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))
 
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))
64
66
 
65
67
 
66
68
class Lock(object):
84
86
        return self.lock_url == other.lock_url and self.details == other.details
85
87
 
86
88
    def __repr__(self):
87
 
        return '%s(%s%s)' % (self.__class__.__name__,
 
89
        return '%s(%s, %s)' % (self.__class__.__name__,
88
90
                             self.lock_url, self.details)
89
91
 
90
92
 
 
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
 
91
107
def cant_unlock_not_held(locked_object):
92
108
    """An attempt to unlock failed because the object was not locked.
93
109
 
157
173
            self.f.close()
158
174
            self.f = None
159
175
 
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
 
 
166
176
    def unlock(self):
167
177
        raise NotImplementedError()
168
178
 
527
537
    locked the same way), and -Drelock is set, then this will trace.note a
528
538
    message about it.
529
539
    """
530
 
    
 
540
 
531
541
    _prev_lock = None
532
542
 
533
543
    def _note_lock(self, lock_type):
536
546
                type_name = 'read'
537
547
            else:
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
541
551
 
 
552
@contextlib.contextmanager
 
553
def write_locked(lockable):
 
554
    lockable.lock_write()
 
555
    try:
 
556
        yield lockable
 
557
    finally:
 
558
        lockable.unlock()