bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1553.5.39
by Martin Pool
More lock docs |
1 |
# Copyright (C) 2005, 2006 Canonical Ltd
|
577
by Martin Pool
- merge portable lock module from John |
2 |
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
||
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
||
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
||
1553.5.39
by Martin Pool
More lock docs |
18 |
"""Locking using OS file locks or file existence.
|
577
by Martin Pool
- merge portable lock module from John |
19 |
|
1553.5.46
by Martin Pool
doc |
20 |
Note: This method of locking is generally deprecated in favour of LockDir, but
|
21 |
is used to lock local WorkingTrees, and by some old formats. It's accessed
|
|
22 |
through Transport.lock_read(), etc.
|
|
577
by Martin Pool
- merge portable lock module from John |
23 |
|
24 |
This module causes two methods, lock() and unlock() to be defined in
|
|
25 |
any way that works on the current platform.
|
|
26 |
||
27 |
It is not specified whether these locks are reentrant (i.e. can be
|
|
28 |
taken repeatedly by a single process) or whether they exclude
|
|
1553.5.39
by Martin Pool
More lock docs |
29 |
different threads in a single process. That reentrancy is provided by
|
30 |
LockableFiles.
|
|
615
by Martin Pool
Major rework of locking code: |
31 |
|
32 |
This defines two classes: ReadLock and WriteLock, which can be
|
|
33 |
implemented in different ways on different platforms. Both have an
|
|
34 |
unlock() method.
|
|
614
by Martin Pool
- unify two defintions of LockError |
35 |
"""
|
577
by Martin Pool
- merge portable lock module from John |
36 |
|
1185.65.29
by Robert Collins
Implement final review suggestions. |
37 |
import errno |
38 |
import os |
|
656
by Martin Pool
- create branch lock files if they don't exist |
39 |
import sys |
577
by Martin Pool
- merge portable lock module from John |
40 |
|
1773.4.1
by Martin Pool
Add pyflakes makefile target; fix many warnings |
41 |
from bzrlib.trace import mutter |
1711.8.5
by John Arbash Meinel
Move the new locking tests into their own files, and move the helper functions into a test helper. |
42 |
from bzrlib.errors import LockError |
1711.8.1
by John Arbash Meinel
Branch.lock_read/lock_write/unlock should handle failures |
43 |
|
577
by Martin Pool
- merge portable lock module from John |
44 |
|
615
by Martin Pool
Major rework of locking code: |
45 |
class _base_Lock(object): |
46 |
def _open(self, filename, filemode): |
|
656
by Martin Pool
- create branch lock files if they don't exist |
47 |
try: |
48 |
self.f = open(filename, filemode) |
|
49 |
return self.f |
|
50 |
except IOError, e: |
|
51 |
if e.errno != errno.ENOENT: |
|
52 |
raise
|
|
53 |
||
54 |
# maybe this is an old branch (before may 2005)
|
|
1185.31.4
by John Arbash Meinel
Fixing mutter() calls to not have to do string processing. |
55 |
mutter("trying to create missing branch lock %r", filename) |
656
by Martin Pool
- create branch lock files if they don't exist |
56 |
|
1185.65.29
by Robert Collins
Implement final review suggestions. |
57 |
self.f = open(filename, 'wb+') |
656
by Martin Pool
- create branch lock files if they don't exist |
58 |
return self.f |
59 |
||
615
by Martin Pool
Major rework of locking code: |
60 |
def __del__(self): |
61 |
if self.f: |
|
62 |
from warnings import warn |
|
63 |
warn("lock on %r not released" % self.f) |
|
64 |
self.unlock() |
|
656
by Martin Pool
- create branch lock files if they don't exist |
65 |
|
615
by Martin Pool
Major rework of locking code: |
66 |
def unlock(self): |
67 |
raise NotImplementedError() |
|
68 |
||
69 |
||
70 |
############################################################
|
|
71 |
# msvcrt locks
|
|
72 |
||
73 |
||
577
by Martin Pool
- merge portable lock module from John |
74 |
try: |
75 |
import fcntl |
|
615
by Martin Pool
Major rework of locking code: |
76 |
|
77 |
class _fcntl_FileLock(_base_Lock): |
|
78 |
f = None |
|
79 |
||
80 |
def unlock(self): |
|
1185.9.2
by Harald Meland
Use fcntl.lockf() rather than fcntl.flock() to support NFS file systems. |
81 |
fcntl.lockf(self.f, fcntl.LOCK_UN) |
615
by Martin Pool
Major rework of locking code: |
82 |
self.f.close() |
83 |
del self.f |
|
84 |
||
85 |
class _fcntl_WriteLock(_fcntl_FileLock): |
|
86 |
def __init__(self, filename): |
|
1185.65.29
by Robert Collins
Implement final review suggestions. |
87 |
# standard IO errors get exposed directly.
|
88 |
self._open(filename, 'wb') |
|
615
by Martin Pool
Major rework of locking code: |
89 |
try: |
1185.65.29
by Robert Collins
Implement final review suggestions. |
90 |
fcntl.lockf(self.f, fcntl.LOCK_EX) |
91 |
except IOError, e: |
|
92 |
# we should be more precise about whats a locking
|
|
93 |
# error and whats a random-other error
|
|
615
by Martin Pool
Major rework of locking code: |
94 |
raise LockError(e) |
95 |
||
96 |
class _fcntl_ReadLock(_fcntl_FileLock): |
|
1185.65.29
by Robert Collins
Implement final review suggestions. |
97 |
|
615
by Martin Pool
Major rework of locking code: |
98 |
def __init__(self, filename): |
1185.65.29
by Robert Collins
Implement final review suggestions. |
99 |
# standard IO errors get exposed directly.
|
100 |
self._open(filename, 'rb') |
|
615
by Martin Pool
Major rework of locking code: |
101 |
try: |
1185.65.29
by Robert Collins
Implement final review suggestions. |
102 |
fcntl.lockf(self.f, fcntl.LOCK_SH) |
103 |
except IOError, e: |
|
104 |
# we should be more precise about whats a locking
|
|
105 |
# error and whats a random-other error
|
|
615
by Martin Pool
Major rework of locking code: |
106 |
raise LockError(e) |
107 |
||
108 |
WriteLock = _fcntl_WriteLock |
|
109 |
ReadLock = _fcntl_ReadLock |
|
577
by Martin Pool
- merge portable lock module from John |
110 |
|
1185.65.29
by Robert Collins
Implement final review suggestions. |
111 |
|
577
by Martin Pool
- merge portable lock module from John |
112 |
except ImportError: |
113 |
try: |
|
114 |
import win32con, win32file, pywintypes |
|
615
by Martin Pool
Major rework of locking code: |
115 |
|
116 |
||
1185.23.3
by Aaron Bentley
win32 locking fix from Belchenko |
117 |
LOCK_SH = 0 # the default |
118 |
LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK |
|
119 |
LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY |
|
615
by Martin Pool
Major rework of locking code: |
120 |
|
121 |
class _w32c_FileLock(_base_Lock): |
|
122 |
def _lock(self, filename, openmode, lockmode): |
|
123 |
try: |
|
124 |
self._open(filename, openmode) |
|
125 |
self.hfile = win32file._get_osfhandle(self.f.fileno()) |
|
126 |
overlapped = pywintypes.OVERLAPPED() |
|
127 |
win32file.LockFileEx(self.hfile, lockmode, 0, 0x7fff0000, overlapped) |
|
128 |
except Exception, e: |
|
1711.7.31
by John Arbash Meinel
Clean up win32 locks when they fail to lock, so we don't try to unlock them later. |
129 |
if self.f: |
130 |
self.f.close() |
|
131 |
self.f = None |
|
615
by Martin Pool
Major rework of locking code: |
132 |
raise LockError(e) |
133 |
||
134 |
def unlock(self): |
|
135 |
try: |
|
136 |
overlapped = pywintypes.OVERLAPPED() |
|
137 |
win32file.UnlockFileEx(self.hfile, 0, 0x7fff0000, overlapped) |
|
138 |
self.f.close() |
|
139 |
self.f = None |
|
140 |
except Exception, e: |
|
141 |
raise LockError(e) |
|
142 |
||
143 |
||
144 |
class _w32c_ReadLock(_w32c_FileLock): |
|
145 |
def __init__(self, filename): |
|
1185.23.3
by Aaron Bentley
win32 locking fix from Belchenko |
146 |
_w32c_FileLock._lock(self, filename, 'rb', |
147 |
LOCK_NB) |
|
615
by Martin Pool
Major rework of locking code: |
148 |
|
149 |
class _w32c_WriteLock(_w32c_FileLock): |
|
150 |
def __init__(self, filename): |
|
151 |
_w32c_FileLock._lock(self, filename, 'wb', |
|
1185.23.3
by Aaron Bentley
win32 locking fix from Belchenko |
152 |
LOCK_EX + LOCK_NB) |
615
by Martin Pool
Major rework of locking code: |
153 |
|
154 |
||
155 |
WriteLock = _w32c_WriteLock |
|
156 |
ReadLock = _w32c_ReadLock |
|
157 |
||
577
by Martin Pool
- merge portable lock module from John |
158 |
except ImportError: |
159 |
try: |
|
160 |
import msvcrt |
|
615
by Martin Pool
Major rework of locking code: |
161 |
|
162 |
||
577
by Martin Pool
- merge portable lock module from John |
163 |
# Unfortunately, msvcrt.locking() doesn't distinguish between
|
164 |
# read locks and write locks. Also, the way the combinations
|
|
165 |
# work to get non-blocking is not the same, so we
|
|
166 |
# have to write extra special functions here.
|
|
167 |
||
615
by Martin Pool
Major rework of locking code: |
168 |
|
169 |
class _msvc_FileLock(_base_Lock): |
|
170 |
LOCK_SH = 1 |
|
171 |
LOCK_EX = 2 |
|
172 |
LOCK_NB = 4 |
|
173 |
def unlock(self): |
|
174 |
_msvc_unlock(self.f) |
|
1185.7.1
by Martin Pool
- Quieten warnings about locking; patch from Matt Lavin. |
175 |
self.f.close() |
176 |
self.f = None |
|
615
by Martin Pool
Major rework of locking code: |
177 |
|
178 |
||
179 |
class _msvc_ReadLock(_msvc_FileLock): |
|
180 |
def __init__(self, filename): |
|
181 |
_msvc_lock(self._open(filename, 'rb'), self.LOCK_SH) |
|
182 |
||
183 |
||
184 |
class _msvc_WriteLock(_msvc_FileLock): |
|
185 |
def __init__(self, filename): |
|
186 |
_msvc_lock(self._open(filename, 'wb'), self.LOCK_EX) |
|
187 |
||
188 |
||
189 |
def _msvc_lock(f, flags): |
|
577
by Martin Pool
- merge portable lock module from John |
190 |
try: |
191 |
# Unfortunately, msvcrt.LK_RLCK is equivalent to msvcrt.LK_LOCK
|
|
192 |
# according to the comments, LK_RLCK is open the lock for writing.
|
|
193 |
||
194 |
# Unfortunately, msvcrt.locking() also has the side effect that it
|
|
195 |
# will only block for 10 seconds at most, and then it will throw an
|
|
196 |
# exception, this isn't terrible, though.
|
|
197 |
if type(f) == file: |
|
198 |
fpos = f.tell() |
|
199 |
fn = f.fileno() |
|
200 |
f.seek(0) |
|
201 |
else: |
|
202 |
fn = f |
|
203 |
fpos = os.lseek(fn, 0,0) |
|
204 |
os.lseek(fn, 0,0) |
|
615
by Martin Pool
Major rework of locking code: |
205 |
|
951
by Martin Pool
- fix up self reference in msvc lock object |
206 |
if flags & _msvc_FileLock.LOCK_SH: |
207 |
if flags & _msvc_FileLock.LOCK_NB: |
|
577
by Martin Pool
- merge portable lock module from John |
208 |
lock_mode = msvcrt.LK_NBLCK |
209 |
else: |
|
210 |
lock_mode = msvcrt.LK_LOCK |
|
951
by Martin Pool
- fix up self reference in msvc lock object |
211 |
elif flags & _msvc_FileLock.LOCK_EX: |
212 |
if flags & _msvc_FileLock.LOCK_NB: |
|
577
by Martin Pool
- merge portable lock module from John |
213 |
lock_mode = msvcrt.LK_NBRLCK |
214 |
else: |
|
215 |
lock_mode = msvcrt.LK_RLCK |
|
216 |
else: |
|
217 |
raise ValueError('Invalid lock mode: %r' % flags) |
|
218 |
try: |
|
219 |
msvcrt.locking(fn, lock_mode, -1) |
|
220 |
finally: |
|
221 |
os.lseek(fn, fpos, 0) |
|
222 |
except Exception, e: |
|
223 |
raise LockError(e) |
|
224 |
||
615
by Martin Pool
Major rework of locking code: |
225 |
def _msvc_unlock(f): |
577
by Martin Pool
- merge portable lock module from John |
226 |
try: |
227 |
if type(f) == file: |
|
228 |
fpos = f.tell() |
|
229 |
fn = f.fileno() |
|
230 |
f.seek(0) |
|
231 |
else: |
|
232 |
fn = f |
|
233 |
fpos = os.lseek(fn, 0,0) |
|
234 |
os.lseek(fn, 0,0) |
|
235 |
||
236 |
try: |
|
237 |
msvcrt.locking(fn, msvcrt.LK_UNLCK, -1) |
|
238 |
finally: |
|
239 |
os.lseek(fn, fpos, 0) |
|
240 |
except Exception, e: |
|
241 |
raise LockError(e) |
|
615
by Martin Pool
Major rework of locking code: |
242 |
|
243 |
||
244 |
WriteLock = _msvc_WriteLock |
|
245 |
ReadLock = _msvc_ReadLock |
|
577
by Martin Pool
- merge portable lock module from John |
246 |
except ImportError: |
615
by Martin Pool
Major rework of locking code: |
247 |
raise NotImplementedError("please write a locking method " |
248 |
"for platform %r" % sys.platform) |