|
| 1 | +"""Update the 'exact' redirects on Read the Docs to match an in-tree file's contents. |
| 2 | +
|
| 3 | +Relevant API reference: https://docs.readthedocs.io/en/stable/api/v3.html#redirects |
| 4 | +""" |
| 5 | +import operator |
| 6 | +import os |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import httpx |
| 11 | +import rich |
| 12 | +import yaml |
| 13 | + |
| 14 | +try: |
| 15 | + _TOKEN = os.environ["RTD_API_TOKEN"] |
| 16 | +except KeyError: |
| 17 | + rich.print( |
| 18 | + "[bold]error[/]: [red]No API token provided. Please set `RTD_API_TOKEN`.[/]", |
| 19 | + file=sys.stderr, |
| 20 | + ) |
| 21 | + sys.exit(1) |
| 22 | + |
| 23 | +RTD_API_HEADERS = {"Authorization": f"token {_TOKEN}"} |
| 24 | +RTD_API_BASE_URL = "https://readthedocs.org/api/v3/projects/pip/" |
| 25 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 26 | + |
| 27 | + |
| 28 | +# -------------------------------------------------------------------------------------- |
| 29 | +# Helpers |
| 30 | +# -------------------------------------------------------------------------------------- |
| 31 | +def next_step(msg: str) -> None: |
| 32 | + rich.print(f"> [blue]{msg}[/]") |
| 33 | + |
| 34 | + |
| 35 | +def log_response(response: httpx.Response) -> None: |
| 36 | + request = response.request |
| 37 | + rich.print(f"[bold magenta]{request.method}[/] {request.url} -> {response}") |
| 38 | + |
| 39 | + |
| 40 | +def get_rtd_api() -> httpx.Client: |
| 41 | + return httpx.Client( |
| 42 | + headers=RTD_API_HEADERS, |
| 43 | + base_url=RTD_API_BASE_URL, |
| 44 | + event_hooks={"response": [log_response]}, |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +# -------------------------------------------------------------------------------------- |
| 49 | +# Actual logic |
| 50 | +# -------------------------------------------------------------------------------------- |
| 51 | +next_step("Loading local redirects from the yaml file.") |
| 52 | + |
| 53 | +with open(REPO_ROOT / ".readthedocs-custom-redirects.yml") as f: |
| 54 | + local_redirects = yaml.safe_load(f) |
| 55 | + |
| 56 | +rich.print("Loaded local redirects!") |
| 57 | +for src, dst in sorted(local_redirects.items()): |
| 58 | + rich.print(f" [yellow]{src}[/] --> {dst}") |
| 59 | +rich.print(f"{len(local_redirects)} entries.") |
| 60 | + |
| 61 | + |
| 62 | +next_step("Fetch redirects configured on RTD.") |
| 63 | + |
| 64 | +with get_rtd_api() as rtd_api: |
| 65 | + response = rtd_api.get("redirects/") |
| 66 | + response.raise_for_status() |
| 67 | + |
| 68 | + rtd_redirects = response.json() |
| 69 | + |
| 70 | +for redirect in sorted( |
| 71 | + rtd_redirects["results"], key=operator.itemgetter("type", "from_url", "to_url") |
| 72 | +): |
| 73 | + if redirect["type"] != "exact": |
| 74 | + rich.print(f" [magenta]{redirect['type']}[/]") |
| 75 | + continue |
| 76 | + |
| 77 | + pk = redirect["pk"] |
| 78 | + src = redirect["from_url"] |
| 79 | + dst = redirect["to_url"] |
| 80 | + rich.print(f" [yellow]{src}[/] -({pk:^5})-> {dst}") |
| 81 | + |
| 82 | +rich.print(f"{rtd_redirects['count']} entries.") |
| 83 | + |
| 84 | + |
| 85 | +next_step("Compare and determine modifications.") |
| 86 | + |
| 87 | +redirects_to_remove: list[int] = [] |
| 88 | +redirects_to_add: dict[str, str] = {} |
| 89 | + |
| 90 | +for redirect in rtd_redirects["results"]: |
| 91 | + if redirect["type"] != "exact": |
| 92 | + continue |
| 93 | + |
| 94 | + rtd_src = redirect["from_url"] |
| 95 | + rtd_dst = redirect["to_url"] |
| 96 | + redirect_id = redirect["pk"] |
| 97 | + |
| 98 | + if rtd_src not in local_redirects: |
| 99 | + redirects_to_remove.append(redirect_id) |
| 100 | + continue |
| 101 | + |
| 102 | + local_dst = local_redirects[rtd_src] |
| 103 | + if local_dst != rtd_dst: |
| 104 | + redirects_to_remove.append(redirect_id) |
| 105 | + redirects_to_add[rtd_src] = local_dst |
| 106 | + |
| 107 | + del local_redirects[rtd_src] |
| 108 | + |
| 109 | +for src, dst in sorted(local_redirects.items()): |
| 110 | + redirects_to_add[src] = dst |
| 111 | + del local_redirects[src] |
| 112 | + |
| 113 | +assert not local_redirects |
| 114 | + |
| 115 | +if not redirects_to_remove: |
| 116 | + rich.print("Nothing to remove.") |
| 117 | +else: |
| 118 | + rich.print(f"To remove: ({len(redirects_to_remove)} entries)") |
| 119 | + for redirect_id in redirects_to_remove: |
| 120 | + rich.print(" ", redirect_id) |
| 121 | + |
| 122 | +if not redirects_to_add: |
| 123 | + rich.print("Nothing to add.") |
| 124 | +else: |
| 125 | + rich.print(f"To add: ({len(redirects_to_add)} entries)") |
| 126 | + for src, dst in redirects_to_add.items(): |
| 127 | + rich.print(f" {src} --> {dst}") |
| 128 | + |
| 129 | + |
| 130 | +next_step("Update the RTD redirects.") |
| 131 | + |
| 132 | +if not (redirects_to_add or redirects_to_remove): |
| 133 | + rich.print("[green]Nothing to do![/]") |
| 134 | + sys.exit(0) |
| 135 | + |
| 136 | +exit_code = 0 |
| 137 | +with get_rtd_api() as rtd_api: |
| 138 | + for redirect_id in redirects_to_remove: |
| 139 | + response = rtd_api.delete(f"redirects/{redirect_id}/") |
| 140 | + response.raise_for_status() |
| 141 | + if response.status_code != 204: |
| 142 | + rich.print("[red]This might not have been removed correctly.[/]") |
| 143 | + exit_code = 1 |
| 144 | + |
| 145 | + for src, dst in redirects_to_add.items(): |
| 146 | + response = rtd_api.post( |
| 147 | + "redirects/", |
| 148 | + json={"from_url": src, "to_url": dst, "type": "exact"}, |
| 149 | + ) |
| 150 | + response.raise_for_status() |
| 151 | + if response.status_code != 201: |
| 152 | + rich.print("[red]This might not have been added correctly.[/]") |
| 153 | + exit_code = 1 |
| 154 | + |
| 155 | +sys.exit(exit_code) |
0 commit comments