~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Robert Collins
  • Date: 2006-01-25 01:43:34 UTC
  • mto: (1534.1.15 integration)
  • mto: This revision was merged to the branch mainline in revision 1550.
  • Revision ID: robertc@robertcollins.net-20060125014334-8dd9ed73c26c5956
Implement final review suggestions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
unlock() method.
35
35
"""
36
36
 
37
 
 
 
37
import errno
 
38
import os
38
39
import sys
39
 
import os
40
40
 
41
41
from bzrlib.trace import mutter, note, warning
42
42
from bzrlib.errors import LockError
43
43
 
44
44
class _base_Lock(object):
45
45
    def _open(self, filename, filemode):
46
 
        import errno
47
46
        try:
48
47
            self.f = open(filename, filemode)
49
48
            return self.f
54
53
            # maybe this is an old branch (before may 2005)
55
54
            mutter("trying to create missing branch lock %r", filename)
56
55
            
57
 
            self.f = open(filename, 'wb')
 
56
            self.f = open(filename, 'wb+')
58
57
            return self.f
59
58
 
60
 
 
61
59
    def __del__(self):
62
60
        if self.f:
63
61
            from warnings import warn
64
62
            warn("lock on %r not released" % self.f)
65
63
            self.unlock()
66
64
            
67
 
 
68
65
    def unlock(self):
69
66
        raise NotImplementedError()
70
67
 
88
85
            self.f.close()
89
86
            del self.f 
90
87
 
91
 
 
92
88
    class _fcntl_WriteLock(_fcntl_FileLock):
93
89
        def __init__(self, filename):
 
90
            # standard IO errors get exposed directly.
 
91
            self._open(filename, 'wb')
94
92
            try:
95
 
                fcntl.lockf(self._open(filename, 'wb'), fcntl.LOCK_EX)
96
 
            except Exception, e:
 
93
                fcntl.lockf(self.f, fcntl.LOCK_EX)
 
94
            except IOError, e:
 
95
                # we should be more precise about whats a locking
 
96
                # error and whats a random-other error
97
97
                raise LockError(e)
98
98
 
99
 
 
100
99
    class _fcntl_ReadLock(_fcntl_FileLock):
 
100
 
101
101
        def __init__(self, filename):
 
102
            # standard IO errors get exposed directly.
 
103
            self._open(filename, 'rb')
102
104
            try:
103
 
                fcntl.lockf(self._open(filename, 'rb'), fcntl.LOCK_SH)
104
 
            except Exception, e:
 
105
                fcntl.lockf(self.f, fcntl.LOCK_SH)
 
106
            except IOError, e:
 
107
                # we should be more precise about whats a locking
 
108
                # error and whats a random-other error
105
109
                raise LockError(e)
106
110
 
107
111
    WriteLock = _fcntl_WriteLock
108
112
    ReadLock = _fcntl_ReadLock
109
113
 
 
114
 
110
115
except ImportError:
111
116
    try:
112
117
        import win32con, win32file, pywintypes