def lock_file(filename, interval=.5, timeout=15):
    '''
    Lock a file; if it is already locked, then wait for it to become available
    before locking it.

    Note that these locks are only recognized by Salt Cloud, and not other
    programs or platforms.
    '''
    log.trace('Attempting to obtain lock for %s', filename)
    lock = filename + '.lock'
    start = time.time()
    while True:
        if os.path.exists(lock):
            if time.time() - start >= timeout:
                log.warning('Unable to obtain lock for %s', filename)
                return False
            time.sleep(interval)
        else:
            break

    with salt.utils.files.fopen(lock, 'a'):
        pass
Lock a file; if it is already locked, then wait for it to become available
    before locking it.

    Note that these locks are only recognized by Salt Cloud, and not other
    programs or platforms.