+#
+# Hacks to work around lack of Python 2.5's `with` statement.
+#
+
+def with_closing(rsrc):
+ "Utility to emulate Python 2.5's `with closing(rsrc)` context manager."
+ def wrapper(func):
+ try: return func(rsrc)
+ finally: rsrc.close()
+ return wrapper
+
+def with_lock_file(path):
+ """
+ Context manager for lock files. Example:
+
+ @with_lock_file('/tmp/mylock')
+ def input():
+ print 'locked'
+ return raw_input()
+ # decorator is executed immediately
+ print input # prints the input text
+ """
+ def wrapper(func):
+ @with_closing(file(path, 'w'))
+ def g(f):
+ flock(f, LOCK_EX)
+ try: return func()
+ finally: flock(f, LOCK_UN)
+ remove(path)
+ return g
+ return wrapper
+
+#
+# Tests.
+#