autodist-vercmp: new script (taken from Fedora rpmdevtools) using python-rpm to compare two packages EVRs

autodist,autodist-upstream-updates: replace internal version check code with autodist-vercmp
This commit is contained in:
2025-09-19 09:49:32 +02:00
parent 52f4f93d16
commit 5c3bc7f12a
4 changed files with 143 additions and 88 deletions

View File

@@ -75,6 +75,7 @@ install-programs:
@$(INSTALL_SCRIPT) autodist-repository $(DESTDIR)$(bindir)/autodist-repository
@$(INSTALL_SCRIPT) autodist-tool $(DESTDIR)$(bindir)/autodist-tool
@$(INSTALL_SCRIPT) autodist-upstream-updates $(DESTDIR)$(bindir)/autodist-upstream-updates
@$(INSTALL_SCRIPT) autodist-vercmp $(DESTDIR)$(bindir)/autodist-vercmp
@$(INSTALL_SCRIPT) autoport $(DESTDIR)$(bindir)/autoport
@$(INSTALL_SCRIPT) autoport-chroot $(DESTDIR)$(bindir)/autoport-chroot
@$(INSTALL_SCRIPT) autoport-fix-environment $(DESTDIR)$(bindir)/autoport-fix-environment

View File

@@ -289,53 +289,20 @@ function tempfile() {
echo $tmpfile
}
function version_compare()
{
local A B
A=$1
B=$2
if [ ${A/[a-zA-Z_]*} -gt ${B/[a-zA-Z_]*} ]; then
return 1
elif [ ${A/[a-zA-Z_]*} -lt ${B/[a-zA-Z_]*} ]; then
return 2
else
if [[ "$A" > "$B" ]]; then
return 1
elif [[ "$A" < "$B" ]]; then
return 2
fi
fi
return 0
}
function version_find_bigger()
{
local VER1 VER2 FPOS CUTVER1 CUTVER2
VER1=$1
VER2=$2
FPOS=1
while true; do
CUTVER1=`echo $VER1. | cut -d. -f $FPOS`
CUTVER2=`echo $VER2. | cut -d. -f $FPOS`
if [ "$CUTVER1" -a ! "$CUTVER2" ]; then
return 1
elif [ "$CUTVER2" -a ! "$CUTVER1" ]; then
return 2
elif [ ! "$CUTVER1" -a "$CUTVER2" ]; then
return 0
else
version_compare $CUTVER1 $CUTVER2
case $? in
1) return 1 ;;
2) return 2 ;;
esac
fi
FPOS=`expr $FPOS + 1`
done
return 0
autodist-vercmp $1 $2
case $? in
0) return 0 ;;
11) return 1 ;;
12) return 2 ;;
esac
echo "!! Warning: autodist-vercmp('$1','$2') returned unexpected value: $?"
return -1
}
function tail_file() {

View File

@@ -63,54 +63,20 @@ get_job_vector() {
JOB_VARNAMES=(${JOB[1]//,/ })
}
function version_compare()
{
local A B
A=$1
B=$2
if [[ ${1} =~ ^[0-9]+$ && ${2} =~ ^[0-9]+$ ]]; then
if [ $((10#${A/[a-zA-Z_]*})) -gt $((10#${B/[a-zA-Z_]*})) ]; then
return 1
elif [ $((10#${A/[a-zA-Z_]*})) -lt $((10#${B/[a-zA-Z_]*})) ]; then
return 2
fi
else
if [[ ${1} =~ ^[0-9]+$ || "$A" > "$B" ]]; then
return 1
elif [[ "$A" < "$B" ]]; then
return 2
fi
fi
return 0
}
function version_find_bigger()
{
local VER1 VER2 FPOS CUTVER1 CUTVER2
VER1=$1
VER2=$2
FPOS=1
while true; do
CUTVER1=`echo $VER1. | cut -d. -f $FPOS`
CUTVER2=`echo $VER2. | cut -d. -f $FPOS`
if [ "$CUTVER1" -a ! "$CUTVER2" ]; then
return 1
elif [ "$CUTVER2" -a ! "$CUTVER1" ]; then
return 2
elif [ ! "$CUTVER1" -a "$CUTVER2" ]; then
return 0
else
version_compare $CUTVER1 $CUTVER2
case $? in
1) return 1 ;;
2) return 2 ;;
esac
fi
FPOS=`expr $FPOS + 1`
done
return 0
autodist-vercmp $1 $2
case $? in
0) return 0 ;;
11) return 1 ;;
12) return 2 ;;
esac
echo "!! Warning: autodist-vercmp('$1','$2') returned unexpected value: $?"
return -1
}
while [ "$1" ]; do

121
autodist-vercmp Executable file
View File

@@ -0,0 +1,121 @@
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
#
# rpmdev-vercmp -- compare rpm versions
#
# Copyright (c) Seth Vidal, Ville Skyttä
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import sys
import rpm
try:
input = raw_input
except NameError:
pass
def usage():
print("""
rpmdev-vercmp <epoch1> <ver1> <release1> <epoch2> <ver2> <release2>
rpmdev-vercmp <EVR1> <EVR2>
rpmdev-vercmp # with no arguments, prompt
Exit status is 0 if the EVR's are equal, 11 if EVR1 is newer, and 12 if EVR2
is newer. Other exit statuses indicate problems.
""")
def askforstuff(thingname):
thing = input('%8s: ' % thingname)
return thing
# from yum and rpmlint, with less internal assumptions, and returning
# empty strings instead of None for missing bits
def stringToEVR(verstring):
if verstring in (None, ''):
return ('', '', '')
i = verstring.find(':')
if i == -1:
epoch = ''
else:
epoch = verstring[:i]
i += 1
j = verstring.find('-', i)
if j == -1:
version = verstring[i:]
release = ''
else:
version = verstring[i:j]
release = verstring[j + 1:]
return (epoch, version, release)
def main():
if len(sys.argv) > 1 and \
sys.argv[1] in ('-h', '--help', '-help', '--usage'):
usage()
sys.exit(0)
elif len(sys.argv) == 1:
e1 = askforstuff('Epoch1')
v1 = askforstuff('Version1')
r1 = askforstuff('Release1')
e2 = askforstuff('Epoch2')
v2 = askforstuff('Version2')
r2 = askforstuff('Release2')
elif len(sys.argv) == 3:
(e1, v1, r1) = stringToEVR(sys.argv[1])
(e2, v2, r2) = stringToEVR(sys.argv[2])
elif len(sys.argv) == 7:
(e1, v1, r1, e2, v2, r2) = sys.argv[1:]
else:
usage()
sys.exit(1)
warned = False
for tag in 'e1', 'v1', 'r1', 'e2', 'v2', 'r2':
value = eval(tag) or ''
if '-' in value:
if tag.startswith('e'):
tag = 'epoch' + tag[1:]
elif tag.startswith('v'):
tag = 'version' + tag[1:]
elif tag.startswith('r'):
tag = 'release' + tag[1:]
sys.stderr.write('WARNING: hyphen in %s: %s\n' % (tag, value))
warned = True
if warned:
usage()
evr1 = '%s%s%s' % (e1 and e1 + ':' or '', v1 or '', r1 and '-' + r1 or '')
evr2 = '%s%s%s' % (e2 and e2 + ':' or '', v2 or '', r2 and '-' + r2 or '')
rc = rpm.labelCompare((e1 or None, v1 or None, r1 or None),
(e2 or None, v2 or None, r2 or None))
fmt = '%s == %s'
if rc > 0:
fmt = '%s > %s'
rc = 11
elif rc < 0:
fmt = '%s < %s'
rc = 12
#print(fmt % (evr1, evr2))
sys.exit(rc)
if __name__ == "__main__":
main()