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.
            CredentialsFileSymbolicLinkError: if the file is a symbolic
                                              link.
        """
        if self._locked:
            raise AlreadyLockedException('File %s is already locked' %
                                         self._filename)
        start_time = time.time()

        validate_file(self._filename)
        try:
            self._fh = open(self._filename, self._mode)
        except __HOLE__ 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:
                    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)
dataset/ETHPy150Open google/oauth2client/oauth2client/contrib/_fcntl_opener.py/_FcntlOpener.open_and_lock