~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Vincent Ladeuil
  • Date: 2012-03-09 16:48:55 UTC
  • mto: (6437.23.24 2.5)
  • mto: This revision was merged to the branch mainline in revision 6499.
  • Revision ID: v.ladeuil+lp@free.fr-20120309164855-htdn25hp7x65mmir
Rely on sphinx for texinfo doc generation

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
 
37
38
import errno
38
39
import os
39
40
import sys
45
46
    osutils,
46
47
    trace,
47
48
    )
48
 
from bzrlib.hooks import HookPoint, Hooks
49
 
 
 
49
from bzrlib.hooks import Hooks
 
50
from bzrlib.i18n import gettext
50
51
 
51
52
class LockHooks(Hooks):
52
53
 
53
54
    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))
 
55
        Hooks.__init__(self, "bzrlib.lock", "Lock.hooks")
 
56
        self.add_hook('lock_acquired',
 
57
            "Called with a bzrlib.lock.LockResult when a physical lock is "
 
58
            "acquired.", (1, 8))
 
59
        self.add_hook('lock_released',
 
60
            "Called with a bzrlib.lock.LockResult when a physical lock is "
 
61
            "released.", (1, 8))
 
62
        self.add_hook('lock_broken',
 
63
            "Called with a bzrlib.lock.LockResult when a physical lock is "
 
64
            "broken.", (1, 15))
64
65
 
65
66
 
66
67
class Lock(object):
84
85
        return self.lock_url == other.lock_url and self.details == other.details
85
86
 
86
87
    def __repr__(self):
87
 
        return '%s(%s%s)' % (self.__class__.__name__,
 
88
        return '%s(%s, %s)' % (self.__class__.__name__,
88
89
                             self.lock_url, self.details)
89
90
 
90
91
 
 
92
class LogicalLockResult(object):
 
93
    """The result of a lock_read/lock_write/lock_tree_write call on lockables.
 
94
 
 
95
    :ivar unlock: A callable which will unlock the lock.
 
96
    """
 
97
 
 
98
    def __init__(self, unlock):
 
99
        self.unlock = unlock
 
100
 
 
101
    def __repr__(self):
 
102
        return "LogicalLockResult(%s)" % (self.unlock)
 
103
 
 
104
 
 
105
 
91
106
def cant_unlock_not_held(locked_object):
92
107
    """An attempt to unlock failed because the object was not locked.
93
108
 
157
172
            self.f.close()
158
173
            self.f = None
159
174
 
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
175
    def unlock(self):
167
176
        raise NotImplementedError()
168
177
 
527
536
    locked the same way), and -Drelock is set, then this will trace.note a
528
537
    message about it.
529
538
    """
530
 
    
 
539
 
531
540
    _prev_lock = None
532
541
 
533
542
    def _note_lock(self, lock_type):
536
545
                type_name = 'read'
537
546
            else:
538
547
                type_name = 'write'
539
 
            trace.note('%r was %s locked again', self, type_name)
 
548
            trace.note(gettext('{0!r} was {1} locked again'), self, type_name)
540
549
        self._prev_lock = lock_type
541
550