3.4. Resolving dependencies and running a transaction

This tutorial is a follow-up to Creating and configuring a session and potentially one of Loading repositories (to have some packages to work with), you can prepend them directly for a functional piece of code.

 1# Create a goal, which is a class that allows to add items for resolving into
 2# a transaction.
 3goal = libdnf5.base.Goal(base)
 4
 5# Add an RPM package named "one" for installation into the goal.
 6goal.add_rpm_install("one")
 7
 8# Resolve the goal, create a transaction object.
 9#
10# The argument is `allow_erasing`, a flag indicating whether to allow removing
11# packages in the resolved transaction.
12transaction = goal.resolve()
13
14# We can iterate over the resolved transaction and inspect the packages.
15print("Resolved transaction:")
16for tspkg in transaction.get_transaction_packages():
17    print(tspkg.get_package().get_nevra(), ": ",
18          libdnf5.base.transaction.transaction_item_action_to_string(tspkg.get_action()))
19
20# This class demonstrates user-defined callbacks for the package downloads.
21#
22# The callbacks are implemented by inheriting from the callbacks base class
23# and overriding its methods.
24#
25# We only override one of the callbacks here, see
26# `libdnf5.repo.DownloadCallbacks` documentation for a complete list.
27
28
29class PackageDownloadCallbacks(libdnf5.repo.DownloadCallbacks):
30    def mirror_failure(self, user_cb_data, msg, url=""):
31        print("Mirror failure: ", msg)
32        return 0
33
34
35downloader_callbacks = PackageDownloadCallbacks()
36base.set_download_callbacks(
37    libdnf5.repo.DownloadCallbacksUniquePtr(downloader_callbacks))
38
39# Download the packages.
40transaction.download()
41
42# A class for defining the RPM transaction callbacks.
43#
44# Again, only a callback for when an RPM package installation starts, for a
45# complete list of the callbacks see `libdnf5.rpm.TransactionCallbacks`
46# documentation.
47
48
49class TransactionCallbacks(libdnf5.rpm.TransactionCallbacks):
50    def install_start(self, item, total=0):
51        print(libdnf5.base.transaction.transaction_item_action_to_string(item.get_action()), " ",
52              item.get_package().get_nevra())
53
54
55transaction_callbacks = TransactionCallbacks()
56transaction_callbacks_ptr = libdnf5.rpm.TransactionCallbacksUniquePtr(
57    transaction_callbacks)
58transaction.set_callbacks(transaction_callbacks_ptr)
59
60# Add transaction metadata to be stored in the history database.
61transaction.set_description("install package one")
62
63# Run the transaction.
64print("Running the transaction:")
65transaction.run()