From f6a79cdf8f1b37bf4809a6e9195e2cad178987a2 Mon Sep 17 00:00:00 2001 From: Till Maas Date: Mon, 27 Jul 2009 00:30:05 +0200 Subject: handly release candidates in upstream versions --- cnucnu/helper.py | 42 +++++++++++++++++++++++++- cnucnu/package_list.py | 8 ++--- cnucnu/tests/helper_test.py | 72 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 5 deletions(-) create mode 100755 cnucnu/tests/helper_test.py diff --git a/cnucnu/helper.py b/cnucnu/helper.py index ab81e34..5ba9d1c 100644 --- a/cnucnu/helper.py +++ b/cnucnu/helper.py @@ -28,12 +28,52 @@ def get_html(url): def rpm_cmp(v1, v2): import rpm - return rpm.labelCompare((None, v1, None), (None, v2, None)) + diff = rpm.labelCompare((None, v1, None), (None, v2, None)) + return diff def rpm_max(list): list.sort(cmp=rpm_cmp) return list[-1] +def cnucnu_cmp(v1, v2): + import rpm + + v1, rc1 = split_rc(v1) + v2, rc2 = split_rc(v2) + + diff = rpm_cmp(v1, v2) + # base versions are the same, check for rc-status + if diff == 0: + # both are rc, higher rc is newer + if rc1 and rc2: + return cmp(rc1, rc2) + # only first is rc, then second is newer + elif rc1: + return -1 + # only second is rc, then first is newer + elif rc2: + return 1 + # none is rc, both are the same + else: + return 0 + # base versions are different, ignore rc-status + else: + return diff + +def split_rc(version): + import re + RC = re.compile("([^-r]*)(-?rc([0-9]))?") + match = RC.match(version) + + v, ignore, rc = match.groups() + + return (v, rc) + +def cnucnu_max(list): + list.sort(cmp=cnucnu_cmp) + return list[-1] + + """ return a dict that only contains keys that are in key_list """ def filter_dict(d, key_list): diff --git a/cnucnu/package_list.py b/cnucnu/package_list.py index 7034aaf..afc1684 100644 --- a/cnucnu/package_list.py +++ b/cnucnu/package_list.py @@ -26,7 +26,7 @@ import sys import re import errors as cc_errors -from helper import rpm_cmp +from helper import cnucnu_cmp from config import Config class Package(object): @@ -109,8 +109,8 @@ class Package(object): @property def latest_upstream(self): if not self._latest_upstream: - from cnucnu.helper import rpm_max - self._latest_upstream = rpm_max(self.upstream_versions) + from cnucnu.helper import cnucnu_max + self._latest_upstream = cnucnu_max(self.upstream_versions) # invalidate _rpm_diff cache self._rpm_diff = None @@ -126,7 +126,7 @@ class Package(object): @property def rpm_diff(self): if not self._rpm_diff: - self._rpm_diff = rpm_cmp(self.repo_version, self.latest_upstream) + self._rpm_diff = cnucnu_cmp(self.repo_version, self.latest_upstream) return self._rpm_diff @property diff --git a/cnucnu/tests/helper_test.py b/cnucnu/tests/helper_test.py new file mode 100755 index 0000000..0c8b166 --- /dev/null +++ b/cnucnu/tests/helper_test.py @@ -0,0 +1,72 @@ +#!/usr/bin/python +# vim: fileencoding=utf8 foldmethod=marker +# {{{ License header: GPLv2+ +# This file is part of cnucnu. +# +# Cnucnu is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# Cnucnu is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cnucnu. If not, see . +# }}} + +import unittest + +import sys +sys.path.insert(0, '../..') + +from cnucnu.helper import cnucnu_cmp, cnucnu_max, split_rc + +class HelperTest(unittest.TestCase): + + def test_cnucnu_cmp_basic(self): + # equal + self.assertEqual(cnucnu_cmp("0", "0"), 0) + # first newer + self.assertEqual(cnucnu_cmp("1", "0"), 1) + # second newer + self.assertEqual(cnucnu_cmp("0", "1"), -1) + + def test_split_rc(self): + self.assertEqual(split_rc("4.0.0-rc1"), ("4.0.0", "1")) + self.assertEqual(split_rc("4.0.0"), ("4.0.0", None)) + self.assertEqual(split_rc("0"), ("0", None)) + self.assertEqual(split_rc("1"), ("1", None)) + + self.assertEqual(split_rc("4.0.0rc1"), ("4.0.0", "1")) + + def test_cnucnu_cmp_rc(self): + self.assertEqual(cnucnu_cmp("4.0.0", "4.0.0"), 0) + self.assertEqual(cnucnu_cmp("4.0.0", "4.0.0-rc1"), 1) + self.assertEqual(cnucnu_cmp("4.0.0", "3.9.9-rc1"), 1) + self.assertEqual(cnucnu_cmp("4.0.1-rc1", "4.0.0-rc1"), 1) + self.assertEqual(cnucnu_cmp("4.0.1-rc1", "4.0.0"), 1) + + self.assertEqual(cnucnu_cmp("4.0.1rc1", "4.0.0"), 1) + self.assertEqual(cnucnu_cmp("4.0.0", "4.0.0rc1"), 1) + + self.assertEqual(cnucnu_cmp("4.0.0-rc2", "4.0.0-rc1"), 1) + self.assertEqual(cnucnu_cmp("4.0.0-rc2", "4.0.0rc1"), 1) + self.assertEqual(cnucnu_cmp("4.0.0", "4.0.0-rc2"), 1) + + self.assertEqual(cnucnu_cmp("1.0.0", "1.0.0-rc1"), 1) + + def test_cnucnu_max(self): + versions = ["4.0.1", "4.0.0", "4.0.0-rc2", "4.0.0rc1"] + for i in range(0,len(versions) - 1): + self.assertEqual(cnucnu_max(versions[i:]), versions[i]) + + + + +if __name__ == "__main__": + suite = unittest.TestLoader().loadTestsFromTestCase(HelperTest) + unittest.TextTestRunner(verbosity=2).run(suite) + #unittest.main() -- cgit