Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

daemon: New API to get download size of pkg including deps #1626

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions dnf5daemon-server/services/rpm/rpm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "dbus.hpp"
#include "package.hpp"

#include <libdnf5/base/goal_elements.hpp>
#include <libdnf5/rpm/package_query.hpp>
#include <libdnf5/rpm/package_set.hpp>
#include <sdbus-c++/sdbus-c++.h>
Expand Down Expand Up @@ -123,6 +124,16 @@ void Rpm::dbus_register() {
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(*this, &Rpm::system_upgrade, call, session.session_locale);
});
dbus_object->registerMethod(
dnfdaemon::INTERFACE_RPM,
"check_install",
"sa{sv}",
{"pkg_spec", "options"},
"aa{sv}",
{"packages"}, // inbound packages with {reason, name, epoch, version, release, evr, arch, install size, download size}
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(*this, &Rpm::check_install, call, session.session_locale);
});

dbus_object->registerSignal(
dnfdaemon::INTERFACE_RPM, dnfdaemon::SIGNAL_TRANSACTION_BEFORE_BEGIN, "ot", {"session_object_path", "total"});
Expand Down Expand Up @@ -676,3 +687,44 @@ sdbus::MethodReply Rpm::system_upgrade(sdbus::MethodCall & call) {
auto reply = call.createReply();
return reply;
}

sdbus::MethodReply Rpm::check_install(sdbus::MethodCall & call) {
auto base = session.get_base();
session.fill_sack();

// read options from dbus call
std::string pkg_spec;
call >> pkg_spec;
dnfdaemon::KeyValueMap options;
call >> options;

dnfdaemon::KeyValueMapList out_packages;
libdnf5::Goal goal(*base);
libdnf5::GoalJobSettings settings;
goal.add_install(pkg_spec, settings);
auto transaction = goal.resolve();
if (transaction.get_problems() == libdnf5::GoalProblem::NO_PROBLEM) {
std::vector<std::string> pkg_attrs{
"name",
"epoch",
"version",
"release",
"arch",
"repo_id",
"download_size",
"install_size",
"evr",
};
for (auto & tspkg : transaction.get_transaction_packages()) {
if (transaction_item_action_is_inbound(tspkg.get_action())) {
auto pkgmap = package_to_map(tspkg.get_package(), pkg_attrs);
pkgmap["install_reason"] = transaction_item_reason_to_string(tspkg.get_reason());
out_packages.emplace_back(std::move(pkgmap));
}
}
}

auto reply = call.createReply();
reply << out_packages;
return reply;
}
1 change: 1 addition & 0 deletions dnf5daemon-server/services/rpm/rpm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Rpm : public IDbusSessionService {
sdbus::MethodReply downgrade(sdbus::MethodCall & call);
sdbus::MethodReply reinstall(sdbus::MethodCall & call);
sdbus::MethodReply system_upgrade(sdbus::MethodCall & call);
sdbus::MethodReply check_install(sdbus::MethodCall & call);

void list_fd(sdbus::MethodCall & call, const std::string & transfer_id);
};
Expand Down
67 changes: 67 additions & 0 deletions doc/dnf_daemon/examples/check_install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/python3
# Copyright Contributors to the libdnf project.
#
# This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
#
# Libdnf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Libdnf is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libdnf. If not, see <https://www.gnu.org/licenses/>.

import dbus


def human_readable_size(size):
for unit in ['B', 'KiB', 'MiB', 'GiB']:
if size < 1024.0 or unit == 'GiB':
break
size /= 1024.0
return f"{size:.2f} {unit}"


DNFDAEMON_BUS_NAME = 'org.rpm.dnf.v0'
DNFDAEMON_OBJECT_PATH = '/' + DNFDAEMON_BUS_NAME.replace('.', '/')

IFACE_SESSION_MANAGER = '{}.SessionManager'.format(DNFDAEMON_BUS_NAME)
IFACE_RPM = '{}.rpm.Rpm'.format(DNFDAEMON_BUS_NAME)


bus = dbus.SystemBus()
iface_session = dbus.Interface(
bus.get_object(DNFDAEMON_BUS_NAME, DNFDAEMON_OBJECT_PATH),
dbus_interface=IFACE_SESSION_MANAGER)

# set the releasever to the new distribution release
session = iface_session.open_session(
dbus.Dictionary({}, signature=dbus.Signature('sv')))

iface_rpm = dbus.Interface(
bus.get_object(DNFDAEMON_BUS_NAME, session),
dbus_interface=IFACE_RPM)

# Add system upgrade to the transaction
options = {
}
packages = iface_rpm.check_install("0ad", options)

print("Following packages will be downloaded and installed:")
total_download_size = 0
total_install_size = 0
for pkg in packages:
print(pkg["install_reason"], pkg["name"], pkg["evr"])
print(" download size: {}, install size: {}".format(human_readable_size(
pkg["download_size"]), human_readable_size(pkg["install_size"])))
total_download_size += pkg["download_size"]
total_install_size += pkg["install_size"]

print("Need to download {}.".format(human_readable_size(total_download_size)))
print("Instalation will require additional {}.".format(
human_readable_size(total_install_size)))
Loading