Blame dnf/rpm/__init__.py

Packit 6f3914
# __init__.py
Packit 6f3914
#
Packit 6f3914
# Copyright (C) 2012-2016 Red Hat, Inc.
Packit 6f3914
#
Packit 6f3914
# This copyrighted material is made available to anyone wishing to use,
Packit 6f3914
# modify, copy, or redistribute it subject to the terms and conditions of
Packit 6f3914
# the GNU General Public License v.2, or (at your option) any later version.
Packit 6f3914
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit 6f3914
# ANY WARRANTY expressed or implied, including the implied warranties of
Packit 6f3914
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 6f3914
# Public License for more details.  You should have received a copy of the
Packit 6f3914
# GNU General Public License along with this program; if not, write to the
Packit 6f3914
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6f3914
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit 6f3914
# source code or documentation are not subject to the GNU General Public
Packit 6f3914
# License and may only be used or replicated with the express permission of
Packit 6f3914
# Red Hat, Inc.
Packit 6f3914
#
Packit 6f3914
Packit 6f3914
from __future__ import absolute_import
Packit 6f3914
from __future__ import unicode_literals
Packit 6f3914
from . import transaction
Packit 6f3914
from dnf.pycomp import is_py3bytes
Packit 6f3914
import dnf.const
Packit 6f3914
import dnf.exceptions
Packit 6f3914
import rpm  # used by ansible (dnf.rpm.rpm.labelCompare in lib/ansible/modules/packaging/os/dnf.py)
Packit 6f3914
Packit 6f3914
Packit 6f3914
def detect_releasever(installroot):
Packit 6f3914
    # :api
Packit 6f3914
    """Calculate the release version for the system."""
Packit 6f3914
Packit 6f3914
    ts = transaction.initReadOnlyTransaction(root=installroot)
Packit 6f3914
    ts.pushVSFlags(~(rpm._RPMVSF_NOSIGNATURES | rpm._RPMVSF_NODIGESTS))
Packit 6f3914
    for distroverpkg in dnf.const.DISTROVERPKG:
Packit 6f3914
        if dnf.pycomp.PY3:
Packit 6f3914
            distroverpkg = bytes(distroverpkg, 'utf-8')
Packit 6f3914
        try:
Packit 6f3914
            idx = ts.dbMatch('provides', distroverpkg)
Packit 6f3914
        except (TypeError, rpm.error) as e:
Packit 6f3914
            raise dnf.exceptions.Error('Error: %s' % str(e))
Packit 6f3914
        if not len(idx):
Packit 6f3914
            continue
Packit 6f3914
        try:
Packit 6f3914
            hdr = next(idx)
Packit 6f3914
        except StopIteration:
Packit 6f3914
            msg = 'Error: rpmdb failed to list provides. Try: rpm --rebuilddb'
Packit 6f3914
            raise dnf.exceptions.Error(msg)
Packit 6f3914
        releasever = hdr['version']
Packit 6f3914
        try:
Packit 6f3914
            try:
Packit 6f3914
                # header returns bytes -> look for bytes
Packit 6f3914
                # it may fail because rpm returns a decoded string since 10 Apr 2019
Packit 6f3914
                off = hdr[rpm.RPMTAG_PROVIDENAME].index(distroverpkg)
Packit 6f3914
            except ValueError:
Packit 6f3914
                # header returns a string -> look for a string
Packit 6f3914
                off = hdr[rpm.RPMTAG_PROVIDENAME].index(distroverpkg.decode("utf8"))
Packit 6f3914
            flag = hdr[rpm.RPMTAG_PROVIDEFLAGS][off]
Packit 6f3914
            ver = hdr[rpm.RPMTAG_PROVIDEVERSION][off]
Packit 6f3914
            if flag == rpm.RPMSENSE_EQUAL and ver:
Packit 6f3914
                if hdr['name'] not in (distroverpkg, distroverpkg.decode("utf8")):
Packit 6f3914
                    # override the package version
Packit 6f3914
                    releasever = ver
Packit 6f3914
        except (ValueError, KeyError, IndexError):
Packit 6f3914
            pass
Packit 6f3914
Packit 6f3914
        if is_py3bytes(releasever):
Packit 6f3914
            releasever = str(releasever, "utf-8")
Packit 6f3914
        return releasever
Packit 6f3914
    return None
Packit 6f3914
Packit 6f3914
Packit 6f3914
def _header(path):
Packit 6f3914
    """Return RPM header of the file."""
Packit 6f3914
    ts = transaction.initReadOnlyTransaction()
Packit 6f3914
    with open(path) as package:
Packit 6f3914
        fdno = package.fileno()
Packit 6f3914
        try:
Packit 6f3914
            hdr = ts.hdrFromFdno(fdno)
Packit 6f3914
        except rpm.error as e:
Packit 6f3914
            raise dnf.exceptions.Error("{0}: '{1}'".format(e, path))
Packit 6f3914
        return hdr
Packit 6f3914
Packit 6f3914
Packit 6f3914
def _invert(dct):
Packit 6f3914
    return {v: k for k in dct for v in dct[k]}
Packit 6f3914
Packit 6f3914
_BASEARCH_MAP = _invert({
Packit 6f3914
    'aarch64': ('aarch64',),
Packit 6f3914
    'alpha': ('alpha', 'alphaev4', 'alphaev45', 'alphaev5', 'alphaev56',
Packit 6f3914
              'alphaev6', 'alphaev67', 'alphaev68', 'alphaev7', 'alphapca56'),
Packit 6f3914
    'arm': ('armv5tejl', 'armv5tel', 'armv5tl', 'armv6l', 'armv7l', 'armv8l'),
Packit 6f3914
    'armhfp': ('armv6hl', 'armv7hl', 'armv7hnl', 'armv8hl'),
Packit 6f3914
    'i386': ('i386', 'athlon', 'geode', 'i386', 'i486', 'i586', 'i686'),
Packit 6f3914
    'ia64': ('ia64',),
Packit 6f3914
    'mips': ('mips',),
Packit 6f3914
    'mipsel': ('mipsel',),
Packit 6f3914
    'mips64': ('mips64',),
Packit 6f3914
    'mips64el': ('mips64el',),
Packit 6f3914
    'noarch': ('noarch',),
Packit 6f3914
    'ppc': ('ppc',),
Packit 6f3914
    'ppc64': ('ppc64', 'ppc64iseries', 'ppc64p7', 'ppc64pseries'),
Packit 6f3914
    'ppc64le': ('ppc64le',),
Packit 6f3914
    'riscv32' : ('riscv32',),
Packit 6f3914
    'riscv64' : ('riscv64',),
Packit 6f3914
    'riscv128' : ('riscv128',),
Packit 6f3914
    's390': ('s390',),
Packit 6f3914
    's390x': ('s390x',),
Packit 6f3914
    'sh3': ('sh3',),
Packit 6f3914
    'sh4': ('sh4', 'sh4a'),
Packit 6f3914
    'sparc': ('sparc', 'sparc64', 'sparc64v', 'sparcv8', 'sparcv9',
Packit 6f3914
              'sparcv9v'),
Packit 6f3914
    'x86_64': ('x86_64', 'amd64', 'ia32e'),
Packit 6f3914
})
Packit 6f3914
Packit 6f3914
Packit 6f3914
def basearch(arch):
Packit 6f3914
    # :api
Packit 6f3914
    return _BASEARCH_MAP[arch]
Packit 6f3914
Packit 6f3914
Packit 6f3914
def getheader(rpm_hdr, key):
Packit 6f3914
    '''
Packit 6f3914
    Returns value of rpm_hdr[key] as a string. Rpm has switched from bytes to str
Packit 6f3914
    and we need to handle both properly.
Packit 6f3914
    '''
Packit 6f3914
    value = rpm_hdr[key]
Packit 6f3914
    if is_py3bytes(value):
Packit 6f3914
        value = str(value, "utf-8")
Packit 6f3914
    return value