diff --git a/src/octoprint/plugins/backup/__init__.py b/src/octoprint/plugins/backup/__init__.py index 85453b664..9e204d223 100644 --- a/src/octoprint/plugins/backup/__init__.py +++ b/src/octoprint/plugins/backup/__init__.py @@ -1154,8 +1154,8 @@ class BackupPlugin( metadata_bytes = zip.read(metadata_zipinfo) metadata = json.loads(metadata_bytes) - backup_version = get_comparable_version(metadata["version"], base=True) - if backup_version > get_octoprint_version(base=True): + backup_version = get_comparable_version(metadata["version"], cut=1) + if backup_version > get_octoprint_version(cut=1): if callable(on_invalid_backup): on_invalid_backup( "Backup is from a newer version of OctoPrint and cannot be applied" diff --git a/src/octoprint/util/version.py b/src/octoprint/util/version.py index f14dec50f..19e46494f 100644 --- a/src/octoprint/util/version.py +++ b/src/octoprint/util/version.py @@ -18,9 +18,9 @@ def get_octoprint_version_string(): return __version__ -def get_octoprint_version(base=False): - octoprint_version_string = get_octoprint_version_string() - return get_comparable_version(octoprint_version_string, base=base) +def get_octoprint_version(cut=None, **kwargs): + octoprint_version_string = normalize_version(get_octoprint_version_string()) + return get_comparable_version(octoprint_version_string, cut=cut, **kwargs) def is_released_octoprint_version(version=None): @@ -129,11 +129,7 @@ def is_octoprint_compatible(*compatibility_entries, **kwargs): def get_python_version_string(): from platform import python_version - version_string = python_version() - - # Debian has the python version set to 2.7.15+ which is not PEP440 compliant (bug 914072) - if version_string.endswith("+"): - version_string = version_string[:-1] + version_string = normalize_version(python_version()) return version_string @@ -154,21 +150,27 @@ def is_python_compatible(compat, **kwargs): return python_version in s -def get_comparable_version(version_string, base=False): - if "-" in version_string: - version_string = version_string[: version_string.find("-")] +def get_comparable_version(version_string, cut=None, **kwargs): + """ + Args: + version_string: The version string for which to create a comparable version instance + cut: optional, how many version digits to remove (e.g., cut=1 will turn 1.2.3 into 1.2). + Defaults to ``None``, meaning no further action. Settings this to 0 will remove + anything up to the last digit, e.g. dev or rc information. - # Debian has the python version set to 2.7.15+ which is not PEP440 compliant (bug 914072) - if version_string.endswith("+"): - version_string = version_string[:-1] + Returns: + A comparable version + """ + if "base" in kwargs and kwargs.get("base", False) and cut is None: + cut = 0 + if cut is not None and (cut < 0 or not isinstance(cut, int)): + raise ValueError("level must be a positive integer") + + version_string = normalize_version(version_string) version = pkg_resources.parse_version(version_string) - # A leading v is common in github release tags and old setuptools doesn't remove it. - if version and isinstance(version, tuple) and version[0].lower() == "*v": - version = version[1:] - - if base: + if cut is not None: if isinstance(version, tuple): # old setuptools base_version = [] @@ -176,11 +178,17 @@ def get_comparable_version(version_string, base=False): if part.startswith("*"): break base_version.append(part) + if 0 < cut < len(base_version): + base_version = base_version[:-cut] base_version.append("*final") version = tuple(base_version) else: # new setuptools version = pkg_resources.parse_version(version.base_version) + if 0 < cut < len(version.release): + reduced = version.release[:-cut] + version = pkg_resources.parse_version(".".join(str(x) for x in reduced)) + return version @@ -193,3 +201,17 @@ def is_prerelease(version_string): else: # new setuptools return version.is_prerelease + + +def normalize_version(version): + if "-" in version: + version = version[: version.find("-")] + + # Debian has the python version set to 2.7.15+ which is not PEP440 compliant (bug 914072) + if version.endswith("+"): + version = version[:-1] + + if version[0].lower() == "v": + version = version[1:] + + return version.strip() diff --git a/tests/util/test_version.py b/tests/util/test_version.py new file mode 100644 index 000000000..59df747ea --- /dev/null +++ b/tests/util/test_version.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function, unicode_literals + +__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" +__copyright__ = "Copyright (C) 2021 The OctoPrint Project - Released under terms of the AGPLv3 License" + +import unittest + +import ddt +import pkg_resources + + +@ddt.ddt +class VersionUtilTest(unittest.TestCase): + @ddt.data( + ("1.6.0.dev303+g328853170.dirty", None, "1.6.0.dev303+g328853170.dirty"), + ("1.6.0.dev303+g328853170.dirty", 0, "1.6.0"), + ("1.6.0.dev303+g328853170.dirty", 1, "1.6"), + ("1.6.0", 0, "1.6.0"), + ("1.6.0", 23, "1.6.0"), + ("1.6.0", -1, ValueError), + ) + @ddt.unpack + def test_get_comparable_version(self, version, cut, expected): + from octoprint.util.version import get_comparable_version + + try: + actual = get_comparable_version(version, cut=cut) + except Exception as exc: + if isinstance(expected, type) and isinstance(exc, expected): + pass + else: + raise + else: + self.assertEqual(actual, pkg_resources.parse_version(expected)) + + def test_get_comparable_version_base(self): + from octoprint.util.version import get_comparable_version + + actual = get_comparable_version("1.6.0.dev303+g328853170.dirty", base=True) + self.assertEqual(actual, pkg_resources.parse_version("1.6.0")) + + @ddt.data( + ("1.6.0", "1.6.0"), + ("v1.6.0", "1.6.0"), + ("V1.6.0", "1.6.0"), + ("1.6.0+", "1.6.0"), + ("\t1.6.0 \r\n", "1.6.0"), + ("1.6.0-dev123", "1.6.0"), + ) + @ddt.unpack + def test_normalize_version(self, version, expected): + from octoprint.util.version import normalize_version + + actual = normalize_version(version) + + self.assertEqual(actual, expected)