#!/usr/bin/python # -*- 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 def usage(): print (""" rpmdev-vercmp rpmdev-vercmp 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 = raw_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) == 3: (e1, v1, r1) = stringToEVR(sys.argv[1]) (e2, v2, r2) = stringToEVR(sys.argv[2]) elif len(sys.argv) < 7: e1 = askforstuff('Epoch1') v1 = askforstuff('Version1') r1 = askforstuff('Release1') e2 = askforstuff('Epoch2') v2 = askforstuff('Version2') r2 = askforstuff('Release2') else: (e1, v1, r1, e2, v2, r2) = sys.argv[1:] 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()