Skip to content

Commit

Permalink
factorized validation of the release version name
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaim committed Nov 28, 2024
1 parent b618cb7 commit 28739a7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 58 deletions.
66 changes: 9 additions & 57 deletions releaser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import datetime
import re
from version_scheme import version_info

template = {"version": None, "changes": []}

Expand All @@ -14,65 +15,16 @@
}


def apply_changelog(name, version, changes):
if "-" in version:
raise ValueError(
"'{}' is not a valid version name : `-` is reserved for another usage in conda packages version names".format(
version
)
)

VALID_VERSION_PRERELEASE_TYPES = ("alpha", "beta")
version_fields = version.split(".")
version_fields_count = len(version_fields)
if version_fields_count < 3:
raise ValueError(
"'{}' is not a valid version name : valid version scheme contains 3 or more dots-separated fields, the pre-release name starting with the 4th field (valid examples: 1.2.3, 0.1.2.alpha3, 0.1.2.alpha.3)".format(
version
)
)

version_major = version_fields[0]
version_minor = version_fields[1]
version_patch = version_fields[2]
version_prerelease = ""
if version_fields_count > 3:
# we assume here that all the additional dot-separated values are part of the pre-release name
version_prerelease = ".".join(version_fields[3:])

version_errors = []

if not version_major.isdigit():
version_errors.append("'{}' is not a valid major version number".format(version_major))
if not version_minor.isdigit():
version_errors.append("'{}' is not a valid minor version number".format(version_minor))
if not version_patch.isdigit():
version_errors.append("'{}' is not a valid patch version number".format(version_patch))

if version_prerelease != "" and not version_prerelease.startswith(
VALID_VERSION_PRERELEASE_TYPES
):
version_errors.append(
"'{}' is not a valid pre-release name, pre-release names must start with either : {} ".format(
version_prerelease, VALID_VERSION_PRERELEASE_TYPES
)
)

if len(version_errors) > 0:
error_message = "'{}' is not a valid version name:".format(version)
for error in version_errors:
error_message += "\n - {}".format(error)
hint = "examples of valid versions: 1.2.3, 0.1.2, 1.2.3.alpha0, 1.2.3.beta1, 3.4.5.beta.2"
error_message += "\n{}".format(hint)
raise ValueError(error_message)
def apply_changelog(name, version_name, changes):
version = version_info(version_name)

def template_substitute(contents):
x = contents.replace("{{ version_major }}", version_major)
x = x.replace("{{ version_minor }}", version_minor)
x = x.replace("{{ version_patch }}", version_patch)
x = x.replace("{{ version_is_prerelease }}", "1" if version_prerelease else "0")
x = x.replace("{{ version_prerelease_name }}", version_prerelease)
x = x.replace("{{ version_name }}", version)
x = contents.replace("{{ version_major }}", version.major)
x = x.replace("{{ version_minor }}", version.minor)
x = x.replace("{{ version_patch }}", version.patch)
x = x.replace("{{ version_is_prerelease }}", "1" if version.pre_release else "0")
x = x.replace("{{ version_prerelease_name }}", version.pre_release)
x = x.replace("{{ version_name }}", version_name)
return x

if name in templates:
Expand Down
3 changes: 2 additions & 1 deletion update_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import json
import re
import subprocess
from version_scheme import version_info


def validate_date(date_str):
Expand Down Expand Up @@ -88,7 +89,7 @@ def main():
"Enter the starting date of commits to be included in the release in the format YYYY-MM-DD: "
)
validate_date(commits_starting_date)
release_version = input("Enter the version to be released: ")
release_version = version_info(input("Enter the version to be released: "))

# Get commits to include in the release
log_cmd = "git log --since=" + commits_starting_date
Expand Down
65 changes: 65 additions & 0 deletions version_scheme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class version_info:
major = ""
minor = ""
patch = ""
pre_release = ""
name = ""

def __init__(self, version: str):
if "-" in version:
raise ValueError(
"'{}' is not a valid version name : `-` is reserved for another usage in conda packages version names".format(
version
)
)

VALID_VERSION_PRERELEASE_TYPES = ("alpha", "beta")
version_fields = version.split(".")
version_fields_count = len(version_fields)
if version_fields_count < 3:
raise ValueError(
"'{}' is not a valid version name : valid version scheme contains 3 or more dots-separated fields, the pre-release name starting with the 4th field (valid examples: 1.2.3, 0.1.2.alpha3, 0.1.2.alpha.3)".format(
version
)
)

self.major = version_fields[0]
self.minor = version_fields[1]
self.patch = version_fields[2]
self.pre_release = ""
if version_fields_count > 3:
# we assume here that all the additional dot-separated values are part of the pre-release name
self.pre_release = ".".join(version_fields[3:])

version_errors = []

if not self.major.isdigit():
version_errors.append("'{}' is not a valid major version number".format(self.major))
if not self.minor.isdigit():
version_errors.append("'{}' is not a valid minor version number".format(self.minor))
if not self.patch.isdigit():
version_errors.append("'{}' is not a valid patch version number".format(self.patch))

if self.pre_release != "" and not self.pre_release.startswith(
VALID_VERSION_PRERELEASE_TYPES
):
version_errors.append(
"'{}' is not a valid pre-release name, pre-release names must start with either : {} ".format(
self.pre_release, VALID_VERSION_PRERELEASE_TYPES
)
)

if len(version_errors) > 0:
error_message = "'{}' is not a valid version name:".format(version)
for error in version_errors:
error_message += "\n - {}".format(error)
hint = (
"examples of valid versions: 1.2.3, 0.1.2, 1.2.3.alpha0, 1.2.3.beta1, 3.4.5.beta.2"
)
error_message += "\n{}".format(hint)
raise ValueError(error_message)

self.name = version

def __str__(self):
return self.name

0 comments on commit 28739a7

Please sign in to comment.