Skip to content

Commit 46d4947

Browse files
committed
use pep440_version_utils and simplify release script
1 parent 08b0782 commit 46d4947

File tree

2 files changed

+36
-95
lines changed

2 files changed

+36
-95
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ sphinxcontrib-trio = "==1.1.1"
156156
sphinx-tabs = "~1.1.13"
157157
sphinx-autodoc-typehints = "==1.6.0"
158158
rasabaster = "^0.7.23"
159+
pep440-version-utils = "^0.2.0"
159160

160161
[tool.poetry.extras]
161162
spacy = [ "spacy",]

scripts/release.py

Lines changed: 35 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import os
1010
import re
1111
import sys
12-
import packaging.version as pep440_version
1312
from pathlib import Path
1413
from subprocess import CalledProcessError, check_call, check_output
1514
from typing import Text, Set
1615

1716
import questionary
1817
import toml
19-
from semantic_version import Version as BaseVersion
18+
from pep440_version_utils import Version, is_valid_version
19+
2020

2121
VERSION_FILE_PATH = "rasa/version.py"
2222

@@ -28,31 +28,17 @@
2828

2929
PRERELEASE_FLAVORS = ("alpha", "rc")
3030

31-
PRERELEASE_FLAVOR_CODES = {"alpha": "a", "rc": "rc"}
32-
33-
PRERELEASE_VERSION_PATTERN = re.compile(r"^(a|rc)([1-9]\d*)$")
34-
3531
RELEASE_BRANCH_PATTERN = re.compile(r"^\d+\.\d+\.x$")
3632

3733

38-
class Version(BaseVersion):
39-
"""
40-
A PEP440 compatible version that supports prereleases:
41-
https://www.python.org/dev/peps/pep-0440/#pre-releases
42-
"""
43-
44-
def __str__(self):
45-
return super().__str__().replace("-", "")
46-
47-
4834
def create_argument_parser() -> argparse.ArgumentParser:
4935
"""Parse all the command line arguments for the release script."""
5036

5137
parser = argparse.ArgumentParser(description="prepare the next library release")
5238
parser.add_argument(
5339
"--next_version",
5440
type=str,
55-
help="Either next version number or 'major', 'minor', 'patch', 'alpha', 'rc'",
41+
help="Either next version number or 'major', 'minor', 'micro', 'alpha', 'rc'",
5642
)
5743

5844
return parser
@@ -146,28 +132,36 @@ def ask_version() -> Text:
146132
"""Allow the user to confirm the version number."""
147133

148134
def is_valid_version_number(v: Text) -> bool:
149-
return v in {"major", "minor", "patch", "alpha", "rc"} or validate_version(v)
135+
return v in {"major", "minor", "micro", "alpha", "rc"} or is_valid_version(v)
150136

151-
current_version = Version.coerce(get_current_version())
152-
next_patch_version = str(current_version.next_patch())
153-
next_alpha_version = str(next_prerelease(current_version, "alpha"))
137+
current_version = Version(get_current_version())
138+
next_micro_version = str(current_version.next_micro())
139+
next_alpha_version = str(current_version.next_alpha())
154140
version = questionary.text(
155141
f"What is the version number you want to release "
156-
f"('major', 'minor', 'patch', 'alpha', 'rc' or valid version number "
157-
f"e.g. '{next_patch_version}' or '{next_alpha_version}')?",
142+
f"('major', 'minor', 'micro', 'alpha', 'rc' or valid version number "
143+
f"e.g. '{next_micro_version}' or '{next_alpha_version}')?",
158144
validate=is_valid_version_number,
159145
).ask()
160146

161147
if version in PRERELEASE_FLAVORS and not current_version.prerelease:
162148
# at this stage it's hard to guess the kind of version bump the
163149
# releaser wants, so we ask them
150+
if version == "alpha"
151+
choices = [
152+
str(current_version.next_alpha("minor")),
153+
str(current_version.next_alpha("micro")),
154+
str(current_version.next_alpha("major")),
155+
]
156+
else:
157+
choices = [
158+
str(current_version.next_release_candidate("minor")),
159+
str(current_version.next_release_candidate("micro")),
160+
str(current_version.next_release_candidate("major")),
161+
]
164162
version = questionary.select(
165163
f"Which {version} do you want to release?",
166-
choices=[
167-
str(next_prerelease(current_version.next_minor(), version)),
168-
str(next_prerelease(current_version.next_patch(), version)),
169-
str(next_prerelease(current_version.next_major(), version)),
170-
],
164+
choices=choices,
171165
).ask()
172166

173167
if version:
@@ -193,8 +187,8 @@ def get_rasa_sdk_version() -> Text:
193187
def validate_code_is_release_ready(version: Version) -> None:
194188
"""Make sure the code base is valid (e.g. Rasa SDK is up to date)."""
195189

196-
sdk = get_rasa_sdk_version()
197-
sdk_version = (Version.coerce(sdk).major, Version.coerce(sdk).minor)
190+
sdk = Version(get_rasa_sdk_version())
191+
sdk_version = (sdk.major, sdk.minor)
198192
rasa_version = (version.major, version.minor)
199193

200194
if sdk_version != rasa_version:
@@ -267,74 +261,20 @@ def ensure_clean_git() -> None:
267261
sys.exit(1)
268262

269263

270-
def validate_version(version: Text) -> bool:
271-
"""
272-
Ensure that the version follows semver
273-
and that the prerelease follows the format `a1`, `rc2`, etc...
274-
"""
275-
if isinstance(pep440_version.parse(version), pep440_version.LegacyVersion):
276-
return False
277-
278-
version_object = Version.coerce(version)
279-
return not version_object.prerelease or is_prerelease_version(version_object)
280-
281-
282-
def is_prerelease_version(version: Version) -> bool:
283-
"""
284-
Validate that the prerelease part in a version follows
285-
the pattern specified in `PRERELEASE_VERSION_PATTERN`.
286-
"""
287-
return (
288-
len(version.prerelease) == 1
289-
and PRERELEASE_VERSION_PATTERN.match(version.prerelease[0]) is not None
290-
)
291-
292-
293-
def is_alpha_version(version: Version) -> bool:
294-
"""
295-
Validate that the alpha part in a version follows
296-
the pattern specified in `PRERELEASE_VERSION_PATTERN`
297-
and is an alpha (as opposed to a release candidate).
298-
"""
299-
if len(version.prerelease) != 1:
300-
return False
301-
302-
version_match = PRERELEASE_VERSION_PATTERN.match(version.prerelease[0])
303-
if version_match is None:
304-
return False
305-
306-
return version_match.group(1) == "a"
307-
308-
309-
def next_prerelease(version: Version, flavor: Text) -> Version:
310-
"""Bump the current version to the next prerelease."""
311-
prerelease_number = 0
312-
if version.prerelease:
313-
prerelease_number = int(
314-
PRERELEASE_VERSION_PATTERN.match(version.prerelease[0]).group(2)
315-
)
316-
317-
return Version(
318-
major=version.major,
319-
minor=version.minor,
320-
patch=version.patch,
321-
prerelease=(f"{PRERELEASE_FLAVOR_CODES[flavor]}{prerelease_number + 1}",),
322-
partial=version.partial,
323-
)
324-
325-
326264
def parse_next_version(version: Text) -> Version:
327265
"""Find the next version as a proper semantic version string."""
328266
if version == "major":
329-
return Version.coerce(get_current_version()).next_major()
267+
return Version(get_current_version()).next_major()
330268
elif version == "minor":
331-
return Version.coerce(get_current_version()).next_minor()
332-
elif version == "patch":
333-
return Version.coerce(get_current_version()).next_patch()
334-
elif version in PRERELEASE_FLAVORS:
335-
return next_prerelease(Version.coerce(get_current_version()), version)
336-
elif validate_version(version):
337-
return Version.coerce(version)
269+
return Version(get_current_version()).next_minor()
270+
elif version == "micro":
271+
return Version(get_current_version()).next_micro()
272+
elif version == "alpha":
273+
return Version(get_current_version()).next_alpha()
274+
elif version == "rc":
275+
return Version(get_current_version()).next_release_candidate()
276+
elif is_valid_version(version):
277+
return Version(version)
338278
else:
339279
raise Exception(f"Invalid version number '{cmdline_args.next_version}'.")
340280

@@ -396,7 +336,7 @@ def main(args: argparse.Namespace) -> None:
396336
generate_changelog(version)
397337

398338
# alpha workflow on feature branch when a version bump is required
399-
if is_alpha_version(version) and not git_current_branch_is_master_or_release():
339+
if version.is_alpha and not git_current_branch_is_master_or_release():
400340
create_commit(version)
401341
push_changes()
402342

0 commit comments

Comments
 (0)