如何比较 Debian 软件包版本?

发布于 2024-10-17 04:49:26 字数 239 浏览 0 评论 0原文

我查看了 python-apt 和 python-debian,它们似乎没有比较软件包版本的功能。我必须自己写吗,还是有什么我可以使用的?

理想情况下,它看起来像:

>>> v1 = apt.version("1:1.3.10-0.3")
>>> v2 = apt.version("1.3.4-1")
>>> v1 > v2
True

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟花易冷人易散 2024-10-24 04:49:26

也许是因为标题没有提到Python(尽管标签提到了),当谷歌问同样的问题但希望得到一个bash答案时,谷歌把我带到了这里。这似乎是:

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

要安装至少与 lenny-backports 版本一样新的 ruby​​gems 版本,并且在 lenny 和挤压安装上不会出现错误:

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 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.

尬尬 2024-10-24 04:49:26

您可以使用 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')        

感谢

version a > version b

Tshepang 在评论中指出
对于较新版本:apt.VersionCompare 现在是 apt_pkg.version_compare

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.

抹茶夏天i‖ 2024-10-24 04:49:26

python-debian 也可以做到这一点。它的使用方式与 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')

输出:

version a > version b

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
等待我真够勒 2024-10-24 04:49:26

正如您已经提到的 python-aptpython-debian,但现在已经是 2022 年了,Python 2.7 已终止,这是基于 Debian 的系统的 Python 3 代码,您已在其中安装了 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 将自动使用 python3-apt 中更高效的版本。但您也可以通过从 apt 导入 Version 来显式使用它:

from apt import Version

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文