dnf-json: allow passing arch as an argument

dnf can do cross-arch depsolving, but in case repositories for multiple
arches are provided and it is running on a different architecture than
the image build requires, it can lead to errors.

This patch makes sure that we only include packages from the
architecture we want.

It uses substitutions as defined in dnf documentation:
https://dnf.readthedocs.io/en/latest/api_conf.html#dnf.conf.Conf.substitutions
Unfortunately the docs are very sparse on details and the Fedora docs
are not updated any more:
https://docs.fedoraproject.org/en-US/Fedora/26/html/System_Administrators_Guide/sec-Using_DNF_Variables.html
Also dnf team is migrating the code to libdnf:
https://github.com/rpm-software-management/libdnf
which does not yet have any documentation.
This commit is contained in:
Martin Sehnoutka 2020-03-23 10:20:31 +01:00 committed by Tom Gundersen
parent 9d2dacbcab
commit 7e69299259

View file

@ -37,12 +37,14 @@ def dnfrepo(desc, parent_conf=None):
return repo
def create_base(repos, module_platform_id, persistdir, cachedir):
def create_base(repos, module_platform_id, persistdir, cachedir, arch):
base = dnf.Base()
base.conf.module_platform_id = module_platform_id
base.conf.config_file_path = "/dev/null"
base.conf.persistdir = persistdir
base.conf.cachedir = cachedir
base.conf.substitutions['arch'] = arch
base.conf.substitutions['basearch'] = dnf.rpm.basearch(arch)
for repo in repos:
base.repos.add(dnfrepo(repo, base.conf))
@ -84,12 +86,13 @@ call = json.load(sys.stdin)
command = call["command"]
arguments = call["arguments"]
repos = arguments.get("repos", {})
arch = arguments["arch"]
cachedir = arguments["cachedir"]
module_platform_id = arguments["module_platform_id"]
with tempfile.TemporaryDirectory() as persistdir:
try:
base = create_base(repos, module_platform_id, persistdir, cachedir)
base = create_base(repos, module_platform_id, persistdir, cachedir, arch)
except dnf.exceptions.RepoError as e:
exit_with_dnf_error("RepoError", f"Error occurred when setting up repo: {e}")