implement keep-packages option to ar and mr
authorJosef Reidinger <jreidinger@suse.cz>
Fri, 2 May 2008 11:18:08 +0000 (11:18 +0000)
committerJosef Reidinger <jreidinger@suse.cz>
Fri, 2 May 2008 11:18:08 +0000 (11:18 +0000)
Implement mr filter by local, remote or protocol specific repositories and all option (no filter).

src/zypper-repos.cc
src/zypper-repos.h
src/zypper.cc

index 1629b8e..6963a84 100644 (file)
@@ -3,10 +3,12 @@
 #include <boost/format.hpp>
 #include <boost/logic/tribool.hpp>
 #include <boost/lexical_cast.hpp>
+#include <iterator>
 
 #include "zypp/ZYpp.h"
 #include "zypp/base/Logger.h"
 #include "zypp/base/IOStream.h"
+#include "zypp/base/String.h"
 
 #include "zypp/RepoManager.h"
 #include "zypp/RepoInfo.h"
@@ -1293,7 +1295,7 @@ void add_repo(Zypper & zypper, RepoInfo & repo)
 void add_repo_by_url( Zypper & zypper,
                      const zypp::Url & url, const string & alias,
                      const string & type,
-                     tribool enabled, tribool autorefresh)
+                     tribool enabled, tribool autorefresh, tribool keepPackages)
 {
   MIL << "going to add repository by url (alias=" << alias << ", url=" << url
       << ")" << endl;
@@ -1313,6 +1315,8 @@ void add_repo_by_url( Zypper & zypper,
     repo.setEnabled((enabled == true));
   if ( !indeterminate(autorefresh) )
     repo.setAutorefresh((autorefresh == false));
+  if ( !indeterminate(keepPackages) )
+    repo.setKeepPackages(keepPackages);
 
   add_repo(zypper, repo);
 }
@@ -1321,8 +1325,8 @@ void add_repo_by_url( Zypper & zypper,
 
 //! \todo handle zypp exceptions
 void add_repo_from_file( Zypper & zypper,
-                         const std::string & repo_file_url,
-                         tribool enabled, tribool autorefresh)
+                         const std::string & repo_file_url, tribool enabled,
+                         tribool autorefresh, tribool keepPackages)
 {
   //! \todo handle local .repo files, validate the URI
   Url url = make_url(repo_file_url);
@@ -1391,6 +1395,8 @@ void add_repo_from_file( Zypper & zypper,
       repo.setEnabled((enabled == true));
     if ( !indeterminate(autorefresh) )
       repo.setAutorefresh((autorefresh == false));
+    if ( !indeterminate(keepPackages) )
+      repo.setKeepPackages(keepPackages);
     MIL << "enabled: " << repo.enabled() << " autorefresh: " << repo.autorefresh() << endl;
 
     add_repo(zypper, repo);
@@ -1453,7 +1459,81 @@ void rename_repo(Zypper & zypper,
 }
 
 // ----------------------------------------------------------------------------
+void modify_repos_by_option( Zypper & zypper )
+{
+  RepoManager manager(zypper.globalOpts().rm_options);
+  const std::list<RepoInfo>& repos = manager.knownRepositories();
+  std::set<std::string> toModify;
+
+  if ( copts.count("all") )
+  {
+    for_(it, repos.begin(),repos.end())
+    {
+      string alias = it->alias();
+      modify_repo( zypper, alias );
+    }
+    return; //no more repository is possible
+  }
+  
+  if ( copts.count("local") )
+  {
+    for_(it, repos.begin(),repos.end())
+    {
+      for_( it2,it->baseUrlsBegin(), it->baseUrlsEnd() )
+      {
+        if ( it2->isLocal() )
+        {
+          string alias = it->alias();
+          toModify.insert( alias );
+          break;
+        }
+      }
+    }
+  }
+
+  if ( copts.count("remote") )
+  {
+    for_(it, repos.begin(),repos.end())
+    {
+      for_( it2,it->baseUrlsBegin(), it->baseUrlsEnd() )
+      {
+        if ( !it2->isLocal() )
+        {
+          string alias = it->alias();
+          toModify.insert( alias );
+          break;
+        }
+      }
+    }
+  }
+
+  if ( copts.count("medium-type") )
+  {
+    string par = copts["medium-type"].front();
+    std::set<string> scheme;
+    insert_iterator<std::set<string> > ii (scheme,scheme.begin());
+    str::split( par, ii, ",");
+
+    for_(it, repos.begin(),repos.end())
+    {
+      for_( it2,it->baseUrlsBegin(), it->baseUrlsEnd() )
+      {
+        if ( scheme.find(it2->getScheme())!= scheme.end() )
+        {
+          string alias = it->alias();
+          toModify.insert( alias );
+          break;
+        }
+      }
+    }
+  }
+
+  for_(it, toModify.begin(), toModify.end())
+  {
+    modify_repo( zypper, *it );
+  }
 
+}
 void modify_repo(Zypper & zypper, const string & alias)
 {
   // tell whether currenlty processed options are contradicting each other
@@ -1499,6 +1579,24 @@ void modify_repo(Zypper & zypper, const string & alias)
   }
   DBG << "autoref = " << autoref << endl;
 
+  tribool keepPackages = indeterminate;
+  if (copts.count("keep-packages"))
+    keepPackages = true;
+  if (copts.count("no-keep-packages"))
+  {
+    if (keepPackages)
+    {
+      zypper.out().warning(boost::str(format(msg_contradition)
+          % "--keep-packages" % "--no-keep-package"));
+
+      keepPackages = indeterminate;
+    }
+    else
+      keepPackages = false;
+  }
+  DBG << "keepPackages = " << keepPackages << endl;
+
+
   try
   {
     RepoManager manager(zypper.globalOpts().rm_options);
@@ -1506,6 +1604,7 @@ void modify_repo(Zypper & zypper, const string & alias)
     bool chnaged_enabled = false;
     bool changed_autoref = false;
     bool changed_prio = false;
+    bool changed_keeppackages = false;
 
     if (!indeterminate(enable))
     {
@@ -1521,6 +1620,13 @@ void modify_repo(Zypper & zypper, const string & alias)
       repo.setAutorefresh(autoref);
     }
 
+    if (!indeterminate(keepPackages))
+    {
+      if (keepPackages != repo.keepPackages())
+        changed_keeppackages = true;
+      repo.setKeepPackages(keepPackages);
+    }
+
     int prio = 0;
     parsed_opts::const_iterator tmp1;
     if ((tmp1 = zypper.cOpts().find("priority")) != zypper.cOpts().end())
@@ -1549,7 +1655,8 @@ void modify_repo(Zypper & zypper, const string & alias)
       }
     }
 
-    if (chnaged_enabled || changed_autoref || changed_prio)
+    if (chnaged_enabled || changed_autoref || changed_prio
+        || changed_keeppackages)
     {
       manager.modifyRepository(alias, repo);
 
@@ -1573,6 +1680,16 @@ void modify_repo(Zypper & zypper, const string & alias)
             _("Autorefresh has been disabled for repository '%s'.")) % alias));
       }
 
+      if (changed_keeppackages)
+      {
+        if (repo.keepPackages())
+          zypper.out().info(boost::str(format(
+            _("Keep packages has been enabled for repository '%s'.")) % alias));
+        else
+          zypper.out().info(boost::str(format(
+            _("Keep packages has been disabled for repository '%s'.")) % alias));
+      }
+
       if (changed_prio)
       {
         zypper.out().info(boost::str(format(
index d36bc8c..74c014f 100644 (file)
@@ -80,7 +80,8 @@ void add_repo_by_url(Zypper & zypper,
                      const std::string & alias,
                      const std::string & type = "",
                      boost::tribool enabled = boost::indeterminate,
-                     boost::tribool autorefresh = boost::indeterminate);
+                     boost::tribool autorefresh = boost::indeterminate,
+                     boost::tribool keepPackages = boost::indeterminate);
 
 /**
  * Add repository specified in given repo file on \a repo_file_url. All repos
@@ -94,7 +95,9 @@ void add_repo_by_url(Zypper & zypper,
 void add_repo_from_file(Zypper & zypper,
                         const std::string & repo_file_url,
                         boost::tribool enabled = boost::indeterminate,
-                        boost::tribool autorefresh = boost::indeterminate);
+                        boost::tribool autorefresh = boost::indeterminate,
+                        boost::tribool keepPackages = boost::indeterminate);
+
 
 /**
  * Add repository specified by \repo to system repositories. 
@@ -120,6 +123,18 @@ void rename_repo(Zypper & zypper,
 void modify_repo(Zypper & zypper, const std::string & alias);
 
 /**
+ * Modify all repositories properties.
+ * 
+ */
+void modify_all_repos(Zypper & zypper);
+
+/**
+ * Modify repositories which is matching filter options
+ * like all, local, remote or medium-type
+ */
+void modify_repos_by_option( Zypper & zypper );
+
+/**
  * Load both repository and target resolvables.
  *
  * \see load_repo_resolvables(bool)
index 3805394..2304dc9 100644 (file)
@@ -944,6 +944,8 @@ void Zypper::processCommandOptions()
       {"check", no_argument, 0, 'c'},
       {"no-check", no_argument, 0, 'x'},
       {"name", no_argument, 0, 'n'},
+      {"keep-packages", no_argument, 0, 'k'},
+      {"no-keep-packages", no_argument, 0, 'K'}, //TODO not documented
       {0, 0, 0, 0}
     };
     specific_options = service_add_options;
@@ -1067,6 +1069,12 @@ void Zypper::processCommandOptions()
       {"no-refresh", no_argument, 0, 'n'},
       {"disable-autorefresh", no_argument, 0, 0 }, // backward compatibility
       {"priority", required_argument, 0, 'p'},
+      {"keep-packages", no_argument, 0, 'k'},
+      {"no-keep-packages", no_argument, 0, 'K'}, //TODO not documented
+      {"all", no_argument, 0, 'A' }, //TODO not documented
+      {"local", no_argument, 0, 'l' }, //TODO not documented
+      {"remote", no_argument, 0, 't' }, //TODO not documented
+      {"medium-type", required_argument, 0, 'm' }, //TODO not documented
       {0, 0, 0, 0}
     };
     specific_options = service_modify_options;
@@ -1919,12 +1927,18 @@ void Zypper::doCommand()
     if (copts.count("disable") || copts.count("disabled"))
       enabled = false;
 
+    tribool keepPackages;
+    if (copts.count("keep-packages"))
+      keepPackages = true;
+    else if (copts.count("no-keep-packages"))
+      keepPackages = false;
+
     try
     {
       // add repository specified in .repo file
       if (copts.count("repo"))
       {
-        add_repo_from_file(*this,copts["repo"].front(), enabled);
+        add_repo_from_file(*this,copts["repo"].front(), enabled, keepPackages);
         return;
       }
   
@@ -1971,7 +1985,7 @@ void Zypper::doCommand()
         }
         else
         {
-          add_repo_from_file(*this,_arguments[0], enabled);
+          add_repo_from_file(*this,_arguments[0], enabled, keepPackages);
           break;
         }
       case 2:
@@ -2003,7 +2017,7 @@ void Zypper::doCommand()
         init_target(*this);
 
         add_repo_by_url(
-           *this, url, _arguments[1]/*alias*/, type, enabled);
+           *this, url, _arguments[1]/*alias*/, type, enabled, keepPackages);
         return;
       }
     }
@@ -2145,8 +2159,12 @@ void Zypper::doCommand()
       return;
     }
 
-    if (_arguments.size() < 1)
+    bool non_alias = copts.count("all") || copts.count("local") || 
+        copts.count("remote") || copts.count("medium-type");
+
+    if (_arguments.size() < 1 && !non_alias)
     {
+      //TODO add all option to help text
       out().error(_("Alias is a required argument."));
       ERR << "No alias argument given." << endl;
       out().info(_command_help);
@@ -2154,23 +2172,31 @@ void Zypper::doCommand()
       return;
     }
     // too many arguments
-    if (_arguments.size() > 1)
+    if (_arguments.size() > 1
+       || (_arguments.size() > 0 && non_alias))
     {
       report_too_many_arguments(_command_help);
       setExitCode(ZYPPER_EXIT_ERR_INVALID_ARGS);
       return;
     }
-  
-    RepoInfo repo;
-    if (match_repo(*this,_arguments[0],&repo))
+    
+    if (non_alias)
     {
-      modify_repo(*this, repo.alias());
+      modify_repos_by_option(*this);
     }
-    else 
+    else
     {
-      out().error(
-        boost::str(format(_("Repository %s not found.")) % _arguments[0]));
-      ERR << "Repo " << _arguments[0] << " not found" << endl;
+      RepoInfo repo;
+      if (match_repo(*this,_arguments[0],&repo))
+      {
+        modify_repo(*this, repo.alias());
+      }
+      else 
+      {
+        out().error(
+          boost::str(format(_("Repository %s not found.")) % _arguments[0]));
+        ERR << "Repo " << _arguments[0] << " not found" << endl;
+      }
     }
     
     break;
@@ -2338,6 +2364,7 @@ void Zypper::doCommand()
       repo.setAutorefresh(true);
       repo.setAlias(TMP_RPM_REPO_ALIAS);
       repo.setName(_("Plain RPM files cache"));
+      repo.setKeepPackages(false);
 
       // shut up zypper
       Out::Verbosity tmp = out().verbosity();