Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 730fcda

Browse files
author
David Robertson
committed
Update release script to be poetry-aware
Poetry now manages the project version in pyproject.toml.
1 parent 99ab454 commit 730fcda

File tree

1 file changed

+21
-47
lines changed

1 file changed

+21
-47
lines changed

scripts-dev/release.py

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
import urllib.request
2626
from os import path
2727
from tempfile import TemporaryDirectory
28-
from typing import List, Optional, Tuple
28+
from typing import List, Optional
2929

3030
import attr
3131
import click
3232
import commonmark
3333
import git
34-
import redbaron
34+
3535
from click.exceptions import ClickException
3636
from github import Github
3737
from packaging import version
@@ -100,7 +100,7 @@ def prepare():
100100
repo.remote().fetch()
101101

102102
# 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()
104104

105105
# Figure out what sort of release we're doing and calcuate the new version.
106106
rc = click.confirm("RC", default=True)
@@ -162,7 +162,7 @@ def prepare():
162162
click.get_current_context().abort()
163163

164164
# Switch to the release branch.
165-
parsed_new_version = version.parse(new_version)
165+
parsed_new_version: version.Version = version.parse(new_version)
166166

167167
# We assume for debian changelogs that we only do RCs or full releases.
168168
assert not parsed_new_version.is_devrelease
@@ -207,17 +207,15 @@ def prepare():
207207
# Create the new release branch
208208
release_branch = repo.create_head(release_branch_name, commit=base_branch)
209209

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.
211211
repo.git.checkout(release_branch_name)
212212
update_branch(repo)
213213

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])
218216

219217
# Generate changelogs.
220-
generate_and_write_changelog(current_version)
218+
generate_and_write_changelog(current_version, new_version)
221219

222220
# Generate debian changelogs
223221
if parsed_new_version.pre is not None:
@@ -284,7 +282,7 @@ def tag(gh_token: Optional[str]):
284282
repo.remote().fetch()
285283

286284
# Find out the version and tag name.
287-
current_version, _, _ = parse_version_from_module()
285+
current_version = get_package_version()
288286
tag_name = f"v{current_version}"
289287

290288
# Check we haven't released this version.
@@ -362,7 +360,7 @@ def publish(gh_token: str):
362360
if repo.is_dirty():
363361
raise click.ClickException("Uncommitted changes exist.")
364362

365-
current_version, _, _ = parse_version_from_module()
363+
current_version = get_package_version()
366364
tag_name = f"v{current_version}"
367365

368366
if not click.confirm(f"Publish {tag_name}?", default=True):
@@ -396,7 +394,7 @@ def publish(gh_token: str):
396394
def upload():
397395
"""Upload release to pypi."""
398396

399-
current_version, _, _ = parse_version_from_module()
397+
current_version = get_package_version()
400398
tag_name = f"v{current_version}"
401399

402400
pypi_asset_names = [
@@ -424,7 +422,7 @@ def upload():
424422
def announce():
425423
"""Generate markdown to announce the release."""
426424

427-
current_version, _, _ = parse_version_from_module()
425+
current_version = get_package_version()
428426
tag_name = f"v{current_version}"
429427

430428
click.echo(
@@ -455,37 +453,11 @@ def announce():
455453
)
456454

457455

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)
489461

490462

491463
def find_ref(repo: git.Repo, ref_name: str) -> Optional[git.HEAD]:
@@ -565,11 +537,13 @@ class VersionSection:
565537
return "\n".join(version_changelog)
566538

567539

568-
def generate_and_write_changelog(current_version: version.Version):
540+
def generate_and_write_changelog(current_version: version.Version, new_version: str):
569541
# We do this by getting a draft so that we can edit it before writing to the
570542
# changelog.
571543
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,
573547
)
574548
new_changes = result.stdout.decode("utf-8")
575549
new_changes = new_changes.replace(

0 commit comments

Comments
 (0)