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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// 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 Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "fmt/args.h"
#include "utils/string.hpp"
#include "libdnf5-cli/output/repoquery.hpp"
#include <libdnf5/common/exception.hpp>
#include <libdnf5/utils/bgettext/bgettext-mark-domain.h>
#include <set>
#include <variant>
namespace libdnf5::cli::output {
namespace {
using StrGetter = std::string (libdnf5::rpm::Package::*)() const;
using VecStrGetter = std::vector<std::string> (libdnf5::rpm::Package::*)() const;
using UnsignedLongLongGetter = unsigned long long (libdnf5::rpm::Package::*)() const;
using ReldepListGetter = libdnf5::rpm::ReldepList (libdnf5::rpm::Package::*)() const;
using TransactionItemReasonGetter = libdnf5::transaction::TransactionItemReason (libdnf5::rpm::Package::*)() const;
using StrGetterLambda = std::function<std::string(const libdnf5::rpm::Package &)>;
using Getter = std::variant<
StrGetter,
VecStrGetter,
UnsignedLongLongGetter,
ReldepListGetter,
TransactionItemReasonGetter,
StrGetterLambda>;
const std::unordered_map<std::string, Getter> NAME_TO_GETTER = {
{"name", &libdnf5::rpm::Package::get_name},
{"epoch", &libdnf5::rpm::Package::get_epoch},
{"version", &libdnf5::rpm::Package::get_version},
{"release", &libdnf5::rpm::Package::get_release},
{"arch", &libdnf5::rpm::Package::get_arch},
{"evr", &libdnf5::rpm::Package::get_evr},
{"full_nevra", &libdnf5::rpm::Package::get_full_nevra},
{"group", &libdnf5::rpm::Package::get_group},
{"downloadsize", &libdnf5::rpm::Package::get_download_size},
{"installsize", &libdnf5::rpm::Package::get_install_size},
{"license", &libdnf5::rpm::Package::get_license},
{"source_name", &libdnf5::rpm::Package::get_source_name},
{"sourcerpm", &libdnf5::rpm::Package::get_sourcerpm},
{"buildtime", &libdnf5::rpm::Package::get_build_time},
{"packager", &libdnf5::rpm::Package::get_packager},
{"vendor", &libdnf5::rpm::Package::get_vendor},
{"url", &libdnf5::rpm::Package::get_url},
{"summary", &libdnf5::rpm::Package::get_summary},
{"description", &libdnf5::rpm::Package::get_description},
{"provides", &libdnf5::rpm::Package::get_provides},
{"requires", &libdnf5::rpm::Package::get_requires},
{"requires_pre", &libdnf5::rpm::Package::get_requires_pre},
{"conflicts", &libdnf5::rpm::Package::get_conflicts},
{"obsoletes", &libdnf5::rpm::Package::get_obsoletes},
{"prereq_ignoreinst", &libdnf5::rpm::Package::get_prereq_ignoreinst},
{"regular_requires", &libdnf5::rpm::Package::get_regular_requires},
{"recommends", &libdnf5::rpm::Package::get_recommends},
{"suggests", &libdnf5::rpm::Package::get_suggests},
{"enhances", &libdnf5::rpm::Package::get_enhances},
{"supplements", &libdnf5::rpm::Package::get_supplements},
{"depends", &libdnf5::rpm::Package::get_depends},
{"from_repo", &libdnf5::rpm::Package::get_from_repo_id},
{"installtime", &libdnf5::rpm::Package::get_install_time},
{"repoid", &libdnf5::rpm::Package::get_repo_id},
{"reponame", &libdnf5::rpm::Package::get_repo_name},
{"reason", &libdnf5::rpm::Package::get_reason},
{"debug_name", &libdnf5::rpm::Package::get_debuginfo_name},
{"source_debug_name", &libdnf5::rpm::Package::get_debuginfo_name_of_source},
{"files", &libdnf5::rpm::Package::get_files},
{"location",
[](const libdnf5::rpm::Package & pkg) -> std::string {
auto locs = pkg.get_remote_locations();
if (locs.empty()) {
return {};
}
return locs[0];
}},
};
// Argument format contains partially copied and converted queryformat, for example: "name: %-30{{name".
// We know the tag "name" is valid so this function check align spec "-30" and if it is valid
// it overwrites the format with fmt formatting: "name: {:<30}".
bool replace_tag_in_format(
std::string & format,
std::string::size_type & format_size,
std::string::size_type tag_start,
std::string::size_type tag_name_start) {
std::string align_spec;
// Check if there is valid align spec between '%' and '{' -> ..%-30{...
if (tag_start + 1 != tag_name_start) {
align_spec = format.substr(tag_start + 1, tag_name_start - tag_start - 1);
auto iter = align_spec.begin();
// first char can be either a digit or '-' sign
if ((*iter != '-') && (std::isdigit(*iter) == 0)) {
return false;
}
// verify the rest is only digits
++iter;
while (iter != align_spec.end() && (std::isdigit(*iter) != 0)) {
++iter;
}
if (iter != align_spec.end()) {
return false;
}
}
// Since we use positional args for fmt::vprint we have to remove the tag name.
// We do this by overwriting the previously copied tag.
format_size = tag_start;
format[format_size] = '{';
format_size++;
if (!align_spec.empty()) {
format[format_size] = ':';
format_size++;
auto iter = align_spec.begin();
if (*iter == '-') {
format[format_size] = '<';
iter++; //skip the sign
} else {
format[format_size] = '>';
}
format_size++;
while (iter != align_spec.end()) {
format[format_size] = *iter;
format_size++;
++iter;
}
}
format[format_size] = '}';
format_size++;
return true;
}
std::pair<std::vector<Getter>, std::string> parse_queryformat(const std::string & queryformat) {
std::vector<Getter> getters;
std::string format;
// format max possible len is 2 * queryformat.size() (if it contained just curly braces)
format.resize(2 * queryformat.size());
std::string::size_type format_size = 0;
std::string::size_type tag_start = 0;
std::string::size_type tag_name_start = 0;
char previous_qf_char = 0;
enum State { OUTSIDE, IN_TAG, IN_TAG_NAME };
State state = OUTSIDE;
// Prepare format string and getters.
// We try to match rpm query formatting.
// For example: "%-30{name}%{evr}" needs to be converted to "{:<30}{}" that is
// two positional fmt arguments (first one with align formatting) and we need
// get_name() and get_evr() getters.
for (char qf_char : queryformat) {
if (qf_char == '%') { // start of tag
state = IN_TAG;
tag_start = format_size;
} else if (qf_char == '{' && state == IN_TAG) { // start of tag name
state = IN_TAG_NAME;
tag_name_start = format_size;
} else if (qf_char == '}' && state == IN_TAG_NAME) { // end of tag
state = OUTSIDE;
// To get the name we add/subtract 2 because each tag name (after brace expansion) starts with "{{".
auto getter_name = format.substr(tag_name_start + 2, format_size - tag_name_start - 2);
auto getter = NAME_TO_GETTER.find(libdnf5::utils::string::tolower(getter_name));
if (getter != NAME_TO_GETTER.end()) {
if (replace_tag_in_format(format, format_size, tag_start, tag_name_start)) {
getters.push_back(getter->second);
continue; // continue to skip adding the current qf_char ('}')
}
}
}
// Copy each char of queryformat to format string with required changes.
if (qf_char == '{' || qf_char == '}') {
// double the brace -> escaping for fmt
format[format_size] = qf_char;
format_size++;
format[format_size] = qf_char;
format_size++;
} else if (previous_qf_char == '\\' && qf_char == 'n') {
// replace new lines, two characters in input: '\' 'n' -> '\n'
format[format_size - 1] = '\n'; //replace the previous '\'
} else {
format[format_size] = qf_char;
format_size++;
}
previous_qf_char = qf_char;
}
// Resize the format to resulting size to trim excess characters.
format.resize(format_size);
return {getters, format};
}
} // namespace
bool requires_filelists(const std::string & queryformat) {
auto [getters, _] = parse_queryformat(queryformat);
for (auto getter : getters) {
auto * getter_pointer = std::get_if<VecStrGetter>(&getter);
if (getter_pointer && (*getter_pointer == &libdnf5::rpm::Package::get_files)) {
return true;
}
}
return false;
}
void print_pkg_set_with_format(
std::FILE * target, const libdnf5::rpm::PackageSet & pkgs, const std::string & queryformat) {
auto [getters, format] = parse_queryformat(queryformat);
std::set<std::string> output;
// Format and print each package.
fmt::dynamic_format_arg_store<fmt::format_context> arg_store;
arg_store.reserve(getters.size(), getters.size());
for (auto package : pkgs) {
arg_store.clear();
for (const auto & getter : getters) {
std::visit(
[&arg_store, &package](const auto & getter_func) {
using T = std::decay_t<decltype(getter_func)>;
if constexpr (std::is_same_v<T, ReldepListGetter>) {
std::string joined;
for (const auto & reldep : (package.*getter_func)()) {
joined.append(std::move(reldep.to_string()));
joined.push_back('\n');
}
arg_store.push_back(joined);
} else if constexpr (std::is_same_v<T, VecStrGetter>) {
std::string joined;
for (const auto & str : (package.*getter_func)()) {
joined.append(std::move(str));
joined.push_back('\n');
}
arg_store.push_back(joined);
} else if constexpr (std::is_same_v<T, TransactionItemReasonGetter>) {
arg_store.push_back(std::move(transaction_item_reason_to_string((package.*getter_func)())));
} else if constexpr (std::is_same_v<T, StrGetterLambda>) {
arg_store.push_back((getter_func)(package));
} else {
arg_store.push_back((package.*getter_func)());
}
},
getter);
}
output.insert(fmt::vformat(format, arg_store));
}
for (const auto & line : output) {
fmt::print(target, "{}", line);
}
}
void print_pkg_attr_uniq_sorted(
std::FILE * target, const libdnf5::rpm::PackageSet & pkgs, const std::string & getter_name) {
auto getter = NAME_TO_GETTER.find(getter_name);
if (getter == NAME_TO_GETTER.end()) {
libdnf_throw_assertion("Package attribute getter: \"{}\" not available", getter_name);
}
std::set<std::string> output;
for (auto package : pkgs) {
std::visit(
[&output, &package](const auto & getter_func) {
using T = std::decay_t<decltype(getter_func)>;
if constexpr (std::is_same_v<T, ReldepListGetter>) {
for (const auto & reldep : (package.*getter_func)()) {
output.insert(std::move(reldep.to_string()));
}
} else if constexpr (std::is_same_v<T, UnsignedLongLongGetter>) {
output.insert(std::move(std::to_string((package.*getter_func)())));
} else if constexpr (std::is_same_v<T, TransactionItemReasonGetter>) {
output.insert(std::move(transaction_item_reason_to_string((package.*getter_func)())));
} else if constexpr (std::is_same_v<T, VecStrGetter>) {
for (const auto & str : (package.*getter_func)()) {
output.insert(std::move(str));
}
} else if constexpr (std::is_same_v<T, StrGetterLambda>) {
output.insert(std::move((getter_func)(package)));
} else {
output.insert(std::move((package.*getter_func)()));
}
},
getter->second);
}
for (const auto & line : output) {
fmt::print(target, "{}\n", line);
}
}
void print_available_pkg_attrs(std::FILE * target) {
std::set<std::string> output;
for (const auto & pair : NAME_TO_GETTER) {
output.insert(pair.first);
}
for (const auto & line : output) {
fmt::print(target, "{}\n", line);
}
}
libdnf5::rpm::ReldepList get_reldeplist_for_attr(
const libdnf5::rpm::PackageSet & pkgs, const std::string & getter_name) {
auto getter = NAME_TO_GETTER.find(getter_name);
if (getter == NAME_TO_GETTER.end()) {
libdnf_throw_assertion("Package attribute getter: \"{}\" not available", getter_name);
}
rpm::ReldepList output(pkgs.get_base());
for (auto package : pkgs) {
std::visit(
[&output, &package, &getter_name](const auto & getter_func) {
using T = std::decay_t<decltype(getter_func)>;
if constexpr (std::is_same_v<T, ReldepListGetter>) {
auto rlds = (package.*getter_func)();
output.append(rlds);
} else {
libdnf_throw_assertion("Cannot get pkg attribute: \"{}\" as ReldepList", getter_name);
}
},
getter->second);
}
return output;
}
} // namespace libdnf5::cli::output
|