/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 bzrlib/lock.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
import sys
42
42
import warnings
43
43
 
44
 
from . import (
 
44
from bzrlib import (
45
45
    debug,
46
46
    errors,
47
47
    osutils,
48
48
    trace,
49
49
    )
50
 
from .hooks import Hooks
51
 
from .i18n import gettext
 
50
from bzrlib.hooks import Hooks
 
51
from bzrlib.i18n import gettext
52
52
 
53
53
class LockHooks(Hooks):
54
54
 
55
55
    def __init__(self):
56
 
        Hooks.__init__(self, "breezy.lock", "Lock.hooks")
 
56
        Hooks.__init__(self, "bzrlib.lock", "Lock.hooks")
57
57
        self.add_hook('lock_acquired',
58
 
            "Called with a breezy.lock.LockResult when a physical lock is "
 
58
            "Called with a bzrlib.lock.LockResult when a physical lock is "
59
59
            "acquired.", (1, 8))
60
60
        self.add_hook('lock_released',
61
 
            "Called with a breezy.lock.LockResult when a physical lock is "
 
61
            "Called with a bzrlib.lock.LockResult when a physical lock is "
62
62
            "released.", (1, 8))
63
63
        self.add_hook('lock_broken',
64
 
            "Called with a breezy.lock.LockResult when a physical lock is "
 
64
            "Called with a bzrlib.lock.LockResult when a physical lock is "
65
65
            "broken.", (1, 15))
66
66
 
67
67
 
96
96
    :ivar unlock: A callable which will unlock the lock.
97
97
    """
98
98
 
99
 
    def __init__(self, unlock, token=None):
 
99
    def __init__(self, unlock):
100
100
        self.unlock = unlock
101
 
        self.token = token
102
101
 
103
102
    def __repr__(self):
104
103
        return "LogicalLockResult(%s)" % (self.unlock)
105
104
 
106
 
    def __enter__(self):
107
 
        return self
108
 
 
109
 
    def __exit__(self, exc_type, exc_val, exc_tb):
110
 
        # If there was an error raised, prefer the original one
111
 
        try:
112
 
            self.unlock()
113
 
        except:
114
 
            if exc_type is None:
115
 
                raise
116
 
        return False
117
105
 
118
106
 
119
107
def cant_unlock_not_held(locked_object):
167
155
        try:
168
156
            self.f = open(self.filename, filemode)
169
157
            return self.f
170
 
        except IOError as e:
 
158
        except IOError, e:
171
159
            if e.errno in (errno.EACCES, errno.EPERM):
172
160
                raise errors.LockFailed(self.filename, str(e))
173
161
            if e.errno != errno.ENOENT:
229
217
                # LOCK_NB will cause IOError to be raised if we can't grab a
230
218
                # lock right away.
231
219
                fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB)
232
 
            except IOError as e:
 
220
            except IOError, e:
233
221
                if e.errno in (errno.EAGAIN, errno.EACCES):
234
222
                    # We couldn't grab the lock
235
223
                    self.unlock()
264
252
                # LOCK_NB will cause IOError to be raised if we can't grab a
265
253
                # lock right away.
266
254
                fcntl.lockf(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB)
267
 
            except IOError as e:
 
255
            except IOError, e:
268
256
                # we should be more precise about whats a locking
269
257
                # error and whats a random-other error
270
258
                raise errors.LockContention(self.filename, e)
325
313
            # done by _fcntl_ReadLock
326
314
            try:
327
315
                new_f = open(self.filename, 'rb+')
328
 
            except IOError as e:
 
316
            except IOError, e:
329
317
                if e.errno in (errno.EACCES, errno.EPERM):
330
318
                    raise errors.LockFailed(self.filename, str(e))
331
319
                raise
333
321
                # LOCK_NB will cause IOError to be raised if we can't grab a
334
322
                # lock right away.
335
323
                fcntl.lockf(new_f, fcntl.LOCK_EX | fcntl.LOCK_NB)
336
 
            except IOError as e:
 
324
            except IOError, e:
337
325
                # TODO: Raise a more specific error based on the type of error
338
326
                raise errors.LockContention(self.filename, e)
339
327
            _fcntl_WriteLock._open_locks.add(self.filename)
357
345
 
358
346
 
359
347
if have_pywin32 and sys.platform == 'win32':
360
 
    win32file_CreateFile = win32file.CreateFileW
 
348
    if os.path.supports_unicode_filenames:
 
349
        # for Windows NT/2K/XP/etc
 
350
        win32file_CreateFile = win32file.CreateFileW
 
351
    else:
 
352
        # for Windows 98
 
353
        win32file_CreateFile = win32file.CreateFile
361
354
 
362
355
    class _w32c_FileLock(_OSLock):
363
356
 
367
360
                self._handle = win32file_CreateFile(filename, access, share,
368
361
                    None, win32file.OPEN_ALWAYS,
369
362
                    win32file.FILE_ATTRIBUTE_NORMAL, None)
370
 
            except pywintypes.error as e:
 
363
            except pywintypes.error, e:
371
364
                if e.args[0] == winerror.ERROR_ACCESS_DENIED:
372
365
                    raise errors.LockFailed(filename, e)
373
366
                if e.args[0] == winerror.ERROR_SHARING_VIOLATION:
430
423
    from ctypes.wintypes import DWORD, LPCSTR, LPCWSTR
431
424
    LPSECURITY_ATTRIBUTES = ctypes.c_void_p # used as NULL no need to declare
432
425
    HANDLE = ctypes.c_int # rather than unsigned as in ctypes.wintypes
433
 
    _function_name = "CreateFileW"
 
426
    if os.path.supports_unicode_filenames:
 
427
        _function_name = "CreateFileW"
 
428
        LPTSTR = LPCWSTR
 
429
    else:
 
430
        _function_name = "CreateFileA"
 
431
        class LPTSTR(LPCSTR):
 
432
            def __new__(cls, obj):
 
433
                return LPCSTR.__new__(cls, obj.encode("mbcs"))
434
434
 
435
435
    # CreateFile <http://msdn.microsoft.com/en-us/library/aa363858.aspx>
436
436
    _CreateFile = ctypes.WINFUNCTYPE(
437
437
            HANDLE,                # return value
438
 
            LPWSTR,                # lpFileName
 
438
            LPTSTR,                # lpFileName
439
439
            DWORD,                 # dwDesiredAccess
440
440
            DWORD,                 # dwShareMode
441
441
            LPSECURITY_ATTRIBUTES, # lpSecurityAttributes