Bump the version in CHANGELOG.md to new_version and date to new_date Returns True on success, False on failure
(new_version, new_date)
| 393 | |
| 394 | |
| 395 | def bump_version_changelog_md(new_version, new_date): |
| 396 | """ |
| 397 | Bump the version in CHANGELOG.md to new_version and date to new_date |
| 398 | Returns True on success, False on failure |
| 399 | """ |
| 400 | changelog_md_path = "CHANGELOG.md" |
| 401 | pattern = r"(^\s*##\s*Unreleased\s*$)" |
| 402 | new_header = f"\n\n## [{new_version}] - {new_date}\n" |
| 403 | |
| 404 | with open(changelog_md_path, "r") as f: |
| 405 | content = f.read() |
| 406 | |
| 407 | # Check if the header already exists, so that we don't add a double header |
| 408 | already_exists_pattern = rf"(^\s*##\s*\[ *{re.escape(new_version)} *\])" |
| 409 | if re.search(already_exists_pattern, content, flags=re.MULTILINE): |
| 410 | print( |
| 411 | f"Header for version {new_version} already exists", |
| 412 | f"in {changelog_md_path}.", |
| 413 | ) |
| 414 | return True |
| 415 | |
| 416 | new_content, n_subs = re.subn( |
| 417 | pattern, |
| 418 | rf"\g<1>{new_header}", |
| 419 | content, |
| 420 | count=1, # replace only the first match |
| 421 | flags=re.MULTILINE, |
| 422 | ) |
| 423 | if n_subs < 1: |
| 424 | print( |
| 425 | f"FAILED to update version in {changelog_md_path}.", |
| 426 | "Please update manually.", |
| 427 | ) |
| 428 | return False |
| 429 | |
| 430 | with open(changelog_md_path, "w") as f: |
| 431 | f.write(new_content) |
| 432 | print( |
| 433 | f"Added header in {changelog_md_path} with version {new_version}", |
| 434 | f"and release date {new_date}", |
| 435 | ) |
| 436 | return True |
| 437 | |
| 438 | |
| 439 | def bump_version(args): |
no test coverage detected