Atomically "move" @own_file to @lock_file if the latter does not exists,
    else just remove @own_file.

    @own_file: filepath of the temporary file that contains our PID
    @lock_file: destination filepath
    @own_content: content of @own_file

    Return True if the lock has been successfully taken, else False.
    (Caller should also be prepared for OSError exceptions)

def take_file_lock(own_file, lock_file, own_content):
    """
    Atomically "move" @own_file to @lock_file if the latter does not exists,
    else just remove @own_file.

    @own_file: filepath of the temporary file that contains our PID
    @lock_file: destination filepath
    @own_content: content of @own_file

    Return True if the lock has been successfully taken, else False.
    (Caller should also be prepared for OSError exceptions)
    """
    try:
        try:
            os.link(own_file, lock_file)
        finally:
            os.unlink(own_file)
    except OSError, e:
        if e.errno == errno.EEXIST:
            log.warning("The lock file %r already exists - won't "
                    "overwrite it.  An other instance of ourself "
                    "is probably running.", lock_file)
            return False
        else:
            raise
    content = file(lock_file).read(len(own_content) + 1)
    if content != own_content:
        log.warning(
                "I thought I successfully took the lock file %r but "
                "it does not contain what was expected.  Somebody is "
                "playing with us.", lock_file)
        return False
    return True