def detect_releasever(installroot): # :api """Calculate the release version for the system.""" ts = transaction.initReadOnlyTransaction(root=installroot) ts.pushVSFlags(~(rpm._RPMVSF_NOSIGNATURES | rpm._RPMVSF_NODIGESTS)) for distroverpkg in dnf.const.DISTROVERPKG: try: idx = ts.dbMatch('provides', distroverpkg) except (TypeError, rpm.error) as e: raise dnf.exceptions.Error('Error: %s' % str(e)) if not len(idx): continue try: hdr = next(idx) except StopIteration: msg = 'Error: rpmdb failed to list provides. Try: rpm --rebuilddb' raise dnf.exceptions.Error(msg) releasever = hdr['version'] try: off = hdr[rpm.RPMTAG_PROVIDENAME].index(distroverpkg) flag = hdr[rpm.RPMTAG_PROVIDEFLAGS][off] ver = hdr[rpm.RPMTAG_PROVIDEVERSION][off] if flag == rpm.RPMSENSE_EQUAL and ver: if hdr['name'] != distroverpkg: # override the package version releasever = ver except (ValueError, KeyError, IndexError): pass if is_py3bytes(releasever): releasever = str(releasever, "utf-8") return releasever return None
def getheader(rpm_hdr, key): ''' Returns value of rpm_hdr[key] as a string. Rpm has switched from bytes to str and we need to handle both properly. ''' value = rpm_hdr[key] if is_py3bytes(value): value = str(value, "utf-8") return value
def ucd(obj): """ Like the builtin unicode() but tries to use a reasonable encoding. """ if PY3: if is_py3bytes(obj): return str(obj, _guess_encoding()) elif isinstance(obj, str): return obj return str(obj) else: if hasattr(obj, '__unicode__'): # see the doc for the unicode() built-in. The logic here is: if obj # implements __unicode__, let it take a crack at it, but handle the # situation if it fails: try: return unicode(obj) except UnicodeError: pass return unicode(str(obj), _guess_encoding())
def ucd(obj): """ Like the builtin unicode() but tries to use a reasonable encoding. """ if PY3: if is_py3bytes(obj): return str(obj, _guess_encoding()) elif isinstance(obj, str): return obj return str(obj) else: if isinstance(obj, unicode): return obj if hasattr(obj, '__unicode__'): # see the doc for the unicode() built-in. The logic here is: if obj # implements __unicode__, let it take a crack at it, but handle the # situation if it fails: try: return unicode(obj) except UnicodeError: pass return unicode(str(obj), _guess_encoding())
def _getsysver(installroot, distroverpkg): '''Calculate the release version for the system. @param installroot: The value of the installroot option. @param distroverpkg: The value of the distroverpkg option. @return: The release version as a string (eg. '4' for FC4) ''' ts = dnf.rpmUtils.transaction.initReadOnlyTransaction(root=installroot) ts.pushVSFlags(~(rpm._RPMVSF_NOSIGNATURES | rpm._RPMVSF_NODIGESTS)) try: idx = ts.dbMatch('provides', distroverpkg) except TypeError as e: # This is code for "cannot open rpmdb" # this is for pep 352 compliance on python 2.6 and above :( if sys.hexversion < 0x02050000: if hasattr(e, 'message'): raise dnf.exceptions.Error("Error: " + str(e.message)) else: raise dnf.exceptions.Error("Error: " + str(e)) raise dnf.exceptions.Error("Error: " + str(e)) except rpm.error as e: # This is the "new" code for "cannot open rpmdb", 4.8.0 ish raise dnf.exceptions.Error("Error: " + str(e)) # we're going to take the first one - if there is more than one of these # then the user needs a beating if len(idx) == 0: releasever = '$releasever' else: try: hdr = next(idx) except StopIteration: raise dnf.exceptions.Error( "Error: rpmdb failed release provides. Try: rpm --rebuilddb") releasever = hdr['version'] if is_py3bytes(releasever): releasever = str(releasever, "utf-8") del hdr del idx del ts return releasever
def detect_releasever(installroot): """Calculate the release version for the system. :api""" ts = transaction.initReadOnlyTransaction(root=installroot) ts.pushVSFlags(~(rpm._RPMVSF_NOSIGNATURES | rpm._RPMVSF_NODIGESTS)) for distroverpkg in dnf.const.DISTROVERPKG: try: idx = ts.dbMatch('provides', distroverpkg) except (TypeError, rpm.error) as e: raise dnf.exceptions.Error('Error: %s' % str(e)) if not len(idx): continue try: hdr = next(idx) except StopIteration: msg = 'Error: rpmdb failed to list provides. Try: rpm --rebuilddb' raise dnf.exceptions.Error(msg) releasever = hdr['version'] if is_py3bytes(releasever): releasever = str(releasever, "utf-8") return releasever return None
def detect_releasever(installroot): """Calculate the release version for the system. :api""" ts = transaction.initReadOnlyTransaction(root=installroot) ts.pushVSFlags(~(rpm._RPMVSF_NOSIGNATURES|rpm._RPMVSF_NODIGESTS)) for distroverpkg in dnf.const.DISTROVERPKG: try: idx = ts.dbMatch('provides', distroverpkg) except (TypeError, rpm.error) as e: raise dnf.exceptions.Error('Error: %s' % str(e)) if not len(idx): continue try: hdr = next(idx) except StopIteration: msg = 'Error: rpmdb failed to list provides. Try: rpm --rebuilddb' raise dnf.exceptions.Error(msg) releasever = hdr['version'] if is_py3bytes(releasever): releasever = str(releasever, "utf-8") return releasever return None
def _getsysver(installroot, distroverpkg): '''Calculate the release version for the system. @param installroot: The value of the installroot option. @param distroverpkg: The value of the distroverpkg option. @return: The release version as a string (eg. '4' for FC4) ''' ts = dnf.rpmUtils.transaction.initReadOnlyTransaction(root=installroot) ts.pushVSFlags(~(rpm._RPMVSF_NOSIGNATURES|rpm._RPMVSF_NODIGESTS)) try: idx = ts.dbMatch('provides', distroverpkg) except TypeError as e: # This is code for "cannot open rpmdb" # this is for pep 352 compliance on python 2.6 and above :( if sys.hexversion < 0x02050000: if hasattr(e,'message'): raise dnf.exceptions.Error("Error: " + str(e.message)) else: raise dnf.exceptions.Error("Error: " + str(e)) raise dnf.exceptions.Error("Error: " + str(e)) except rpm.error as e: # This is the "new" code for "cannot open rpmdb", 4.8.0 ish raise dnf.exceptions.Error("Error: " + str(e)) # we're going to take the first one - if there is more than one of these # then the user needs a beating if idx.count() == 0: releasever = '$releasever' else: try: hdr = next(idx) except StopIteration: raise dnf.exceptions.Error("Error: rpmdb failed release provides. Try: rpm --rebuilddb") releasever = hdr['version'] if is_py3bytes(releasever): releasever = str(releasever, "utf-8") del hdr del idx del ts return releasever