Blame SOURCES/0008-Add-repoquery-command.patch

d469b3
From 1ded5d5babfe620488aa8965c7fb922361fa6eaa Mon Sep 17 00:00:00 2001
d469b3
From: Jaroslav Rohel <jrohel@redhat.com>
d469b3
Date: Fri, 8 Nov 2019 13:27:17 +0100
d469b3
Subject: [PATCH 1/2] Add repoquery command (RhBug:1769245)
d469b3
d469b3
Searches for selected packages and displays the requested information
d469b3
about them.
d469b3
d469b3
Command options:
d469b3
  --available   display available packages (default)
d469b3
  --installed   display installed packages
d469b3
d469b3
Signed-off-by: Jaroslav Rohel <jrohel@redhat.com>
d469b3
---
d469b3
 dnf/CMakeLists.txt                            |   6 +
d469b3
 dnf/meson.build                               |   9 +
d469b3
 dnf/plugins/repoquery/dnf-command-repoquery.c | 158 ++++++++++++++++++
d469b3
 .../dnf-command-repoquery.gresource.xml       |   6 +
d469b3
 dnf/plugins/repoquery/dnf-command-repoquery.h |  33 ++++
d469b3
 dnf/plugins/repoquery/repoquery.plugin        |   9 +
d469b3
 6 files changed, 221 insertions(+)
d469b3
 create mode 100644 dnf/plugins/repoquery/dnf-command-repoquery.c
d469b3
 create mode 100644 dnf/plugins/repoquery/dnf-command-repoquery.gresource.xml
d469b3
 create mode 100644 dnf/plugins/repoquery/dnf-command-repoquery.h
d469b3
 create mode 100644 dnf/plugins/repoquery/repoquery.plugin
d469b3
d469b3
diff --git a/dnf/CMakeLists.txt b/dnf/CMakeLists.txt
d469b3
index eb73c11..2585c06 100644
d469b3
--- a/dnf/CMakeLists.txt
d469b3
+++ b/dnf/CMakeLists.txt
d469b3
@@ -20,6 +20,11 @@ glib_compile_resources (DNF_COMMAND_REPOLIST plugins/repolist/dnf-command-repoli
d469b3
                         INTERNAL)
d469b3
 list (APPEND DNF_COMMAND_REPOLIST "plugins/repolist/dnf-command-repolist.c")
d469b3
 
d469b3
+glib_compile_resources (DNF_COMMAND_REPOQUERY plugins/repoquery/dnf-command-repoquery.gresource.xml
d469b3
+                        C_PREFIX dnf_command_repoquery
d469b3
+                        INTERNAL)
d469b3
+list (APPEND DNF_COMMAND_REPOQUERY "plugins/repoquery/dnf-command-repoquery.c")
d469b3
+
d469b3
 glib_compile_resources (DNF_COMMAND_CLEAN plugins/clean/dnf-command-clean.gresource.xml
d469b3
                         C_PREFIX dnf_command_clean
d469b3
                         INTERNAL)
d469b3
@@ -31,6 +36,7 @@ add_executable (microdnf dnf-main.c ${DNF_SRCS}
d469b3
                 ${DNF_COMMAND_REMOVE}
d469b3
                 ${DNF_COMMAND_UPDATE}
d469b3
                 ${DNF_COMMAND_REPOLIST}
d469b3
+                ${DNF_COMMAND_REPOQUERY}
d469b3
                 ${DNF_COMMAND_CLEAN})
d469b3
 
d469b3
 target_link_libraries (microdnf
d469b3
diff --git a/dnf/meson.build b/dnf/meson.build
d469b3
index d368180..d71a533 100644
d469b3
--- a/dnf/meson.build
d469b3
+++ b/dnf/meson.build
d469b3
@@ -39,6 +39,15 @@ microdnf_srcs = [
d469b3
   ),
d469b3
   'plugins/repolist/dnf-command-repolist.c',
d469b3
 
d469b3
+  # repoquery
d469b3
+  gnome.compile_resources(
d469b3
+    'dnf-repoquery',
d469b3
+    'plugins/repoquery/dnf-command-repoquery.gresource.xml',
d469b3
+    c_name : 'dnf_command_repoquery',
d469b3
+    source_dir : 'plugins/repoquery',
d469b3
+  ),
d469b3
+  'plugins/repoquery/dnf-command-repoquery.c',
d469b3
+
d469b3
   # clean
d469b3
   gnome.compile_resources(
d469b3
     'dnf-clean',
d469b3
diff --git a/dnf/plugins/repoquery/dnf-command-repoquery.c b/dnf/plugins/repoquery/dnf-command-repoquery.c
d469b3
new file mode 100644
d469b3
index 0000000..7db1a8f
d469b3
--- /dev/null
d469b3
+++ b/dnf/plugins/repoquery/dnf-command-repoquery.c
d469b3
@@ -0,0 +1,158 @@
d469b3
+/*
d469b3
+ * Copyright (C) 2019 Red Hat, Inc.
d469b3
+ *
d469b3
+ * Licensed under the GNU Lesser General Public License Version 2.1
d469b3
+ *
d469b3
+ * This library is free software; you can redistribute it and/or
d469b3
+ * modify it under the terms of the GNU Lesser General Public
d469b3
+ * License as published by the Free Software Foundation; either
d469b3
+ * version 2.1 of the License, or (at your option) any later version.
d469b3
+ *
d469b3
+ * This library is distributed in the hope that it will be useful,
d469b3
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
d469b3
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d469b3
+ * Lesser General Public License for more details.
d469b3
+ *
d469b3
+ * You should have received a copy of the GNU Lesser General Public
d469b3
+ * License along with this library; if not, write to the Free Software
d469b3
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d469b3
+ */
d469b3
+
d469b3
+#include "dnf-command-repoquery.h"
d469b3
+
d469b3
+struct _DnfCommandRepoquery
d469b3
+{
d469b3
+  PeasExtensionBase parent_instance;
d469b3
+};
d469b3
+
d469b3
+static void dnf_command_repoquery_iface_init (DnfCommandInterface *iface);
d469b3
+
d469b3
+G_DEFINE_DYNAMIC_TYPE_EXTENDED (DnfCommandRepoquery,
d469b3
+                                dnf_command_repoquery,
d469b3
+                                PEAS_TYPE_EXTENSION_BASE,
d469b3
+                                0,
d469b3
+                                G_IMPLEMENT_INTERFACE (DNF_TYPE_COMMAND,
d469b3
+                                                       dnf_command_repoquery_iface_init))
d469b3
+
d469b3
+static void
d469b3
+dnf_command_repoquery_init (DnfCommandRepoquery *self)
d469b3
+{
d469b3
+}
d469b3
+
d469b3
+static void
d469b3
+disable_available_repos (DnfContext *ctx)
d469b3
+{
d469b3
+  GPtrArray *repos = dnf_context_get_repos (ctx);
d469b3
+  for (guint i = 0; i < repos->len; ++i)
d469b3
+    {
d469b3
+      DnfRepo * repo = g_ptr_array_index (repos, i);
d469b3
+      dnf_repo_set_enabled (repo, DNF_REPO_ENABLED_NONE);
d469b3
+    }
d469b3
+}
d469b3
+
d469b3
+static gint
d469b3
+gptrarr_dnf_package_cmp (gconstpointer a, gconstpointer b)
d469b3
+{
d469b3
+  return dnf_package_cmp(*(DnfPackage**)a, *(DnfPackage**)b);
d469b3
+}
d469b3
+
d469b3
+static gboolean
d469b3
+dnf_command_repoquery_run (DnfCommand     *cmd,
d469b3
+                          int              argc,
d469b3
+                          char            *argv[],
d469b3
+                          GOptionContext  *opt_ctx,
d469b3
+                          DnfContext      *ctx,
d469b3
+                          GError         **error)
d469b3
+{
d469b3
+  gboolean opt_available = FALSE;
d469b3
+  gboolean opt_installed = FALSE;
d469b3
+  g_auto(GStrv) opt_key = NULL;
d469b3
+  const GOptionEntry opts[] = {
d469b3
+    { "available", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_available, "display available packages (default)", NULL },
d469b3
+    { "installed", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_installed, "display installed packages", NULL },
d469b3
+    { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &opt_key, NULL, NULL },
d469b3
+    { NULL }
d469b3
+  };
d469b3
+  g_option_context_add_main_entries (opt_ctx, opts, NULL);
d469b3
+
d469b3
+  if (!g_option_context_parse (opt_ctx, &argc, &argv, error))
d469b3
+    return FALSE;
d469b3
+
d469b3
+  // --available is default (compatibility with YUM/DNF)
d469b3
+  if (!opt_available && !opt_installed)
d469b3
+    opt_available = TRUE;
d469b3
+
d469b3
+  if (opt_available && opt_installed)
d469b3
+    opt_available = opt_installed = FALSE;
d469b3
+
d469b3
+  if (opt_installed)
d469b3
+    disable_available_repos (ctx);
d469b3
+
d469b3
+  DnfState * state = dnf_context_get_state (ctx);
d469b3
+  DnfContextSetupSackFlags sack_flags = opt_available ? DNF_CONTEXT_SETUP_SACK_FLAG_SKIP_RPMDB
d469b3
+                                                      : DNF_CONTEXT_SETUP_SACK_FLAG_NONE;
d469b3
+  dnf_context_setup_sack_with_flags (ctx, state, sack_flags, error);
d469b3
+  DnfSack *sack = dnf_context_get_sack (ctx);
d469b3
+
d469b3
+  hy_autoquery HyQuery query = hy_query_create (sack);
d469b3
+
d469b3
+  if (opt_key)
d469b3
+    {
d469b3
+      hy_query_filter_empty (query);
d469b3
+      for (char **pkey = opt_key; *pkey; ++pkey)
d469b3
+        {
d469b3
+          g_auto(HySubject) subject = hy_subject_create (*pkey);
d469b3
+          HyNevra out_nevra;
d469b3
+          hy_autoquery HyQuery key_query = hy_subject_get_best_solution (subject, sack, NULL,
d469b3
+            &out_nevra, TRUE, TRUE, FALSE, TRUE, TRUE);
d469b3
+          if (out_nevra)
d469b3
+            hy_nevra_free(out_nevra);
d469b3
+          hy_query_union (query, key_query);
d469b3
+        }
d469b3
+    }
d469b3
+
d469b3
+  g_autoptr(GPtrArray) pkgs = hy_query_run (query);
d469b3
+
d469b3
+  g_ptr_array_sort (pkgs, gptrarr_dnf_package_cmp);
d469b3
+
d469b3
+  // print packages without duplicated lines
d469b3
+  const char *prev_line = "";
d469b3
+  for (guint i = 0; i < pkgs->len; ++i)
d469b3
+    {
d469b3
+      DnfPackage *package = g_ptr_array_index (pkgs, i);
d469b3
+      const char * line = dnf_package_get_nevra (package);
d469b3
+      if (strcmp (line, prev_line) != 0)
d469b3
+        {
d469b3
+          g_print ("%s\n", line);
d469b3
+          prev_line = line;
d469b3
+        }
d469b3
+    }
d469b3
+
d469b3
+  return TRUE;
d469b3
+}
d469b3
+
d469b3
+static void
d469b3
+dnf_command_repoquery_class_init (DnfCommandRepoqueryClass *klass)
d469b3
+{
d469b3
+}
d469b3
+
d469b3
+static void
d469b3
+dnf_command_repoquery_iface_init (DnfCommandInterface *iface)
d469b3
+{
d469b3
+  iface->run = dnf_command_repoquery_run;
d469b3
+}
d469b3
+
d469b3
+static void
d469b3
+dnf_command_repoquery_class_finalize (DnfCommandRepoqueryClass *klass)
d469b3
+{
d469b3
+}
d469b3
+
d469b3
+G_MODULE_EXPORT void
d469b3
+dnf_command_repoquery_register_types (PeasObjectModule *module)
d469b3
+{
d469b3
+  dnf_command_repoquery_register_type (G_TYPE_MODULE (module));
d469b3
+
d469b3
+  peas_object_module_register_extension_type (module,
d469b3
+                                              DNF_TYPE_COMMAND,
d469b3
+                                              DNF_TYPE_COMMAND_REPOQUERY);
d469b3
+}
d469b3
diff --git a/dnf/plugins/repoquery/dnf-command-repoquery.gresource.xml b/dnf/plugins/repoquery/dnf-command-repoquery.gresource.xml
d469b3
new file mode 100644
d469b3
index 0000000..6765af2
d469b3
--- /dev/null
d469b3
+++ b/dnf/plugins/repoquery/dnf-command-repoquery.gresource.xml
d469b3
@@ -0,0 +1,6 @@
d469b3
+
d469b3
+<gresources>
d469b3
+  <gresource prefix="/org/fedoraproject/dnf/plugins/repoquery">
d469b3
+    <file>repoquery.plugin</file>
d469b3
+  </gresource>
d469b3
+</gresources>
d469b3
diff --git a/dnf/plugins/repoquery/dnf-command-repoquery.h b/dnf/plugins/repoquery/dnf-command-repoquery.h
d469b3
new file mode 100644
d469b3
index 0000000..80fae53
d469b3
--- /dev/null
d469b3
+++ b/dnf/plugins/repoquery/dnf-command-repoquery.h
d469b3
@@ -0,0 +1,33 @@
d469b3
+/*
d469b3
+ * Copyright (C) 2019 Red Hat, Inc.
d469b3
+ *
d469b3
+ * Licensed under the GNU Lesser General Public License Version 2.1
d469b3
+ *
d469b3
+ * This library is free software; you can redistribute it and/or
d469b3
+ * modify it under the terms of the GNU Lesser General Public
d469b3
+ * License as published by the Free Software Foundation; either
d469b3
+ * version 2.1 of the License, or (at your option) any later version.
d469b3
+ *
d469b3
+ * This library is distributed in the hope that it will be useful,
d469b3
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
d469b3
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d469b3
+ * Lesser General Public License for more details.
d469b3
+ *
d469b3
+ * You should have received a copy of the GNU Lesser General Public
d469b3
+ * License along with this library; if not, write to the Free Software
d469b3
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d469b3
+ */
d469b3
+
d469b3
+#pragma once
d469b3
+
d469b3
+#include "dnf-command.h"
d469b3
+#include <libpeas/peas.h>
d469b3
+
d469b3
+G_BEGIN_DECLS
d469b3
+
d469b3
+#define DNF_TYPE_COMMAND_REPOQUERY dnf_command_repoquery_get_type ()
d469b3
+G_DECLARE_FINAL_TYPE (DnfCommandRepoquery, dnf_command_repoquery, DNF, COMMAND_REPOQUERY, PeasExtensionBase)
d469b3
+
d469b3
+G_MODULE_EXPORT void dnf_command_repoquery_register_types (PeasObjectModule *module);
d469b3
+
d469b3
+G_END_DECLS
d469b3
diff --git a/dnf/plugins/repoquery/repoquery.plugin b/dnf/plugins/repoquery/repoquery.plugin
d469b3
new file mode 100644
d469b3
index 0000000..a107720
d469b3
--- /dev/null
d469b3
+++ b/dnf/plugins/repoquery/repoquery.plugin
d469b3
@@ -0,0 +1,9 @@
d469b3
+[Plugin]
d469b3
+Module = command_repoquery
d469b3
+Embedded = dnf_command_repoquery_register_types
d469b3
+Name = repoquery
d469b3
+Description = Search for packages matching keyword
d469b3
+Authors = Jaroslav Rohel <jrohel@redhat.com>
d469b3
+License = GPL-3.0+
d469b3
+Copyright = Copyright (C) 2019 Red Hat, Inc.
d469b3
+X-Command-Syntax = repoquery [OPTION…] [KEY…]
d469b3
-- 
d469b3
2.21.0
d469b3
d469b3
d469b3
From 99831d883f2a95f3540a844fe8455f896b9b097d Mon Sep 17 00:00:00 2001
d469b3
From: Jaroslav Rohel <jrohel@redhat.com>
d469b3
Date: Sun, 10 Nov 2019 15:32:31 +0100
d469b3
Subject: [PATCH 2/2] [repoquery] add "--info" and "--nevra" options
d469b3
d469b3
--info   show detailed information about the packages
d469b3
--nevra  use name-epoch:version-release.architecture format
d469b3
         for displaying packages (default)
d469b3
d469b3
Signed-off-by: Jaroslav Rohel <jrohel@redhat.com>
d469b3
---
d469b3
 dnf/plugins/repoquery/dnf-command-repoquery.c | 68 +++++++++++++++++--
d469b3
 1 file changed, 63 insertions(+), 5 deletions(-)
d469b3
d469b3
diff --git a/dnf/plugins/repoquery/dnf-command-repoquery.c b/dnf/plugins/repoquery/dnf-command-repoquery.c
d469b3
index 7db1a8f..721c990 100644
d469b3
--- a/dnf/plugins/repoquery/dnf-command-repoquery.c
d469b3
+++ b/dnf/plugins/repoquery/dnf-command-repoquery.c
d469b3
@@ -20,6 +20,8 @@
d469b3
 
d469b3
 #include "dnf-command-repoquery.h"
d469b3
 
d469b3
+#include <libsmartcols.h>
d469b3
+
d469b3
 struct _DnfCommandRepoquery
d469b3
 {
d469b3
   PeasExtensionBase parent_instance;
d469b3
@@ -56,6 +58,49 @@ gptrarr_dnf_package_cmp (gconstpointer a, gconstpointer b)
d469b3
   return dnf_package_cmp(*(DnfPackage**)a, *(DnfPackage**)b);
d469b3
 }
d469b3
 
d469b3
+static void
d469b3
+package_info_add_line (struct libscols_table *table, const char *key, const char *value)
d469b3
+{
d469b3
+  struct libscols_line *ln = scols_table_new_line (table, NULL);
d469b3
+  scols_line_set_data (ln, 0, key);
d469b3
+  scols_line_set_data (ln, 1, value);
d469b3
+}
d469b3
+
d469b3
+static void
d469b3
+print_package_info (DnfPackage *package)
d469b3
+{
d469b3
+  struct libscols_table *table = scols_new_table ();
d469b3
+  scols_table_enable_noheadings (table, 1);
d469b3
+  scols_table_set_column_separator (table, " : ");
d469b3
+  scols_table_new_column (table, "key", 5, 0);
d469b3
+  struct libscols_column *cl = scols_table_new_column (table, "value", 10, SCOLS_FL_WRAP);
d469b3
+  scols_column_set_safechars (cl, "\n");
d469b3
+  scols_column_set_wrapfunc (cl, scols_wrapnl_chunksize, scols_wrapnl_nextchunk, NULL);
d469b3
+
d469b3
+  package_info_add_line (table, "Name", dnf_package_get_name (package));
d469b3
+  guint64 epoch = dnf_package_get_epoch (package);
d469b3
+  if (epoch != 0)
d469b3
+    {
d469b3
+      g_autofree gchar *str_epoch = g_strdup_printf ("%ld", epoch);
d469b3
+      package_info_add_line (table, "Epoch", str_epoch);
d469b3
+    }
d469b3
+  package_info_add_line (table, "Version", dnf_package_get_version (package));
d469b3
+  package_info_add_line (table, "Release", dnf_package_get_release (package));
d469b3
+  package_info_add_line (table, "Architecture", dnf_package_get_arch (package));
d469b3
+  g_autofree gchar *size = g_format_size_full (dnf_package_get_size (package),
d469b3
+                                               G_FORMAT_SIZE_LONG_FORMAT | G_FORMAT_SIZE_IEC_UNITS);
d469b3
+  package_info_add_line (table, "Size", size);
d469b3
+  package_info_add_line (table, "Source", dnf_package_get_sourcerpm (package));
d469b3
+  package_info_add_line (table, "Repository", dnf_package_get_reponame (package));
d469b3
+  package_info_add_line (table, "Summanry", dnf_package_get_summary (package));
d469b3
+  package_info_add_line (table, "URL", dnf_package_get_url (package));
d469b3
+  package_info_add_line (table, "License", dnf_package_get_license (package));
d469b3
+  package_info_add_line (table, "Description", dnf_package_get_description (package));
d469b3
+
d469b3
+  scols_print_table (table);
d469b3
+  scols_unref_table (table);
d469b3
+}
d469b3
+
d469b3
 static gboolean
d469b3
 dnf_command_repoquery_run (DnfCommand     *cmd,
d469b3
                           int              argc,
d469b3
@@ -65,11 +110,16 @@ dnf_command_repoquery_run (DnfCommand     *cmd,
d469b3
                           GError         **error)
d469b3
 {
d469b3
   gboolean opt_available = FALSE;
d469b3
+  gboolean opt_info = FALSE;
d469b3
   gboolean opt_installed = FALSE;
d469b3
+  gboolean opt_nevra = FALSE;
d469b3
   g_auto(GStrv) opt_key = NULL;
d469b3
   const GOptionEntry opts[] = {
d469b3
     { "available", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_available, "display available packages (default)", NULL },
d469b3
+    { "info", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_info, "show detailed information about the packages", NULL },
d469b3
     { "installed", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_installed, "display installed packages", NULL },
d469b3
+    { "nevra", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_nevra,
d469b3
+      "use name-epoch:version-release.architecture format for displaying packages (default)", NULL },
d469b3
     { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &opt_key, NULL, NULL },
d469b3
     { NULL }
d469b3
   };
d469b3
@@ -115,16 +165,24 @@ dnf_command_repoquery_run (DnfCommand     *cmd,
d469b3
 
d469b3
   g_ptr_array_sort (pkgs, gptrarr_dnf_package_cmp);
d469b3
 
d469b3
-  // print packages without duplicated lines
d469b3
   const char *prev_line = "";
d469b3
   for (guint i = 0; i < pkgs->len; ++i)
d469b3
     {
d469b3
       DnfPackage *package = g_ptr_array_index (pkgs, i);
d469b3
-      const char * line = dnf_package_get_nevra (package);
d469b3
-      if (strcmp (line, prev_line) != 0)
d469b3
+      if (opt_nevra || !opt_info)
d469b3
+        {
d469b3
+          const char * line = dnf_package_get_nevra (package);
d469b3
+          // print nevras without duplicated lines
d469b3
+          if (opt_info || strcmp (line, prev_line) != 0)
d469b3
+            {
d469b3
+              g_print ("%s\n", line);
d469b3
+              prev_line = line;
d469b3
+            }
d469b3
+        }
d469b3
+      if (opt_info)
d469b3
         {
d469b3
-          g_print ("%s\n", line);
d469b3
-          prev_line = line;
d469b3
+          print_package_info (package);
d469b3
+          g_print ("\n");
d469b3
         }
d469b3
     }
d469b3
 
d469b3
-- 
d469b3
2.21.0
d469b3