File: transaction.py

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (65 lines) | stat: -rw-r--r-- 2,267 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Create a goal, which is a class that allows to add items for resolving into
# a transaction.
goal = libdnf5.base.Goal(base)

# Add an RPM package named "one" for installation into the goal.
goal.add_rpm_install("one")

# Resolve the goal, create a transaction object.
#
# The argument is `allow_erasing`, a flag indicating whether to allow removing
# packages in the resolved transaction.
transaction = goal.resolve()

# We can iterate over the resolved transaction and inspect the packages.
print("Resolved transaction:")
for tspkg in transaction.get_transaction_packages():
    print(tspkg.get_package().get_nevra(), ": ",
          libdnf5.base.transaction.transaction_item_action_to_string(tspkg.get_action()))

# This class demonstrates user-defined callbacks for the package downloads.
#
# The callbacks are implemented by inheriting from the callbacks base class
# and overriding its methods.
#
# We only override one of the callbacks here, see
# `libdnf5.repo.DownloadCallbacks` documentation for a complete list.


class PackageDownloadCallbacks(libdnf5.repo.DownloadCallbacks):
    def mirror_failure(self, user_cb_data, msg, url=""):
        print("Mirror failure: ", msg)
        return 0


downloader_callbacks = PackageDownloadCallbacks()
base.set_download_callbacks(
    libdnf5.repo.DownloadCallbacksUniquePtr(downloader_callbacks))

# Download the packages.
transaction.download()

# A class for defining the RPM transaction callbacks.
#
# Again, only a callback for when an RPM package installation starts, for a
# complete list of the callbacks see `libdnf5.rpm.TransactionCallbacks`
# documentation.


class TransactionCallbacks(libdnf5.rpm.TransactionCallbacks):
    def install_start(self, item, total=0):
        print(libdnf5.base.transaction.transaction_item_action_to_string(item.get_action()), " ",
              item.get_package().get_nevra())


transaction_callbacks = TransactionCallbacks()
transaction_callbacks_ptr = libdnf5.rpm.TransactionCallbacksUniquePtr(
    transaction_callbacks)
transaction.set_callbacks(transaction_callbacks_ptr)

# Add transaction metadata to be stored in the history database.
transaction.set_description("install package one")

# Run the transaction.
print("Running the transaction:")
transaction.run()