Skip to content

Instantly share code, notes, and snippets.

@amitsaha
Created January 30, 2014 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitsaha/8701831 to your computer and use it in GitHub Desktop.
Save amitsaha/8701831 to your computer and use it in GitHub Desktop.
Check two RPMs and print which is the newer of the two.
"""
Usage:
rpm_vercmp.py <rpmfile1> <rpmfile2>
Result:
RPM which is newer of the two or
'Same version'
"""
import sys
import rpm
def get_header(rpm_file):
"""Returns rpm information by querying a rpm"""
ts = rpm.ts()
with open(rpm_file) as f:
try:
hdr = ts.hdrFromFdno(f.fileno())
except rpm.error:
ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES)
f.seek(0)
hdr = ts.hdrFromFdno(f.fileno())
return hdr
hdr1 = get_header(sys.argv[1])
hdr2 = get_header(sys.argv[2])
cmp_result = rpm.versionCompare(hdr1, hdr2)
#http://rpm.org/api/4.7.0/rpmlib_8h.html
# 1 if hdr1 is newer
# -1 if hdr2 is newer
# 0 if same
if cmp_result == -1:
print(sys.argv[2])
elif cmp_result == 1:
print(sys.argv[1])
elif cmp_result == 0:
print('Same version')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment