def delete(path):
    trace('deleting %s' % path)
    if not os.path.isabs(path):
        raise BockbuildException('Relative paths are not allowed: %s' % path)

    if not os.path.lexists(path):
        raise CommandException('Invalid path to rm: %s' % path)

    if os.getcwd() == path:
        raise BockbuildException(
            'Will not delete current directory: %s' % path)

    # get the dir out of the way so that we don't have to deal with
    # inconsistent state if we fail
    if os.path.isfile(path):
        os.remove(path)
        return

    # directory removal
    if os.path.islink(path):
        os.unlink(path)
        return

    orig_path = path
    unprotect_dir(path, recursive=True)
    path = path + '.deleting'
    if os.path.exists(path):
        delete(path)

    shutil.move(orig_path, path)
    for x in range(1, 5):
        try:
            if os.path.isfile(path) or os.path.islink(path):
                os.remove(path)
            elif os.path.isdir(path):
                shutil.rmtree(path, ignore_errors=False)
        except __HOLE__ as e:
            pass
        finally:
            if not os.path.exists(path):
                break
        warn('retrying delete of %s' % path)
        # try to sabotage whoever else is writing in the directory...
        protect_dir(path, recursive=True)
        time.sleep(1)
        unprotect_dir(path, recursive=True)

    if os.path.exists(path):
        error('Deleting failed: %s' % orig_path)
dataset/ETHPy150Open mono/bockbuild/bockbuild/util/util.py/delete