diff --git a/dnf5/commands/download/download.cpp b/dnf5/commands/download/download.cpp index 03491d5ad..30241a24a 100644 --- a/dnf5/commands/download/download.cpp +++ b/dnf5/commands/download/download.cpp @@ -69,6 +69,9 @@ void DownloadCommand::set_argument_parser() { url_option = dynamic_cast( parser.add_init_value(std::unique_ptr(new libdnf5::OptionBool(false)))); + srpm_option = dynamic_cast( + parser.add_init_value(std::unique_ptr(new libdnf5::OptionBool(false)))); + auto resolve = parser.add_new_named_arg("resolve"); resolve->set_long_name("resolve"); resolve->set_description("Resolve and download needed dependencies"); @@ -82,6 +85,12 @@ void DownloadCommand::set_argument_parser() { alldeps->set_const_value("true"); alldeps->link_value(alldeps_option); + auto srpm = parser.add_new_named_arg("srpm"); + srpm->set_long_name("srpm"); + srpm->set_description("Download the src.rpm instead"); + srpm->set_const_value("true"); + srpm->link_value(srpm_option); + auto url = parser.add_new_named_arg("url"); url->set_long_name("url"); url->set_description("Print a URL where the rpms can be downloaded instead of downloading"); @@ -140,6 +149,7 @@ void DownloadCommand::set_argument_parser() { create_destdir_option(*this); cmd.register_named_arg(resolve); cmd.register_positional_arg(keys); + cmd.register_named_arg(srpm); cmd.register_named_arg(url); cmd.register_named_arg(urlprotocol); cmd.register_named_arg(arch); @@ -164,6 +174,11 @@ void DownloadCommand::configure() { } else { context.set_load_system_repo(false); } + + if (srpm_option->get_value()) { + context.base.get_repo_sack()->enable_source_repos(); + } + context.set_load_available_repos(Context::LoadAvailableRepos::ENABLED); // Default destination for downloaded rpms is the current directory context.base.get_config().get_destdir_option().set(libdnf5::Option::Priority::PLUGINDEFAULT, "."); @@ -215,6 +230,38 @@ void DownloadCommand::run() { return; } + if (srpm_option->get_value()) { + std::map source_pkgs; + + libdnf5::rpm::PackageQuery source_pkg_query(ctx.base); + source_pkg_query.filter_arch({"src"}); + source_pkg_query.filter_available(); + + for (auto & [nevra, pkg] : download_pkgs) { + auto sourcerpm = pkg.get_sourcerpm(); + + if (!sourcerpm.empty()) { + libdnf5::rpm::PackageQuery pkg_query(source_pkg_query); + pkg_query.filter_epoch({pkg.get_epoch()}); + + // Remove ".rpm" to get sourcerpm nevra + sourcerpm.erase(sourcerpm.length() - 4); + pkg_query.resolve_pkg_spec(sourcerpm, {}, true); + + for (const auto & spkg : pkg_query) { + source_pkgs.insert(create_nevra_pkg_pair(spkg)); + } + } else if (pkg.get_arch() == "src") { + source_pkgs.insert(create_nevra_pkg_pair(pkg)); + } else { + ctx.base.get_logger()->info("No source rpm defined for package: \"{}\"", pkg.get_name()); + continue; + } + } + + download_pkgs = source_pkgs; + } + if (url_option->get_value()) { // If no urlprotocols are specified, all values within the urlprotocol_valid_options will be used if (urlprotocol_option.empty()) { diff --git a/dnf5/commands/download/download.hpp b/dnf5/commands/download/download.hpp index 6a793f933..e7a564ec3 100644 --- a/dnf5/commands/download/download.hpp +++ b/dnf5/commands/download/download.hpp @@ -47,6 +47,7 @@ class DownloadCommand : public Command { libdnf5::OptionBool * resolve_option{nullptr}; libdnf5::OptionBool * alldeps_option{nullptr}; libdnf5::OptionBool * url_option{nullptr}; + libdnf5::OptionBool * srpm_option{nullptr}; std::vector> * patterns_to_download_options{nullptr}; }; diff --git a/doc/changes.rst b/doc/changes.rst index 8f7215e19..3a733d8f3 100644 --- a/doc/changes.rst +++ b/doc/changes.rst @@ -106,6 +106,10 @@ Downgrade command * When any argument does not match any package or it is not installed, DNF5 fail. The behavior can be modified by the ``--skip-unavailable`` option. +Download command +---------------- + * Option ``--source`` was renamed to ``--srpm``. + Group command ------------- * Dropped ``group mark install`` and ``group mark remove`` subcommands in favour of the diff --git a/doc/commands/download.8.rst b/doc/commands/download.8.rst index 866b7022b..dce4ebf0f 100644 --- a/doc/commands/download.8.rst +++ b/doc/commands/download.8.rst @@ -56,6 +56,8 @@ Options ``--arch`` | Limit to packages of given architectures. This option can be used multiple times. +``--srpm`` + | Download the source rpm. Enables source repositories of all enabled binary repositories. Examples ======== @@ -78,6 +80,8 @@ Examples ``dnf5 download python --arch x86_64`` | Downloads python with the ``x86_64`` architecture. +``dnf5 download dnf5 --srpm`` + | Download the ``dnf5`` source rpm. See Also ========