[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Multiple 'dpkg --compare-versions' in one process


Daniel Macks <dmacks@netspace.org> wrote:

> I've got a script that needs to do several thousand comparisons of
> debian version-strings. Is there way for 'dpkg --compare-versions' (or
> a work-alike) to be spawned for an interactive session or a daemon,
> and handle multiple checks via pipe or socket communication?

You can use libapt-pkg for that. The attached script performs 10000
comparisons in half a second on my Athlon XP 1800+.

#! /usr/bin/env python

import sys, apt_pkg, random

def random_version():
    return "%u.%u-%u" % (random.randint(0, 15),
                         random.randint(0, 15),
                         random.randint(0, 15))

def main():
    for i in xrange(10000):
        ver1 = random_version()
        ver2 = random_version()

        res = apt_pkg.VersionCompare(ver1, ver2)
        if len(sys.argv) >= 2 and sys.argv[1] == "print":
            if res < 0:
                print "%s < %s" % (ver1, ver2)
            elif res == 0:
                print "%s = %s" % (ver1, ver2)
                print "%s > %s" % (ver1, ver2)


if __name__ == "__main__": main()

Reply to: