extras-buildsys/utils/pushscript LockFile.py,1.2,1.3
Michael Schwendt
mschwendt at fedoraproject.org
Mon Sep 29 15:53:05 UTC 2008
Author: mschwendt
Update of /cvs/fedora/extras-buildsys/utils/pushscript
In directory cvs1.fedora.phx.redhat.com:/tmp/cvs-serv25913
Modified Files:
LockFile.py
Log Message:
switch to a later implementation that adds optional POSIX fcntl() write-lock support
Index: LockFile.py
===================================================================
RCS file: /cvs/fedora/extras-buildsys/utils/pushscript/LockFile.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- LockFile.py 8 Feb 2007 18:52:32 -0000 1.2
+++ LockFile.py 29 Sep 2008 15:53:05 -0000 1.3
@@ -14,27 +14,34 @@
# 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
+#
+# Author: Michael Schwendt <mschwendt at fedoraproject.org>
+
+import errno, fcntl, os, struct, sys
-import os, sys
-import errno, fcntl
class LockFileLocked(Exception):
+ """Exception thrown if a conflicting lock is found in non-blocking mode"""
pass
-class LockFile:
+class SimplePosixLock:
+ """An exclusive lock on a file via fcntl()"""
def __init__(self, name, blocking=False):
- self.name = name
- self.mode = fcntl.LOCK_EX
- if not blocking:
- self.mode |= fcntl.LOCK_NB
+ self._fname = name
+ self._lock = struct.pack('hhqqhh', fcntl.F_WRLCK, os.SEEK_SET, 0, 0, 0, 0)
+ self._unlock = struct.pack('hhqqhh', fcntl.F_UNLCK, os.SEEK_SET, 0, 0, 0, 0)
+ if blocking:
+ self._l_cmd = fcntl.F_SETLKW
+ else:
+ self._l_cmd = fcntl.F_SETLK
def lock(self):
try:
- self.file = open(self.name,'w')
- self.rc = fcntl.flock(self.file, self.mode)
+ self._file = open(self._fname,'w')
+ rv = fcntl.fcntl(self._file.fileno(), self._l_cmd, self._lock)
except IOError, (err, strerr):
if ( err == errno.EAGAIN ):
raise LockFileLocked
@@ -43,44 +50,53 @@
def unlock(self):
- fcntl.flock(self.file, fcntl.LOCK_UN)
- self.file.close()
- os.remove(self.name)
+ rv = fcntl.fcntl(self._file.fileno(), self._l_cmd, self._unlock)
+ self._file.close()
-## Main
-import unittest
+class SimpleFlock:
+ """An exclusive lock on a file via flock()"""
+
+ def __init__(self, name, blocking=False):
+ self._fname = name
+ self._mode = fcntl.LOCK_EX
+ if not blocking:
+ self._mode |= fcntl.LOCK_NB
-class TestSequenceFunctions(unittest.TestCase):
- def testnonblocking(self):
- name = '.test.lock'
- l1 = LockFile(name,False)
- l1.lock()
- l2 = LockFile(name,False)
+ def lock(self):
try:
- l2.lock()
- self.assert_(False)
- except LockFileLocked:
- self.assert_(True)
- l1.unlock()
+ self._file = open(self._fname,'w')
+ rc = fcntl.flock(self._file, self._mode)
+ except IOError, (err, strerr):
+ if ( err == errno.EAGAIN ):
+ raise LockFileLocked
+ else:
+ raise
- 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)
+ def unlock(self):
+ fcntl.flock(self._file, fcntl.LOCK_UN)
+ self._file.close()
+
+
+class LockFile:
+
+ def __init__(self, name, blocking=False, type='flock'):
+ if type == 'flock':
+ self._base = SimpleFlock(name,blocking)
+ elif type == 'posix':
+ self._base = SimplePosixLock(name,blocking)
+ else:
+ sys.stderr.write("Invalid LockFile type!\n")
+ self._base = SimpleFlock(name,blocking)
+
+
+ def lock(self):
+ self._base.lock()
+
+
+ def unlock(self):
+ self._base.unlock()
-if __name__ == '__main__':
- unittest.main()
More information about the fedora-extras-commits
mailing list