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