From 22b49fe4959168d4a080432be2be28eee42588ec Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Sun, 30 Jun 2019 16:11:43 +0200 Subject: [PATCH] Add pmb.parse.version.check_string() (!1796) Compare a version against a check string. This will be used in "pmbootstrap kconfig check", to only require certain options if the pkgver is in a specified range. --- pmb/parse/version.py | 39 +++++++++++++++++++++++++++++++++++++++ test/test_version.py | 14 ++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/pmb/parse/version.py b/pmb/parse/version.py index b3fe048c..4d030164 100644 --- a/pmb/parse/version.py +++ b/pmb/parse/version.py @@ -284,3 +284,42 @@ def compare(a_version, b_version, fuzzy=False): # The tokens are not the same, but previous checks revealed that it # is equal anyway (e.g. "1.0" == "1"). return 0 + + +""" +Convenience functions below are not modeled after apk's version.c. +""" + + +def check_string(a_version, rule): + """ + Compare a version against a check string. This is used in "pmbootstrap + kconfig check", to only require certain options if the pkgver is in a + specified range (#1795). + + :param a_version: "3.4.1" + :param rule: ">=1.0.0" + :returns: True if a_version matches rule, false otherwise. + """ + # Operators and the expected returns of compare(a,b) + operator_results = {">=": [1, 0], + "<": [-1]} + + # Find the operator + b_version = None + expected_results = None + for operator in operator_results: + if rule.startswith(operator): + b_version = rule[len(operator):] + expected_results = operator_results[operator] + break + + # No operator found + if not b_version: + raise RuntimeError("Could not find operator in '" + rule + "'. You" + " probably need to adjust check_string() in" + " pmb/parse/version.py.") + + # Compare + result = compare(a_version, b_version) + return result in expected_results diff --git a/test/test_version.py b/test/test_version.py index 6b465158..1914998e 100644 --- a/test/test_version.py +++ b/test/test_version.py @@ -72,3 +72,17 @@ def test_version(args): for error in errors: print(error) assert errors == [] + + +def test_version_check_string(): + func = pmb.parse.version.check_string + assert func("3.2.4", ">=0.0.0") is True + assert func("3.2.4", ">=3.2.4") is True + assert func("3.2.4", "<4.0.0") is True + + assert func("0.0.0", ">=0.0.1") is False + assert func("4.0.0", "<4.0.0") is False + assert func("4.0.1", "<4.0.0") is False + + assert func("5.2.0_rc3", "<5.2.0") is False + assert func("5.2.0_rc3", ">=5.2.0") is True