/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/lock.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-10 01:35:53 UTC
  • mto: (6670.4.8 move-bzr)
  • mto: This revision was merged to the branch mainline in revision 6681.
  • Revision ID: jelmer@jelmer.uk-20170610013553-560y7mn3su4pp763
Fix remaining tests.

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
40
42
import warnings
41
43
 
42
 
from bzrlib import (
 
44
from . import (
43
45
    debug,
44
46
    errors,
45
47
    osutils,
46
48
    trace,
47
49
    )
48
 
from bzrlib.hooks import HookPoint, Hooks
49
 
 
 
50
from .hooks import Hooks
 
51
from .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, "breezy.lock", "Lock.hooks")
 
57
        self.add_hook('lock_acquired',
 
58
            "Called with a breezy.lock.LockResult when a physical lock is "
 
59
            "acquired.", (1, 8))
 
60
        self.add_hook('lock_released',
 
61
            "Called with a breezy.lock.LockResult when a physical lock is "
 
62
            "released.", (1, 8))
 
63
        self.add_hook('lock_broken',
 
64
            "Called with a breezy.lock.LockResult when a physical lock is "
 
65
            "broken.", (1, 15))
64
66
 
65
67
 
66
68
class Lock(object):
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
 
139
155
        try:
140
156
            self.f = open(self.filename, filemode)
141
157
            return self.f
142
 
        except IOError, e:
 
158
        except IOError as e:
143
159
            if e.errno in (errno.EACCES, errno.EPERM):
144
160
                raise errors.LockFailed(self.filename, str(e))
145
161
            if e.errno != errno.ENOENT:
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
 
207
217
                # LOCK_NB will cause IOError to be raised if we can't grab a
208
218
                # lock right away.
209
219
                fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB)
210
 
            except IOError, e:
 
220
            except IOError as e:
211
221
                if e.errno in (errno.EAGAIN, errno.EACCES):
212
222
                    # We couldn't grab the lock
213
223
                    self.unlock()
242
252
                # LOCK_NB will cause IOError to be raised if we can't grab a
243
253
                # lock right away.
244
254
                fcntl.lockf(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB)
245
 
            except IOError, e:
 
255
            except IOError as e:
246
256
                # we should be more precise about whats a locking
247
257
                # error and whats a random-other error
248
258
                raise errors.LockContention(self.filename, e)
303
313
            # done by _fcntl_ReadLock
304
314
            try:
305
315
                new_f = open(self.filename, 'rb+')
306
 
            except IOError, e:
 
316
            except IOError as e:
307
317
                if e.errno in (errno.EACCES, errno.EPERM):
308
318
                    raise errors.LockFailed(self.filename, str(e))
309
319
                raise
311
321
                # LOCK_NB will cause IOError to be raised if we can't grab a
312
322
                # lock right away.
313
323
                fcntl.lockf(new_f, fcntl.LOCK_EX | fcntl.LOCK_NB)
314
 
            except IOError, e:
 
324
            except IOError as e:
315
325
                # TODO: Raise a more specific error based on the type of error
316
326
                raise errors.LockContention(self.filename, e)
317
327
            _fcntl_WriteLock._open_locks.add(self.filename)
335
345
 
336
346
 
337
347
if have_pywin32 and sys.platform == 'win32':
338
 
    if os.path.supports_unicode_filenames:
339
 
        # for Windows NT/2K/XP/etc
340
 
        win32file_CreateFile = win32file.CreateFileW
341
 
    else:
342
 
        # for Windows 98
343
 
        win32file_CreateFile = win32file.CreateFile
 
348
    win32file_CreateFile = win32file.CreateFileW
344
349
 
345
350
    class _w32c_FileLock(_OSLock):
346
351
 
350
355
                self._handle = win32file_CreateFile(filename, access, share,
351
356
                    None, win32file.OPEN_ALWAYS,
352
357
                    win32file.FILE_ATTRIBUTE_NORMAL, None)
353
 
            except pywintypes.error, e:
 
358
            except pywintypes.error as e:
354
359
                if e.args[0] == winerror.ERROR_ACCESS_DENIED:
355
360
                    raise errors.LockFailed(filename, e)
356
361
                if e.args[0] == winerror.ERROR_SHARING_VIOLATION:
413
418
    from ctypes.wintypes import DWORD, LPCSTR, LPCWSTR
414
419
    LPSECURITY_ATTRIBUTES = ctypes.c_void_p # used as NULL no need to declare
415
420
    HANDLE = ctypes.c_int # rather than unsigned as in ctypes.wintypes
416
 
    if os.path.supports_unicode_filenames:
417
 
        _function_name = "CreateFileW"
418
 
        LPTSTR = LPCWSTR
419
 
    else:
420
 
        _function_name = "CreateFileA"
421
 
        class LPTSTR(LPCSTR):
422
 
            def __new__(cls, obj):
423
 
                return LPCSTR.__new__(cls, obj.encode("mbcs"))
 
421
    _function_name = "CreateFileW"
424
422
 
425
423
    # CreateFile <http://msdn.microsoft.com/en-us/library/aa363858.aspx>
426
424
    _CreateFile = ctypes.WINFUNCTYPE(
427
425
            HANDLE,                # return value
428
 
            LPTSTR,                # lpFileName
 
426
            LPWSTR,                # lpFileName
429
427
            DWORD,                 # dwDesiredAccess
430
428
            DWORD,                 # dwShareMode
431
429
            LPSECURITY_ATTRIBUTES, # lpSecurityAttributes
527
525
    locked the same way), and -Drelock is set, then this will trace.note a
528
526
    message about it.
529
527
    """
530
 
    
 
528
 
531
529
    _prev_lock = None
532
530
 
533
531
    def _note_lock(self, lock_type):
536
534
                type_name = 'read'
537
535
            else:
538
536
                type_name = 'write'
539
 
            trace.note('%r was %s locked again', self, type_name)
 
537
            trace.note(gettext('{0!r} was {1} locked again'), self, type_name)
540
538
        self._prev_lock = lock_type
541
539
 
 
540
@contextlib.contextmanager
 
541
def write_locked(lockable):
 
542
    lockable.lock_write()
 
543
    try:
 
544
        yield lockable
 
545
    finally:
 
546
        lockable.unlock()