-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathsdk-version-bump.py
More file actions
417 lines (330 loc) · 11.8 KB
/
sdk-version-bump.py
File metadata and controls
417 lines (330 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env python3
"""
SDK Version Bump Script
Checks PyPI for new releases of claude-agent-sdk and anthropic, and updates
the runner's pyproject.toml + uv.lock when newer versions are available.
Generates a changelog from GitHub releases for PR descriptions.
Used by .github/workflows/sdk-version-bump.yml (daily schedule).
Usage:
python scripts/sdk-version-bump.py [--check-only] [--package PKG]
Exit codes:
0 - Success (update applied or no update needed)
1 - Error
2 - Update available (--check-only mode)
"""
import argparse
import json
import re
import subprocess
import sys
import urllib.request
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
sys.path.insert(0, str(Path(__file__).resolve().parent))
from sdk_report import build_report, render_report_markdown
# Packages to track: (pypi_name, github_owner/repo, pyproject_key)
TRACKED_PACKAGES = [
{
"pypi_name": "claude-agent-sdk",
"github_repo": "anthropics/claude-agent-sdk-python",
"pyproject_key": "claude-agent-sdk",
},
{
"pypi_name": "anthropic",
"github_repo": "anthropics/anthropic-sdk-python",
"pyproject_key": "anthropic[vertex]",
},
]
PYPROJECT_PATH = Path("components/runners/ambient-runner/pyproject.toml")
@dataclass
class VersionInfo:
"""Version state for a tracked package."""
package_name: str
pypi_name: str
github_repo: str
current_version: str
latest_version: str
needs_update: bool
@dataclass
class ChangelogEntry:
"""A single release changelog entry."""
version: str
body: str
def fetch_pypi_latest(package_name: str) -> Optional[str]:
"""Fetch the latest version of a package from PyPI.
Args:
package_name: The PyPI package name.
Returns:
Latest version string, or None on failure.
"""
url = f"https://pypi.org/pypi/{package_name}/json"
try:
req = urllib.request.Request(url, headers={"Accept": "application/json"})
with urllib.request.urlopen(req, timeout=30) as resp:
data = json.loads(resp.read().decode())
return data["info"]["version"]
except Exception as e:
print(f" Warning: Failed to fetch PyPI info for {package_name}: {e}")
return None
def parse_current_version(pyproject_path: Path, dep_key: str) -> Optional[str]:
"""Parse the current minimum version constraint from pyproject.toml.
Looks for patterns like: "package>=1.2.3" or "package[extras]>=1.2.3"
Args:
pyproject_path: Path to pyproject.toml.
dep_key: Dependency key as it appears in pyproject.toml (e.g. "anthropic[vertex]").
Returns:
Current version string (without operator), or None if not found.
"""
content = pyproject_path.read_text()
# Escape brackets for regex
escaped_key = re.escape(dep_key)
pattern = rf'"{escaped_key}>=([\d.]+)"'
match = re.search(pattern, content)
if match:
return match.group(1)
return None
def update_pyproject_version(
pyproject_path: Path, dep_key: str, old_version: str, new_version: str
) -> bool:
"""Update a dependency version in pyproject.toml.
Args:
pyproject_path: Path to pyproject.toml.
dep_key: Dependency key (e.g. "claude-agent-sdk").
old_version: Current version string.
new_version: New version string.
Returns:
True if the file was modified.
"""
content = pyproject_path.read_text()
old_spec = f'"{dep_key}>={old_version}"'
new_spec = f'"{dep_key}>={new_version}"'
if old_spec not in content:
print(f" Error: Could not find {old_spec} in {pyproject_path}")
return False
content = content.replace(old_spec, new_spec)
pyproject_path.write_text(content)
return True
def regenerate_lockfile(runner_dir: Path) -> bool:
"""Run `uv lock` to regenerate the lockfile.
Args:
runner_dir: Path to the runner component directory.
Returns:
True if successful.
"""
print(" Running uv lock...")
result = subprocess.run(
["uv", "lock"],
cwd=runner_dir,
capture_output=True,
text=True,
timeout=120,
check=False,
)
if result.returncode != 0:
print(f" Error: uv lock failed:\n{result.stderr}")
return False
print(" Lock file regenerated successfully.")
return True
def _fetch_all_releases(github_repo: str) -> list[dict]:
"""Fetch all releases from GitHub, paginating if necessary."""
releases: list[dict] = []
page = 1
while True:
url = (
f"https://api.github.com/repos/{github_repo}"
f"/releases?per_page=100&page={page}"
)
try:
req = urllib.request.Request(
url,
headers={
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
)
with urllib.request.urlopen(req, timeout=30) as resp:
batch = json.loads(resp.read().decode())
except Exception as e:
print(
f" Warning: Failed to fetch GitHub releases "
f"for {github_repo} (page {page}): {e}"
)
break
if not batch:
break
releases.extend(batch)
if len(batch) < 100:
break
page += 1
return releases
def fetch_github_changelog(
github_repo: str, from_version: str, to_version: str
) -> list[ChangelogEntry]:
"""Fetch release notes from GitHub for versions between from and to (exclusive/inclusive).
Args:
github_repo: GitHub owner/repo string.
from_version: Current version (exclusive).
to_version: Target version (inclusive).
Returns:
List of ChangelogEntry objects, newest first.
"""
releases = _fetch_all_releases(github_repo)
entries = []
from_parts = _version_tuple(from_version)
to_parts = _version_tuple(to_version)
for release in releases:
tag = release.get("tag_name", "")
# Strip leading 'v'
version_str = tag.lstrip("v")
version_parts = _version_tuple(version_str)
# Include versions where: from < version <= to
if from_parts < version_parts <= to_parts:
body = release.get("body", "") or ""
entries.append(ChangelogEntry(version=version_str, body=body.strip()))
# Newest first
entries.sort(key=lambda e: _version_tuple(e.version), reverse=True)
return entries
def _version_tuple(version_str: str) -> tuple[int, ...]:
"""Parse a version string into a comparable tuple of ints.
Handles pre-release suffixes like '1.0.0rc1' by extracting leading digits.
"""
try:
parts = []
for segment in version_str.split("."):
match = re.match(r"(\d+)", segment)
parts.append(int(match.group(1)) if match else 0)
return tuple(parts) if parts else (0,)
except (ValueError, AttributeError):
return (0,)
def check_versions(repo_root: Path) -> list[VersionInfo]:
"""Check all tracked packages for available updates.
Args:
repo_root: Repository root path.
Returns:
List of VersionInfo for each tracked package.
"""
pyproject = repo_root / PYPROJECT_PATH
results = []
for pkg in TRACKED_PACKAGES:
print(f"Checking {pkg['pypi_name']}...")
current = parse_current_version(pyproject, pkg["pyproject_key"])
if current is None:
print(f" Warning: Could not find {pkg['pyproject_key']} in {pyproject}")
continue
latest = fetch_pypi_latest(pkg["pypi_name"])
if latest is None:
continue
needs_update = _version_tuple(latest) > _version_tuple(current)
status = "UPDATE AVAILABLE" if needs_update else "up to date"
print(f" Current: {current}, Latest: {latest} ({status})")
results.append(
VersionInfo(
package_name=pkg["pyproject_key"],
pypi_name=pkg["pypi_name"],
github_repo=pkg["github_repo"],
current_version=current,
latest_version=latest,
needs_update=needs_update,
)
)
return results
def apply_updates(repo_root: Path, versions: list[VersionInfo]) -> tuple[bool, str]:
"""Apply version updates to pyproject.toml and regenerate lockfile.
Args:
repo_root: Repository root path.
versions: Version info list (only those with needs_update=True are applied).
Returns:
Tuple of (success, pr_body_markdown).
"""
pyproject = repo_root / PYPROJECT_PATH
runner_dir = pyproject.parent
updates = [v for v in versions if v.needs_update]
if not updates:
return True, ""
# Update pyproject.toml for each package
for v in updates:
print(f"Updating {v.package_name}: {v.current_version} -> {v.latest_version}")
if not update_pyproject_version(
pyproject, v.package_name, v.current_version, v.latest_version
):
return False, ""
# Regenerate lock file
if not regenerate_lockfile(runner_dir):
return False, ""
# Build feature analysis reports
reports = []
for v in updates:
print(f"Fetching changelog for {v.pypi_name}...")
entries = fetch_github_changelog(
v.github_repo, v.current_version, v.latest_version
)
report = build_report(v.pypi_name, v.current_version, v.latest_version, entries)
reports.append(report)
pr_body = render_report_markdown(reports)
return True, pr_body
def build_pr_title(versions: list[VersionInfo]) -> str:
"""Build a concise PR title from the updated packages.
Args:
versions: Version info list (only those with needs_update=True).
Returns:
PR title string.
"""
updates = [v for v in versions if v.needs_update]
if not updates:
return "deps(runner): no updates"
parts = []
for v in updates:
parts.append(f"{v.pypi_name} {v.latest_version}")
return f"deps(runner): bump {', '.join(parts)}"
def main() -> int:
parser = argparse.ArgumentParser(description="SDK version bump tool")
parser.add_argument(
"--check-only",
action="store_true",
help="Only check for updates, don't apply them",
)
parser.add_argument(
"--package",
choices=["claude-agent-sdk", "anthropic", "all"],
default="all",
help="Which package to check (default: all)",
)
args = parser.parse_args()
# Determine repo root
script_dir = Path(__file__).resolve().parent
repo_root = script_dir.parent
print("=" * 60)
print("SDK Version Bump")
print("=" * 60)
print()
# Check versions
versions = check_versions(repo_root)
if args.package != "all":
versions = [v for v in versions if v.pypi_name == args.package]
updates_available = any(v.needs_update for v in versions)
if not updates_available:
print("\nAll tracked SDKs are up to date.")
return 0
if args.check_only:
print("\nUpdates available (--check-only mode, not applying).")
return 2
# Apply updates
print()
success, pr_body = apply_updates(repo_root, versions)
if not success:
print("\nFailed to apply updates.")
return 1
# Write outputs for GitHub Actions
pr_title = build_pr_title(versions)
# Write to files so the workflow can read them
output_dir = repo_root / ".sdk-bump-output"
output_dir.mkdir(exist_ok=True)
(output_dir / "pr-title.txt").write_text(pr_title)
(output_dir / "pr-body.md").write_text(pr_body)
print(f"\nPR title: {pr_title}")
print(f"PR body written to {output_dir / 'pr-body.md'}")
print("\nDone.")
return 0
if __name__ == "__main__":
sys.exit(main())