|
| 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 | +TAG_PREFIXES = { |
| 13 | + "livekit": "rtc", |
| 14 | + "livekit-api": "api", |
| 15 | + "livekit-protocol": "protocol", |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +def _esc(*codes: int) -> str: |
| 20 | + return "\033[" + ";".join(str(c) for c in codes) + "m" |
| 21 | + |
| 22 | + |
| 23 | +def read_version(f: pathlib.Path) -> str: |
| 24 | + text = f.read_text() |
| 25 | + m = re.search(r'__version__\s*=\s*[\'"]([^\'"]+)[\'"]', text) |
| 26 | + if not m: |
| 27 | + raise ValueError(f"could not find __version__ in {f}") |
| 28 | + return m.group(1) |
| 29 | + |
| 30 | + |
| 31 | +def write_new_version(f: pathlib.Path, new_version: str) -> None: |
| 32 | + text = f.read_text() |
| 33 | + new_text = re.sub( |
| 34 | + r'__version__\s*=\s*[\'"][^\'"]*[\'"]', |
| 35 | + f'__version__ = "{new_version}"', |
| 36 | + text, |
| 37 | + count=1, |
| 38 | + ) |
| 39 | + f.write_text(new_text) |
| 40 | + |
| 41 | + |
| 42 | +def bump_version(cur: str, bump_type: str) -> str: |
| 43 | + v = Version(cur) |
| 44 | + if bump_type == "release": |
| 45 | + return v.base_version |
| 46 | + if bump_type == "patch": |
| 47 | + return f"{v.major}.{v.minor}.{v.micro + 1}" |
| 48 | + if bump_type == "minor": |
| 49 | + return f"{v.major}.{v.minor + 1}.0" |
| 50 | + if bump_type == "major": |
| 51 | + return f"{v.major + 1}.0.0" |
| 52 | + raise ValueError(f"unknown bump type: {bump_type}") |
| 53 | + |
| 54 | + |
| 55 | +def bump_prerelease(cur: str, bump_type: str) -> str: |
| 56 | + v = Version(cur) |
| 57 | + base = v.base_version |
| 58 | + if bump_type == "rc": |
| 59 | + if v.pre and v.pre[0] == "rc": |
| 60 | + next_rc = v.pre[1] + 1 |
| 61 | + else: |
| 62 | + next_rc = 1 |
| 63 | + return f"{base}.rc{next_rc}" |
| 64 | + raise ValueError(f"unknown prerelease bump type: {bump_type}") |
| 65 | + |
| 66 | + |
| 67 | +def update_api_protocol_dependency(new_protocol_version: str) -> None: |
| 68 | + """Update livekit-api's dependency on livekit-protocol.""" |
| 69 | + pyproject = pathlib.Path("livekit-api/pyproject.toml") |
| 70 | + if not pyproject.exists(): |
| 71 | + return |
| 72 | + old_text = pyproject.read_text() |
| 73 | + new_text = re.sub( |
| 74 | + r'"livekit-protocol>=[\w.\-]+,', |
| 75 | + f'"livekit-protocol>={new_protocol_version},', |
| 76 | + old_text, |
| 77 | + ) |
| 78 | + if new_text != old_text: |
| 79 | + pyproject.write_text(new_text) |
| 80 | + print(f"Updated livekit-api dependency on livekit-protocol to >={new_protocol_version}") |
| 81 | + |
| 82 | + |
| 83 | +def do_bump(package: str, bump_type: str) -> None: |
| 84 | + version_path = PACKAGES[package] |
| 85 | + vf = pathlib.Path(version_path) |
| 86 | + cur = read_version(vf) |
| 87 | + new = bump_version(cur, bump_type) |
| 88 | + print(f"{package}: {_esc(31)}{cur}{_esc(0)} -> {_esc(32)}{new}{_esc(0)}") |
| 89 | + write_new_version(vf, new) |
| 90 | + |
| 91 | + if package == "livekit-protocol": |
| 92 | + update_api_protocol_dependency(new) |
| 93 | + |
| 94 | + |
| 95 | +def do_prerelease(package: str, prerelease_type: str) -> None: |
| 96 | + version_path = PACKAGES[package] |
| 97 | + vf = pathlib.Path(version_path) |
| 98 | + cur = read_version(vf) |
| 99 | + new = bump_prerelease(cur, prerelease_type) |
| 100 | + print(f"{package}: {_esc(31)}{cur}{_esc(0)} -> {_esc(32)}{new}{_esc(0)}") |
| 101 | + write_new_version(vf, new) |
| 102 | + |
| 103 | + if package == "livekit-protocol": |
| 104 | + update_api_protocol_dependency(new) |
| 105 | + |
| 106 | + |
| 107 | +@click.command() |
| 108 | +@click.option( |
| 109 | + "--package", |
| 110 | + type=click.Choice(list(PACKAGES.keys())), |
| 111 | + required=True, |
| 112 | + help="Package to bump.", |
| 113 | +) |
| 114 | +@click.option( |
| 115 | + "--pre", |
| 116 | + type=click.Choice(["rc", "none"]), |
| 117 | + default="none", |
| 118 | + help="Pre-release type.", |
| 119 | +) |
| 120 | +@click.option( |
| 121 | + "--bump-type", |
| 122 | + type=click.Choice(["patch", "minor", "major", "release"]), |
| 123 | + default="patch", |
| 124 | + help="Type of version bump.", |
| 125 | +) |
| 126 | +@click.option( |
| 127 | + "--print-version", |
| 128 | + is_flag=True, |
| 129 | + default=False, |
| 130 | + help="Print current version and tag prefix, don't bump.", |
| 131 | +) |
| 132 | +def bump(package: str, pre: str, bump_type: str, print_version: bool) -> None: |
| 133 | + if print_version: |
| 134 | + vf = pathlib.Path(PACKAGES[package]) |
| 135 | + version = read_version(vf) |
| 136 | + tag_prefix = TAG_PREFIXES[package] |
| 137 | + # Output as key=value for easy parsing |
| 138 | + print(f"version={version}") |
| 139 | + print(f"tag_prefix={tag_prefix}") |
| 140 | + return |
| 141 | + |
| 142 | + if pre == "none": |
| 143 | + do_bump(package, bump_type) |
| 144 | + else: |
| 145 | + do_prerelease(package, pre) |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + bump() |
0 commit comments