diff --git a/pisi/util.py b/pisi/util.py index 2c11afe5..7488a314 100644 --- a/pisi/util.py +++ b/pisi/util.py @@ -24,6 +24,7 @@ import sys import sha import shutil import statvfs +import operator import gettext __trans = gettext.translation('pisi', fallback=True) @@ -67,9 +68,15 @@ class Checks: print_errors = staticmethod(print_errors) ######################### -# string/list functions # +# string/list/functional# ######################### +def every(seq, pred): + return reduce(operator.and_, map(pred, seq)) + +def any(seq, pred): + return reduce(operator.or_, map(pred, seq)) + def unzip(seq): return zip(*seq) diff --git a/pisi/version.py b/pisi/version.py index 440c9e57..a0a70655 100644 --- a/pisi/version.py +++ b/pisi/version.py @@ -9,10 +9,17 @@ # # Please read the COPYING file. # - -# version structure - # Authors: Eray Ozkural +# Baris Metin +# +# History: +# Eray wrote the first version +# Baris added support for the fancy keywords and +# alphanumeric version string components + + +"""version structure""" + import re @@ -59,6 +66,9 @@ class VersionItem: # one! self._value = itemstring[len(self._keyword):] + def __str__(self): + return str(self._value) + def __lt__(self,rhs): l = keywords[self._keyword] r = keywords[rhs._keyword] @@ -130,39 +140,42 @@ class Version: def string(self): return self.verstring - def pred(self, rhs, pred): - - loop = len(self.comps) - if len(rhs.comps) > loop: - loop = len(rhs.comps) + def compare(self, rhs): + """this comparison routine is essentially a comparison routine + for two rationals in (0,1) interval. we compare two sequences + of digits one by one. We start with the leftmost digit + in the expansion. If they are equal, we proceed to the next. If + not we use the comparison operator. And we iterate to the left. + The result is, 0 if two are equal, -1 if self < rhs, and +1 + if self>rhs""" + lhs = self.comps + rhs = rhs.comps + # pad the short version string with zeros + if len(lhs) < len(rhs): + lhs.extend( [VersionItem('0')] * (len(rhs) - len(lhs)) ) + elif len(lhs) > len(rhs): + rhs.extend( [VersionItem('0')] * (len(lhs) - len(rhs)) ) + # now let's iterate from left to right + for (litem, ritem) in zip(lhs, rhs): + if litem < ritem: + return -1 + elif litem > ritem: + return +1 + return 0 - for i in range(0, loop): - try: - litem = self.comps[i] - except IndexError: - litem = VersionItem("") - - try: - ritem = rhs.comps[i] - except IndexError: - ritem = VersionItem("") - - if not pred(litem, ritem): - return False - - return True + # premature optimization is the root of all evil def __lt__(self,rhs): - return self.pred(rhs, lambda x,y: xy) + return self.compare(rhs) > 0 def __ge__(self,rhs): - return self.pred(rhs, lambda x,y: x>=y) + return self.compare(rhs) >= 0 def __eq__(self,rhs): - return self.pred(rhs, lambda x,y: x==y) + return self.compare(rhs) == 0 diff --git a/tests/run.py b/tests/run.py index 0ba5e13d..901215dc 100755 --- a/tests/run.py +++ b/tests/run.py @@ -37,6 +37,7 @@ def run_all(): import versiontests import configfiletests import packagetests + import dependency alltests = unittest.TestSuite(( utiltests.suite, @@ -54,7 +55,8 @@ def run_all(): graphtests.suite, versiontests.suite, configfiletests.suite, - packagetests.suite + packagetests.suite, + dependencytests.suite )) runTestSuite(alltests)