def locked_get(self): """Retrieve Credential from file. Returns: oauth2client.client.Credentials Raises: IOError if the file is a symbolic link. """ credentials = None util.validate_file(self._filename) try: f = open(self._filename, 'rb') content = f.read() f.close() except IOError: return credentials try: credentials = client.Credentials.new_from_json(content) credentials.set_store(self) except ValueError: pass return credentials
def open_and_lock(self, timeout, delay): """Open the file and lock it. Args: timeout: float, How long to try to lock for. delay: float, How long to wait between retries Raises: AlreadyLockedException: if the lock is already acquired. IOError: if the open fails. IOError: if the file is a symbolic link. """ if self._locked: raise locked_file.AlreadyLockedException( 'File {0} is already locked'.format(self._filename)) start_time = time.time() util.validate_file(self._filename) try: self._fh = open(self._filename, self._mode) except IOError as e: # If we can't access with _mode, try _fallback_mode # and don't lock. if e.errno == errno.EACCES: self._fh = open(self._filename, self._fallback_mode) return # We opened in _mode, try to lock the file. while True: try: hfile = win32file._get_osfhandle(self._fh.fileno()) win32file.LockFileEx( hfile, (win32con.LOCKFILE_FAIL_IMMEDIATELY | win32con.LOCKFILE_EXCLUSIVE_LOCK), 0, -0x10000, pywintypes.OVERLAPPED()) self._locked = True return except pywintypes.error as e: if timeout == 0: raise # If the error is not that the file is already # in use, raise. if e[0] != _Win32Opener.FILE_IN_USE_ERROR: raise # We could not acquire the lock. Try again. if (time.time() - start_time) >= timeout: locked_file.logger.warn('Could not lock %s in %s seconds', self._filename, timeout) if self._fh: self._fh.close() self._fh = open(self._filename, self._fallback_mode) return time.sleep(delay)
def open_and_lock(self, timeout, delay): """Open the file and lock it. Args: timeout: float, How long to try to lock for. delay: float, How long to wait between retries Raises: AlreadyLockedException: if the lock is already acquired. IOError: if the open fails. IOError: if the file is a symbolic link. """ if self._locked: raise locked_file.AlreadyLockedException( 'File {0} is already locked'.format(self._filename)) start_time = time.time() util.validate_file(self._filename) try: self._fh = open(self._filename, self._mode) except IOError as e: # If we can't access with _mode, try _fallback_mode # and don't lock. if e.errno == errno.EACCES: self._fh = open(self._filename, self._fallback_mode) return # We opened in _mode, try to lock the file. while True: try: hfile = win32file._get_osfhandle(self._fh.fileno()) win32file.LockFileEx(hfile, (win32con.LOCKFILE_FAIL_IMMEDIATELY | win32con.LOCKFILE_EXCLUSIVE_LOCK), 0, -0x10000, pywintypes.OVERLAPPED()) self._locked = True return except pywintypes.error as e: if timeout == 0: raise # If the error is not that the file is already # in use, raise. if e[0] != _Win32Opener.FILE_IN_USE_ERROR: raise # We could not acquire the lock. Try again. if (time.time() - start_time) >= timeout: locked_file.logger.warn('Could not lock %s in %s seconds', self._filename, timeout) if self._fh: self._fh.close() self._fh = open(self._filename, self._fallback_mode) return time.sleep(delay)
def locked_put(self, credentials): """Write Credentials to file. Args: credentials: Credentials, the credentials to store. Raises: IOError if the file is a symbolic link. """ self._create_file_if_needed() util.validate_file(self._filename) f = open(self._filename, 'w') f.write(credentials.to_json()) f.close()
def open_and_lock(self, timeout, delay): """Open the file and lock it. Args: timeout: float, How long to try to lock for. delay: float, How long to wait between retries Raises: AlreadyLockedException: if the lock is already acquired. IOError: if the open fails. IOError: if the file is a symbolic link. """ if self._locked: raise locked_file.AlreadyLockedException( 'File {0} is already locked'.format(self._filename)) start_time = time.time() util.validate_file(self._filename) try: self._fh = open(self._filename, self._mode) except IOError as e: # If we can't access with _mode, try _fallback_mode and # don't lock. if e.errno in (errno.EPERM, errno.EACCES): self._fh = open(self._filename, self._fallback_mode) return # We opened in _mode, try to lock the file. while True: try: fcntl.lockf(self._fh.fileno(), fcntl.LOCK_EX) self._locked = True return except IOError as e: # If not retrying, then just pass on the error. if timeout == 0: raise if e.errno != errno.EACCES: raise # We could not acquire the lock. Try again. if (time.time() - start_time) >= timeout: locked_file.logger.warn('Could not lock %s in %s seconds', self._filename, timeout) if self._fh: self._fh.close() self._fh = open(self._filename, self._fallback_mode) return time.sleep(delay)
def open_and_lock(self, timeout, delay): """Open the file and lock it. Tries to create a .lock file next to the file we're trying to open. Args: timeout: float, How long to try to lock for. delay: float, How long to wait between retries. Raises: AlreadyLockedException: if the lock is already acquired. IOError: if the open fails. IOError: if the file is a symbolic link. """ if self._locked: raise AlreadyLockedException("File {0} is already locked".format(self._filename)) self._locked = False util.validate_file(self._filename) try: self._fh = open(self._filename, self._mode) except IOError as e: # If we can't access with _mode, try _fallback_mode and don't lock. if e.errno == errno.EACCES: self._fh = open(self._filename, self._fallback_mode) return lock_filename = self._posix_lockfile(self._filename) start_time = time.time() while True: try: self._lock_fd = os.open(lock_filename, os.O_CREAT | os.O_EXCL | os.O_RDWR) self._locked = True break except OSError as e: if e.errno != errno.EEXIST: raise if (time.time() - start_time) >= timeout: logger.warn("Could not acquire lock %s in %s seconds", lock_filename, timeout) # Close the file and open in fallback_mode. if self._fh: self._fh.close() self._fh = open(self._filename, self._fallback_mode) return time.sleep(delay)