[elbe-devel] [PATCH 1/2] contrib: add check to compare Debian with Python versions

Thomas Weißschuh thomas.weissschuh at linutronix.de
Wed Jul 3 13:31:33 CEST 2024


The release process of elbe contains both Debian and Python version
strings which have to be kept synchronized.
To make sure these don't diverge, add a script that can be used by the
automation to detect differences.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 contrib/check-deb-py-versions.py | 96 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)

diff --git a/contrib/check-deb-py-versions.py b/contrib/check-deb-py-versions.py
new file mode 100755
index 000000000000..d28b1b7104e4
--- /dev/null
+++ b/contrib/check-deb-py-versions.py
@@ -0,0 +1,96 @@
+#!/usr/bin/env python3
+
+"""
+Debian and Python have different ways to represent non-release versions.
+Compare two version strings respecting these differences.
+"""
+
+import re
+
+from debian.debian_support import Version as DebianVersion
+
+from packaging.version import Version as PythonVersion
+
+
+_deb_pre_re = re.compile(r'^(\w+)(\d+$)')
+_deb_pre_to_py_mapping = {
+    'alpha': 'a',
+    'beta': 'b',
+}
+
+
+def _dep_pre_to_py_pre(deb_pre):
+    if deb_pre is None:
+        return None
+
+    match = _deb_pre_re.match(deb_pre)
+    if match is None:
+        raise ValueError(f'Could not match "{deb_pre}"')
+
+    pre = match.group(1)
+    num = match.group(2)
+
+    return _deb_pre_to_py_mapping[pre], int(num)
+
+
+def compare_debian_with_python_version(deb_ver: DebianVersion, py_ver: PythonVersion) -> bool:
+    if str(deb_ver) == str(py_ver):
+        return True
+
+    if deb_ver.debian_revision is not None or deb_ver.debian_version is not None:
+        raise ValueError('Unsupported Debian version components')
+
+    if py_ver.post is not None or py_ver.local is not None:
+        raise ValueError('Unsupported Python version components')
+
+    if deb_ver.epoch is None:
+        deb_epoch = 0
+    else:
+        deb_epoch = deb_ver.epoch
+
+    if '~' in deb_ver.upstream_version:
+        deb_baseversion, deb_pre = deb_ver.upstream_version.split('~')
+    else:
+        deb_baseversion, deb_pre = deb_ver.upstream_version, None
+
+    if deb_epoch != py_ver.epoch:
+        return False
+
+    if deb_baseversion != py_ver.base_version:
+        return False
+
+    if _dep_pre_to_py_pre(deb_pre) != py_ver.pre:
+        return False
+
+    return True
+
+
+def test_compare_debian_with_python_version():
+    testcases = [
+        ('1.0.0',        '1.0.0',   True),
+        ('0.0.1',        '0.0.1',   True),
+        ('1.0.0',        '1.0.0a1', False),
+        ('1.0.0~alpha1', '1.0.0',   False),
+        ('1.0.0~alpha1', '1.0.0a1', True),
+        ('1.0.0~alpha1', '1.0.0a2', False),
+        ('1.0.0~alpha1', '1.0.0b1', False),
+    ]
+
+    for deb_ver, py_ver, result in testcases:
+        assert compare_debian_with_python_version(
+                DebianVersion(deb_ver),
+                PythonVersion(py_ver),
+        ) == result
+
+
+if __name__ == '__main__':
+    import argparse
+    import sys
+
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument('debian_version', type=DebianVersion, help='Debian version to compare')
+    parser.add_argument('python_version', type=PythonVersion, help='Python version to compare')
+
+    args = parser.parse_args()
+
+    sys.exit(not compare_debian_with_python_version(args.debian_version, args.python_version))

-- 
2.45.2



More information about the elbe-devel mailing list