Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare Debian package versions?

Tags:

python

debian

apt

I looked at python-apt and python-debian, and they don't seem to have functionality to compare package versions. Do I have to write my own, or is there something I can use?

Ideally, it would look something like:

>>> v1 = apt.version("1:1.3.10-0.3")
>>> v2 = apt.version("1.3.4-1")
>>> v1 > v2
True
like image 991
tshepang Avatar asked Feb 10 '11 13:02

tshepang


People also ask

How do I check my Debian package version?

By typing “lsb_release -a”, you can get information about your current Debian version as well as all other base versions in your distribution. By typing “lsb_release -d”, you can get an overview of all system information, including your Debian version.

What is Debian package?

A Debian "package", or a Debian archive file, contains the executable files, libraries, and documentation associated with a particular suite of program or set of related programs. Normally, a Debian archive file has a filename that ends in . deb .


4 Answers

Perhaps because the title doesn't mention Python (though the tags do), Google brought me here when asking the same question but hoping for a bash answer. That seems to be:

$ dpkg --compare-versions 11a lt 100a && echo true
true
$ dpkg --compare-versions 11a gt 100a && echo true
$ 

To install a version of rubygems that's at least as new as the version from lenny-backports in a way that gives no errors on lenny and squeeze installations:

sudo apt-get install rubygems &&
VERSION=`dpkg-query --show --showformat '${Version}' rubygems` &&
dpkg --compare-versions $VERSION lt 1.3.4-1~bpo50+1 &&
sudo apt-get install -t lenny-backports rubygems

Perhaps I should have asked how to do that in a separate question, in the hope of getting a less clunky answer.

like image 173
Martin Dorey Avatar answered Nov 03 '22 09:11

Martin Dorey


You could use apt_pkg.version_compare:

import apt_pkg
apt_pkg.init_system()

a = '1:1.3.10-0.3'
b = '1.3.4-1'
vc = apt_pkg.version_compare(a,b)
if vc > 0:
    print('version a > version b')
elif vc == 0:
    print('version a == version b')
elif vc < 0:
    print('version a < version b')        

yields

version a > version b

Thanks to Tshepang for noting in the comments that for newer versions: apt.VersionCompare is now apt_pkg.version_compare.

like image 28
unutbu Avatar answered Nov 03 '22 08:11

unutbu


python-debian can do this too. It's used in an almost identical way to python-apt:

from debian import debian_support 

a = '1:1.3.10-0.3'
b = '1.3.4-1'
vc = debian_support.version_compare(a,b)
if vc > 0:
    print('version a > version b')
elif vc == 0:
    print('version a == version b')
elif vc < 0:
    print('version a < version b')

ouput:

version a > version b
like image 35
Jeremy Davis Avatar answered Nov 03 '22 08:11

Jeremy Davis


As you already mentioned python-apt and python-debian, but it is 2022 by now and Python 2.7 is end-of-life, here's the Python 3 code for a Debian based system, where you have installed python3-debian:

from debian.debian_support import Version
v1 = Version("1:1.3.10-0.3")
v2 = Version("1.3.4-1")
print(v1 > v2)

python3-debian will automatically used the more efficient version from python3-apt if it is installed. But you also can use it explicitly by importing Version from apt:

from apt import Version
like image 2
pmhahn Avatar answered Nov 03 '22 07:11

pmhahn