Code

Xapian indexing improved: Slightly faster and slightly smaller database.
[roundup.git] / roundup / backends / portalocker.py
index 9c8389e7a87bfd37231d0d99ba62117a5c68ad97..18b2748450f295f6d39e42c49a8d28d1be8023a4 100644 (file)
@@ -2,11 +2,11 @@
 #                  Requires python 1.5.2 or better.
 
 # ID line added by richard for Roundup file tracking
-# $Id: portalocker.py,v 1.7 2003-08-26 00:29:20 richard Exp $
+# $Id: portalocker.py,v 1.9 2006-09-09 05:42:45 richard Exp $
 
-""" Cross-platform (posix/nt) API for flock-style file locking.
+"""Cross-platform (posix/nt) API for flock-style file locking.
 
-Synopsis:
+Synopsis::
 
    import portalocker
    file = open("somefile", "r+")
@@ -15,18 +15,18 @@ Synopsis:
    file.write("foo")
    file.close()
 
-If you know what you're doing, you may choose to
+If you know what you're doing, you may choose to::
 
    portalocker.unlock(file)
 
 before closing the file, but why?
 
-Methods:
+Methods::
 
    lock( file, flags )
    unlock( file )
 
-Constants:
+Constants::
 
    LOCK_EX
    LOCK_SH
@@ -36,10 +36,12 @@ 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**
+: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**
 """
+__docformat__ = 'restructuredtext'
+
 import os
 
 if os.name == 'nt':
@@ -64,7 +66,8 @@ if os.name == 'nt':
     FFFF0000 = -65536
     def lock(file, flags):
         hfile = win32file._get_osfhandle(file.fileno())
-        # LockFileEx is not supported on all Win32 platforms (Win95, Win98, WinME).
+        # LockFileEx is not supported on all Win32 platforms (Win95, Win98,
+        # WinME).
         # If it's not supported, win32file will raise an exception.
         # Try LockFileEx first, as it has more functionality and handles
         # blocking locks more efficiently.
@@ -78,10 +81,12 @@ if os.name == 'nt':
             
             # LockFileEx is not supported. Use LockFile.
             # LockFile does not support shared locking -- always exclusive.
-            # Care: the low/high length params are reversed compared to LockFileEx.
+            # Care: the low/high length params are reversed compared to
+            # LockFileEx.
             if not flags & LOCK_EX:
                 import warnings
-                warnings.warn("PortaLocker does not support shared locking on Win9x", RuntimeWarning)
+                warnings.warn("PortaLocker does not support shared "
+                    "locking on Win9x", RuntimeWarning)
             # LockFile only supports immediate-fail locking.
             if flags & LOCK_NB:
                 win32file.LockFile(hfile, 0, 0, FFFF0000, 0)
@@ -94,7 +99,8 @@ if os.name == 'nt':
                         win32file.LockFile(hfile, 0, 0, FFFF0000, 0)
                         break
                     except win32file.error, e:
-                        # Propagate upwards all exceptions other than lock violation.
+                        # Propagate upwards all exceptions other than lock
+                        # violation.
                         if e[0] != winerror.ERROR_LOCK_VIOLATION:
                             raise e
                     # Sleep and poll again.
@@ -103,7 +109,8 @@ if os.name == 'nt':
                     
     def unlock(file):
         hfile = win32file._get_osfhandle(file.fileno())
-        # UnlockFileEx is not supported on all Win32 platforms (Win95, Win98, WinME).
+        # UnlockFileEx is not supported on all Win32 platforms (Win95, Win98,
+        # WinME).
         # If it's not supported, win32file will raise an api_error exception.
         try:
             win32file.UnlockFileEx(hfile, 0, FFFF0000, __overlapped)
@@ -114,7 +121,8 @@ if os.name == 'nt':
                 raise e
             
             # UnlockFileEx is not supported. Use UnlockFile.
-            # Care: the low/high length params are reversed compared to UnLockFileEx.
+            # Care: the low/high length params are reversed compared to
+            # UnLockFileEx.
             win32file.UnlockFile(hfile, 0, 0, FFFF0000, 0)
 
 elif os.name =='posix':
@@ -128,10 +136,9 @@ elif os.name =='posix':
 if __name__ == '__main__':
     from time import time, strftime, localtime
     import sys
-    import portalocker
 
     log = open('log.txt', "a+")
-    portalocker.lock(log, portalocker.LOCK_EX)
+    lock(log, LOCK_EX)
 
     timestamp = strftime("%m/%d/%Y %H:%M:%S\n", localtime(time()))
     log.write( timestamp )