OLD | NEW |
1 # Copyright 2011 Google Inc. | 1 # Copyright 2014 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
6 # | 6 # |
7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
8 # | 8 # |
9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 # See the License for the specific language governing permissions and | 12 # See the License for the specific language governing permissions and |
13 # limitations under the License. | 13 # limitations under the License. |
14 | 14 |
15 """Locked file interface that should work on Unix and Windows pythons. | 15 """Locked file interface that should work on Unix and Windows pythons. |
16 | 16 |
17 This module first tries to use fcntl locking to ensure serialized access | 17 This module first tries to use fcntl locking to ensure serialized access |
18 to a file, then falls back on a lock file if that is unavialable. | 18 to a file, then falls back on a lock file if that is unavialable. |
19 | 19 |
20 Usage: | 20 Usage:: |
| 21 |
21 f = LockedFile('filename', 'r+b', 'rb') | 22 f = LockedFile('filename', 'r+b', 'rb') |
22 f.open_and_lock() | 23 f.open_and_lock() |
23 if f.is_locked(): | 24 if f.is_locked(): |
24 print 'Acquired filename with r+b mode' | 25 print('Acquired filename with r+b mode') |
25 f.file_handle().write('locked data') | 26 f.file_handle().write('locked data') |
26 else: | 27 else: |
27 print 'Aquired filename with rb mode' | 28 print('Acquired filename with rb mode') |
28 f.unlock_and_close() | 29 f.unlock_and_close() |
| 30 |
29 """ | 31 """ |
30 | 32 |
| 33 from __future__ import print_function |
| 34 |
31 __author__ = 'cache@google.com (David T McWherter)' | 35 __author__ = 'cache@google.com (David T McWherter)' |
32 | 36 |
33 import errno | 37 import errno |
34 import logging | 38 import logging |
35 import os | 39 import os |
36 import time | 40 import time |
37 | 41 |
38 from . import util | 42 from . import util |
39 | 43 |
40 logger = logging.getLogger(__name__) | 44 logger = logging.getLogger(__name__) |
(...skipping 22 matching lines...) Expand all Loading... |
63 Args: | 67 Args: |
64 filename: string, The pathname of the file. | 68 filename: string, The pathname of the file. |
65 mode: string, The preferred mode to access the file with. | 69 mode: string, The preferred mode to access the file with. |
66 fallback_mode: string, The mode to use if locking fails. | 70 fallback_mode: string, The mode to use if locking fails. |
67 """ | 71 """ |
68 self._locked = False | 72 self._locked = False |
69 self._filename = filename | 73 self._filename = filename |
70 self._mode = mode | 74 self._mode = mode |
71 self._fallback_mode = fallback_mode | 75 self._fallback_mode = fallback_mode |
72 self._fh = None | 76 self._fh = None |
| 77 self._lock_fd = None |
73 | 78 |
74 def is_locked(self): | 79 def is_locked(self): |
75 """Was the file locked.""" | 80 """Was the file locked.""" |
76 return self._locked | 81 return self._locked |
77 | 82 |
78 def file_handle(self): | 83 def file_handle(self): |
79 """The file handle to the file. Valid only after opened.""" | 84 """The file handle to the file. Valid only after opened.""" |
80 return self._fh | 85 return self._fh |
81 | 86 |
82 def filename(self): | 87 def filename(self): |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 CredentialsFileSymbolicLinkError if the file is a symbolic link. | 120 CredentialsFileSymbolicLinkError if the file is a symbolic link. |
116 """ | 121 """ |
117 if self._locked: | 122 if self._locked: |
118 raise AlreadyLockedException('File %s is already locked' % | 123 raise AlreadyLockedException('File %s is already locked' % |
119 self._filename) | 124 self._filename) |
120 self._locked = False | 125 self._locked = False |
121 | 126 |
122 validate_file(self._filename) | 127 validate_file(self._filename) |
123 try: | 128 try: |
124 self._fh = open(self._filename, self._mode) | 129 self._fh = open(self._filename, self._mode) |
125 except IOError, e: | 130 except IOError as e: |
126 # If we can't access with _mode, try _fallback_mode and don't lock. | 131 # If we can't access with _mode, try _fallback_mode and don't lock. |
127 if e.errno == errno.EACCES: | 132 if e.errno == errno.EACCES: |
128 self._fh = open(self._filename, self._fallback_mode) | 133 self._fh = open(self._filename, self._fallback_mode) |
129 return | 134 return |
130 | 135 |
131 lock_filename = self._posix_lockfile(self._filename) | 136 lock_filename = self._posix_lockfile(self._filename) |
132 start_time = time.time() | 137 start_time = time.time() |
133 while True: | 138 while True: |
134 try: | 139 try: |
135 self._lock_fd = os.open(lock_filename, | 140 self._lock_fd = os.open(lock_filename, |
136 os.O_CREAT|os.O_EXCL|os.O_RDWR) | 141 os.O_CREAT|os.O_EXCL|os.O_RDWR) |
137 self._locked = True | 142 self._locked = True |
138 break | 143 break |
139 | 144 |
140 except OSError, e: | 145 except OSError as e: |
141 if e.errno != errno.EEXIST: | 146 if e.errno != errno.EEXIST: |
142 raise | 147 raise |
143 if (time.time() - start_time) >= timeout: | 148 if (time.time() - start_time) >= timeout: |
144 logger.warn('Could not acquire lock %s in %s seconds' % ( | 149 logger.warn('Could not acquire lock %s in %s seconds', |
145 lock_filename, timeout)) | 150 lock_filename, timeout) |
146 # Close the file and open in fallback_mode. | 151 # Close the file and open in fallback_mode. |
147 if self._fh: | 152 if self._fh: |
148 self._fh.close() | 153 self._fh.close() |
149 self._fh = open(self._filename, self._fallback_mode) | 154 self._fh = open(self._filename, self._fallback_mode) |
150 return | 155 return |
151 time.sleep(delay) | 156 time.sleep(delay) |
152 | 157 |
153 def unlock_and_close(self): | 158 def unlock_and_close(self): |
154 """Unlock a file by removing the .lock file, and close the handle.""" | 159 """Unlock a file by removing the .lock file, and close the handle.""" |
155 if self._locked: | 160 if self._locked: |
(...skipping 29 matching lines...) Expand all Loading... |
185 CredentialsFileSymbolicLinkError if the file is a symbolic link. | 190 CredentialsFileSymbolicLinkError if the file is a symbolic link. |
186 """ | 191 """ |
187 if self._locked: | 192 if self._locked: |
188 raise AlreadyLockedException('File %s is already locked' % | 193 raise AlreadyLockedException('File %s is already locked' % |
189 self._filename) | 194 self._filename) |
190 start_time = time.time() | 195 start_time = time.time() |
191 | 196 |
192 validate_file(self._filename) | 197 validate_file(self._filename) |
193 try: | 198 try: |
194 self._fh = open(self._filename, self._mode) | 199 self._fh = open(self._filename, self._mode) |
195 except IOError, e: | 200 except IOError as e: |
196 # If we can't access with _mode, try _fallback_mode and don't lock. | 201 # If we can't access with _mode, try _fallback_mode and don't lock. |
197 if e.errno == errno.EACCES: | 202 if e.errno in (errno.EPERM, errno.EACCES): |
198 self._fh = open(self._filename, self._fallback_mode) | 203 self._fh = open(self._filename, self._fallback_mode) |
199 return | 204 return |
200 | 205 |
201 # We opened in _mode, try to lock the file. | 206 # We opened in _mode, try to lock the file. |
202 while True: | 207 while True: |
203 try: | 208 try: |
204 fcntl.lockf(self._fh.fileno(), fcntl.LOCK_EX) | 209 fcntl.lockf(self._fh.fileno(), fcntl.LOCK_EX) |
205 self._locked = True | 210 self._locked = True |
206 return | 211 return |
207 except IOError, e: | 212 except IOError as e: |
208 # If not retrying, then just pass on the error. | 213 # If not retrying, then just pass on the error. |
209 if timeout == 0: | 214 if timeout == 0: |
210 raise e | 215 raise |
211 if e.errno != errno.EACCES: | 216 if e.errno != errno.EACCES: |
212 raise e | 217 raise |
213 # We could not acquire the lock. Try again. | 218 # We could not acquire the lock. Try again. |
214 if (time.time() - start_time) >= timeout: | 219 if (time.time() - start_time) >= timeout: |
215 logger.warn('Could not lock %s in %s seconds' % ( | 220 logger.warn('Could not lock %s in %s seconds', |
216 self._filename, timeout)) | 221 self._filename, timeout) |
217 if self._fh: | 222 if self._fh: |
218 self._fh.close() | 223 self._fh.close() |
219 self._fh = open(self._filename, self._fallback_mode) | 224 self._fh = open(self._filename, self._fallback_mode) |
220 return | 225 return |
221 time.sleep(delay) | 226 time.sleep(delay) |
222 | 227 |
223 def unlock_and_close(self): | 228 def unlock_and_close(self): |
224 """Close and unlock the file using the fcntl.lockf primitive.""" | 229 """Close and unlock the file using the fcntl.lockf primitive.""" |
225 if self._locked: | 230 if self._locked: |
226 fcntl.lockf(self._fh.fileno(), fcntl.LOCK_UN) | 231 fcntl.lockf(self._fh.fileno(), fcntl.LOCK_UN) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 CredentialsFileSymbolicLinkError if the file is a symbolic link. | 265 CredentialsFileSymbolicLinkError if the file is a symbolic link. |
261 """ | 266 """ |
262 if self._locked: | 267 if self._locked: |
263 raise AlreadyLockedException('File %s is already locked' % | 268 raise AlreadyLockedException('File %s is already locked' % |
264 self._filename) | 269 self._filename) |
265 start_time = time.time() | 270 start_time = time.time() |
266 | 271 |
267 validate_file(self._filename) | 272 validate_file(self._filename) |
268 try: | 273 try: |
269 self._fh = open(self._filename, self._mode) | 274 self._fh = open(self._filename, self._mode) |
270 except IOError, e: | 275 except IOError as e: |
271 # If we can't access with _mode, try _fallback_mode and don't lock. | 276 # If we can't access with _mode, try _fallback_mode and don't lock. |
272 if e.errno == errno.EACCES: | 277 if e.errno == errno.EACCES: |
273 self._fh = open(self._filename, self._fallback_mode) | 278 self._fh = open(self._filename, self._fallback_mode) |
274 return | 279 return |
275 | 280 |
276 # We opened in _mode, try to lock the file. | 281 # We opened in _mode, try to lock the file. |
277 while True: | 282 while True: |
278 try: | 283 try: |
279 hfile = win32file._get_osfhandle(self._fh.fileno()) | 284 hfile = win32file._get_osfhandle(self._fh.fileno()) |
280 win32file.LockFileEx( | 285 win32file.LockFileEx( |
281 hfile, | 286 hfile, |
282 (win32con.LOCKFILE_FAIL_IMMEDIATELY| | 287 (win32con.LOCKFILE_FAIL_IMMEDIATELY| |
283 win32con.LOCKFILE_EXCLUSIVE_LOCK), 0, -0x10000, | 288 win32con.LOCKFILE_EXCLUSIVE_LOCK), 0, -0x10000, |
284 pywintypes.OVERLAPPED()) | 289 pywintypes.OVERLAPPED()) |
285 self._locked = True | 290 self._locked = True |
286 return | 291 return |
287 except pywintypes.error, e: | 292 except pywintypes.error as e: |
288 if timeout == 0: | 293 if timeout == 0: |
289 raise e | 294 raise |
290 | 295 |
291 # If the error is not that the file is already in use, raise. | 296 # If the error is not that the file is already in use, raise. |
292 if e[0] != _Win32Opener.FILE_IN_USE_ERROR: | 297 if e[0] != _Win32Opener.FILE_IN_USE_ERROR: |
293 raise | 298 raise |
294 | 299 |
295 # We could not acquire the lock. Try again. | 300 # We could not acquire the lock. Try again. |
296 if (time.time() - start_time) >= timeout: | 301 if (time.time() - start_time) >= timeout: |
297 logger.warn('Could not lock %s in %s seconds' % ( | 302 logger.warn('Could not lock %s in %s seconds' % ( |
298 self._filename, timeout)) | 303 self._filename, timeout)) |
299 if self._fh: | 304 if self._fh: |
300 self._fh.close() | 305 self._fh.close() |
301 self._fh = open(self._filename, self._fallback_mode) | 306 self._fh = open(self._filename, self._fallback_mode) |
302 return | 307 return |
303 time.sleep(delay) | 308 time.sleep(delay) |
304 | 309 |
305 def unlock_and_close(self): | 310 def unlock_and_close(self): |
306 """Close and unlock the file using the win32 primitive.""" | 311 """Close and unlock the file using the win32 primitive.""" |
307 if self._locked: | 312 if self._locked: |
308 try: | 313 try: |
309 hfile = win32file._get_osfhandle(self._fh.fileno()) | 314 hfile = win32file._get_osfhandle(self._fh.fileno()) |
310 win32file.UnlockFileEx(hfile, 0, -0x10000, pywintypes.OVERLAPPED()) | 315 win32file.UnlockFileEx(hfile, 0, -0x10000, pywintypes.OVERLAPPED()) |
311 except pywintypes.error, e: | 316 except pywintypes.error as e: |
312 if e[0] != _Win32Opener.FILE_ALREADY_UNLOCKED_ERROR: | 317 if e[0] != _Win32Opener.FILE_ALREADY_UNLOCKED_ERROR: |
313 raise | 318 raise |
314 self._locked = False | 319 self._locked = False |
315 if self._fh: | 320 if self._fh: |
316 self._fh.close() | 321 self._fh.close() |
317 except ImportError: | 322 except ImportError: |
318 _Win32Opener = None | 323 _Win32Opener = None |
319 | 324 |
320 | 325 |
321 class LockedFile(object): | 326 class LockedFile(object): |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 delay: float, The number of seconds to wait between retry attempts. | 368 delay: float, The number of seconds to wait between retry attempts. |
364 | 369 |
365 Raises: | 370 Raises: |
366 AlreadyLockedException: if the lock is already acquired. | 371 AlreadyLockedException: if the lock is already acquired. |
367 IOError: if the open fails. | 372 IOError: if the open fails. |
368 """ | 373 """ |
369 self._opener.open_and_lock(timeout, delay) | 374 self._opener.open_and_lock(timeout, delay) |
370 | 375 |
371 def unlock_and_close(self): | 376 def unlock_and_close(self): |
372 """Unlock and close a file.""" | 377 """Unlock and close a file.""" |
373 self._opener.unlock_and_close() | 378 self._opener.unlock_and_close() |
OLD | NEW |