Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import typing as t |
| 2 | from enum import Enum |
| 3 | from itertools import chain |
| 4 | from pathlib import Path |
| 5 | |
| 6 | import click |
| 7 | import tomli |
| 8 | from semver import VersionInfo |
| 9 | |
| 10 | CWD = Path.cwd() |
| 11 | SKIPDIR = (".venv", "venv", ".nox", ".tox", "temp", "node_modules", "site-packages") |
| 12 | PYPROJECT = "pyproject.toml" |
| 13 | SETUP = "setup.py" |
| 14 | |
| 15 | BumpTypes = { |
| 16 | "M": "major", |
| 17 | "m": "minor", |
| 18 | "p": "patch", |
| 19 | "b": "post", |
| 20 | "c": "pre", |
| 21 | "s": "skip", |
| 22 | } |
| 23 | BumpTypesPrompt = " | ".join([f"{k}-{v.title()}" for k, v in BumpTypes.items()]) |
| 24 | |
| 25 | |
| 26 | class BumpType(Enum): |
| 27 | MAJOR = "major" |
| 28 | MINOR = "minor" |
| 29 | PATCH = "patch" |
| 30 | PRE = "pre" |
| 31 | POST = "post" |
| 32 | |
| 33 | |
| 34 | def _get_bumped_version(current: VersionInfo, bump_type: BumpType) -> VersionInfo: |
| 35 | if bump_type == BumpType.MAJOR: |
| 36 | return current.next_version("major") |
| 37 | |
| 38 | if bump_type == BumpType.MINOR: |
| 39 | return current.next_version("minor") |
| 40 | |
| 41 | if bump_type == BumpType.PATCH: |
| 42 | return current.next_version("patch") |
| 43 | |
| 44 | if bump_type == BumpType.PRE: |
| 45 | return current.next_version("prerelease") |
| 46 | |
| 47 | return current.bump_build(token="post") |
| 48 | |
| 49 | |
| 50 | def _skip(file: Path) -> bool: |
| 51 | return any(skipdir in file.parts for skipdir in SKIPDIR) |
| 52 | |
| 53 | |
| 54 | def _find_packages(): |
| 55 | files = [] |
| 56 | for file in chain(CWD.glob(f"**/{PYPROJECT}"), CWD.glob(f"**/{SETUP}")): |
| 57 | if _skip(file=file): |
| 58 | continue |
| 59 | files.append(file) |
| 60 | return files |
| 61 | |
| 62 | |
| 63 | def _get_version_update( |
| 64 | package: str, |
| 65 | version: str, |
| 66 | bump_type_selector: t.Optional[str] = None, |
| 67 | ) -> t.Optional[str]: |
| 68 | pre = None |
| 69 | if "-" in version: |
| 70 | version, pre = version.split("-", maxsplit=1) |
| 71 | |
| 72 | if "rc" in version: |
| 73 | version, marker, number = version.partition("rc") |
| 74 | pre = f"{marker}{number}" |
| 75 | |
| 76 | # TODO: Add support for post builds |
| 77 | current_version = VersionInfo.parse(version=version) |
| 78 | current_version._prerelease = pre |
| 79 | bump_type_selector = t.cast( |
| 80 | str, |
| 81 | bump_type_selector |
| 82 | or click.prompt(f"* Bump {package}@{version} -> {BumpTypesPrompt}"), |
| 83 | ) |
| 84 | if bump_type_selector == "s": |
| 85 | return None |
| 86 | |
| 87 | try: |
| 88 | _bump_type = BumpType(BumpTypes[bump_type_selector]) |
| 89 | except (ValueError, KeyError) as e: |
| 90 | raise click.ClickException(f"Invalid bump type: {e}") |
| 91 | |
| 92 | return str(_get_bumped_version(current=current_version, bump_type=_bump_type)) |
| 93 | |
| 94 | |
| 95 | def _bump_setup_py(file: Path, bump_type: t.Optional[str] = None): |
| 96 | # Load config |
| 97 | content = file.read_text(encoding="utf-8") |
| 98 | _, _, arguments = content.partition("setup(") |
| 99 | |
| 100 | package, version = None, None |
| 101 | for line in arguments.split("\n"): |
| 102 | if "=" not in line: |
| 103 | continue |
| 104 | |
| 105 | arg, val = line.strip().split("=", maxsplit=1) |
| 106 | val = val.replace('"', "").replace(",", "").strip() |
| 107 | if arg == "name": |
| 108 | package = val |
| 109 | |
| 110 | if arg == "version": |
| 111 | version = val |
| 112 | |
| 113 | # Skip the main setup.py |
| 114 | if package == "composio": |
| 115 | return |
| 116 | |
| 117 | if package is None or version is None: |
| 118 | raise ValueError(f"Package or version not found in {file}") |
| 119 | |
| 120 | # Bump version |
| 121 | next_version = _get_version_update( |
| 122 | package=package, |
| 123 | version=version, |
| 124 | bump_type_selector=bump_type, |
| 125 | ) |
| 126 | if next_version is None: |
| 127 | click.echo(f"* Skipping {package}") |
| 128 | return |
| 129 | |
| 130 | click.echo(f"* Bumping {package} {version} -> {next_version}") |
| 131 | content = content.replace(f'version="{version}"', f'version="{next_version}"') |
| 132 | file.write_text(content, encoding="utf-8") |
| 133 | |
| 134 | |
| 135 | def _bump_pyproject_toml(file: Path, bump_type: t.Optional[str] = None): |
| 136 | # Load config |
| 137 | content = file.read_text(encoding="utf-8") |
| 138 | config = tomli.loads(content) |
| 139 | project = t.cast(dict, config["project"]) |
| 140 | |
| 141 | # Extract meta |
| 142 | package = project["name"] |
| 143 | version = project["version"] |
| 144 | |
| 145 | # Bump version |
| 146 | next_version = _get_version_update( |
| 147 | package=package, |
| 148 | version=version, |
| 149 | bump_type_selector=bump_type, |
| 150 | ) |
| 151 | if next_version is None: |
| 152 | click.echo(f"* Skipping {package}") |
| 153 | return |
| 154 | |
| 155 | click.echo(f"* Bumping {package} {version} -> {next_version}") |
| 156 | content = content.replace(f'version = "{version}"', f'version = "{next_version}"') |
| 157 | file.write_text(content, encoding="utf-8") |
| 158 | |
| 159 | |
| 160 | def _bump_package(file: Path, bump_type: t.Optional[str] = None): |
| 161 | if file.name == PYPROJECT: |
| 162 | return _bump_pyproject_toml(file=file, bump_type=bump_type) |
| 163 | return _bump_setup_py(file=file, bump_type=bump_type) |
| 164 | |
| 165 | |
| 166 | @click.command("bump") |
| 167 | @click.option("--pre", "bump_type", flag_value="c") |
| 168 | @click.option("--post", "bump_type", flag_value="b") |
| 169 | @click.option("--patch", "bump_type", flag_value="p") |
| 170 | @click.option("--minor", "bump_type", flag_value="m") |
| 171 | @click.option("--major", "bump_type", flag_value="M") |
| 172 | def bump(bump_type: t.Optional[str]): |
| 173 | packages = _find_packages() |
| 174 | for package in packages: |
| 175 | try: |
| 176 | _bump_package(file=package, bump_type=bump_type) |
| 177 | except Exception as e: |
| 178 | raise e.__class__(f"Error bumping {package}") |
| 179 | |
| 180 | |
| 181 | if __name__ == "__main__": |
| 182 | bump() |