]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.2/Demo/pdist/cvslock.py
edk2: Remove AppPkg, StdLib, StdLibPrivateInternalFiles
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Demo / pdist / cvslock.py
diff --git a/AppPkg/Applications/Python/Python-2.7.2/Demo/pdist/cvslock.py b/AppPkg/Applications/Python/Python-2.7.2/Demo/pdist/cvslock.py
deleted file mode 100644 (file)
index 95f5c58..0000000
+++ /dev/null
@@ -1,280 +0,0 @@
-"""CVS locking algorithm.\r
-\r
-CVS locking strategy\r
-====================\r
-\r
-As reverse engineered from the CVS 1.3 sources (file lock.c):\r
-\r
-- Locking is done on a per repository basis (but a process can hold\r
-write locks for multiple directories); all lock files are placed in\r
-the repository and have names beginning with "#cvs.".\r
-\r
-- Before even attempting to lock, a file "#cvs.tfl.<pid>" is created\r
-(and removed again), to test that we can write the repository.  [The\r
-algorithm can still be fooled (1) if the repository's mode is changed\r
-while attempting to lock; (2) if this file exists and is writable but\r
-the directory is not.]\r
-\r
-- While creating the actual read/write lock files (which may exist for\r
-a long time), a "meta-lock" is held.  The meta-lock is a directory\r
-named "#cvs.lock" in the repository.  The meta-lock is also held while\r
-a write lock is held.\r
-\r
-- To set a read lock:\r
-\r
-        - acquire the meta-lock\r
-        - create the file "#cvs.rfl.<pid>"\r
-        - release the meta-lock\r
-\r
-- To set a write lock:\r
-\r
-        - acquire the meta-lock\r
-        - check that there are no files called "#cvs.rfl.*"\r
-                - if there are, release the meta-lock, sleep, try again\r
-        - create the file "#cvs.wfl.<pid>"\r
-\r
-- To release a write lock:\r
-\r
-        - remove the file "#cvs.wfl.<pid>"\r
-        - rmdir the meta-lock\r
-\r
-- To release a read lock:\r
-\r
-        - remove the file "#cvs.rfl.<pid>"\r
-\r
-\r
-Additional notes\r
-----------------\r
-\r
-- A process should read-lock at most one repository at a time.\r
-\r
-- A process may write-lock as many repositories as it wishes (to avoid\r
-deadlocks, I presume it should always lock them top-down in the\r
-directory hierarchy).\r
-\r
-- A process should make sure it removes all its lock files and\r
-directories when it crashes.\r
-\r
-- Limitation: one user id should not be committing files into the same\r
-repository at the same time.\r
-\r
-\r
-Turn this into Python code\r
---------------------------\r
-\r
-rl = ReadLock(repository, waittime)\r
-\r
-wl = WriteLock(repository, waittime)\r
-\r
-list = MultipleWriteLock([repository1, repository2, ...], waittime)\r
-\r
-"""\r
-\r
-\r
-import os\r
-import time\r
-import stat\r
-import pwd\r
-\r
-\r
-# Default wait time\r
-DELAY = 10\r
-\r
-\r
-# XXX This should be the same on all Unix versions\r
-EEXIST = 17\r
-\r
-\r
-# Files used for locking (must match cvs.h in the CVS sources)\r
-CVSLCK = "#cvs.lck"\r
-CVSRFL = "#cvs.rfl."\r
-CVSWFL = "#cvs.wfl."\r
-\r
-\r
-class Error:\r
-\r
-    def __init__(self, msg):\r
-        self.msg = msg\r
-\r
-    def __repr__(self):\r
-        return repr(self.msg)\r
-\r
-    def __str__(self):\r
-        return str(self.msg)\r
-\r
-\r
-class Locked(Error):\r
-    pass\r
-\r
-\r
-class Lock:\r
-\r
-    def __init__(self, repository = ".", delay = DELAY):\r
-        self.repository = repository\r
-        self.delay = delay\r
-        self.lockdir = None\r
-        self.lockfile = None\r
-        pid = repr(os.getpid())\r
-        self.cvslck = self.join(CVSLCK)\r
-        self.cvsrfl = self.join(CVSRFL + pid)\r
-        self.cvswfl = self.join(CVSWFL + pid)\r
-\r
-    def __del__(self):\r
-        print "__del__"\r
-        self.unlock()\r
-\r
-    def setlockdir(self):\r
-        while 1:\r
-            try:\r
-                self.lockdir = self.cvslck\r
-                os.mkdir(self.cvslck, 0777)\r
-                return\r
-            except os.error, msg:\r
-                self.lockdir = None\r
-                if msg[0] == EEXIST:\r
-                    try:\r
-                        st = os.stat(self.cvslck)\r
-                    except os.error:\r
-                        continue\r
-                    self.sleep(st)\r
-                    continue\r
-                raise Error("failed to lock %s: %s" % (\r
-                        self.repository, msg))\r
-\r
-    def unlock(self):\r
-        self.unlockfile()\r
-        self.unlockdir()\r
-\r
-    def unlockfile(self):\r
-        if self.lockfile:\r
-            print "unlink", self.lockfile\r
-            try:\r
-                os.unlink(self.lockfile)\r
-            except os.error:\r
-                pass\r
-            self.lockfile = None\r
-\r
-    def unlockdir(self):\r
-        if self.lockdir:\r
-            print "rmdir", self.lockdir\r
-            try:\r
-                os.rmdir(self.lockdir)\r
-            except os.error:\r
-                pass\r
-            self.lockdir = None\r
-\r
-    def sleep(self, st):\r
-        sleep(st, self.repository, self.delay)\r
-\r
-    def join(self, name):\r
-        return os.path.join(self.repository, name)\r
-\r
-\r
-def sleep(st, repository, delay):\r
-    if delay <= 0:\r
-        raise Locked(st)\r
-    uid = st[stat.ST_UID]\r
-    try:\r
-        pwent = pwd.getpwuid(uid)\r
-        user = pwent[0]\r
-    except KeyError:\r
-        user = "uid %d" % uid\r
-    print "[%s]" % time.ctime(time.time())[11:19],\r
-    print "Waiting for %s's lock in" % user, repository\r
-    time.sleep(delay)\r
-\r
-\r
-class ReadLock(Lock):\r
-\r
-    def __init__(self, repository, delay = DELAY):\r
-        Lock.__init__(self, repository, delay)\r
-        ok = 0\r
-        try:\r
-            self.setlockdir()\r
-            self.lockfile = self.cvsrfl\r
-            fp = open(self.lockfile, 'w')\r
-            fp.close()\r
-            ok = 1\r
-        finally:\r
-            if not ok:\r
-                self.unlockfile()\r
-            self.unlockdir()\r
-\r
-\r
-class WriteLock(Lock):\r
-\r
-    def __init__(self, repository, delay = DELAY):\r
-        Lock.__init__(self, repository, delay)\r
-        self.setlockdir()\r
-        while 1:\r
-            uid = self.readers_exist()\r
-            if not uid:\r
-                break\r
-            self.unlockdir()\r
-            self.sleep(uid)\r
-        self.lockfile = self.cvswfl\r
-        fp = open(self.lockfile, 'w')\r
-        fp.close()\r
-\r
-    def readers_exist(self):\r
-        n = len(CVSRFL)\r
-        for name in os.listdir(self.repository):\r
-            if name[:n] == CVSRFL:\r
-                try:\r
-                    st = os.stat(self.join(name))\r
-                except os.error:\r
-                    continue\r
-                return st\r
-        return None\r
-\r
-\r
-def MultipleWriteLock(repositories, delay = DELAY):\r
-    while 1:\r
-        locks = []\r
-        for r in repositories:\r
-            try:\r
-                locks.append(WriteLock(r, 0))\r
-            except Locked, instance:\r
-                del locks\r
-                break\r
-        else:\r
-            break\r
-        sleep(instance.msg, r, delay)\r
-    return list\r
-\r
-\r
-def test():\r
-    import sys\r
-    if sys.argv[1:]:\r
-        repository = sys.argv[1]\r
-    else:\r
-        repository = "."\r
-    rl = None\r
-    wl = None\r
-    try:\r
-        print "attempting write lock ..."\r
-        wl = WriteLock(repository)\r
-        print "got it."\r
-        wl.unlock()\r
-        print "attempting read lock ..."\r
-        rl = ReadLock(repository)\r
-        print "got it."\r
-        rl.unlock()\r
-    finally:\r
-        print [1]\r
-        sys.exc_traceback = None\r
-        print [2]\r
-        if rl:\r
-            rl.unlock()\r
-        print [3]\r
-        if wl:\r
-            wl.unlock()\r
-        print [4]\r
-        rl = None\r
-        print [5]\r
-        wl = None\r
-        print [6]\r
-\r
-\r
-if __name__ == '__main__':\r
-    test()\r