/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
  • Author(s): Richard Wilbur
  • Date: 2017-05-30 23:37:11 UTC
  • mto: This revision was merged to the branch mainline in revision 6645.
  • Revision ID: jelmer@jelmer.uk-20170530233711-r0m0qp8hpkqzpopw
Fix order in which files are processed.

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)
350
360
                self._handle = win32file_CreateFile(filename, access, share,
351
361
                    None, win32file.OPEN_ALWAYS,
352
362
                    win32file.FILE_ATTRIBUTE_NORMAL, None)
353
 
            except pywintypes.error, e:
 
363
            except pywintypes.error as e:
354
364
                if e.args[0] == winerror.ERROR_ACCESS_DENIED:
355
365
                    raise errors.LockFailed(filename, e)
356
366
                if e.args[0] == winerror.ERROR_SHARING_VIOLATION:
527
537
    locked the same way), and -Drelock is set, then this will trace.note a
528
538
    message about it.
529
539
    """
530
 
    
 
540
 
531
541
    _prev_lock = None
532
542
 
533
543
    def _note_lock(self, lock_type):
536
546
                type_name = 'read'
537
547
            else:
538
548
                type_name = 'write'
539
 
            trace.note('%r was %s locked again', self, type_name)
 
549
            trace.note(gettext('{0!r} was {1} locked again'), self, type_name)
540
550
        self._prev_lock = lock_type
541
551
 
 
552
@contextlib.contextmanager
 
553
def write_locked(lockable):
 
554
    lockable.lock_write()
 
555
    try:
 
556
        yield lockable
 
557
    finally:
 
558
        lockable.unlock()