Skip to content

Commit

Permalink
dnfdaemon: Enhance comps.Group.list() method
Browse files Browse the repository at this point in the history
New accepted options:

- with_hidden - boolean, defaults to false
    whether include hidden (uservisible=false) groups into output
- scope - string, accepted values "all" (default), "installed",
  "available"
- match_group_id - boolean, defaults to true
    whether patterns should be matched against group ids
- match_group_name - boolean, defaults to false
    whether patterns should be matched against group names
- contains_pkgs - list of strings
    only include groups containing given packages

Change in behavior:

- in case the scope is "all" (default), duplicated groups are removed
  from the output. Duplicities basically mean that the group is
  installed and previously both installed and available groups were
  returned.
  • Loading branch information
m-blaha authored and pkratoch committed Apr 3, 2024
1 parent 796789a commit e19cda9
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions dnf5daemon-server/services/comps/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "dbus.hpp"

#include <libdnf5/common/sack/query_cmp.hpp>
#include <libdnf5/comps/group/group.hpp>
#include <libdnf5/comps/group/query.hpp>
#include <sdbus-c++/sdbus-c++.h>
Expand Down Expand Up @@ -131,16 +132,58 @@ sdbus::MethodReply Group::list(sdbus::MethodCall & call) {
session.fill_sack();
auto base = session.get_base();

// patterns to search
std::vector<std::string> patterns =
key_value_map_get<std::vector<std::string>>(options, "patterns", std::vector<std::string>());

libdnf5::comps::GroupQuery query(base->get_weak_ptr());

// patterns to search
const auto patterns = key_value_map_get<std::vector<std::string>>(options, "patterns", std::vector<std::string>());
if (patterns.size() > 0) {
libdnf5::comps::GroupQuery query_names(query);
query_names.filter_name(patterns, libdnf5::sack::QueryCmp::IGLOB);
query.filter_groupid(patterns, libdnf5::sack::QueryCmp::IGLOB);
query |= query_names;
libdnf5::comps::GroupQuery patterns_query(base->get_weak_ptr(), true);
const auto match_group_id = key_value_map_get<bool>(options, "match_group_id", true);
if (match_group_id) {
libdnf5::comps::GroupQuery query_id(query);
query_id.filter_groupid(patterns, libdnf5::sack::QueryCmp::IGLOB);
patterns_query |= query_id;
}
const auto match_group_name = key_value_map_get<bool>(options, "match_group_name", false);
if (match_group_name) {
libdnf5::comps::GroupQuery query_name(query);
query_name.filter_name(patterns, libdnf5::sack::QueryCmp::IGLOB);
patterns_query |= query_name;
}
query = patterns_query;
}

// filter hidden groups
const auto with_hidden = key_value_map_get<bool>(options, "with_hidden", false);
if (!with_hidden) {
query.filter_uservisible(true);
}

const auto scope = key_value_map_get<std::string>(options, "scope", "all");
if (scope == "installed") {
query.filter_installed(true);
} else if (scope == "available") {
query.filter_installed(false);
} else if (scope == "all") {
// to remove duplicities in the output remove from query all available
// groups with the same groupid as any of the installed groups.
libdnf5::comps::GroupQuery query_installed(query);
query_installed.filter_installed(true);
std::vector<std::string> installed_ids;
for (const auto & grp : query_installed) {
installed_ids.emplace_back(grp.get_groupid());
}
libdnf5::comps::GroupQuery query_available(query);
query_available.filter_installed(false);
query_available.filter_groupid(installed_ids);
query -= query_available;
} else {
throw sdbus::Error(dnfdaemon::ERROR, fmt::format("Unsupported scope for group filtering \"{}\".", scope));
}

const auto contains_pkgs = key_value_map_get<std::vector<std::string>>(options, "contains_pkgs", {});
if (!contains_pkgs.empty()) {
query.filter_package_name(contains_pkgs, libdnf5::sack::QueryCmp::IGLOB);
}

// create reply from the query
Expand Down

0 comments on commit e19cda9

Please sign in to comment.