"""
Provide functions for check if file is locked.
"""

from fcntl import flock, LOCK_EX, LOCK_NB
import os.path
import sys
import logging
import os

fh = None

def set_lock(fname):
    """
    Try to lock file and write PID.
    
    Return the status of operation.
    """

    global fh
    fh = open(fname, 'w')
    try:
        flock(fh.fileno(), LOCK_EX | LOCK_NB)
    except Exception, ex:
        return False
    else:
        fh.write(str(os.getpid()))
        fh.flush()
    return True


def assert_lock(fname):
    """
    If file is locked then terminate program else lock file.
    """

    logging.debug('Trying to lock: %s' % fname)
    if not set_lock(fname):
        logging.error(u'%s is already locked. Terminating.' % fname)
        sys.exit()

@@ -53,50 +53,8 @@
 %22%22%0A%0A
-from fcntl import flock, LOCK_EX, LOCK_NB%0A
 impo
@@ -222,24 +222,28 @@
 on.%0A    %22%22%22%0A
+    
 %0A    global 
@@ -275,63 +275,637 @@
 w')%0A
+%0A
     
-try:%0A        flock(fh.fileno(), LOCK_EX %7C LOCK_NB
+if os.name == 'nt':%0A        # Code for NT systems got from: http://code.activestate.com/recipes/65203/%0A        %0A        import win32con%0A        import win32file%0A        import pywintypes%0A        %0A        LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK%0A        LOCK_SH = 0 # the default%0A        LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY%0A        %0A        # is there any reason not to reuse the following structure?%0A        __overlapped = pywintypes.OVERLAPPED()%0A        %0A        hfile = win32file._get_osfhandle(fh.fileno())%0A        try:%0A            win32file.LockFileEx(hfile, LOCK_EX %7C LOCK_NB, 0, -0x10000, __overlapped
 )%0A
+    
     
@@ -915,58 +915,449 @@
 ept 
-Exception, ex:%0A        return False%0A    else:%0A    
+pywintypes.error, exc_value:%0A            # error: (33, 'LockFileEx', 'The process cannot access %0A            # the file because another process has locked a portion%0A            # of the file.')%0A            if exc_value%5B0%5D == 33:%0A                return False%0A    else:%0A        from fcntl import flock, LOCK_EX, LOCK_NB%0A        try:%0A            flock(fh.fileno(), LOCK_EX %7C LOCK_NB)%0A        except Exception, ex:%0A            return False%0A    %0A
     
@@ -1383,20 +1383,16 @@
 pid()))%0A
-    
     fh.f

Make grab.tools.lock module cross-platform