def get_version(): """Derive Ceph release from an installed package.""" import apt_pkg as apt cache = apt_cache() package = "ceph" try: pkg = cache[package] except: # the package is unknown to the current apt cache. e = 'Could not determine version of package with no installation ' \ 'candidate: %s' % package error_out(e) if not pkg.current_ver: # package is known, but no version is currently installed. e = 'Could not determine version of uninstalled package: %s' % package error_out(e) vers = apt.upstream_version(pkg.current_ver.ver_str) # x.y match only for 20XX.X # and ignore patch level for other packages match = re.match('^(\d+)\.(\d+)', vers) if match: vers = match.group(0) return float(vers)
def fetch_source(self, destdir="", progress=None, unpack=True): """Get the source code of a package. The parameter *destdir* specifies the directory where the source will be fetched to. The parameter *progress* may refer to an apt_pkg.AcquireProgress() object. If not specified or None, apt.progress.text.AcquireProgress() is used. The parameter *unpack* describes whether the source should be unpacked (``True``) or not (``False``). By default, it is unpacked. If *unpack* is ``True``, the path to the extracted directory is returned. Otherwise, the path to the .dsc file is returned. """ src = apt_pkg.SourceRecords() acq = apt_pkg.Acquire(progress or apt.progress.text.AcquireProgress()) dsc = None record = self._records source_name = record.source_pkg or self.package.shortname source_version = record.source_ver or self._cand.ver_str source_lookup = src.lookup(source_name) while source_lookup and source_version != src.version: source_lookup = src.lookup(source_name) if not source_lookup: raise ValueError("No source for %r" % self) files = list() for md5, size, path, type_ in src.files: base = os.path.basename(path) destfile = os.path.join(destdir, base) if type_ == 'dsc': dsc = destfile if _file_is_same(destfile, size, md5): print('Ignoring already existing file: %s' % destfile) continue files.append( apt_pkg.AcquireFile(acq, src.index.archive_uri(path), md5, size, base, destfile=destfile)) acq.run() for item in acq.items: if item.status != item.STAT_DONE: raise FetchError("The item %r could not be fetched: %s" % (item.destfile, item.error_text)) if unpack: outdir = src.package + '-' + apt_pkg.upstream_version(src.version) outdir = os.path.join(destdir, outdir) subprocess.check_call(["dpkg-source", "-x", dsc, outdir]) return os.path.abspath(outdir) else: return os.path.abspath(dsc)
def get_ceph_version(): apt.init() cache = apt.Cache() pkg = cache['ceph'] if pkg.current_ver: return apt.upstream_version(pkg.current_ver.ver_str) else: return None
def rabbit_version(): apt.init() cache = apt.Cache() pkg = cache['rabbitmq-server'] if pkg.current_ver: return apt.upstream_version(pkg.current_ver.ver_str) else: return None
def get_ceph_version(package=None): apt.init() cache = apt.Cache() pkg = cache[package or 'ceph'] if pkg.current_ver: return apt.upstream_version(pkg.current_ver.ver_str) else: return None
def fetch_source(self, destdir="", progress=None, unpack=True): """Get the source code of a package. The parameter *destdir* specifies the directory where the source will be fetched to. The parameter *progress* may refer to an apt_pkg.AcquireProgress() object. If not specified or None, apt.progress.text.AcquireProgress() is used. The parameter *unpack* describes whether the source should be unpacked (``True``) or not (``False``). By default, it is unpacked. If *unpack* is ``True``, the path to the extracted directory is returned. Otherwise, the path to the .dsc file is returned. """ src = apt_pkg.SourceRecords() acq = apt_pkg.Acquire(progress or apt.progress.text.AcquireProgress()) dsc = None record = self._records source_name = record.source_pkg or self.package.name source_version = record.source_ver or self._cand.ver_str source_lookup = src.lookup(source_name) while source_lookup and source_version != src.version: source_lookup = src.lookup(source_name) if not source_lookup: raise ValueError("No source for %r" % self) files = list() for md5, size, path, type_ in src.files: base = os.path.basename(path) destfile = os.path.join(destdir, base) if type_ == 'dsc': dsc = destfile if _file_is_same(destfile, size, md5): print 'Ignoring already existing file:', destfile continue files.append(apt_pkg.AcquireFile(acq, src.index.archive_uri(path), md5, size, base, destfile=destfile)) acq.run() for item in acq.items: if item.status != item.STAT_DONE: raise FetchError("The item %r could not be fetched: %s" % (item.destfile, item.error_text)) if unpack: outdir = src.package + '-' + apt_pkg.upstream_version(src.version) outdir = os.path.join(destdir, outdir) subprocess.check_call(["dpkg-source", "-x", dsc, outdir]) return os.path.abspath(outdir) else: return os.path.abspath(dsc)
def get_os_codename_package(package, fatal=True): '''Derive OpenStack release codename from an installed package.''' import apt_pkg as apt cache = apt_cache() try: pkg = cache[package] except: if not fatal: return None # the package is unknown to the current apt cache. e = 'Could not determine version of package with no installation '\ 'candidate: %s' % package error_out(e) if not pkg.current_ver: if not fatal: return None # package is known, but no version is currently installed. e = 'Could not determine version of uninstalled package: %s' % package error_out(e) vers = apt.upstream_version(pkg.current_ver.ver_str) if 'swift' in pkg.name: # Fully x.y.z match for swift versions match = re.match('^(\d+)\.(\d+)\.(\d+)', vers) else: # x.y match only for 20XX.X # and ignore patch level for other packages match = re.match('^(\d+)\.(\d+)', vers) if match: vers = match.group(0) # >= Liberty independent project versions if (package in PACKAGE_CODENAMES and vers in PACKAGE_CODENAMES[package]): return PACKAGE_CODENAMES[package][vers] else: # < Liberty co-ordinated project versions try: if 'swift' in pkg.name: return SWIFT_CODENAMES[vers] else: return OPENSTACK_CODENAMES[vers] except KeyError: if not fatal: return None e = 'Could not determine OpenStack codename for version %s' % vers error_out(e)
def get_os_codename_package(package, codenames, fatal=True): """Derive OpenStack release codename from an installed package. :param package: str Package name to lookup (ie. in apt cache) :param codenames: dict of OrderedDict eg (not applicable for snap pkgs) { 'pkg1': collections.OrderedDict([ ('2', 'mitaka'), ('3', 'newton'), ('4', 'ocata'), ]), 'pkg2': collections.OrderedDict([ ('12.6', 'mitaka'), ('13.2', 'newton'), ('14.7', 'ocata'), ]), } :param fatal: bool Raise exception if pkg not installed :returns: str OpenStack version name corresponding to package """ cache = fetch.apt_cache() try: pkg = cache[package] except KeyError: if not fatal: return None # the package is unknown to the current apt cache. e = ('Could not determine version of package with no installation ' 'candidate: {}'.format(package)) raise Exception(e) if not pkg.current_ver: if not fatal: return None vers = apt.upstream_version(pkg.current_ver.ver_str) # x.y match only for 20XX.X # and ignore patch level for other packages match = re.match('^(\d+)\.(\d+)', vers) if match: vers = match.group(0) # Generate a major version number for newer semantic # versions of openstack projects major_vers = vers.split('.')[0] if (package in codenames and major_vers in codenames[package]): return codenames[package][major_vers]
def get_os_codename_package(package, codenames, fatal=True): """Derive OpenStack release codename from an installed package. :param package: str Package name to lookup in apt cache :param codenames: dict of OrderedDict eg { 'pkg1': collections.OrderedDict([ ('2', 'mitaka'), ('3', 'newton'), ('4', 'ocata'), ]), 'pkg2': collections.OrderedDict([ ('12.6', 'mitaka'), ('13.2', 'newton'), ('14.7', 'ocata'), ]), } :param fatal: bool Raise exception if pkg not installed :returns: str OpenStack version name corresponding to package """ cache = charmhelpers.fetch.apt_cache() try: pkg = cache[package] except KeyError: if not fatal: return None # the package is unknown to the current apt cache. e = ('Could not determine version of package with no installation ' 'candidate: {}'.format(package)) raise Exception(e) if not pkg.current_ver: if not fatal: return None vers = apt.upstream_version(pkg.current_ver.ver_str) # x.y match only for 20XX.X # and ignore patch level for other packages match = re.match('^(\d+)\.(\d+)', vers) if match: vers = match.group(0) # Generate a major version number for newer semantic # versions of openstack projects major_vers = vers.split('.')[0] if (package in codenames and major_vers in codenames[package]): return codenames[package][major_vers]
def get_upstream_version(package): """Determine upstream version based on installed package @returns None (if not installed) or the upstream version """ import apt_pkg cache = fetch.apt_cache() try: pkg = cache[package] except: # the package is unknown to the current apt cache. return None if not pkg.current_ver: # package is known, but no version is currently installed. return None return apt_pkg.upstream_version(pkg.current_ver.ver_str)
def current_release_tracking(self): """The PackageUpstreamTracking state for the current release.""" upstream_release = self.context.productseries.getLatestRelease() current_release = self.context.currentrelease if upstream_release is None or current_release is None: # Launchpad is missing data. There is not enough information to # track releases. return PackageUpstreamTracking.NONE # Compare the base version contained in the full debian version # to upstream release's version. base_version = upstream_version(current_release.version) age = version_compare(upstream_release.version, base_version) if age > 0: return PackageUpstreamTracking.NEWER elif age < 0: return PackageUpstreamTracking.OLDER else: # age == 0: return PackageUpstreamTracking.CURRENT
def get_os_codename_package(package, fatal=True): '''Derive OpenStack release codename from an installed package.''' import apt_pkg as apt apt.init() # Tell apt to build an in-memory cache to prevent race conditions (if # another process is already building the cache). apt.config.set("Dir::Cache::pkgcache", "") cache = apt.Cache() try: pkg = cache[package] except: if not fatal: return None # the package is unknown to the current apt cache. e = 'Could not determine version of package with no installation '\ 'candidate: %s' % package error_out(e) if not pkg.current_ver: if not fatal: return None # package is known, but no version is currently installed. e = 'Could not determine version of uninstalled package: %s' % package error_out(e) vers = apt.upstream_version(pkg.current_ver.ver_str) try: if 'swift' in pkg.name: swift_vers = vers[:5] if swift_vers not in SWIFT_CODENAMES: # Deal with 1.10.0 upward swift_vers = vers[:6] return SWIFT_CODENAMES[swift_vers] else: vers = vers[:6] return OPENSTACK_CODENAMES[vers] except KeyError: e = 'Could not determine OpenStack codename for version %s' % vers error_out(e)
def get_os_codename_package(pkg): '''Derive OpenStack release codename from an installed package.''' apt.init() cache = apt.Cache() try: pkg = cache[pkg] except: e = 'Could not determine version of installed package: %s' % pkg error_out(e) vers = apt.upstream_version(pkg.current_ver.ver_str) try: if 'swift' in pkg.name: vers = vers[:5] return swift_codenames[vers] else: vers = vers[:6] return openstack_codenames[vers] except KeyError: e = 'Could not determine OpenStack codename for version %s' % vers error_out(e)
def get_os_codename_package(package, fatal=True): '''Derive OpenStack release codename from an installed package.''' import apt_pkg as apt cache = apt_cache() try: pkg = cache[package] except: if not fatal: return None # the package is unknown to the current apt cache. e = 'Could not determine version of package with no installation '\ 'candidate: %s' % package error_out(e) if not pkg.current_ver: if not fatal: return None # package is known, but no version is currently installed. e = 'Could not determine version of uninstalled package: %s' % package error_out(e) vers = apt.upstream_version(pkg.current_ver.ver_str) try: if 'swift' in pkg.name: swift_vers = vers[:5] if swift_vers not in SWIFT_CODENAMES: # Deal with 1.10.0 upward swift_vers = vers[:6] return SWIFT_CODENAMES[swift_vers] else: vers = vers[:6] return OPENSTACK_CODENAMES[vers] except KeyError: e = 'Could not determine OpenStack codename for version %s' % vers error_out(e)
def upstream_version(v): return apt_pkg.upstream_version(v)
def upstream_version_compare(a, b): return apt_pkg.version_compare(apt_pkg.upstream_version(a), apt_pkg.upstream_version(b))
if 'DEBUG' in os.environ and os.environ['DEBUG'] == 'get': sys.exit(0) debug('Comparing versions...') for name in package_names: package = packages[name] if package.debian_version == package.tanglu_version: package.is_synchronised = True if compare_versions(package.debian_version, package.tanglu_version) > 0: package.is_debian_newer = True if package.upstream_version is None: debug ("Upstream version of %s was None!" % (name)) else: if package.tanglu_version is None or compare_versions(apt_pkg.upstream_version(package.upstream_version), apt_pkg.upstream_version(package.tanglu_version)) > 0: package.is_upstream_newer = True if package.upstream_unstable_version is not None and \ compare_versions(package.upstream_unstable_version, package.tanglu_version) > 0: # HACK? don't list gnome3 ppa version as uptodate, we don't want to overlook things # because they are only in the ppa, could be yet another section though # and (package.ubuntu_unstable_version is None or compare_versions(package.upstream_unstable_version, package.ubuntu_unstable_version) > 0): package.is_upstream_unstable_newer = True def get_package_class(package): if package.stable_url == UNTRACKED or package.upstream_version == '': return 6 elif package.is_upstream_newer and package.is_debian_newer: return 0 elif package.is_upstream_newer and not package.is_synchronised: return 1