commit 23896d40da89538c4133e9192e726a593f8e6859 Author: Christian Fromme kaner@strace.org Date: Tue Aug 30 16:12:42 2011 +0200
Make GetTor packaging a bit more intelligent - If we see more than one version of a package: - Throw out -alpha versions - Throw out -beta versions - Throw out -rc versions - Look up the latest version --- lib/gettor/packages.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++- lib/gettor/utils.py | 11 ++++++ 2 files changed, 102 insertions(+), 1 deletions(-)
diff --git a/lib/gettor/packages.py b/lib/gettor/packages.py index c3dbe65..9d42b55 100644 --- a/lib/gettor/packages.py +++ b/lib/gettor/packages.py @@ -12,6 +12,48 @@ import gettor.utils import re import glob
+# Stolen from Mike's TorCtl: +class RouterVersion: + """ Represents a Router's version. Overloads all comparison operators + to check for newer, older, or equivalent versions. """ + def __init__(self, version): + if version: + v = re.search("(\d+).(\d+).(\d+).(\d+)", version).groups() + self.version = int(v[0])*0x1000000 + int(v[1])*0x10000 + int(v[2])*0x100 + int(v[3]) + self.ver_string = version + else: + self.version = version + self.ver_string = "unknown" + + def __lt__(self, other): return self.version < other.version + def __gt__(self, other): return self.version > other.version + def __ge__(self, other): return self.version >= other.version + def __le__(self, other): return self.version <= other.version + def __eq__(self, other): return self.version == other.version + def __ne__(self, other): return self.version != other.version + def __str__(self): return self.ver_string + +class TBBVersion: + """ Represents a TBB's version. Overloads all comparison operators + to check for newer, older, or equivalent versions. """ + def __init__(self, version): + if version: + v = re.search("(\d+).(\d+).(\d+)", version).groups() + self.version = int(v[0])*0x10000 + int(v[1])*0x100 + int(v[2]) + self.ver_string = version + else: + self.version = version + self.ver_string = "unknown" + + def __lt__(self, other): return self.version < other.version + def __gt__(self, other): return self.version > other.version + def __ge__(self, other): return self.version >= other.version + def __le__(self, other): return self.version <= other.version + def __eq__(self, other): return self.version == other.version + def __ne__(self, other): return self.version != other.version + def __str__(self): return self.ver_string + + class Packages:
def __init__(self, config, silent=False): @@ -32,7 +74,55 @@ class Packages: fileName = os.path.join(self.distDir, regex) fileList = glob.glob(fileName) if len(fileList) != 1: - return "" + # Looks like we have more than one file to choose from. Great. + # Let's do some more or less intelligent tricks. + + # Remove all alphas + fileList = gettor.utils.removeFromListByRegex(fileList, "-alpha") + if len(fileList) == 1: + return fileList[0] + + # Remove all betas + fileList = gettor.utils.removeFromListByRegex(fileList, "-beta") + if len(fileList) == 1: + return fileList[0] + + # Remove all release candidates + fileList = gettor.utils.removeFromListByRegex(fileList, "-rc") + if len(fileList) == 1: + return fileList[0] + + # Still more than 1 file? Look at the version strings. + r = RouterVersion("0.0.0.0") + ret = None + for f in fileList: + try: + if RouterVersion(f) > r: + r = RouterVersion(f) + ret = f + except: + return "" + + if ret is not None: + logging.debug("Of the list %s, I return %s" % (fileList, ret)) + return ret + + # Still no result? Sort by TBB versions + r = TBBVersion("0.0.0") + ret = None + for f in fileList: + try: + if TBBVersion(f) > r: + r = TBBVersion(f) + ret = f + except: + return "" + + if ret is not None: + logging.debug("Of the list %s, I return %s" % (fileList, ret)) + return ret + + return ""
return fileList[0]
diff --git a/lib/gettor/utils.py b/lib/gettor/utils.py index f29ddf6..d7f0725 100644 --- a/lib/gettor/utils.py +++ b/lib/gettor/utils.py @@ -289,3 +289,14 @@ def getHash(string): """Return hash of given string """ return str(hashlib.sha1(string).hexdigest()) + +def removeFromListByRegex(l, string): + """Remove entries from a list that match a certain regular expression + """ + for f in l: + m = re.search(string, f) + if m: + l.remove(f) + + return l +