3.1. Creating and configuring a session

 1import libdnf5
 2
 3# Create a new Base object
 4base = libdnf5.base.Base()
 5
 6# Optionally set installroot.
 7#
 8# Installroot is set to '/' when we're working with the system, but we can set
 9# it to a different location. The Base instance will then work with the
10# installroot directory tree as its root for the rest of its lifetime.
11base_config = base.get_config()
12base_config.installroot = installroot
13
14# Optionally prevent of loading of plugins. Plugins are loaded by default from the host
15base_config.plugins = False
16
17# Optionally load configuration from the config files.
18#
19# The Base's config is initialized with default values, one of which is
20# "config_file_path". This contains the default path to the config file
21# ("/etc/dnf/dnf.conf"). If the file does not exist the distribution config file
22# is loaded. Function also loads configuration files from distribution and
23# user ("/etc/dnf/libdnf5.conf.d") drop-in directories.
24# Optionally set a custom value to "config_file_path" before calling this method
25# to load configuration from a another configuration file.
26base.load_config()
27
28# Optionally you can set and get vars
29# vars = base.get_vars().get()
30# vars.set('releasever', '33')
31#
32# Its value can be printed by get_value method
33# print(vars.get_value('releasever'))
34#
35# There are plans in the future to support the methods get() or get_priority()
36
37# Optionally set cachedir.
38#
39# By default, system cachedir or user cachedir is used, but we can set it to a
40# different location. This is useful for tests.
41base_config.cachedir = cachedir
42
43# Load vars and do other initialization (of libsolv pool, etc.) based on the
44# configuration.  Locks the installroot and varsdir configuration values so
45# that they can't be changed.
46base.setup()

3.1.1. Override the system architecture

For the dnf5 command-line tool, the –forcearch, dnf5-forcearch(7) option is available. Here’s how you can achieve the same effect using the Python API:

 1import libdnf5
 2
 3# Create a new Base object
 4base = libdnf5.base.Base()
 5
 6# Optionally load configuration from the config files.
 7base.load_config()
 8
 9# Override the detected system architecture, similar to how the
10# `--forcearch=aarch64` switch works in the dnf5 command line tool.
11vars = base.get_vars().get()
12vars.set("arch", "aarch64")
13
14# This is sufficient for loading repositories and querying packages using the
15# `aarch64` architecture.
16# However, if you also want to run a transaction (e.g., you want to modify a
17# foreign system from "outside" using `installroot`), you need to set the
18# `ignorearch` option to instruct RPM to permit packages that are incompatible
19# with the system architecture.
20base.get_config().get_ignorearch_option().set(True)
21
22# The base.setup() call configures the architecture for the solver, so the
23# `arch` variable needs to be set beforehand.
24base.setup()