Skip to content

Commit

Permalink
Another try at semver validation
Browse files Browse the repository at this point in the history
  • Loading branch information
chipkent authored Jun 5, 2024
1 parent 1ceba02 commit b0414b2
Showing 1 changed file with 6 additions and 69 deletions.
75 changes: 6 additions & 69 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,81 +22,18 @@
raise Exception("ibapi version must be set via the IB_VERSION environment variable.")


# def is_semver(version):
# """
# Checks if a string is in valid semver format.

# Args:
# version: The string to validate.

# Returns:
# True if the string is in valid semver format, False otherwise.
# """

# # Split the version string into components
# components = version.split(".")

# # Check for basic structure (3 components)
# if len(components) != 3:
# return False

# # Validate each component as a non-negative integer
# try:
# for component in components:
# if int(component) < 0:
# return False
# except ValueError:
# return False

# # Check for optional pre-release and build identifiers
# if "-" in version:
# pre_release, build = version.split("-")
# # Pre-release can be alphanumeric with hyphens or numeric with dots
# if not (pre_release.isalnum() and all(c in "-.0123456789" for c in pre_release) or all(c.isdigit() for c in pre_release.split("."))):
# return False
# else:
# pre_release = None

# if "+" in version:
# if pre_release is None:
# return False # Plus sign requires pre-release identifier
# build = version.split("+")[-1]
# # Build can be alphanumeric with hyphens
# if not build.isalnum() and not all(c in "-" for c in build):
# return False
# else:
# build = None

# return True

_semver_regex = r"""
^
(?P<major>\d+)\.
(?P<minor>\d+)\.
(?P<patch>\d+)
(?:
-(?P<prerelease>
(?:[a-z][a-z0-9-]*)
|(?:[0-9]+(?:\.[0-9]+)*)
)
)?
(?:\+(?P<build>[a-z0-9]+(?:-[a-z0-9]+)*))?
$
"""


def is_semver(version):
def is_valid_semver(version):
"""
Checks if a string is in valid semver format.
Args:
version: The string to validate.
version: The string to validate.
Returns:
True if the string is in semver format, False otherwise.
True if the string is in valid semver format, False otherwise.
"""
match = re.match(_semver_regex, version, re.VERBOSE)
return match is not None
pattern = r'^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\+[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)?$'
return bool(re.match(pattern, version))


def version_assert_format(version: str) -> None:
Expand All @@ -111,7 +48,7 @@ def version_assert_format(version: str) -> None:
if not version:
raise ValueError("Version string is empty.")

if not is_semver(version):
if not is_valid_semver(version):
raise ValueError(f"Version string is not in semver format: {version}")


Expand Down

0 comments on commit b0414b2

Please sign in to comment.