Example D-Bus usage

Examples using dnf5daemon server via D-Bus from Python.

System upgrade

Perform a system-upgrade.

 1import dbus
 2
 3DNFDAEMON_BUS_NAME = 'org.rpm.dnf.v0'
 4DNFDAEMON_OBJECT_PATH = '/' + DNFDAEMON_BUS_NAME.replace('.', '/')
 5
 6IFACE_SESSION_MANAGER = '{}.SessionManager'.format(DNFDAEMON_BUS_NAME)
 7IFACE_RPM = '{}.rpm.Rpm'.format(DNFDAEMON_BUS_NAME)
 8IFACE_GOAL = '{}.Goal'.format(DNFDAEMON_BUS_NAME)
 9
10
11bus = dbus.SystemBus()
12iface_session = dbus.Interface(
13    bus.get_object(DNFDAEMON_BUS_NAME, DNFDAEMON_OBJECT_PATH),
14    dbus_interface=IFACE_SESSION_MANAGER)
15
16# set the releasever to the new distribution release
17session = iface_session.open_session(
18    dbus.Dictionary({"releasever": "40"}, signature=dbus.Signature('sv')))
19
20iface_rpm = dbus.Interface(
21    bus.get_object(DNFDAEMON_BUS_NAME, session),
22    dbus_interface=IFACE_RPM)
23iface_goal = dbus.Interface(
24    bus.get_object(DNFDAEMON_BUS_NAME, session),
25    dbus_interface=IFACE_GOAL)
26
27# Add system upgrade to the transaction
28options = {
29    "mode": "distrosync",
30}
31iface_rpm.system_upgrade(options)
32
33# resolve the transaction
34resolved, result = iface_goal.resolve({})
35
36# now you can print the transaction table and ask the user for confirmation
37print("Resolved.")
38
39if result == 0:
40    # execute the transaction offline (durint the next reboot)
41    iface_goal.do_transaction({"offline": True}, timeout=2000)
42    print("Reboot to continue with system upgrade...")
43else:
44    errors = iface_goal.get_transaction_problems_string()
45    print("Errors while resolving the transaction:")
46    for error in errors:
47        print(error)

list_fd()

D-Bus API bindings for dnfdaemon org.rpm.dnf.v0.rpm.Rpm.list_fd() exmaple.

 1import dbus
 2
 3DNFDAEMON_BUS_NAME = 'org.rpm.dnf.v0'
 4DNFDAEMON_OBJECT_PATH = '/' + DNFDAEMON_BUS_NAME.replace('.', '/')
 5
 6IFACE_SESSION_MANAGER = '{}.SessionManager'.format(DNFDAEMON_BUS_NAME)
 7IFACE_RPM = '{}.rpm.Rpm'.format(DNFDAEMON_BUS_NAME)
 8IFACE_GOAL = '{}.Goal'.format(DNFDAEMON_BUS_NAME)
 9
10
11bus = dbus.SystemBus()
12iface_session = dbus.Interface(
13    bus.get_object(DNFDAEMON_BUS_NAME, DNFDAEMON_OBJECT_PATH),
14    dbus_interface=IFACE_SESSION_MANAGER)
15
16# set the releasever to the new distribution release
17session = iface_session.open_session(
18    dbus.Dictionary({"releasever": "40"}, signature=dbus.Signature('sv')))
19
20iface_rpm = dbus.Interface(
21    bus.get_object(DNFDAEMON_BUS_NAME, session),
22    dbus_interface=IFACE_RPM)
23iface_goal = dbus.Interface(
24    bus.get_object(DNFDAEMON_BUS_NAME, session),
25    dbus_interface=IFACE_GOAL)
26
27# Add system upgrade to the transaction
28options = {
29    "mode": "distrosync",
30}
31iface_rpm.system_upgrade(options)
32
33# resolve the transaction
34resolved, result = iface_goal.resolve({})
35
36# now you can print the transaction table and ask the user for confirmation
37print("Resolved.")
38
39if result == 0:
40    # execute the transaction offline (durint the next reboot)
41    iface_goal.do_transaction({"offline": True}, timeout=2000)
42    print("Reboot to continue with system upgrade...")
43else:
44    errors = iface_goal.get_transaction_problems_string()
45    print("Errors while resolving the transaction:")
46    for error in errors:
47        print(error)