diff roundup/backends/portalocker.py @ 690:509a101305da

node ids are now generated from a lockable store - no more race conditions We're using the portalocker code by Jonathan Feinberg that was contributed to the ASPN Python cookbook. This gives us locking across Unix and Windows.
author Richard Jones <richard@users.sourceforge.net>
date Mon, 15 Apr 2002 23:25:15 +0000
parents
children 54333751e98d 8dd4f736370b
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/roundup/backends/portalocker.py	Mon Apr 15 23:25:15 2002 +0000
@@ -0,0 +1,93 @@
+# portalocker.py - Cross-platform (posix/nt) API for flock-style file locking.
+#                  Requires python 1.5.2 or better.
+
+# ID line added by richard for Roundup file tracking
+# $Id: portalocker.py,v 1.1 2002-04-15 23:25:15 richard Exp $
+
+"""Cross-platform (posix/nt) API for flock-style file locking.
+
+Synopsis:
+
+   import portalocker
+   file = open("somefile", "r+")
+   portalocker.lock(file, portalocker.LOCK_EX)
+   file.seek(12)
+   file.write("foo")
+   file.close()
+
+If you know what you're doing, you may choose to
+
+   portalocker.unlock(file)
+
+before closing the file, but why?
+
+Methods:
+
+   lock( file, flags )
+   unlock( file )
+
+Constants:
+
+   LOCK_EX
+   LOCK_SH
+   LOCK_NB
+
+I learned the win32 technique for locking files from sample code
+provided by John Nielsen <nielsenjf@my-deja.com> in the documentation
+that accompanies the win32 modules.
+
+Author: Jonathan Feinberg <jdf@pobox.com>
+Version: Id: portalocker.py,v 1.3 2001/05/29 18:47:55 Administrator Exp 
+         **un-cvsified by richard so the version doesn't change**
+"""
+import os
+
+if os.name == 'nt':
+	import win32con
+	import win32file
+	import pywintypes
+	LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
+	LOCK_SH = 0 # the default
+	LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
+	# is there any reason not to reuse the following structure?
+	__overlapped = pywintypes.OVERLAPPED()
+elif os.name == 'posix':
+	import fcntl
+	LOCK_EX = fcntl.LOCK_EX
+	LOCK_SH = fcntl.LOCK_SH
+	LOCK_NB = fcntl.LOCK_NB
+else:
+	raise RuntimeError("PortaLocker only defined for nt and posix platforms")
+
+if os.name == 'nt':
+	def lock(file, flags):
+		hfile = win32file._get_osfhandle(file.fileno())
+		win32file.LockFileEx(hfile, flags, 0, 0xffff0000, __overlapped)
+
+	def unlock(file):
+		hfile = win32file._get_osfhandle(file.fileno())
+		win32file.UnlockFileEx(hfile, 0, 0xffff0000, __overlapped)
+
+elif os.name =='posix':
+	def lock(file, flags):
+		fcntl.flock(file.fileno(), flags)
+
+	def unlock(file):
+		fcntl.flock(file.fileno(), fcntl.LOCK_UN)
+
+if __name__ == '__main__':
+	from time import time, strftime, localtime
+	import sys
+	import portalocker
+
+	log = open('log.txt', "a+")
+	portalocker.lock(log, portalocker.LOCK_EX)
+
+	timestamp = strftime("%m/%d/%Y %H:%M:%S\n", localtime(time()))
+	log.write( timestamp )
+
+	print "Wrote lines. Hit enter to release lock."
+	dummy = sys.stdin.readline()
+
+	log.close()
+

Roundup Issue Tracker: http://roundup-tracker.org/