Skip to content

Commit eafd0fd

Browse files
authored
chore: add python bump scripts (#590)
1 parent 54bf1b8 commit eafd0fd

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

python/scripts/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Getting Started
2+
3+
From the `python` folder, run:
4+
5+
```
6+
./scripts/version.sh
7+
```
8+
9+
This will consume changelogs in each Python package, determine the new version, update the version in all relevant files, and produce updates to all relevant CHANGELOG.md files.
10+
11+
After running the script, you'll need to add and commit the changes:
12+
13+
```
14+
git add .
15+
git commit -m "chore: version packages"
16+
```
17+
18+
## Adding new packages
19+
20+
To add a new package, modify the `bump_configs` variable in `version.py`. The key should be the name of the top-level folder housing the package, and the value should be a dictionary containing the following keys:
21+
22+
- `files`: A list of dictionaries, each containing the following keys:
23+
- `path`: The path to the file to update.
24+
- `version_key`: The key to update in the file.

python/scripts/version.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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}")

python/scripts/version.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
python3 scripts/version.py
2+
3+
changed_packages=()
4+
5+
all_packages=(
6+
coinbase-agentkit
7+
create-onchain-agent
8+
framework-extensions/langchain
9+
framework-extensions/openai-agents-sdk
10+
)
11+
12+
echo ""
13+
14+
for package in "${all_packages[@]}"; do
15+
if git diff --quiet HEAD -- "$package/pyproject.toml"; then
16+
echo "No changes in $package/pyproject.toml"
17+
else
18+
echo "Changes detected in $package/pyproject.toml"
19+
changed_packages+=("$package")
20+
fi
21+
done
22+
23+
for package in "${changed_packages[@]}"; do
24+
cd $package
25+
poetry install
26+
poetry run -- towncrier build --yes
27+
cd - > /dev/null
28+
done

0 commit comments

Comments
 (0)