- fixed installed status column in zypper search (bnc #375161)
authorJan Kupec <jkupec@suse.cz>
Wed, 16 Apr 2008 14:42:25 +0000 (14:42 +0000)
committerJan Kupec <jkupec@suse.cz>
Wed, 16 Apr 2008 14:42:25 +0000 (14:42 +0000)
- 'packages' command added (fate #302148)

src/zypper-command.cc
src/zypper-command.h
src/zypper-search.cc
src/zypper-search.h
src/zypper.cc
src/zypper.h

index b0fd6d7..83adbe3 100644 (file)
@@ -34,10 +34,10 @@ const ZypperCommand ZypperCommand::SEARCH(ZypperCommand::SEARCH_e);
 const ZypperCommand ZypperCommand::INFO(ZypperCommand::INFO_e);
 const ZypperCommand ZypperCommand::LIST_UPDATES(ZypperCommand::LIST_UPDATES_e);
 const ZypperCommand ZypperCommand::PATCH_CHECK(ZypperCommand::PATCH_CHECK_e);
-//const ZypperCommand ZypperCommand::PACKAGES(ZypperCommand::PACKAGES_e);
+const ZypperCommand ZypperCommand::PACKAGES(ZypperCommand::PACKAGES_e);
 const ZypperCommand ZypperCommand::PATCHES(ZypperCommand::PATCHES_e);
 const ZypperCommand ZypperCommand::PATTERNS(ZypperCommand::PATTERNS_e);
-//const ZypperCommand ZypperCommand::PRODUCTS(ZypperCommand::PRODUCTS_e);
+const ZypperCommand ZypperCommand::PRODUCTS(ZypperCommand::PRODUCTS_e);
 const ZypperCommand ZypperCommand::XML_LIST_UPDATES_PATCHES(ZypperCommand::XML_LIST_UPDATES_PATCHES_e);
 
 const ZypperCommand ZypperCommand::ADD_LOCK(ZypperCommand::ADD_LOCK_e);
@@ -83,10 +83,10 @@ ZypperCommand::Command ZypperCommand::parse(const std::string & strval_r)
     _table["info"] = _table["if"] = ZypperCommand::INFO_e;
     _table["list-updates"] = _table["lu"] = ZypperCommand::LIST_UPDATES_e;
     _table["patch-check"] = _table["pchk"] = ZypperCommand::PATCH_CHECK_e;
-    //_table["packages"] = _table["pa"] = _table["pkg"] = ZypperCommand::PACKAGES_e;
+    _table["packages"] = _table["pa"] = _table["pkg"] = ZypperCommand::PACKAGES_e;
     _table["patches"] = _table["pch"] = ZypperCommand::PATCHES_e;
     _table["patterns"] = _table["pt"] = ZypperCommand::PATTERNS_e;
-    //_table["products"] = _table["pd"] = ZypperCommand::PRODUCTS_e;
+    _table["products"] = _table["pd"] = ZypperCommand::PRODUCTS_e;
     _table["xml-updates"] = _table["xu"] = ZypperCommand::XML_LIST_UPDATES_PATCHES_e;
 
     _table["addlock"] = _table["al"] = _table["lock-add"] = _table["la"] = ZypperCommand::ADD_LOCK_e;
index f9e7eae..a7ba59e 100644 (file)
@@ -29,10 +29,10 @@ struct ZypperCommand
   static const ZypperCommand INFO;
   static const ZypperCommand LIST_UPDATES;
   static const ZypperCommand PATCH_CHECK;
-  //static const ZypperCommand PACKAGES;
+  static const ZypperCommand PACKAGES;
   static const ZypperCommand PATCHES;
   static const ZypperCommand PATTERNS;
-  //static const ZypperCommand PRODUCTS;
+  static const ZypperCommand PRODUCTS;
   static const ZypperCommand XML_LIST_UPDATES_PATCHES;
 
   static const ZypperCommand ADD_LOCK;
@@ -75,10 +75,10 @@ struct ZypperCommand
     INFO_e,
     LIST_UPDATES_e,
     PATCH_CHECK_e,
-    //PACKAGES_e,
+    PACKAGES_e,
     PATCHES_e,
     PATTERNS_e,
-    //PRODUCTS_e,
+    PRODUCTS_e,
     XML_LIST_UPDATES_PATCHES_e,
 
     ADD_LOCK_e,
index 78365c3..d25473a 100644 (file)
@@ -5,6 +5,8 @@
 #include "zypp/Patch.h"
 #include "zypp/Pattern.h"
 
+#include "zypp/ResPoolProxy.h"
+
 #include "zypper.h"
 #include "zypper-main.h"
 #include "zypper-tabulator.h"
@@ -17,11 +19,47 @@ using namespace std;
 
 extern ZYpp::Ptr God;
 
+
+string selectable_search_repo_str(const ui::Selectable & s)
+{
+  string repostr;
+  for_(it, s.installedBegin(), s.installedEnd())
+  {
+    if (!(*it)->repository().isSystemRepo())
+    {
+      cout << "can this ever be true?" << endl;
+      if (repostr.empty())
+        repostr = (*it)->repository().info().name();
+      else
+      {
+        repostr = _("(multiple)");
+        return repostr;
+      }
+    }
+  }
+
+  // show available objects
+  for_(it, s.availableBegin(), s.availableEnd())
+  {      
+    if (repostr.empty())
+      repostr = (*it)->repository().info().name();
+    else
+    {
+      repostr = _("(multiple)");
+      return repostr;
+    }
+  }
+  
+  return string();
+}
+
+
 static string string_status (const ResStatus& rs)
 {
   return rs.isInstalled () ? _("Installed"): _("Uninstalled");
 }
 
+
 static string string_weak_status(const ResStatus & rs)
 {
   if (rs.isRecommended())
@@ -107,6 +145,77 @@ void list_patterns(Zypper & zypper)
     cout << tbl;
 }
 
+void list_packages(Zypper & zypper)
+{
+  MIL << "Going to list patterns." << std::endl;
+
+  Table tbl;
+  TableHeader th;
+
+  // translators: S for installed Status
+  th << _("S");
+  if (zypper.globalOpts().is_rug_compatible)
+    // translators: Bundle is a term used in rug. See rug for how to translate it.
+    th << _("Bundle");
+  else
+    th << _("Repository");
+  th << _("Name") << _("Version") << _("Arch");
+  tbl << th;
+  
+  bool installed_only = zypper.cOpts().count("installed-only");
+  bool notinst_only = zypper.cOpts().count("uninstalled-only");
+
+  ResPoolProxy::const_iterator
+    it = God->pool().proxy().byKindBegin(ResKind::package),
+    e  = God->pool().proxy().byKindEnd(ResKind::package);
+  for (; it != e; ++it )
+  {
+    ui::Selectable::constPtr s = *it;
+
+    // get the first installed object
+    PoolItem installed;
+    if (!s->installedEmpty())
+      installed = s->installedObj();
+
+    // show available objects
+    for_(it, s->availableBegin(), s->availableEnd())
+    {
+      TableRow row;
+
+      zypp::PoolItem pi = *it;
+      if (installed)
+      {
+        if (notinst_only)
+          continue;
+        row << (equalNVRA(*installed.resolvable(), *pi.resolvable()) ? "i" : "v");
+      }
+      else
+      {
+        if (installed_only)
+          continue;
+        row << "";
+      }
+      row << (zypper.globalOpts().is_rug_compatible ? "" : pi->repository().info().name())
+          << pi->name()
+          << pi->edition().asString()
+          << pi->arch().asString();
+
+      tbl << row;
+    }
+  }
+  if (zypper.cOpts().count("sort-by-repo") || zypper.cOpts().count("sort-by-catalog"))
+    tbl.sort(1); // Repo
+  else
+    tbl.sort(2); // Name
+
+  if (tbl.empty())
+    zypper.out().info(_("No packages found."));
+  else
+    // display the result, even if --quiet specified
+    cout << tbl;
+}
+
+
 // Local Variables:
 // c-basic-offset: 2
 // End:
index 18cbbc5..cf404b3 100644 (file)
 #include "zypper-utils.h" // for kind_to_string_localized
 #include "zypper-tabulator.h"
 
+std::string selectable_search_repo_str(const zypp::ui::Selectable & s);
+
 /**
  * Functor for filling search output table in rug style.
  */
-struct FillTable
+struct FillSearchTableSolvable
 {
   // the table used for output
   Table * _table;
   const GlobalOptions & _gopts;
 
-  FillTable( Table & table )
+  FillSearchTableSolvable( Table & table )
   : _table( &table )
   , _gopts(Zypper::instance()->globalOpts())
   {
     TableHeader header;
 
-    // TranslatorExplanation S as Status
+    // translators: S for installed Status
     header << _("S");
 
     if (_gopts.is_rug_compatible)
@@ -42,7 +44,7 @@ struct FillTable
       header << _("Repository");
 
     if (_gopts.is_rug_compatible)
-      // TranslatorExplanation This is Bundle in as used in rug.
+      // translators: Bundle is a term used in rug. See rug for how to translate it.
       header << _("Bundle");
     else
       header << _("Type");
@@ -71,12 +73,96 @@ struct FillTable
         *_table << row;
     return true;
   }
+
+  bool operator()(const zypp::ui::Selectable::constPtr & s) const
+  {
+    // show installed objects
+    for_(it, s->installedBegin(), s->installedEnd())
+    {
+      TableRow row;
+      zypp::PoolItem pi = *it;
+      row << "i";
+      row << pi->repository().info().name()
+          // TODO what about rug's Bundle?
+          << (_gopts.is_rug_compatible ? "" : kind_to_string_localized(pi->kind(), 1))
+          << pi->name()
+          << pi->edition().asString()
+          << pi->arch().asString();
+      *_table << row;
+    }
+
+    // get the first installed object
+    zypp::PoolItem installed;
+    if (!s->installedEmpty())
+      installed = s->installedObj();
+
+    // show available objects
+    for_(it, s->availableBegin(), s->availableEnd())
+    {
+      TableRow row;
+
+      zypp::PoolItem pi = *it;
+      if (installed)
+      {
+        row << (equalNVRA(*installed.resolvable(), *pi.resolvable()) ?  "i" : "v");
+      }
+      else
+        row << "";
+      row << pi->repository().info().name();
+      row << (_gopts.is_rug_compatible ? "" : kind_to_string_localized(pi->kind(), 1))
+          << pi->name()
+          << pi->edition().asString()
+          << pi->arch().asString();
+
+      *_table << row;
+    }
+    return true;
+  }
+};
+
+struct FillSearchTableSelectable
+{
+  // the table used for output
+  Table * _table;
+  const GlobalOptions & _gopts;
+
+  FillSearchTableSelectable( Table & table )
+  : _table( &table )
+  , _gopts(Zypper::instance()->globalOpts())
+  {
+    TableHeader header;
+    // translators: S for installed Status
+    header << _("S");
+    header << _("Name");
+    header << _("Summary");
+    header << _("Type");
+    header << _("Repository");
+    *_table << header;
+  }
+
+  bool operator()(const zypp::ui::Selectable::constPtr & s) const
+  {
+    TableRow row;
+
+    row << (s->installedEmpty() ? "" : "i");
+    //! \todo improve the abbreviation
+    row << (_gopts.no_abbrev || s->theObj()->summary().size() < 33 ?
+        s->theObj()->summary() : s->theObj()->summary().substr(0,28) + "...");
+    row << kind_to_string_localized(s->kind(), 1);
+    row << selectable_search_repo_str(*s);
+    *_table << row;
+    return true;
+  }
 };
 
-/** List patches */
+/** List all patches with specific info in specified repos */
 void list_patches(Zypper & zypper);
 
-/** List paterns */
+/** List all patterns with specific info in specified repos */
 void list_patterns(Zypper & zypper);
 
+/** List all packages with specific info in specified repos
+ *  - currently looks like zypper search -t package -r foorepo */
+void list_packages(Zypper & zypper);
+
 #endif /*ZYPPERSEARCH_H_*/
index dbad47d..4e74474 100644 (file)
 #include <boost/format.hpp>
 
 #include "zypp/ZYppFactory.h"
-#include "zypp/base/Logger.h"
+#include "zypp/zypp_detail/ZYppReadOnlyHack.h"
 
+#include "zypp/base/Logger.h"
+#include "zypp/base/Algorithm.h"
 #include "zypp/base/UserRequestException.h"
-#include "zypp/repo/RepoException.h"
-#include "zypp/zypp_detail/ZYppReadOnlyHack.h"
+
 #include "zypp/sat/SolvAttr.h"
 #include "zypp/PoolQuery.h"
 
@@ -1278,6 +1279,37 @@ void Zypper::processCommandOptions()
     break;
   }
 
+  case ZypperCommand::PACKAGES_e:
+  {
+    static struct option options[] = {
+      {"repo", required_argument, 0, 'r'},
+      // rug compatibility option, we have --repo
+      {"catalog", required_argument, 0, 'c'},
+      {"installed-only", no_argument, 0, 'i'},
+      {"uninstalled-only", no_argument, 0, 'u'},
+      {"sort-by-name", no_argument, 0, 'N'},
+      {"sort-by-repo", no_argument, 0, 'R'},
+      {"sort-by-catalog", no_argument, 0, 0},
+      {"help", no_argument, 0, 'h'},
+      {0, 0, 0, 0}
+    };
+    specific_options = options;
+    _command_help = _(
+      "packages (pa) [options] [repository] ...\n"
+      "\n"
+      "List all packages available in specified repositories.\n"
+      "\n"
+      "  Command options:\n"
+      "\n"
+      "-r, --repo <alias|#|URI>  Just another means to specify repository.\n"
+      "-i, --installed-only      Show only installed patterns.\n"
+      "-u, --uninstalled-only    Show only patterns wich are not installed.\n"
+      "-N, --sort-by-name        Sort the list by package name.\n"
+      "-R, --sort-by-repo        Sort the list by repository.\n"
+    );
+    break;
+  }
+
   case ZypperCommand::PATTERNS_e:
   {
     static struct option options[] = {
@@ -2218,10 +2250,8 @@ void Zypper::doCommand()
 
     try
     {
-      FillTable callback( t );
-      query.execute(callback);
-      //unsigned int count = invokeOnEach(query.begin(), query.end(), callback);
-      //cout << "query: " << endl << query;
+      FillSearchTableSolvable callback(t);
+      invokeOnEach(query.selectableBegin(), query.selectableEnd(), callback);
 
       if (t.empty())
         out().info(_("No resolvables found."), Out::QUIET);
@@ -2234,8 +2264,6 @@ void Zypper::doCommand()
           t.sort(3); // sort by name
 
         cout << t; //! \todo out().table()?
-
-//        cout << "invocations:" << count << endl;        
       }
     }
     catch (const Exception & e)
@@ -2296,7 +2324,8 @@ void Zypper::doCommand()
   // --------------------------( patches )------------------------------------
 
   else if (command() == ZypperCommand::PATCHES ||
-           command() == ZypperCommand::PATTERNS)
+           command() == ZypperCommand::PATTERNS ||
+           command() == ZypperCommand::PACKAGES)
   {
     if (runningHelp()) { out().info(_command_help, Out::QUIET); return; }
 
@@ -2314,6 +2343,9 @@ void Zypper::doCommand()
     case ZypperCommand::PATTERNS_e:
       list_patterns(*this);
       break;
+    case ZypperCommand::PACKAGES_e:
+      list_packages(*this);
+      break;
     default:;
     }
 
index 51ecbf4..e8ac806 100644 (file)
@@ -35,7 +35,9 @@ struct GlobalOptions
   no_gpg_checks(false),
   machine_readable(false),
   no_refresh(false),
-  root_dir("/")
+  root_dir("/"),
+  no_abbrev(false),
+  terse(false)
   {}
 
 //  std::list<zypp::Url> additional_sources;
@@ -61,6 +63,8 @@ struct GlobalOptions
   bool no_refresh;
   std::string root_dir;
   zypp::RepoManagerOptions rm_options;
+  bool no_abbrev;
+  bool terse;
 };