|
| 1 | +""" |
| 2 | +Update ReadTheDocs to show and hide releases. |
| 3 | +""" |
| 4 | + |
| 5 | +import re |
| 6 | +import sys |
| 7 | + |
| 8 | +from session import get_session |
| 9 | + |
| 10 | +# How many from each level to show. |
| 11 | +NUM_MAJORS = 3 |
| 12 | +NUM_MINORS = 4 |
| 13 | +OLD_MINORS = 1 |
| 14 | +NUM_MICROS = 1 |
| 15 | +OLD_MICROS = 1 |
| 16 | + |
| 17 | + |
| 18 | +def get_all_versions(project): |
| 19 | + """Pull all the versions for a project from ReadTheDocs.""" |
| 20 | + versions = [] |
| 21 | + session = get_session("RTFD_TOKEN") |
| 22 | + |
| 23 | + url = f"https://readthedocs.org/api/v3/projects/{project}/versions/" |
| 24 | + while url: |
| 25 | + resp = session.get(url) |
| 26 | + resp.raise_for_status() |
| 27 | + data = resp.json() |
| 28 | + versions.extend(data["results"]) |
| 29 | + url = data["next"] |
| 30 | + return versions |
| 31 | + |
| 32 | + |
| 33 | +def version_tuple(vstr): |
| 34 | + """Convert a tag name into a version_info tuple.""" |
| 35 | + m = re.fullmatch(r"[^\d]*(\d+)\.(\d+)(?:\.(\d+))?(?:([abc])(\d+))?", vstr) |
| 36 | + if not m: |
| 37 | + return None |
| 38 | + return ( |
| 39 | + int(m[1]), |
| 40 | + int(m[2]), |
| 41 | + int(m[3] or 0), |
| 42 | + (m[4] or "final"), |
| 43 | + int(m[5] or 0), |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def main(project): |
| 48 | + """Update ReadTheDocs for the versions we want to show.""" |
| 49 | + |
| 50 | + # Get all the tags. Where there are dupes, keep the shorter tag for a version. |
| 51 | + versions = get_all_versions(project) |
| 52 | + versions.sort(key=(lambda v: len(v["verbose_name"])), reverse=True) |
| 53 | + vdict = {} |
| 54 | + for v in versions: |
| 55 | + if v["type"] == "tag": |
| 56 | + vinfo = version_tuple(v["verbose_name"]) |
| 57 | + if vinfo and vinfo[3] == "final": |
| 58 | + vdict[vinfo] = v |
| 59 | + |
| 60 | + # Decide which to show and update them. |
| 61 | + |
| 62 | + majors = set() |
| 63 | + minors = set() |
| 64 | + micros = set() |
| 65 | + minors_to_show = NUM_MINORS |
| 66 | + micros_to_show = NUM_MICROS |
| 67 | + |
| 68 | + session = get_session("RTFD_TOKEN") |
| 69 | + version_list = sorted(vdict.items(), reverse=True) |
| 70 | + for vi, ver in version_list: |
| 71 | + if vi[:1] not in majors: |
| 72 | + majors.add(vi[:1]) |
| 73 | + minors = set() |
| 74 | + if len(majors) > 1: |
| 75 | + minors_to_show = OLD_MINORS |
| 76 | + micros_to_show = OLD_MICROS |
| 77 | + if vi[:2] not in minors: |
| 78 | + minors.add(vi[:2]) |
| 79 | + micros = set() |
| 80 | + if vi[:3] not in micros: |
| 81 | + micros.add(vi[:3]) |
| 82 | + |
| 83 | + show_it = ( |
| 84 | + len(majors) <= NUM_MAJORS |
| 85 | + and len(minors) <= minors_to_show |
| 86 | + and len(micros) <= micros_to_show |
| 87 | + ) |
| 88 | + active = ver["active"] or (len(majors) <= NUM_MAJORS) |
| 89 | + hidden = not show_it |
| 90 | + |
| 91 | + update = ver["active"] != active or ver["hidden"] != hidden |
| 92 | + if update: |
| 93 | + print(f"Updating {ver['verbose_name']} to {active=}, {hidden=}") |
| 94 | + url = ver["_links"]["_self"] |
| 95 | + resp = session.patch(url, data={"active": active, "hidden": hidden}) |
| 96 | + resp.raise_for_status() |
| 97 | + |
| 98 | + # Set the default version. |
| 99 | + latest = version_list[0][1] |
| 100 | + print(f"Setting default version to {latest['slug']}") |
| 101 | + url = latest["_links"]["project"] |
| 102 | + resp = session.patch(url, data={"default_version": latest["slug"]}) |
| 103 | + resp.raise_for_status() |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + main(sys.argv[1]) |
0 commit comments