def _pkg_version_is_older(self, version1, version2): """ Use dpkg to compare the two version Return true if version1 < version2 """ # Avoid forking a new process if the two strings are equals if version1 == version2: return False status = apt_pkg.VersionCompare(version1, version2) return status < 0
def _get_latest_version(pkg): latest = None for ver in pkg.VersionList: if not latest: latest = ver elif apt_pkg.VersionCompare(ver.VerStr, latest.VerStr) > 0: latest = ver return latest
def getNeedVersion(args): """ in: (package,[>>,>,<<,<,==],version) out: package versions for each Debian suite, green color to satisfied version and red color unsatisfied version """ arg = args.split('|') package = arg[0] symbol = arg[1] my_version = arg[2] f = urllib.urlopen("http://packages.debian.org/%s" % package) html = f.read() all = re.compile("""<li><a href="/.*/.*">[a-z]+</a>.*\n<br>.*</li>""", re.DOTALL) all2 = re.compile("""<br>.*:""") parsed1 = re.sub('href="', 'href="http://packages.debian.org', all.findall(html)[0]) for version in uniq(all2.findall(parsed1)): version_tmp = version.replace("<br>", "") version_tmp2 = version_tmp.replace(":", "") parsed1 = parsed1.replace( version_tmp2, "<font color=\"red\">" + version_tmp2 + "</font>") if (cmp(symbol, "==") == 0 or cmp(symbol, "<<") == 0 or cmp(symbol, ">>") == 0) and apt_pkg.VersionCompare( my_version, version_tmp2) == 0: parsed1 = parsed1.replace( version_tmp2, "<font color=\"green\">" + version_tmp2 + "</font>") if (cmp(symbol, ">") == 0 or cmp(symbol, ">>") == 0) and apt_pkg.VersionCompare(my_version, version_tmp2) < 0: parsed1 = parsed1.replace( version_tmp2, "<font color=\"green\">" + version_tmp2 + "</font>") if (cmp(symbol, "<") == 0 or cmp(symbol, "<<") == 0) and apt_pkg.VersionCompare(my_version, version_tmp2) > 0: parsed1 = parsed1.replace( version_tmp2, "<font color=\"green\">" + version_tmp2 + "</font>") return parsed1
def matchPackageOrigin(self, pkg, matcher): """ match 'pkg' origin against 'matcher', take versions between installedVersion and candidateVersion into account too Useful if installed pkg A v1.0 is available in both -updates (as v1.2) and -security (v1.1). we want to display it as a security update then """ inst_ver = pkg._pkg.current_ver cand_ver = self._depcache.get_candidate_ver(pkg._pkg) # init matcher with candidateVer update_origin = matcher[(None, None)] verFileIter = None for (verFileIter, index) in cand_ver.file_list: if matcher.has_key((verFileIter.archive, verFileIter.origin)): indexfile = pkg._pcache._list.find_index(verFileIter) if indexfile: # and indexfile.IsTrusted: match = matcher[verFileIter.archive, verFileIter.origin] update_origin = match break else: # add a node for each origin/archive combination if verFileIter and verFileIter.origin and verFileIter.archive: matcher[verFileIter.archive, verFileIter.origin] = UpdateOrigin( _("Other updates (%s)") % verFileIter.origin, 0) update_origin = matcher[verFileIter.archive, verFileIter.origin] # if the candidate comes from a unknown source (e.g. a PPA) skip # skip the shadow logic below as it would put e.g. a PPA package # in "Recommended updates" when the version in the PPA # is higher than the one in %s-updates if update_origin.importance <= 0: return update_origin # for known packages, check if we have higher versions that # "shadow" this one for ver in pkg._pkg.version_list: # discard is < than installed ver if (inst_ver and apt_pkg.VersionCompare(ver.ver_str, inst_ver.ver_str) <= 0): #print "skipping '%s' " % ver.ver_str continue # check if we have a match for (verFileIter, index) in ver.file_list: if matcher.has_key((verFileIter.archive, verFileIter.origin)): indexfile = pkg._pcache._list.find_index(verFileIter) if indexfile: # and indexfile.IsTrusted: match = matcher[verFileIter.archive, verFileIter.origin] if match.importance > update_origin.importance: update_origin = match return update_origin
def test_remove_package(self): """ Removes a package that has been uploaded to Debian. """ source = self.changes['Source'] log.debug('Checking whether package %s is in the repository' % source) package = meta.session.query(Package).filter_by(name=source).first() if package is None: # The package is not in the repository. This will happen the most often. log.debug('Package is not in the repository') return # Initialise apt_pkg apt_pkg.init() keep_package_versions = [] for package_version in package.package_versions: if apt_pkg.VersionCompare(self.changes['Version'], package_version.version) < 0: keep_package_versions.append(package_version.version) if len(keep_package_versions) == 0: # Get rid of the whole package. self._remove_package_versions(package.package_versions) log.debug('Deleting package database entry: %s' % package.name) meta.session.delete(package) else: # Only remove certain package versions. for package_version in package.package_versions: if package_version not in keep_package_versions: meta.session.delete(package_version) meta.session.commit()
# import apt_pkg apt_pkg.init() cache = apt_pkg.GetCache() packages = cache.Packages uninstalled, updated, upgradable = {}, {}, {} for package in packages: versions = package.VersionList if not versions: continue version = versions[0] for other_version in versions: if apt_pkg.VersionCompare(version.VerStr, other_version.VerStr) < 0: version = other_version if package.CurrentVer: current = package.CurrentVer if apt_pkg.VersionCompare(current.VerStr, version.VerStr) < 0: upgradable[package.Name] = version break else: updated[package.Name] = current else: uninstalled[package.Name] = version for l in (uninstalled, updated, upgradable): print l.items()[0]
import warnings warnings.filterwarnings("ignore", "apt API not stable yet", FutureWarning) import apt import apt_pkg import logging logging.basicConfig(level=logging.DEBUG, filename="/var/log/dist-upgrade/apt-autoinst-fixup.log", format='%(asctime)s %(levelname)s %(message)s', filemode='w') cache = apt.Cache() min_version = "0.6.20ubuntu13" if apt_pkg.VersionCompare(cache["python-apt"].installedVersion, min_version) < 0: logging.error("Need at least python-apt version %s " % min_version) sys.exit(1) # figure out what needs fixup logging.debug("Starting to check for auto-install states that need fixup") need_fixup = set() # work around #105663 need_fixup.add("mdadm") for pkg in cache: if pkg.is_installed and pkg.section == "metapackages": logging.debug("Found installed meta-pkg: '%s' " % pkg.name) dependsList = pkg._pkg.CurrentVer.DependsList for t in ["Depends", "PreDepends", "Recommends"]:
def compare(a, b): return apt_pkg.VersionCompare(a[0], b[0])
def _get_changelog_or_news(self, name, fname, strict_versioning=False, changelogs_uri=None): " helper that fetches the file in question " # don't touch the gui in this function, it needs to be thread-safe pkg = self[name] # get the src package name srcpkg = pkg.sourcePackageName # assume "main" section src_section = "main" # use the section of the candidate as a starting point section = pkg._pcache._depcache.get_candidate_ver(pkg._pkg).section # get the source version, start with the binaries version srcver_epoch = pkg.candidateVersion srcver = self._strip_epoch(srcver_epoch) #print "bin: %s" % binver l = section.split("/") if len(l) > 1: src_section = l[0] # lib is handled special prefix = srcpkg[0] if srcpkg.startswith("lib"): prefix = "lib" + srcpkg[3] # the changelogs_uri argument overrides the default changelogs_uri, # this is useful for e.g. PPAs where we construct the changelogs # path differently if changelogs_uri: uri = changelogs_uri else: uri = CHANGELOGS_URI % (src_section, prefix, srcpkg, srcpkg, srcver, fname) # https uris are not supported when they contain a username/password # because the urllib2 https implementation will not check certificates # and so its possible to do a man-in-the-middle attack to steal the # credentials res = urlparse.urlsplit(uri) if res.scheme == "https" and res.username != "": raise HttpsChangelogsUnsupportedError( "https locations with username/password are not" "supported to fetch changelogs") # print "Trying: %s " % uri changelog = urllib2.urlopen(uri) #print changelog.read() # do only get the lines that are new alllines = "" regexp = "^%s \((.*)\)(.*)$" % (re.escape(srcpkg)) while True: line = changelog.readline() if line == "": break match = re.match(regexp, line) if match: # strip epoch from installed version # and from changelog too installed = pkg.installedVersion if installed and ":" in installed: installed = installed.split(":", 1)[1] changelogver = match.group(1) if changelogver and ":" in changelogver: changelogver = changelogver.split(":", 1)[1] # we test for "==" here for changelogs # to ensure that the version # is actually really in the changelog - if not # just display it all, this catches cases like: # gcc-defaults with "binver=4.3.1" and srcver=1.76 # # for NEWS.Debian we do require the changelogver > installed if strict_versioning: if (installed and apt_pkg.VersionCompare( changelogver, installed) < 0): break else: if (installed and apt_pkg.VersionCompare( changelogver, installed) == 0): break alllines = alllines + line return alllines