|
25 | 25 | import urllib.request
|
26 | 26 | from os import path
|
27 | 27 | from tempfile import TemporaryDirectory
|
28 |
| -from typing import List, Optional, Tuple |
| 28 | +from typing import List, Optional |
29 | 29 |
|
30 | 30 | import attr
|
31 | 31 | import click
|
32 | 32 | import commonmark
|
33 | 33 | import git
|
34 |
| -import redbaron |
| 34 | + |
35 | 35 | from click.exceptions import ClickException
|
36 | 36 | from github import Github
|
37 | 37 | from packaging import version
|
@@ -100,7 +100,7 @@ def prepare():
|
100 | 100 | repo.remote().fetch()
|
101 | 101 |
|
102 | 102 | # Get the current version and AST from root Synapse module.
|
103 |
| - current_version, parsed_synapse_ast, version_node = parse_version_from_module() |
| 103 | + current_version = get_package_version() |
104 | 104 |
|
105 | 105 | # Figure out what sort of release we're doing and calcuate the new version.
|
106 | 106 | rc = click.confirm("RC", default=True)
|
@@ -162,7 +162,7 @@ def prepare():
|
162 | 162 | click.get_current_context().abort()
|
163 | 163 |
|
164 | 164 | # Switch to the release branch.
|
165 |
| - parsed_new_version = version.parse(new_version) |
| 165 | + parsed_new_version: version.Version = version.parse(new_version) |
166 | 166 |
|
167 | 167 | # We assume for debian changelogs that we only do RCs or full releases.
|
168 | 168 | assert not parsed_new_version.is_devrelease
|
@@ -207,17 +207,15 @@ def prepare():
|
207 | 207 | # Create the new release branch
|
208 | 208 | release_branch = repo.create_head(release_branch_name, commit=base_branch)
|
209 | 209 |
|
210 |
| - # Switch to the release branch and ensure its up to date. |
| 210 | + # Switch to the release branch and ensure it's up to date. |
211 | 211 | repo.git.checkout(release_branch_name)
|
212 | 212 | update_branch(repo)
|
213 | 213 |
|
214 |
| - # Update the `__version__` variable and write it back to the file. |
215 |
| - version_node.value = '"' + new_version + '"' |
216 |
| - with open("synapse/__init__.py", "w") as f: |
217 |
| - f.write(parsed_synapse_ast.dumps()) |
| 214 | + # Update the version specified in pyproject.toml. |
| 215 | + subprocess.check_output(["poetry", "version", new_version]) |
218 | 216 |
|
219 | 217 | # Generate changelogs.
|
220 |
| - generate_and_write_changelog(current_version) |
| 218 | + generate_and_write_changelog(current_version, new_version) |
221 | 219 |
|
222 | 220 | # Generate debian changelogs
|
223 | 221 | if parsed_new_version.pre is not None:
|
@@ -284,7 +282,7 @@ def tag(gh_token: Optional[str]):
|
284 | 282 | repo.remote().fetch()
|
285 | 283 |
|
286 | 284 | # Find out the version and tag name.
|
287 |
| - current_version, _, _ = parse_version_from_module() |
| 285 | + current_version = get_package_version() |
288 | 286 | tag_name = f"v{current_version}"
|
289 | 287 |
|
290 | 288 | # Check we haven't released this version.
|
@@ -362,7 +360,7 @@ def publish(gh_token: str):
|
362 | 360 | if repo.is_dirty():
|
363 | 361 | raise click.ClickException("Uncommitted changes exist.")
|
364 | 362 |
|
365 |
| - current_version, _, _ = parse_version_from_module() |
| 363 | + current_version = get_package_version() |
366 | 364 | tag_name = f"v{current_version}"
|
367 | 365 |
|
368 | 366 | if not click.confirm(f"Publish {tag_name}?", default=True):
|
@@ -396,7 +394,7 @@ def publish(gh_token: str):
|
396 | 394 | def upload():
|
397 | 395 | """Upload release to pypi."""
|
398 | 396 |
|
399 |
| - current_version, _, _ = parse_version_from_module() |
| 397 | + current_version = get_package_version() |
400 | 398 | tag_name = f"v{current_version}"
|
401 | 399 |
|
402 | 400 | pypi_asset_names = [
|
@@ -424,7 +422,7 @@ def upload():
|
424 | 422 | def announce():
|
425 | 423 | """Generate markdown to announce the release."""
|
426 | 424 |
|
427 |
| - current_version, _, _ = parse_version_from_module() |
| 425 | + current_version = get_package_version() |
428 | 426 | tag_name = f"v{current_version}"
|
429 | 427 |
|
430 | 428 | click.echo(
|
@@ -455,37 +453,11 @@ def announce():
|
455 | 453 | )
|
456 | 454 |
|
457 | 455 |
|
458 |
| -def parse_version_from_module() -> Tuple[ |
459 |
| - version.Version, redbaron.RedBaron, redbaron.Node |
460 |
| -]: |
461 |
| - # Parse the AST and load the `__version__` node so that we can edit it |
462 |
| - # later. |
463 |
| - with open("synapse/__init__.py") as f: |
464 |
| - red = redbaron.RedBaron(f.read()) |
465 |
| - |
466 |
| - version_node = None |
467 |
| - for node in red: |
468 |
| - if node.type != "assignment": |
469 |
| - continue |
470 |
| - |
471 |
| - if node.target.type != "name": |
472 |
| - continue |
473 |
| - |
474 |
| - if node.target.value != "__version__": |
475 |
| - continue |
476 |
| - |
477 |
| - version_node = node |
478 |
| - break |
479 |
| - |
480 |
| - if not version_node: |
481 |
| - print("Failed to find '__version__' definition in synapse/__init__.py") |
482 |
| - sys.exit(1) |
483 |
| - |
484 |
| - # Parse the current version. |
485 |
| - current_version = version.parse(version_node.value.value.strip('"')) |
486 |
| - assert isinstance(current_version, version.Version) |
487 |
| - |
488 |
| - return current_version, red, version_node |
| 456 | +def get_package_version() -> version.Version: |
| 457 | + version_string = subprocess.check_output(["poetry", "version", "--short"]).decode( |
| 458 | + "utf-8" |
| 459 | + ) |
| 460 | + return version.Version(version_string) |
489 | 461 |
|
490 | 462 |
|
491 | 463 | def find_ref(repo: git.Repo, ref_name: str) -> Optional[git.HEAD]:
|
@@ -565,11 +537,13 @@ class VersionSection:
|
565 | 537 | return "\n".join(version_changelog)
|
566 | 538 |
|
567 | 539 |
|
568 |
| -def generate_and_write_changelog(current_version: version.Version): |
| 540 | +def generate_and_write_changelog(current_version: version.Version, new_version: str): |
569 | 541 | # We do this by getting a draft so that we can edit it before writing to the
|
570 | 542 | # changelog.
|
571 | 543 | result = run_until_successful(
|
572 |
| - "python3 -m towncrier --draft", shell=True, capture_output=True |
| 544 | + f"python3 -m towncrier build --draft --version {new_version}", |
| 545 | + shell=True, |
| 546 | + capture_output=True, |
573 | 547 | )
|
574 | 548 | new_changes = result.stdout.decode("utf-8")
|
575 | 549 | new_changes = new_changes.replace(
|
|
0 commit comments