'Removes a prefix and any empty parent directories from the root.
Raised RemoveFailedError if something goes wrong.'

def remove_install_directory(self, spec):

    path = self.path_for_spec(spec)
    assert path.startswith(self.root)
    if os.path.exists(path):
        try:
            shutil.rmtree(path)
        except OSError as e:
            raise RemoveFailedError(spec, path, e)
    path = os.path.dirname(path)
    while (path != self.root):
        if os.path.isdir(path):
            if os.listdir(path):
                return
            os.rmdir(path)
        path = os.path.dirname(path)
