~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Aaron Bentley
  • Date: 2007-12-09 23:53:50 UTC
  • mto: This revision was merged to the branch mainline in revision 3133.
  • Revision ID: aaron.bentley@utoronto.ca-20071209235350-qp39yk0xzx7a4f6p
Don't use the base if not cherrypicking

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
42
42
    osutils,
43
43
    trace,
44
44
    )
45
 
from bzrlib.hooks import Hooks
46
 
 
47
 
 
48
 
class LockHooks(Hooks):
49
 
 
50
 
    def __init__(self):
51
 
        Hooks.__init__(self)
52
 
 
53
 
        # added in 1.8; called with a LockResult when a physical lock is
54
 
        # acquired
55
 
        self['lock_acquired'] = []
56
 
 
57
 
        # added in 1.8; called with a LockResult when a physical lock is
58
 
        # acquired
59
 
        self['lock_released'] = []
60
 
 
61
 
 
62
 
class Lock(object):
63
 
    """Base class for locks.
64
 
 
65
 
    :cvar hooks: Hook dictionary for operations on locks.
66
 
    """
67
 
 
68
 
    hooks = LockHooks()
69
 
 
70
 
 
71
 
class LockResult(object):
72
 
    """Result of an operation on a lock; passed to a hook"""
73
 
 
74
 
    def __init__(self, lock_url, details=None):
75
 
        """Create a lock result for lock with optional details about the lock."""
76
 
        self.lock_url = lock_url
77
 
        self.details = details
78
 
 
79
 
    def __eq__(self, other):
80
 
        return self.lock_url == other.lock_url and self.details == other.details
81
 
 
82
 
 
83
 
try:
84
 
    import fcntl
85
 
    have_fcntl = True
86
 
except ImportError:
87
 
    have_fcntl = False
88
 
 
89
 
have_pywin32 = False
90
 
have_ctypes_win32 = False
91
 
if sys.platform == 'win32':
92
 
    import msvcrt
93
 
    try:
94
 
        import win32con, win32file, pywintypes, winerror
95
 
        have_pywin32 = True
96
 
    except ImportError:
97
 
        pass
98
 
 
99
 
    try:
100
 
        import ctypes
101
 
        have_ctypes_win32 = True
102
 
    except ImportError:
103
 
        pass
104
45
 
105
46
 
106
47
class _OSLock(object):
142
83
        raise NotImplementedError()
143
84
 
144
85
 
 
86
try:
 
87
    import fcntl
 
88
    have_fcntl = True
 
89
except ImportError:
 
90
    have_fcntl = False
 
91
try:
 
92
    import win32con, win32file, pywintypes, winerror, msvcrt
 
93
    have_pywin32 = True
 
94
except ImportError:
 
95
    have_pywin32 = False
 
96
try:
 
97
    import ctypes, msvcrt
 
98
    have_ctypes = True
 
99
except ImportError:
 
100
    have_ctypes = False
 
101
 
 
102
 
145
103
_lock_classes = []
146
104
 
147
105
 
229
187
 
230
188
            :return: A token which can be used to switch back to a read lock.
231
189
            """
232
 
            if self.filename in _fcntl_WriteLock._open_locks:
233
 
                raise AssertionError('file already locked: %r'
234
 
                    % (self.filename,))
 
190
            assert self.filename not in _fcntl_WriteLock._open_locks
235
191
            try:
236
192
                wlock = _fcntl_TemporaryWriteLock(self)
237
193
            except errors.LockError:
257
213
                # write lock.
258
214
                raise errors.LockContention(self.filename)
259
215
 
260
 
            if self.filename in _fcntl_WriteLock._open_locks:
261
 
                raise AssertionError('file already locked: %r'
262
 
                    % (self.filename,))
 
216
            assert self.filename not in _fcntl_WriteLock._open_locks
263
217
 
264
218
            # See if we can open the file for writing. Another process might
265
219
            # have a read lock. We don't use self._open() because we don't want
374
328
    _lock_classes.append(('pywin32', _w32c_WriteLock, _w32c_ReadLock))
375
329
 
376
330
 
377
 
if have_ctypes_win32:
 
331
if have_ctypes and sys.platform == 'win32':
378
332
    # These constants were copied from the win32con.py module.
379
333
    LOCKFILE_FAIL_IMMEDIATELY = 1
380
334
    LOCKFILE_EXCLUSIVE_LOCK = 2