extras-buildsys/utils/pushscript LockFile.py, NONE, 1.1 Push.py, 1.27, 1.28

Michael Schwendt (mschwendt) fedora-extras-commits at redhat.com
Thu Feb 8 18:45:39 UTC 2007


Author: mschwendt

Update of /cvs/fedora/extras-buildsys/utils/pushscript
In directory cvs-int.fedora.redhat.com:/tmp/cvs-serv26846

Modified Files:
	Push.py 
Added Files:
	LockFile.py 
Log Message:
Split off LockFile class.



--- NEW FILE LockFile.py ---
#!/usr/bin/python -t
# -*- mode: Python; indent-tabs-mode: nil; -*-
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os, sys
import errno, fcntl

class LockFileLocked(Exception):
    pass


class LockFile:

    def __init__(self, name, blocking=False):
        self.name = name
        self.mode = fcntl.LOCK_EX
        if not blocking:
            self.mode |= fcntl.LOCK_NB


    def lock(self):
        try:
            self.file = open(self.name,'w')
            self.rc = fcntl.flock(self.file, self.mode)
        except IOError, (err, strerr):
            if ( err == errno.EAGAIN ):
                raise LockFileLocked
            else:
                raise


    def unlock(self):
        fcntl.flock(self.file, fcntl.LOCK_UN)
        self.file.close()
        os.remove(self.name)

## Main

import unittest

class TestSequenceFunctions(unittest.TestCase):

    def testnonblocking(self):
        name = '.test.lock'
        l1 = LockFile(name,False)
        l1.lock()
        l2 = LockFile(name,False)
        try:
            l2.lock()
            self.assert_(False)
        except LockFileLocked:
            self.assert_(True)
        l1.unlock()


    def testseries(self):
        name = '.test.lock'
        l1 = LockFile(name,False)
        l1.lock()
        l2 = LockFile(name,False)
        try:
            l1.unlock()
            l2.lock()
            l2.unlock()
            l1.lock()
            l1.unlock()
            self.assert_(True)
        except LockFileLocked:
            self.assert_(False)


if __name__ == '__main__':
    suitemain = unittest.makeSuite(TestSequenceFunctions)
    alltests = unittest.TestSuite((suitemain))
    unittest.TextTestRunner(verbosity=2).run(alltests)


Index: Push.py
===================================================================
RCS file: /cvs/fedora/extras-buildsys/utils/pushscript/Push.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -r1.27 -r1.28
--- Push.py	1 Jan 2007 16:00:46 -0000	1.27
+++ Push.py	8 Feb 2007 18:45:36 -0000	1.28
@@ -26,6 +26,7 @@
 
 import Utils, MultiLib, Comps, WhatsNew
 import RepoBuild, RepoPrune, RepoView
+from LockFile import LockFile, LockFileLocked
 
 DEBUG = False
 Utils.setdebug(DEBUG)
@@ -40,32 +41,6 @@
 ts = rpmUtils.transaction.initReadOnlyTransaction()
 # Further globals: cfg, srpmlocdict
 
-class LockFile:
-
-    def __init__(self, name, blocking=False):
-        self.name = name
-        self.mode = fcntl.LOCK_EX
-        if not blocking:
-            self.mode |= fcntl.LOCK_NB
-
-
-    def lock(self):
-        if DEBUG:
-            return
-        try:
-            self.file = open(self.name,'w')
-        except IOError, (err, strerr):
-            print "ERROR: opening lockfile %s failed: %s (error %d)" % (self.name, strerr, err)
-            sys.exit(err)
-        self.rc = fcntl.flock(self.file, self.mode)
-
-
-    def unlock(self):
-        if DEBUG:
-            return
-        fcntl.flock(self.file, fcntl.LOCK_UN)
-        self.file.close()
-
 
 def getrunfilename(dist):
     runfilename = os.path.join(cfg.rundir, dist)
@@ -647,8 +622,8 @@
     lock = LockFile(lockfile)
     try:
         lock.lock()
-    except IOError:
-        print 'ERROR: script locked via lockfile %s - it seems to be running already' % lockfile
+    except LockFileLocked:
+        print 'Script locked via %s\nIt seems to be in use already.' % lockfile
         sys.exit(errno.EPERM)
 
     changed = []      # dists with changes in them




More information about the scm-commits mailing list