@allow_from_sudo()
    def lock(self, num_retries=0):
        """Lock the application directory for exclusive write access.

        If the appdir is already locked by another process/thread then
        EskyLockedError is raised.  There is no way to perform a blocking
        lock on an appdir.

        Locking is achieved by creating a "locked" directory and writing the
        current process/thread ID into it.  os.mkdir is atomic on all platforms
        that we care about.

        This also has the side-effect of failing early if the user does not
        have permission to modify the application directory.
        """
        if self.sudo_proxy is not None:
            return self.sudo_proxy.lock()
        if num_retries > 5:
            raise EskyLockedError
        if threading:
            curthread = threading.currentThread()
            try:
                threadid = curthread.ident
            except __HOLE__:
                threadid = curthread.getName()
        else:
            threadid = "0"
        myid = "%s-%s-%s" % (socket.gethostname(), os.getpid(), threadid)
        lockdir = os.path.join(self.appdir, "locked")
        #  Do I already own the lock?
        if os.path.exists(os.path.join(lockdir, myid)):
            #  Update file mtime to keep it safe from breakers
            os.utime(os.path.join(lockdir, myid), None)
            self._lock_count += 1
            return True
        #  Try to make the "locked" directory.
        try:
            os.mkdir(lockdir)
        except OSError, e:
            if e.errno != errno.EEXIST:
                raise
            #  Is it stale?  If so, break it and try again.
            try:
                newest_mtime = os.path.getmtime(lockdir)
                for nm in os.listdir(lockdir):
                    mtime = os.path.getmtime(os.path.join(lockdir, nm))
                    if mtime > newest_mtime:
                        newest_mtime = mtime
                if newest_mtime + self.lock_timeout < time.time():
                    really_rmtree(lockdir)
                    return self.lock(num_retries+1)
                else:
                    raise EskyLockedError
            except OSError, e:
                if e.errno not in (errno.ENOENT, errno.ENOTDIR,):
                    raise
                return self.lock(num_retries+1)
        else:
            #  Success!  Record my ownership
            open(os.path.join(lockdir, myid), "wb").close()
            self._lock_count = 1
            return True
dataset/ETHPy150Open cloudmatrix/esky/esky/__init__.py/Esky.lock