|
| 1 | +"""Make switcher.json to allow docs to switch between different versions.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import logging |
| 5 | +from argparse import ArgumentParser |
| 6 | +from pathlib import Path |
| 7 | +from subprocess import CalledProcessError, check_output |
| 8 | + |
| 9 | + |
| 10 | +def report_output(stdout: bytes, label: str) -> list[str]: |
| 11 | + """Print and return something received frm stdout.""" |
| 12 | + ret = stdout.decode().strip().split("\n") |
| 13 | + print(f"{label}: {ret}") |
| 14 | + return ret |
| 15 | + |
| 16 | + |
| 17 | +def get_branch_contents(ref: str) -> list[str]: |
| 18 | + """Get the list of directories in a branch.""" |
| 19 | + stdout = check_output(["git", "ls-tree", "-d", "--name-only", ref]) |
| 20 | + return report_output(stdout, "Branch contents") |
| 21 | + |
| 22 | + |
| 23 | +def get_sorted_tags_list() -> list[str]: |
| 24 | + """Get a list of sorted tags in descending order from the repository.""" |
| 25 | + stdout = check_output(["git", "tag", "-l", "--sort=-v:refname"]) |
| 26 | + return report_output(stdout, "Tags list") |
| 27 | + |
| 28 | + |
| 29 | +def get_versions(ref: str, add: str | None) -> list[str]: |
| 30 | + """Generate the file containing the list of all GitHub Pages builds.""" |
| 31 | + # Get the directories (i.e. builds) from the GitHub Pages branch |
| 32 | + try: |
| 33 | + builds = set(get_branch_contents(ref)) |
| 34 | + except CalledProcessError: |
| 35 | + builds = set() |
| 36 | + logging.warning(f"Cannot get {ref} contents") |
| 37 | + |
| 38 | + # Add and remove from the list of builds |
| 39 | + if add: |
| 40 | + builds.add(add) |
| 41 | + |
| 42 | + # Get a sorted list of tags |
| 43 | + tags = get_sorted_tags_list() |
| 44 | + |
| 45 | + # Make the sorted versions list from main branches and tags |
| 46 | + versions: list[str] = [] |
| 47 | + for version in ["master", "main"] + tags: |
| 48 | + if version in builds: |
| 49 | + versions.append(version) |
| 50 | + builds.remove(version) |
| 51 | + |
| 52 | + # Add in anything that is left to the bottom |
| 53 | + versions += sorted(builds) |
| 54 | + print(f"Sorted versions: {versions}") |
| 55 | + return versions |
| 56 | + |
| 57 | + |
| 58 | +def write_json(path: Path, repository: str, versions: list[str]): |
| 59 | + """Write the JSON switcher to path.""" |
| 60 | + org, repo_name = repository.split("/") |
| 61 | + struct = [ |
| 62 | + {"version": version, "url": f"https://{org}.github.io/{repo_name}/{version}/"} |
| 63 | + for version in versions |
| 64 | + ] |
| 65 | + text = json.dumps(struct, indent=2) |
| 66 | + print(f"JSON switcher:\n{text}") |
| 67 | + path.write_text(text, encoding="utf-8") |
| 68 | + |
| 69 | + |
| 70 | +def main(args=None): |
| 71 | + """Parse args and write switcher.""" |
| 72 | + parser = ArgumentParser( |
| 73 | + description="Make a versions.json file from gh-pages directories" |
| 74 | + ) |
| 75 | + parser.add_argument( |
| 76 | + "--add", |
| 77 | + help="Add this directory to the list of existing directories", |
| 78 | + ) |
| 79 | + parser.add_argument( |
| 80 | + "repository", |
| 81 | + help="The GitHub org and repository name: ORG/REPO", |
| 82 | + ) |
| 83 | + parser.add_argument( |
| 84 | + "output", |
| 85 | + type=Path, |
| 86 | + help="Path of write switcher.json to", |
| 87 | + ) |
| 88 | + args = parser.parse_args(args) |
| 89 | + |
| 90 | + # Write the versions file |
| 91 | + versions = get_versions("origin/gh-pages", args.add) |
| 92 | + write_json(args.output, args.repository, versions) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + main() |
0 commit comments