~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-13 00:26:41 UTC
  • mto: This revision was merged to the branch mainline in revision 5498.
  • Revision ID: andrew.bennetts@canonical.com-20101013002641-9tlh9k89mlj1666m
Keep docs-plain working.

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
37
import errno
39
38
import os
40
39
import sys
46
45
    osutils,
47
46
    trace,
48
47
    )
49
 
from bzrlib.hooks import Hooks
50
 
from bzrlib.i18n import gettext
 
48
from bzrlib.hooks import HookPoint, Hooks
 
49
 
51
50
 
52
51
class LockHooks(Hooks):
53
52
 
54
53
    def __init__(self):
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))
 
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))
65
64
 
66
65
 
67
66
class Lock(object):
172
171
            self.f.close()
173
172
            self.f = None
174
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
 
175
180
    def unlock(self):
176
181
        raise NotImplementedError()
177
182
 
536
541
    locked the same way), and -Drelock is set, then this will trace.note a
537
542
    message about it.
538
543
    """
539
 
 
 
544
    
540
545
    _prev_lock = None
541
546
 
542
547
    def _note_lock(self, lock_type):
545
550
                type_name = 'read'
546
551
            else:
547
552
                type_name = 'write'
548
 
            trace.note(gettext('{0!r} was {1} locked again'), self, type_name)
 
553
            trace.note('%r was %s locked again', self, type_name)
549
554
        self._prev_lock = lock_type
550
555