import fcntl
import errno


def obtain_lock(lock_filepath):
    """ Open and obtain a flock on the parameter. Returns a file if successful, None if not
    """
    lock_file = open(lock_filepath, 'w')
    try:
        fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
        return lock_file
    except IOError as err:
        if err.errno != errno.EAGAIN:
            raise
        lock_file.close()
        return None