def validate_tutorial_links()

in scripts/parse_tutorials.py [0:0]


def validate_tutorial_links(repo_dir: str) -> None:
    """Checks that all .ipynb files that present are linked on the website, and vice
    versa, that any linked tutorial has an associated .ipynb file present.
    """
    with open(os.path.join(repo_dir, "website", "tutorials.json"), "r") as infile:
        tutorial_config = json.load(infile)

    tutorial_ids = {x["id"] for v in tutorial_config.values() for x in v}

    tutorials_nbs = {
        fn.replace(".ipynb", "")
        for fn in os.listdir(os.path.join(repo_dir, "tutorials"))
        if fn[-6:] == ".ipynb"
    }

    missing_files = tutorial_ids - tutorials_nbs
    missing_ids = tutorials_nbs - tutorial_ids

    if missing_files:
        raise RuntimeError(
            "The following tutorials are linked on the website, but missing an "
            f"associated .ipynb file: {missing_files}."
        )

    if missing_ids:
        raise RuntimeError(
            "The following tutorial files are present, but are not linked on the "
            "website: {}.".format(", ".join([nbid + ".ipynb" for nbid in missing_ids]))
        )