|
25 | 25 |
|
26 | 26 | import subprocess |
27 | 27 | from tracer.packageManagers.rpm import Rpm |
| 28 | + from tracer.resources.package import Package |
| 29 | + from tracer.resources.collections import PackagesCollection |
28 | 30 |
|
29 | 31 | class Dnf(Rpm): |
30 | 32 |
|
@@ -55,3 +57,69 @@ def package_files(self, pkg_name): |
55 | 57 | process = subprocess.Popen(["dnf", "repoquery", "-q", "-l", pkg_name], stdout=subprocess.PIPE) |
56 | 58 | out = process.communicate()[0] |
57 | 59 | return out.decode().split("\n") |
| 60 | + |
| 61 | + |
| 62 | + class FakeDnf5(Dnf): |
| 63 | + def packages_newer_than(self, unix_time): |
| 64 | + return [] |
| 65 | + |
| 66 | + def package_files(self, pkg_name): |
| 67 | + return [] |
| 68 | + |
| 69 | + |
| 70 | + class Dnf5(Dnf): |
| 71 | + def __new__(cls, *args, **kwargs): |
| 72 | + """ |
| 73 | + We are going to enable DNF5 on all Fedora, EPEL, Mageia, openSUSE, |
| 74 | + etc systems and not all of them may have DNF5 (yet). |
| 75 | + """ |
| 76 | + try: |
| 77 | + import libdnf5 |
| 78 | + return super().__new__(cls) |
| 79 | + except ImportError: |
| 80 | + return FakeDnf5(*args, **kwargs) |
| 81 | + |
| 82 | + def packages_newer_than(self, unix_time): |
| 83 | + import libdnf5 |
| 84 | + base = libdnf5.base.Base() |
| 85 | + base.setup() |
| 86 | + |
| 87 | + # Package names as a keys and timestamps as values |
| 88 | + packages = {} |
| 89 | + |
| 90 | + history = base.get_transaction_history() |
| 91 | + for transaction in history.list_all_transactions(): |
| 92 | + if transaction.get_dt_start() < unix_time: |
| 93 | + continue |
| 94 | + for package in transaction.get_packages(): |
| 95 | + packages[package.get_name()] = transaction.get_dt_start() |
| 96 | + |
| 97 | + collection = PackagesCollection() |
| 98 | + for name, timestamp in packages.items(): |
| 99 | + collection.append(Package(name, timestamp)) |
| 100 | + |
| 101 | + return collection |
| 102 | + |
| 103 | + def package_files(self, pkg_name): |
| 104 | + if not self.opts.get("erased"): |
| 105 | + return super(Dnf, self).package_files(pkg_name) |
| 106 | + |
| 107 | + import libdnf5 |
| 108 | + base = libdnf5.base.Base() |
| 109 | + base.setup() |
| 110 | + |
| 111 | + repo_sack = base.get_repo_sack() |
| 112 | + if hasattr(repo_sack, "load_repos"): |
| 113 | + # On F41+, this is what should be done |
| 114 | + repo_sack.load_repos() |
| 115 | + else: |
| 116 | + # On F40 there is only python3-libdnf5-5.1.17 so we have to use |
| 117 | + # this now-deprecated function |
| 118 | + repo_sack.update_and_load_enabled_repos(True) |
| 119 | + |
| 120 | + query = libdnf5.rpm.PackageQuery(base) |
| 121 | + query.filter_name([pkg_name]) |
| 122 | + paths = [] |
| 123 | + for pkg in query: |
| 124 | + paths.extend(pkg.get_files()) |
| 125 | + return paths |
0 commit comments