From 659355572ca990e4b6ef1809a66aa7a0f961c4ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sat, 30 Jan 2021 15:19:36 +0100 Subject: [PATCH] Add support for PEP 440 (#38) --- awesomeversion/match.py | 10 ++++++++++ awesomeversion/strategy.py | 1 + tests/test_compare.py | 2 ++ tests/test_version_scheme.py | 1 + 4 files changed, 14 insertions(+) diff --git a/awesomeversion/match.py b/awesomeversion/match.py index 147d233..7b3e844 100644 --- a/awesomeversion/match.py +++ b/awesomeversion/match.py @@ -7,6 +7,9 @@ RE_SEMVER = re.compile( 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-]+)*))?$" ) +RE_PEP440 = re.compile( + r"^([1-9][0-9]*!)?(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*))*((a|b|rc)(0|[1-9][0-9]*))?(\.post(0|[1-9][0-9]*))?(\.dev(0|[1-9][0-9]*))?$" +) RE_BUILDVER = re.compile(r"^\d+$") RE_SPECIAL_CONTAINER = re.compile(r"^(latest|dev|stable|beta)$") @@ -33,6 +36,11 @@ def is_semver(version: str) -> bool: return RE_SEMVER.match(version) +def is_pep440(version: str) -> bool: + """Return True if the version is PEP 440 compliant.""" + return RE_PEP440.match(version) + + def is_simple(version: str) -> bool: """Return True if the version is simple.""" return RE_SIMPLE.match(version) @@ -55,4 +63,6 @@ def version_strategy(version: str) -> AwesomeVersionStrategy: return AwesomeVersionStrategy.SPECIALCONTAINER if is_simple(version): return AwesomeVersionStrategy.SIMPLEVER + if is_pep440(version): + return AwesomeVersionStrategy.PEP440 return AwesomeVersionStrategy.UNKNOWN diff --git a/awesomeversion/strategy.py b/awesomeversion/strategy.py index b24d424..5aea13b 100644 --- a/awesomeversion/strategy.py +++ b/awesomeversion/strategy.py @@ -9,6 +9,7 @@ class AwesomeVersionStrategy(str, Enum): CALVER = "CalVer" SEMVER = "SemVer" SIMPLEVER = "SimpleVer" + PEP440 = "PEP 440" UNKNOWN = "unknown" SPECIALCONTAINER = "SpecialContainer" diff --git a/tests/test_compare.py b/tests/test_compare.py index fcc8055..166cee8 100644 --- a/tests/test_compare.py +++ b/tests/test_compare.py @@ -41,6 +41,8 @@ ("1.0.0-alpha0", "1.0.0-rc1"), ("1.0.0-alpha", "1.0.0-beta"), ("1.0.0", "1.0.0-beta"), + ("1.0.0b1", "1.0.0b0"), + ("1.0.0", "1.0.0b0"), ("2020", "2019"), ("2020", "2019"), ("2020", "2019"), diff --git a/tests/test_version_scheme.py b/tests/test_version_scheme.py index cf4f00f..8c60d35 100644 --- a/tests/test_version_scheme.py +++ b/tests/test_version_scheme.py @@ -9,6 +9,7 @@ "version,strategy,dev,beta,modifier,modifier_type", [ ("0.118.0", AwesomeVersionStrategy.SEMVER, False, False, None, None), + ("1.0.0b1", AwesomeVersionStrategy.PEP440, False, True, "b1", "b"), ("1.0.0-beta.1", AwesomeVersionStrategy.SEMVER, False, True, "beta.1", "beta"), ("v1.0.0-beta.1", AwesomeVersionStrategy.SEMVER, False, True, "beta.1", "beta"), ("2021.2.0.dev1", AwesomeVersionStrategy.CALVER, True, False, "dev1", "dev"),