#!/usr/libexec/platform-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()
|