"""Tool to manage the versions.html file at the root of our docs sites.""" import argparse from pathlib import Path from bs4 import BeautifulSoup from bs4.element import Tag from semver import VersionInfo VERSIONS_LIST_ID = "versions-list" def strip_leading_v(version_str: str): """Strip leading v of a version which is not SemVer compatible.""" return version_str[1:] if version_str.startswith("v") else version_str def create_list_element(soup: BeautifulSoup, contents: Tag) -> Tag: """Create a list element for links. Args: soup (BeautifulSoup): The soup to use to create the tag. Returns: Tag: tag containing
  • . """ new_list_element = soup.new_tag("li", **{"class": "toctree-l1"}) new_list_element.contents.append(contents) return new_list_element def create_link_tag_set_string(soup: BeautifulSoup, version_string: str) -> Tag: """Create a link tag on the given soup to version specified by version_string. Args: soup (BeautifulSoup): The soup to use to create the tag. version_string (str): The version string to use. Returns: Tag: tag containing {version_string}. """ new_tag = soup.new_tag( "a", **{ "href": f"{version_string}/", "class": "reference internal", }, ) new_tag.string = version_string return new_tag def main(args): """Entry point.""" invalid_versions = [ version for version in args.add_versions if not VersionInfo.isvalid(strip_leading_v(version)) ] if len(invalid_versions) > 0: message = f"Found invalid versions: {invalid_versions}" raise RuntimeError(message) version_html = None version_html_file_path = Path(args.versions_html_file).resolve() with open(version_html_file_path, "r", encoding="utf-8") as f: version_html = BeautifulSoup(f, "html.parser") if version_html is None: message = f"An error occured while trying to load {str(version_html_file_path)}" raise RuntimeError(message) print(version_html) version_list = version_html.find(id=VERSIONS_LIST_ID) if version_list is None or version_list.name != "ul": message = f"Could not find