def analyze_state(pre, post): """ Analyze state of packages. :param rpm.hdr pre: Header before :param rpm.hdr post: Header after :return: State :rtype: utils.State """ if not pre and post: return State.installed if pre and not post: return State.removed if not pre and not post: return State.absent if pre["sha1header"] == post["sha1header"]: if pre.unload() == post.unload(): return State.unchanged else: return State.reinstalled ver_cmp = rpm.versionCompare(pre, post) if ver_cmp < 0: return State.updated if ver_cmp > 0: return State.downgraded # In theory, it will never happen as sha1header should be different assert ver_cmp == 0 # Most probably we just compare different packages assert False, "{!r} -> {!r}".format(hdr2nevra(pre), hdr2nevra(post))
def _compare_version(vv1, vv2): """ Compare versions of packages. The method compares versions (epoch, version, release, disttag) using the rpm module. :param vv1: version of first package :param vv2: version of second package :return: `0` if versions are identical `1` if the first version is larger `-1` if the first version is less """ v1 = rpm.hdr() v2 = rpm.hdr() v1[rpm.RPMTAG_EPOCH] = vv1[0] v2[rpm.RPMTAG_EPOCH] = vv2[0] v1[rpm.RPMTAG_VERSION] = vv1[1] v2[rpm.RPMTAG_VERSION] = vv2[1] if vv1[2]: v1[rpm.RPMTAG_RELEASE] = vv1[2] if vv2[2]: v2[rpm.RPMTAG_RELEASE] = vv2[2] # check disttag, if true, add it if vv1[3] != '' and vv2[3]: v1[rpm.RPMTAG_DISTTAG] = vv1[3] v2[rpm.RPMTAG_DISTTAG] = vv2[3] eq = rpm.versionCompare(v1, v2) return eq
def main(argv): if len(argv) < 2: sys.stderr.write("Usage: %s PACKAGE_NAME...\n" % (argv[0],)) return 1 ts = rpm.TransactionSet() ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES | rpm._RPMVSF_NODIGESTS) fresh_rpms = {} for f in argv[1:]: if not os.path.exists(f): printError(f, "file was not found") continue try: h = readRpmHeader(ts, f) except IOError as e: printError(f, "I/O error ({0}: {1}".format(e.errno, e.strerror)) continue except: printError(f, "Unexpected error") continue if h is None: continue name = h[rpm.RPMTAG_NAME] arch = h[rpm.RPMTAG_ARCH] if (name,arch not in fresh_rpms or rpm.versionCompare(h, fresh_rpms[name,arch]['header']) > 0): fresh_rpms[name,arch] = {'header': h, 'filename': f} for n, v in fresh_rpms.iteritems(): print v['filename'] return 0
def findpackageset(hdrlist, dbPath='/'): ts = rpm.TransactionSet(dbPath) ts.setVSFlags(~(rpm.RPMVSF_NORSA | rpm.RPMVSF_NODSA | rpm.RPMVSF_NOMD5)) pkgDict = {} # go through and figure out which packages in the header list are # actually applicable for our architecture pkgDict = {} for h in hdrlist: score1 = rpm.archscore(h[rpm.RPMTAG_ARCH]) if (score1): name = h[rpm.RPMTAG_NAME] if pkgDict.has_key(name): score2 = rpm.archscore(pkgDict[name][rpm.RPMTAG_ARCH]) if (score1 < score2): pkgDict[name] = h else: pkgDict[name] = h hdlist = pkgDict.values() pkgDict = {} # loop through packages and find ones which are a newer # version than what we have for pkg in hdlist: mi = ts.dbMatch('name', pkg[rpm.RPMTAG_NAME]) for h in mi: val = rpm.versionCompare(h, pkg) if (val > 0): # dEBUG("found older version of %(name)s" % h) pass elif (val < 0): # dEBUG("found newer version of %(name)s" % h) # check if we already have this package in our dictionary addNewPackageToUpgSet(pkgDict, pkg) else: # dEBUG("found same verison of %(name)s" % h) pass # handle obsoletes for pkg in hdlist: if pkg[rpm.RPMTAG_NAME] in pkgDict.keys(): # dEBUG("%(name)s is already selected" % pkg) continue if pkg[rpm.RPMTAG_OBSOLETENAME] is not None: for obs in pkg[rpm.RPMTAG_OBSOLETENAME]: mi = ts.dbMatch('name', obs) # FIXME: I should really iterate over all matches and verify # versioned obsoletes, but nothing in Red Hat Linux uses # them, so I'll optimize for h in mi: # dEBUG("adding %(name)s to the upgrade set for obsoletes" % pkg) addNewPackageToUpgSet(pkgDict, pkg) break return pkgDict.values()
def cmp_RpmData_by_version(a, b): """ Wrapper function which passes the RPM headers in two RpmData objects to the rpm.versionCompare function. Returns: 1 -- if the first RPM file is a newer version than the second. 0 -- if both RPM files provide the same version of the package. -1 -- if the first RPM file is an older version than the second. """ return rpm.versionCompare(a.header, b.header)
def __cmp__(self, other): if self.name != other.name: raise ValueError('Name mismatch %s != %s' % (self.name, other.name)) levr = "%d:%s-%s" % (self.epoch, self.version, self.release) if self.distepoch: levr += ":" + self.distepoch revr = "%d:%s-%s" % (other.epoch, other.version, other.release) if other.distepoch: revr += ":" + other.distepoch return rpm.versionCompare(levr, revr);
def compareVersion(ha, hb): """Compares RPM header information epoch, version and release. Args: ha (rpm.hdr): RPM header object "a" in the comparison. hb (rpm.hdr): RPM header object "b" in the comparison. Returns: 0 - If equal, -1 if ha < hb, +1 if ha > hb. """ return rpm.versionCompare(ha, hb)
def comparePackageForUpgrade(updDict, h, pkg): val = rpm.versionCompare(h, pkg) if val > 0: # dEBUG("found older version of %(name)s %(arch)s" % h) pass elif val < 0: # dEBUG("found newer version of %(name)s %(arch)s" % h) # check if we already have this package in our dictionary addNewPackageToUpgSet(updDict, pkg) else: # dEBUG("found same verison of %(name)s %(arch)s" % h) pass
def comparePackageForUpgrade(updDict, h, pkg): val = rpm.versionCompare(h, pkg) if (val > 0): # dEBUG("found older version of %(name)s %(arch)s" % h) pass elif (val < 0): # dEBUG("found newer version of %(name)s %(arch)s" % h) # check if we already have this package in our dictionary addNewPackageToUpgSet(updDict, pkg) else: # dEBUG("found same verison of %(name)s %(arch)s" % h) pass
def addNewPackageToUpgSet(pkgDict, pkg): """Check to see if there's already a pkg by the name of pkg already in our dictionary. If not, add this one. If there is, see if this one is 'newer' or has a 'better' arch.""" name = pkg[rpm.RPMTAG_NAME] arch = pkg[rpm.RPMTAG_ARCH] if not pkgDict.has_key((name, arch)): # nope pkgDict[(name, arch)] = pkg else: # first check version val = rpm.versionCompare(pkgDict[(name, arch)], pkg) if val < 0: # we're newer, add this one pkgDict[(name, arch)] = pkg
def _kexec_tools_version_supported(self): result = False min_version = self._get_min_kexec_tools_version() min_hdr = self._create_hdr(*min_version) if min_version is not None: pkgs = self.packager.queryPackages( patterns=(self._KEXEC_TOOLS_PKG,), ) for package in pkgs: cur_hdr = self._create_hdr( 0, package['version'], package['release'] ) if rpm.versionCompare(cur_hdr, min_hdr) >= 0: result = True break return result
def addNewPackageToUpgSet(pkgDict, pkg): """Check to see if there's already a pkg by the name of pkg already in our dictionary. If not, add this one. If there is, see if this one is 'newer' or has a 'better' arch.""" name = pkg[rpm.RPMTAG_NAME] if not pkgDict.has_key(name): # nope pkgDict[name] = pkg else: # first check version val = rpm.versionCompare(pkgDict[name], pkg) if val < 0: # we're newer, add this one pkgDict[name] = pkg elif val == 0: # same version, so check the architecture newscore = rpm.archscore(pkg[rpm.RPMTAG_ARCH]) oldscore = pkgDict[name][rpm.RPMTAG_ARCH] if newscore and newscore < oldscore: # if the score is less, we're "better" pkgDict[name] = pkg
def reboot_required(): """ Check if reboot required by comparing running kernel package's version with the newest installed kernel package's one. """ boot_image_path = kernel_cmdline().get('BOOT_IMAGE') if boot_image_path is None: return None if is_debian(): # For apt-based distros. import apt_pkg apt_pkg.init() kernel_pkg = get_kernel_deb_package(boot_image_path) if kernel_pkg is not None: match = DEBIAN_KERNEL_PKG_NAME_RE.match(kernel_pkg.name) if match: name_parts = match.groups( ) # E.g. ('linux-image-4.4.0-', '174', '-generic') latest_kernel_pkg = get_latest_same_kernel_deb( name_parts[0], name_parts[2]) return apt_pkg.version_compare( latest_kernel_pkg.installed.version, kernel_pkg.installed.version) > 0 elif is_amazon_linux2(): # For Amazon Linux 2. import rpm kernel_pkg = get_kernel_rpm_package(boot_image_path) if kernel_pkg is not None: ts = rpm.ts() # Find the newest kernel package. package_iterator = ts.dbMatch('name', 'kernel') if package_iterator.count() > 0: latest_kernel_pkg = sorted( [package_header for package_header in package_iterator], key=cmp_to_key(rpm.versionCompare), reverse=True)[0] return rpm.versionCompare(latest_kernel_pkg, kernel_pkg) > 0 return None
quiet = False if '-q' in args: args.remove('-q') quiet = True if len(args) < 2: usage() sys.exit(42) try: hdr1 = hdrfromstr(args[0]) hdr2 = hdrfromstr(args[1]) except ValueError, ve: print >> sys.stderr, ve sys.exit(42) ret = rpm.versionCompare(hdr1, hdr2) if not quiet: if ret < 0: result = "is older than" elif ret == 0: result = "is equal to" else: result = "is newer than" print "'%s' %s '%s'" % (hdrtostr(hdr1), result, hdrtostr(hdr2)) sys.exit(ret)
quiet = False if "-q" in args: args.remove("-q") quiet = True if len(args) < 2: usage() sys.exit(42) try: hdr1 = hdrfromstr(args[0]) hdr2 = hdrfromstr(args[1]) except ValueError, ve: print >> sys.stderr, ve sys.exit(42) ret = rpm.versionCompare(hdr1, hdr2) if not quiet: if ret < 0: result = "is older than" elif ret == 0: result = "is equal to" else: result = "is newer than" print "'%s' %s '%s'" % (hdrtostr(hdr1), result, hdrtostr(hdr2)) sys.exit(ret)
def __lt__(self, other): return rpm.versionCompare(self, other) < 0