- --plus-repo added as a global option for adding additional temporary
authorJan Kupec <jkupec@suse.cz>
Thu, 3 Jan 2008 16:06:07 +0000 (16:06 +0000)
committerJan Kupec <jkupec@suse.cz>
Thu, 3 Jan 2008 16:06:07 +0000 (16:06 +0000)
  repository for current transaction (#224886)

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

index 735a831..90b0953 100644 (file)
@@ -91,7 +91,7 @@ static bool refresh_raw_metadata(const Zypper & zypper,
   {
     report_problem(e,
         boost::str(format(_("Problem downloading files from '%s'.")) % repo.name()),
-        _("Please, see the above error message to for a hint."));
+        _("Please see the above error message to for a hint."));
 
     return true; // error
   }
@@ -102,7 +102,7 @@ static bool refresh_raw_metadata(const Zypper & zypper,
     if (!repo.filepath().empty())
       cerr << format(
           // TranslatorExplanation the first %s is a .repo file path
-          _("Please, add one or more base URL (baseurl=URL) entries to %s for repository '%s'."))
+          _("Please add one or more base URL (baseurl=URL) entries to %s for repository '%s'."))
           % repo.filepath() % repo.name() << endl;
 
     return true; // error
@@ -120,7 +120,7 @@ static bool refresh_raw_metadata(const Zypper & zypper,
     ZYPP_CAUGHT(e);
     report_problem(e,
         boost::str(format(_("Repository '%s' is invalid.")) % repo.name()),
-        _("Please, check if the URLs defined for this repository are pointing to a valid repository."));
+        _("Please check if the URLs defined for this repository are pointing to a valid repository."));
 
     return true; // error
   }
@@ -248,6 +248,16 @@ static void do_init_repos(Zypper & zypper)
   else
     gData.repos = manager.knownRepositories();
 
+  // additional repositories (--plus-repo)
+  if (!gData.additional_repos.empty())
+  {
+    for (list<RepoInfo>::iterator it = gData.additional_repos.begin();
+        it != gData.additional_repos.end(); ++it)
+    {
+      add_repo(zypper, *it);
+      gData.repos.push_back(*it);
+    }
+  }
 
   for (std::list<RepoInfo>::iterator it = gData.repos.begin();
        it !=  gData.repos.end(); ++it)
@@ -479,6 +489,12 @@ void list_repos(Zypper & zypper)
     exit(ZYPPER_EXIT_ERR_ZYPP);
   }
 
+  // add the additional repos specified with the --plus-repo to the list
+  if (!gData.additional_repos.empty())
+    repos.insert(repos.end(),
+        gData.additional_repos.begin(),
+        gData.additional_repos.end());
+
   // export to file or stdout in repo file format
   if (copts.count("export"))
   {
@@ -722,8 +738,7 @@ std::string timestamp ()
 
 // ----------------------------------------------------------------------------
 
-static
-int add_repo(Zypper & zypper, RepoInfo & repo)
+void add_repo(Zypper & zypper, RepoInfo & repo)
 {
   RepoManager manager(zypper.globalOpts().rm_options);
 
@@ -756,36 +771,40 @@ int add_repo(Zypper & zypper, RepoInfo & repo)
   catch (const RepoAlreadyExistsException & e)
   {
     ZYPP_CAUGHT(e);
-    cerr << format(_("Repository named '%s' already exists. Please, use another alias."))
+    cerr << format(_("Repository named '%s' already exists. Please use another alias."))
         % repo.alias() << endl;
     ERR << "Repository named '" << repo.alias() << "' already exists." << endl;
-    return ZYPPER_EXIT_ERR_ZYPP;
+    zypper.setExitCode(ZYPPER_EXIT_ERR_ZYPP);
+    return;
   }
   catch (const RepoUnknownTypeException & e)
   {
     ZYPP_CAUGHT(e);
     cerr << _("Can't find a valid repository at given location:") << endl;
     cerr << _("Could not determine the type of the repository."
-        " Please, check if the defined URLs (see below) point to a valid repository:");
+        " Please check if the defined URLs (see below) point to a valid repository:");
     for(RepoInfo::urls_const_iterator uit = repo.baseUrlsBegin();
         uit != repo.baseUrlsEnd(); ++uit)
       cerr << (*uit) << endl;
-    return ZYPPER_EXIT_ERR_ZYPP;
+    zypper.setExitCode(ZYPPER_EXIT_ERR_ZYPP);
+    return;
   }
   catch (const RepoException & e)
   {
     ZYPP_CAUGHT(e);
     report_problem(e,
         _("Problem transferring repository data from specified URL:"),
-        is_cd ? "" : _("Please, check whether the specified URL is accessible."));
+        is_cd ? "" : _("Please check whether the specified URL is accessible."));
     ERR << "Problem transferring repository data from specified URL" << endl;
-    return ZYPPER_EXIT_ERR_ZYPP;
+    zypper.setExitCode(ZYPPER_EXIT_ERR_ZYPP);
+    return;
   }
   catch (const Exception & e)
   {
     ZYPP_CAUGHT(e);
     report_problem(e, _("Unknown problem when adding repository:"));
-    return ZYPPER_EXIT_ERR_BUG;
+    zypper.setExitCode(ZYPPER_EXIT_ERR_BUG);
+    return;
   }
 
   cout << format(_("Repository '%s' successfully added")) % repo.name();
@@ -818,12 +837,11 @@ int add_repo(Zypper & zypper, RepoInfo & repo)
     if (error)
     {
       cerr << format(_("Problem reading data from '%s' media")) % repo.name() << endl;
-      cerr << _("Please, check if your installation media is valid and readable.") << endl;
-      return ZYPPER_EXIT_ERR_ZYPP;
+      cerr << _("Please check if your installation media is valid and readable.") << endl;
+      zypper.setExitCode(ZYPPER_EXIT_ERR_ZYPP);
+      return;
     }
   }
-
-  return ZYPPER_EXIT_OK;
 }
 
 // ----------------------------------------------------------------------------
@@ -850,7 +868,7 @@ void add_repo_by_url( Zypper & zypper,
   if ( !indeterminate(autorefresh) )
     repo.setAutorefresh((autorefresh == true));
 
-  zypper.setExitCode(add_repo(zypper, repo));
+  add_repo(zypper, repo);
 }
 
 // ----------------------------------------------------------------------------
@@ -929,25 +947,7 @@ ostream& operator << (ostream& s, const vector<T>& v) {
 }
 
 // ----------------------------------------------------------------------------
-/*
-static
-bool looks_like_url (const string& s) {
-  static bool schemes_shown = false;
-  if (!schemes_shown) {
-    cerr_vv << "Registered schemes: " << Url::getRegisteredSchemes () << endl;
-    schemes_shown = true;
-  }
 
-  string::size_type pos = s.find (':');
-  if (pos != string::npos) {
-    string scheme (s, 0, pos);
-    if (Url::isRegisteredScheme (scheme)) {
-      return true;
-    }
-  }
-  return false;
-}
-*/
 static bool do_remove_repo(Zypper & zypper, const RepoInfo & repoinfo)
 {
   RepoManager manager(zypper.globalOpts().rm_options);
index efc25b5..37aed31 100644 (file)
@@ -36,8 +36,6 @@ int refresh_repos(Zypper & zypper, std::vector<std::string> & arguments);
  * \param type
  * \param enabled     Whether the repo should be enabled   
  * \param autorefresh Whether the repo should have autorefresh turned on
- * \return ZYPPER_EXIT_ERR_ZYPP on unexpected zypp exception,
- *         ZYPPER_EXIT_OK otherwise
  */
 void add_repo_by_url(Zypper & zypper,
                      const zypp::Url & url,
@@ -54,8 +52,6 @@ void add_repo_by_url(Zypper & zypper,
  * \param repo_file_url Valid URL of the repo file.
  * \param enabled     Whether the repo should be enabled   
  * \param autorefresh Whether the repo should have autorefresh turned on
- * \return ZYPPER_EXIT_ERR_ZYPP on unexpected zypp exception,
- *         ZYPPER_EXIT_OK otherwise
  */
 void add_repo_from_file(Zypper & zypper,
                         const std::string & repo_file_url,
@@ -63,6 +59,11 @@ void add_repo_from_file(Zypper & zypper,
                         boost::tribool autorefresh = boost::indeterminate);
 
 /**
+ * Add repository specified by \repo to system repositories. 
+ */
+void add_repo(Zypper & zypper, zypp::RepoInfo & repo);
+
+/**
  * Delte repository specified by \a alias.
  */
 bool remove_repo(Zypper & zypper, const std::string &alias );
index c2ee860..d4a1ef0 100644 (file)
@@ -95,7 +95,8 @@ int Zypper::main(int argc, char ** argv)
   {
   case ZypperCommand::SHELL_e:
     commandShell();
-    return ZYPPER_EXIT_OK;
+    cleanup();
+    return exitCode();
 
   case ZypperCommand::NONE_e:
   {
@@ -107,6 +108,7 @@ int Zypper::main(int argc, char ** argv)
 
   default:
     safeDoCommand();
+    cleanup();
     return exitCode();
   }
 
@@ -131,6 +133,7 @@ void print_main_help()
     "\t--reposd-dir, D <dir>\tUse alternative repository definition files directory.\n"
     "\t--cache-dir, C <dir>\tUse alternative meta-data cache database directory.\n"
     "\t--raw-cache-dir <dir>\tUse alternative raw meta-data cache directory\n"
+    "\t--plus-repo, p <URI|file>\tUse an additional repository\n"
   );
 
   static string help_commands = _(
@@ -197,6 +200,7 @@ void Zypper::processGlobalOptions()
     {"reposd-dir",      required_argument, 0, 'D'},
     {"cache-dir",       required_argument, 0, 'C'},
     {"raw-cache-dir",   required_argument, 0,  0 },
+    {"plus-repo",       required_argument, 0, 'p'},
     {"opt",             optional_argument, 0, 'o'},
     {"disable-system-resolvables", optional_argument, 0, 'o'},
     {0, 0, 0, 0}
@@ -320,20 +324,7 @@ void Zypper::processGlobalOptions()
     cout_v << _("Ignoring installed resolvables...") << endl;
     _gopts.disable_system_resolvables = true;
   }
-/*
-  if (gopts.count("source"))
-  {
-    list<string> sources = gopts["source"];
-    for (list<string>::const_iterator it = sources.begin(); it != sources.end(); ++it )
-    {
-      Url url = make_url (*it);
-      if (!url.isValid())
-      setExitCode(ZYPPER_EXIT_ERR_INVALID_ARGS);
-      return;
-      _gopts.additional_sources.push_back(url); 
-    }
-  }
-*/
+
   // testing option
   if ((it = gopts.find("opt")) != gopts.end()) {
     cout << "Opt arg: ";
@@ -392,6 +383,48 @@ void Zypper::processGlobalOptions()
     }
   }
 
+  // additional repositories
+  if (gopts.count("plus-repo") || gopts.count("source"))
+  {
+    if (command() == ZypperCommand::ADD_REPO ||
+        command() == ZypperCommand::REMOVE_REPO ||
+        command() == ZypperCommand::MODIFY_REPO ||
+        command() == ZypperCommand::RENAME_REPO ||
+        command() == ZypperCommand::REFRESH)
+    {
+      cout << _("The --plus-repo option has no effect here, ignoring.") << endl;
+    }
+    else
+    {
+      list<string> repos = gopts["plus-repo"];
+      if (repos.empty())
+        repos = gopts["sources"];
+  
+      int count = 1;
+      for (list<string>::const_iterator it = repos.begin();
+          it != repos.end(); ++it)
+      {
+        Url url = make_url (*it);
+        if (!url.isValid())
+        {
+          setExitCode(ZYPPER_EXIT_ERR_INVALID_ARGS);
+          return;
+        }
+
+        RepoInfo repo;
+        repo.addBaseUrl(url);
+        repo.setEnabled(true);
+        repo.setAutorefresh(true);
+        repo.setAlias(boost::str(format("tmp%d") % count));
+        repo.setName(url.asString());
+
+        gData.additional_repos.push_back(repo);
+        DBG << "got additional repo: " << url << endl;
+        count++;
+      }
+    }
+  }
+
   MIL << "DONE" << endl;
 }
 
@@ -2120,6 +2153,16 @@ void Zypper::doCommand()
   setExitCode(ZYPPER_EXIT_ERR_BUG);
 }
 
+void Zypper::cleanup()
+{
+  MIL << "START" << endl;
+
+  // remove the additional repositories specified by --plus-repo
+  for (list<RepoInfo>::const_iterator it = gData.additional_repos.begin();
+         it != gData.additional_repos.end(); ++it)
+    remove_repo(*this, it->alias());
+}
+
 // Local Variables:
 // c-basic-offset: 2
 // End:
index c75dee8..2e1f235 100644 (file)
@@ -104,6 +104,7 @@ private:
   void shellCleanup();
   void safeDoCommand();
   void doCommand();
+  void cleanup();
 
   void setCommand(const ZypperCommand & command) { _command = command; }
   void setRunningShell(bool value = true) { _running_shell = value; }
@@ -137,6 +138,7 @@ struct RuntimeData
   {}
 
   std::list<zypp::RepoInfo> repos;
+  std::list<zypp::RepoInfo> additional_repos;
   int patches_count;
   int security_patches_count;
   zypp::ResStore repo_resolvables;