trustlink / trustlink /

Provide functions for check if file is locked.

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')

    if == 'nt':
        # Code for NT systems got from:
        import win32con
        import win32file
        import pywintypes
        LOCK_SH = 0 # the default
        # is there any reason not to reuse the following structure?
        __overlapped = pywintypes.OVERLAPPED()
        hfile = win32file._get_osfhandle(fh.fileno())
            win32file.LockFileEx(hfile, LOCK_EX | LOCK_NB, 0, -0x10000, __overlapped)
        except pywintypes.error, exc_value:
            # error: (33, 'LockFileEx', 'The process cannot access 
            # the file because another process has locked a portion
            # of the file.')
            if exc_value[0] == 33:
                return False
        from fcntl import flock, LOCK_EX, LOCK_NB
            flock(fh.fileno(), LOCK_EX | LOCK_NB)
        except Exception, ex:
            return False
    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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.