debian_pkgs/debian_compare_versions.py
2024-06-17 12:19:46 +02:00

44 lines
1.3 KiB
Python

import os
command_eq = "dpkg --compare-versions '{}' eq '{}'"
command_gt = "dpkg --compare-versions '{}' gt '{}'"
def debian_compare(a, b):
if os.system(command_eq.format(a, b)) == 0:
return 0
is_gt = os.system(command_gt.format(a, b))
if is_gt == 0:
return 1
return -1
def get_debian_version_triplet(version):
# returns epoch, upstream, debian-revision tuple
epoch, upstream, debian_revision = '', version, ''
if '-' in version:
debian_revision = version.split('-')[-1]
upstream = upstream.rsplit('-' + debian_revision)
if ':' in version:
epoch = version.split(':')[0]
upstream = upstream.lsplit(epoch + ':')
return epoch, upstream, debian_revision
def naive_version_compare(a, b, pr=False):
versions1 = [int(v) for v in a.split(".")]
versions2 = [int(v) for v in b.split(".")]
for i in range(max(len(versions1),len(versions2))):
v1 = versions1[i] if i < len(a) else 0
v2 = versions2[i] if i < len(b) else 0
if v1 > v2:
return 1
elif v1 < v2:
return -1
return 0
if __name__ == '__main__':
# assert debian_compare('3.8.6-1', '3.8.5') == 1
# assert debian_compare('3.8.5', '3.8.5') == 0
# assert debian_compare('3.8.5', '3.8.6-1') == -1
# print(get_debian_version_triplet('2.31.0+dfsg-1'))
print(naive_version_compare('3.8.6', '10.8.5'))