9
9
import os
10
10
import re
11
11
import sys
12
- import packaging .version as pep440_version
13
12
from pathlib import Path
14
13
from subprocess import CalledProcessError , check_call , check_output
15
14
from typing import Text , Set
16
15
17
16
import questionary
18
17
import toml
19
- from semantic_version import Version as BaseVersion
18
+ from pep440_version_utils import Version , is_valid_version
19
+
20
20
21
21
VERSION_FILE_PATH = "rasa/version.py"
22
22
28
28
29
29
PRERELEASE_FLAVORS = ("alpha" , "rc" )
30
30
31
- PRERELEASE_FLAVOR_CODES = {"alpha" : "a" , "rc" : "rc" }
32
-
33
- PRERELEASE_VERSION_PATTERN = re .compile (r"^(a|rc)([1-9]\d*)$" )
34
-
35
31
RELEASE_BRANCH_PATTERN = re .compile (r"^\d+\.\d+\.x$" )
36
32
37
33
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
-
48
34
def create_argument_parser () -> argparse .ArgumentParser :
49
35
"""Parse all the command line arguments for the release script."""
50
36
51
37
parser = argparse .ArgumentParser (description = "prepare the next library release" )
52
38
parser .add_argument (
53
39
"--next_version" ,
54
40
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'" ,
56
42
)
57
43
58
44
return parser
@@ -146,28 +132,36 @@ def ask_version() -> Text:
146
132
"""Allow the user to confirm the version number."""
147
133
148
134
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 )
150
136
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 ( ))
154
140
version = questionary .text (
155
141
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 } ')?" ,
158
144
validate = is_valid_version_number ,
159
145
).ask ()
160
146
161
147
if version in PRERELEASE_FLAVORS and not current_version .prerelease :
162
148
# at this stage it's hard to guess the kind of version bump the
163
149
# 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
+ ]
164
162
version = questionary .select (
165
163
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 ,
171
165
).ask ()
172
166
173
167
if version :
@@ -193,8 +187,8 @@ def get_rasa_sdk_version() -> Text:
193
187
def validate_code_is_release_ready (version : Version ) -> None :
194
188
"""Make sure the code base is valid (e.g. Rasa SDK is up to date)."""
195
189
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 )
198
192
rasa_version = (version .major , version .minor )
199
193
200
194
if sdk_version != rasa_version :
@@ -267,74 +261,20 @@ def ensure_clean_git() -> None:
267
261
sys .exit (1 )
268
262
269
263
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
-
326
264
def parse_next_version (version : Text ) -> Version :
327
265
"""Find the next version as a proper semantic version string."""
328
266
if version == "major" :
329
- return Version . coerce (get_current_version ()).next_major ()
267
+ return Version (get_current_version ()).next_major ()
330
268
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 )
338
278
else :
339
279
raise Exception (f"Invalid version number '{ cmdline_args .next_version } '." )
340
280
@@ -396,7 +336,7 @@ def main(args: argparse.Namespace) -> None:
396
336
generate_changelog (version )
397
337
398
338
# 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 ():
400
340
create_commit (version )
401
341
push_changes ()
402
342
0 commit comments