Example D-Bus usage
Examples using dnf5daemon server via D-Bus from Python.
Print upgrades
Print all available upgrades, the repository they come from and severity of associated advisory if present.
1#
2# You should have received a copy of the GNU General Public License
3# along with libdnf. If not, see <https://www.gnu.org/licenses/>.
4
5import dbus
6import os
7
8DNFDAEMON_BUS_NAME = 'org.rpm.dnf.v0'
9DNFDAEMON_OBJECT_PATH = '/' + DNFDAEMON_BUS_NAME.replace('.', '/')
10
11IFACE_SESSION_MANAGER = '{}.SessionManager'.format(DNFDAEMON_BUS_NAME)
12IFACE_REPO = '{}.rpm.Repo'.format(DNFDAEMON_BUS_NAME)
13IFACE_REPOCONF = '{}.rpm.RepoConf'.format(DNFDAEMON_BUS_NAME)
14IFACE_RPM = '{}.rpm.Rpm'.format(DNFDAEMON_BUS_NAME)
15IFACE_GOAL = '{}.Goal'.format(DNFDAEMON_BUS_NAME)
16IFACE_ADVISORY = '{}.Advisory'.format(DNFDAEMON_BUS_NAME)
17
18
19bus = dbus.SystemBus()
20iface_session = dbus.Interface(
21 bus.get_object(DNFDAEMON_BUS_NAME, DNFDAEMON_OBJECT_PATH),
22 dbus_interface=IFACE_SESSION_MANAGER)
23
24session = iface_session.open_session(
25 dbus.Dictionary({}, signature=dbus.Signature('sv')))
26
27iface_rpm = dbus.Interface(
28 bus.get_object(DNFDAEMON_BUS_NAME, session),
29 dbus_interface=IFACE_RPM)
30iface_advisory = dbus.Interface(
31 bus.get_object(DNFDAEMON_BUS_NAME, session),
32 dbus_interface=IFACE_ADVISORY)
33
34# Information about the severity of the upgrade is stored in the updateinfo
35# repository metadata and is not directly accessible from the list of possible
36# upgrades. This means we first need to retrieve all advisories and then search
37# them for severity for all potential upgrade packages.
38
39# First get all available advisories, we are interested only in "severity" and
40# "collections" (list of packages and modules) fields.
41options = {
42 "advisory_attrs": [
43 # "advisoryid",
44 # "name",
45 # "title",
46 # "type",
47 "severity",
48 # "status",
49 # "vendor",
50 # "description",
51 # "buildtime",
52 # "message",
53 # "rights",
54 "collections",
55 # "references",
56 ],
57 "availability": "available",
58}
59advisory_list = iface_advisory.list(options)
60
61# auxiliary dictionary to map package NEVRA to severity of advisory it belongs to
62nevra_to_severity = dict()
63for adv in advisory_list:
64 severity = str(adv["severity"])
65 for col in adv["collections"]:
66 if "packages" in col:
67 for pkg in col["packages"]:
68 nevra = pkg["nevra"]
69 nevra_to_severity[nevra] = severity
70
71
72# retrieve potential upgrades and print the packages along with their
73# respective severities
74options = {
75 "package_attrs": [
76 "nevra",
77 "repo_id",
78 ],
79 "scope": "upgrades",
80 "latest-limit": 1,
81}
82upgrades = iface_rpm.list(options)
83for pkg in upgrades:
84 nevra = pkg["nevra"]
85 severity = nevra_to_severity.get(nevra, "<unknown>")
86 print("{} (@{}): {}".format(nevra, pkg["repo_id"], severity))
System upgrade
Perform a system-upgrade.
1"""
2This is an example how to perform a system-upgrade with dnf5daemon-server.
3"""
4
5import dbus
6
7DNFDAEMON_BUS_NAME = 'org.rpm.dnf.v0'
8DNFDAEMON_OBJECT_PATH = '/' + DNFDAEMON_BUS_NAME.replace('.', '/')
9
10IFACE_SESSION_MANAGER = '{}.SessionManager'.format(DNFDAEMON_BUS_NAME)
11IFACE_RPM = '{}.rpm.Rpm'.format(DNFDAEMON_BUS_NAME)
12IFACE_GOAL = '{}.Goal'.format(DNFDAEMON_BUS_NAME)
13
14
15bus = dbus.SystemBus()
16iface_session = dbus.Interface(
17 bus.get_object(DNFDAEMON_BUS_NAME, DNFDAEMON_OBJECT_PATH),
18 dbus_interface=IFACE_SESSION_MANAGER)
19
20# set the releasever to the new distribution release
21session = iface_session.open_session(
22 dbus.Dictionary({"releasever": "40"}, signature=dbus.Signature('sv')))
23
24iface_rpm = dbus.Interface(
25 bus.get_object(DNFDAEMON_BUS_NAME, session),
26 dbus_interface=IFACE_RPM)
27iface_goal = dbus.Interface(
28 bus.get_object(DNFDAEMON_BUS_NAME, session),
29 dbus_interface=IFACE_GOAL)
30
31# Add system upgrade to the transaction
32options = {
33 "mode": "distrosync",
34}
35iface_rpm.system_upgrade(options)
36
37# resolve the transaction
38resolved, result = iface_goal.resolve({})
39
40# now you can print the transaction table and ask the user for confirmation
41print("Resolved.")
42
43if result == 0:
44 # execute the transaction offline (durint the next reboot)
45 iface_goal.do_transaction({"offline": True}, timeout=2000)
46 print("Reboot to continue with system upgrade...")
47else:
48 errors = iface_goal.get_transaction_problems_string()
49 print("Errors while resolving the transaction:")
50 for error in errors:
51 print(error)
list_fd()
D-Bus API bindings for dnfdaemon org.rpm.dnf.v0.rpm.Rpm.list_fd() example.
1"""
2This is an example how to perform a system-upgrade with dnf5daemon-server.
3"""
4
5import dbus
6
7DNFDAEMON_BUS_NAME = 'org.rpm.dnf.v0'
8DNFDAEMON_OBJECT_PATH = '/' + DNFDAEMON_BUS_NAME.replace('.', '/')
9
10IFACE_SESSION_MANAGER = '{}.SessionManager'.format(DNFDAEMON_BUS_NAME)
11IFACE_RPM = '{}.rpm.Rpm'.format(DNFDAEMON_BUS_NAME)
12IFACE_GOAL = '{}.Goal'.format(DNFDAEMON_BUS_NAME)
13
14
15bus = dbus.SystemBus()
16iface_session = dbus.Interface(
17 bus.get_object(DNFDAEMON_BUS_NAME, DNFDAEMON_OBJECT_PATH),
18 dbus_interface=IFACE_SESSION_MANAGER)
19
20# set the releasever to the new distribution release
21session = iface_session.open_session(
22 dbus.Dictionary({"releasever": "40"}, signature=dbus.Signature('sv')))
23
24iface_rpm = dbus.Interface(
25 bus.get_object(DNFDAEMON_BUS_NAME, session),
26 dbus_interface=IFACE_RPM)
27iface_goal = dbus.Interface(
28 bus.get_object(DNFDAEMON_BUS_NAME, session),
29 dbus_interface=IFACE_GOAL)
30
31# Add system upgrade to the transaction
32options = {
33 "mode": "distrosync",
34}
35iface_rpm.system_upgrade(options)
36
37# resolve the transaction
38resolved, result = iface_goal.resolve({})
39
40# now you can print the transaction table and ask the user for confirmation
41print("Resolved.")
42
43if result == 0:
44 # execute the transaction offline (durint the next reboot)
45 iface_goal.do_transaction({"offline": True}, timeout=2000)
46 print("Reboot to continue with system upgrade...")
47else:
48 errors = iface_goal.get_transaction_problems_string()
49 print("Errors while resolving the transaction:")
50 for error in errors:
51 print(error)