|
| 1 | +import pathlib |
| 2 | +import re |
| 3 | +import click |
| 4 | +from packaging.version import Version |
| 5 | + |
| 6 | +PACKAGES = { |
| 7 | + "livekit": "livekit-rtc/livekit/rtc/version.py", |
| 8 | + "livekit-api": "livekit-api/livekit/api/version.py", |
| 9 | + "livekit-protocol": "livekit-protocol/livekit/protocol/version.py", |
| 10 | +} |
| 11 | + |
| 12 | + |
| 13 | +def _esc(*codes: int) -> str: |
| 14 | + return "\033[" + ";".join(str(c) for c in codes) + "m" |
| 15 | + |
| 16 | + |
| 17 | +def read_version(f: pathlib.Path) -> str: |
| 18 | + text = f.read_text() |
| 19 | + m = re.search(r'__version__\s*=\s*[\'"]([^\'"]+)[\'"]', text) |
| 20 | + if not m: |
| 21 | + raise ValueError(f"could not find __version__ in {f}") |
| 22 | + return m.group(1) |
| 23 | + |
| 24 | + |
| 25 | +def write_new_version(f: pathlib.Path, new_version: str) -> None: |
| 26 | + text = f.read_text() |
| 27 | + new_text = re.sub( |
| 28 | + r'__version__\s*=\s*[\'"][^\'"]*[\'"]', |
| 29 | + f'__version__ = "{new_version}"', |
| 30 | + text, |
| 31 | + count=1, |
| 32 | + ) |
| 33 | + f.write_text(new_text) |
| 34 | + |
| 35 | + |
| 36 | +def bump_version(cur: str, bump_type: str) -> str: |
| 37 | + v = Version(cur) |
| 38 | + if bump_type == "release": |
| 39 | + return v.base_version |
| 40 | + if bump_type == "patch": |
| 41 | + return f"{v.major}.{v.minor}.{v.micro + 1}" |
| 42 | + if bump_type == "minor": |
| 43 | + return f"{v.major}.{v.minor + 1}.0" |
| 44 | + if bump_type == "major": |
| 45 | + return f"{v.major + 1}.0.0" |
| 46 | + raise ValueError(f"unknown bump type: {bump_type}") |
| 47 | + |
| 48 | + |
| 49 | +def bump_prerelease(cur: str, bump_type: str) -> str: |
| 50 | + v = Version(cur) |
| 51 | + base = v.base_version |
| 52 | + if bump_type == "rc": |
| 53 | + if v.pre and v.pre[0] == "rc": |
| 54 | + next_rc = v.pre[1] + 1 |
| 55 | + else: |
| 56 | + next_rc = 1 |
| 57 | + return f"{base}.rc{next_rc}" |
| 58 | + raise ValueError(f"unknown prerelease bump type: {bump_type}") |
| 59 | + |
| 60 | + |
| 61 | +def update_api_protocol_dependency(new_protocol_version: str) -> None: |
| 62 | + """Update livekit-api's dependency on livekit-protocol.""" |
| 63 | + pyproject = pathlib.Path("livekit-api/pyproject.toml") |
| 64 | + if not pyproject.exists(): |
| 65 | + return |
| 66 | + old_text = pyproject.read_text() |
| 67 | + new_text = re.sub( |
| 68 | + r'"livekit-protocol>=[\w.\-]+,', |
| 69 | + f'"livekit-protocol>={new_protocol_version},', |
| 70 | + old_text, |
| 71 | + ) |
| 72 | + if new_text != old_text: |
| 73 | + pyproject.write_text(new_text) |
| 74 | + print(f"Updated livekit-api dependency on livekit-protocol to >={new_protocol_version}") |
| 75 | + |
| 76 | + |
| 77 | +def do_bump(bump_type: str) -> None: |
| 78 | + new_versions = {} |
| 79 | + for pypi_name, version_path in PACKAGES.items(): |
| 80 | + vf = pathlib.Path(version_path) |
| 81 | + if vf.exists(): |
| 82 | + cur = read_version(vf) |
| 83 | + new = bump_version(cur, bump_type) |
| 84 | + print(f"{pypi_name}: {_esc(31)}{cur}{_esc(0)} -> {_esc(32)}{new}{_esc(0)}") |
| 85 | + write_new_version(vf, new) |
| 86 | + new_versions[pypi_name] = new |
| 87 | + |
| 88 | + if "livekit-protocol" in new_versions: |
| 89 | + update_api_protocol_dependency(new_versions["livekit-protocol"]) |
| 90 | + |
| 91 | + |
| 92 | +def do_prerelease(prerelease_type: str) -> None: |
| 93 | + new_versions = {} |
| 94 | + for pypi_name, version_path in PACKAGES.items(): |
| 95 | + vf = pathlib.Path(version_path) |
| 96 | + if vf.exists(): |
| 97 | + cur = read_version(vf) |
| 98 | + new = bump_prerelease(cur, prerelease_type) |
| 99 | + print(f"{pypi_name}: {_esc(31)}{cur}{_esc(0)} -> {_esc(32)}{new}{_esc(0)}") |
| 100 | + write_new_version(vf, new) |
| 101 | + new_versions[pypi_name] = new |
| 102 | + |
| 103 | + if "livekit-protocol" in new_versions: |
| 104 | + update_api_protocol_dependency(new_versions["livekit-protocol"]) |
| 105 | + |
| 106 | + |
| 107 | +@click.command("bump") |
| 108 | +@click.option( |
| 109 | + "--pre", |
| 110 | + type=click.Choice(["rc", "none"]), |
| 111 | + default="none", |
| 112 | + help="Pre-release type.", |
| 113 | +) |
| 114 | +@click.option( |
| 115 | + "--bump-type", |
| 116 | + type=click.Choice(["patch", "minor", "major", "release"]), |
| 117 | + default="patch", |
| 118 | + help="Type of version bump.", |
| 119 | +) |
| 120 | +def bump(pre: str, bump_type: str) -> None: |
| 121 | + if pre == "none": |
| 122 | + do_bump(bump_type) |
| 123 | + else: |
| 124 | + do_prerelease(pre) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + bump() |
0 commit comments