From 813e93e702899fc9e9b8c9df5c45afaa3d2e64cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20A=C5=9F=C4=B1c=C4=B1?= Date: Sun, 7 Mar 2010 21:31:39 +0000 Subject: [PATCH] Add make_version function pisi.version.Version is one of most frequently used class in pisi (esp. when calculating dependencies). This new function is a fast alternative which returns a tuple representation of version strings. Since python is able to compare tuple and list objects, we do not have to write a special compare function. Here are some results: In [7]: %timeit -n100000 pisi.version.make_version("1.2.3.4.5") 100000 loops, best of 3: 8.12 us per loop In [8]: %timeit -n100000 pisi.version.Version("1.2.3.4.5") 100000 loops, best of 3: 60.6 us per loop In [9]: %timeit -n100000 pisi.version.make_version("1.2") < pisi.version.make_version("1.3") 100000 loops, best of 3: 10.1 us per loop In [10]: %timeit -n100000 pisi.version.Version("1.2") < pisi.version.Version("1.3") 100000 loops, best of 3: 85.4 us per loop --- pisi/version.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/pisi/version.py b/pisi/version.py index da1deae7..77cd3204 100644 --- a/pisi/version.py +++ b/pisi/version.py @@ -14,7 +14,10 @@ import re import string -import exceptions + +import gettext +__trans = gettext.translation('pisi', fallback=True) +_ = __trans.ugettext import pisi import pisi.util as util @@ -35,6 +38,47 @@ keywords = { "p" : 6, } +__keywords = ( + ("alpha", -5), + ("beta", -4), + ("pre", -3), + ("rc", -2), + ("m", -1), + ("p", 1), + ) + +class InvalidVersionError(pisi.Error): + pass + +def __make_version_item(v): + try: + return int(v), None + except ValueError: + return int(v[:-1]), v[-1] + +def make_version(version): + ver, sep, suffix = version.partition("_") + try: + if sep: + # "s" is a string greater than the greatest keyword "rc" + if "a" <= suffix <= "s": + for keyword, value in __keywords: + if suffix.startswith(keyword): + return map(__make_version_item, ver.split(".")), value, \ + map(__make_version_item, suffix[len(keyword):].split(".")) + else: + # Probably an invalid version string. Reset ver string + # to raise an exception in __make_version_item function. + ver = "" + else: + return map(__make_version_item, ver.split(".")), 0, \ + map(__make_version_item, suffix.split(".")) + + return map(__make_version_item, ver.split(".")), 0, [(0, None)] + + except ValueError: + raise InvalidVersionError(_("Invalid version string: '%s'") % version) + # helper functions def has_keyword(versionitem): if versionitem._keyword != "NOKEY":