|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | + |
| 5 | +def detect_new_version_type(package_name: str) -> str | None: |
| 6 | + """ |
| 7 | + Detect based on towncrier entries whether package bump should be minor or patch. |
| 8 | + """ |
| 9 | + |
| 10 | + changelog_files = os.listdir(f"{package_name}/changelog.d") |
| 11 | + |
| 12 | + contains_feature = False |
| 13 | + contains_fix = False |
| 14 | + for file in changelog_files: |
| 15 | + if "feature" in file: |
| 16 | + contains_feature = True |
| 17 | + if "bugfix" in file: |
| 18 | + contains_fix = True |
| 19 | + |
| 20 | + if contains_feature: |
| 21 | + return "minor" |
| 22 | + if contains_fix: |
| 23 | + return "patch" |
| 24 | + return None |
| 25 | + |
| 26 | +def read_version(file_path: str, version_key: str) -> str: |
| 27 | + """ |
| 28 | + Read a file into memory and pull out the existing version number. |
| 29 | + """ |
| 30 | + |
| 31 | + with open(file_path, "r") as file: |
| 32 | + content = file.read() |
| 33 | + |
| 34 | + for line in content.split("\n"): |
| 35 | + match = re.match(rf'{version_key}\s*=\s*(.+?)(?:\{{|$)', line) |
| 36 | + |
| 37 | + if match: |
| 38 | + return match.group(1).strip().replace('"', '').replace("'", "") |
| 39 | + |
| 40 | +def determine_new_version(version_type: str, version: str) -> str: |
| 41 | + """ |
| 42 | + Return the bumped version number based on the version type and existing version. |
| 43 | + """ |
| 44 | + |
| 45 | + [major, minor, patch] = version.split(".") |
| 46 | + |
| 47 | + if (version_type == "minor"): |
| 48 | + return f"{major}.{int(minor) + 1}.0" |
| 49 | + elif (version_type == "patch"): |
| 50 | + return f"{major}.{minor}.{int(patch) + 1}" |
| 51 | + |
| 52 | +def write_version(file_path: str, version: str, version_key: str): |
| 53 | + """ |
| 54 | + Write the bumped version number to the file. |
| 55 | + """ |
| 56 | + |
| 57 | + with open(file_path, "r") as file: |
| 58 | + contents = file.read() |
| 59 | + |
| 60 | + pattern = rf'({version_key}\s*=\s*["\'])[~^>=]*[0-9]+\.[0-9]+\.[0-9]+(["\'])' |
| 61 | + updated_content = re.sub(pattern, rf'\g<1>{str(version)}\g<2>', contents) |
| 62 | + |
| 63 | + with open(file_path, "w") as file: |
| 64 | + file.write(updated_content) |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + bump_configs = { |
| 68 | + "coinbase-agentkit": { |
| 69 | + "files": [ |
| 70 | + { |
| 71 | + "path": "coinbase-agentkit/pyproject.toml", |
| 72 | + "version_key": "version" |
| 73 | + }, |
| 74 | + { |
| 75 | + "path": "coinbase-agentkit/docs/conf.py", |
| 76 | + "version_key": "release" |
| 77 | + }, |
| 78 | + { |
| 79 | + "path": "coinbase-agentkit/coinbase_agentkit/__version__.py", |
| 80 | + "version_key": "__version__" |
| 81 | + }, |
| 82 | + { |
| 83 | + "path": "create-onchain-agent/templates/beginner/pyproject.toml.jinja", |
| 84 | + "version_key": "coinbase-agentkit" |
| 85 | + }, |
| 86 | + { |
| 87 | + "path": "create-onchain-agent/templates/chatbot/pyproject.toml.jinja", |
| 88 | + "version_key": "coinbase-agentkit" |
| 89 | + }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + "create-onchain-agent": { |
| 93 | + "files": [ |
| 94 | + { |
| 95 | + "path": "create-onchain-agent/pyproject.toml", |
| 96 | + "version_key": "version" |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + "framework-extensions/langchain": { |
| 101 | + "files": [ |
| 102 | + { |
| 103 | + "path": "framework-extensions/langchain/pyproject.toml", |
| 104 | + "version_key": "version" |
| 105 | + }, |
| 106 | + { |
| 107 | + "path": "framework-extensions/langchain/docs/conf.py", |
| 108 | + "version_key": "release" |
| 109 | + }, |
| 110 | + { |
| 111 | + "path": "create-onchain-agent/templates/beginner/pyproject.toml.jinja", |
| 112 | + "version_key": "coinbase-agentkit-langchain" |
| 113 | + }, |
| 114 | + { |
| 115 | + "path": "create-onchain-agent/templates/chatbot/pyproject.toml.jinja", |
| 116 | + "version_key": "coinbase-agentkit-langchain" |
| 117 | + }, |
| 118 | + ], |
| 119 | + }, |
| 120 | + "framework-extensions/openai-agents-sdk": { |
| 121 | + "files": [ |
| 122 | + { |
| 123 | + "path": "framework-extensions/openai-agents-sdk/pyproject.toml", |
| 124 | + "version_key": "version" |
| 125 | + }, |
| 126 | + { |
| 127 | + "path": "create-onchain-agent/templates/beginner/pyproject.toml.jinja", |
| 128 | + "version_key": "coinbase-agentkit-openai-agents-sdk" |
| 129 | + }, |
| 130 | + { |
| 131 | + "path": "create-onchain-agent/templates/chatbot/pyproject.toml.jinja", |
| 132 | + "version_key": "coinbase-agentkit-openai-agents-sdk" |
| 133 | + }, |
| 134 | + ], |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + for package_name, config in bump_configs.items(): |
| 139 | + print(f"\nProcessing version bumps for {package_name}") |
| 140 | + |
| 141 | + version_type = detect_new_version_type(package_name) |
| 142 | + |
| 143 | + # no changes, skip |
| 144 | + if (version_type == None): |
| 145 | + print(f"{package_name} didn't change; skipping") |
| 146 | + continue |
| 147 | + |
| 148 | + for file in config["files"]: |
| 149 | + version = read_version(file["path"], file["version_key"]) |
| 150 | + new_version = determine_new_version(version_type, version) |
| 151 | + write_version(file["path"], new_version, file["version_key"]) |
| 152 | + print(f"Bumped version in {file['path']} from {version} to {new_version}") |
0 commit comments