from pathlib import Path
import stat


def rmtree(top: Path):
    """
    Reimplementation of shutil.rmtree, used to remove a directory. Returns a
    boolean on whether top was a directory or not (in the latter case this
    function does nothing).
    The reason shutil.rmtree itself is not used, is of a permission error on
    Windows.
    """
    if not top.is_dir():
        return False

    for newpath in top.iterdir():
        if not rmtree(newpath):
            # could not rmtree newpath because it is a file, hence unlink
            newpath.chmod(stat.S_IWUSR)
            newpath.unlink()

    top.rmdir()
    return True