Author: jkupec
Date: Fri Sep 12 18:23:54 2008
New Revision: 11055
URL: http://svn.opensuse.org/viewcvs/zypp?rev=11055&view=rev
Log:
- modifyservice command added (fate #304915)
Modified:
trunk/zypper/src/Command.cc
trunk/zypper/src/Command.h
trunk/zypper/src/Zypper.cc
trunk/zypper/src/repos.cc
trunk/zypper/src/repos.h
trunk/zypper/src/utils/getopt.cc
trunk/zypper/src/utils/getopt.h
Modified: trunk/zypper/src/Command.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/Command.cc?rev=11055&r1=11054&r2=11055&view=diff
==============================================================================
--- trunk/zypper/src/Command.cc (original)
+++ trunk/zypper/src/Command.cc Fri Sep 12 18:23:54 2008
@@ -24,6 +24,7 @@
const ZypperCommand ZypperCommand::ADD_SERVICE(ZypperCommand::ADD_SERVICE_e);
const ZypperCommand ZypperCommand::REMOVE_SERVICE(ZypperCommand::REMOVE_SERVICE_e);
+const ZypperCommand ZypperCommand::MODIFY_SERVICE(ZypperCommand::MODIFY_SERVICE_e);
const ZypperCommand ZypperCommand::LIST_SERVICES(ZypperCommand::LIST_SERVICES_e);
const ZypperCommand ZypperCommand::REFRESH_SERVICES(ZypperCommand::REFRESH_SERVICES_e);
@@ -94,6 +95,7 @@
// initialize it
_table["addservice"] = _table["as"] = _table["service-add"] = _table["sa"] = ZypperCommand::ADD_SERVICE_e;
_table["removeservice"] = _table["rs"] = _table["service-delete"] = _table["sd"] = ZypperCommand::REMOVE_SERVICE_e;
+ _table["modifyservice"] = _table["ms"] = ZypperCommand::MODIFY_SERVICE_e;
_table["services"] = _table["ls"] = _table["service-list"] = _table["sl"] = ZypperCommand::LIST_SERVICES_e;
_table["refresh-services"] = _table["refs"] = ZypperCommand::REFRESH_SERVICES_e;
Modified: trunk/zypper/src/Command.h
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/Command.h?rev=11055&r1=11054&r2=11055&view=diff
==============================================================================
--- trunk/zypper/src/Command.h (original)
+++ trunk/zypper/src/Command.h Fri Sep 12 18:23:54 2008
@@ -19,6 +19,7 @@
{
static const ZypperCommand ADD_SERVICE;
static const ZypperCommand REMOVE_SERVICE;
+ static const ZypperCommand MODIFY_SERVICE;
static const ZypperCommand LIST_SERVICES;
static const ZypperCommand REFRESH_SERVICES;
@@ -86,6 +87,7 @@
{
ADD_SERVICE_e,
REMOVE_SERVICE_e,
+ MODIFY_SERVICE_e,
LIST_SERVICES_e,
REFRESH_SERVICES_e,
Modified: trunk/zypper/src/Zypper.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/Zypper.cc?rev=11055&r1=11054&r2=11055&view=diff
==============================================================================
--- trunk/zypper/src/Zypper.cc (original)
+++ trunk/zypper/src/Zypper.cc Fri Sep 12 18:23:54 2008
@@ -198,6 +198,7 @@
static string help_service_commands = _("\tService Handling:\n"
"\tservices, ls\t\tList all defined services.\n"
"\taddservice, as\t\tAdd a new service.\n"
+ "\tmodifyservice, ms\tModify specified service.\n"
"\tremoveservice, rs\tRemove specified service.\n"
"\trefresh-services, refs\tRefresh all services.\n"
);
@@ -1028,6 +1029,45 @@
);
break;
}
+
+ case ZypperCommand::MODIFY_SERVICE_e:
+ {
+ static struct option service_modify_options[] = {
+ {"help", no_argument, 0, 'h'},
+ {"disable", no_argument, 0, 'd'},
+ {"enable", no_argument, 0, 'e'},
+ {"refresh", no_argument, 0, 'r'},
+ {"no-refresh", no_argument, 0, 'R'},
+ {"name", required_argument, 0, 'n'},
+ {"all", no_argument, 0, 'a' },
+ {"local", no_argument, 0, 'l' },
+ {"remote", no_argument, 0, 't' },
+ {"medium-type", required_argument, 0, 'm' },
+ {0, 0, 0, 0}
+ };
+ specific_options = service_modify_options;
+ _command_help = str::form(_(
+ // translators: %s is "--all" and "--all"
+ "modifyservice (ms) <options> \n"
+ "modifyservice (ms) <options> <%s>\n"
+ "\n"
+ "Modify properties of repositories specified by alias, number or URI or"
+ " all.\n"
+ "\n"
+ " Command options:\n"
+ "-d, --disable Disable the service (but don't remove it).\n"
+ "-e, --enable Enable a disabled service.\n"
+ "-r, --refresh Enable auto-refresh of the service.\n"
+ "-R, --no-refresh Disable auto-refresh of the service.\n"
+ "-n, --name Set a descriptive name for the service.\n"
+ "-a, --all Apply changes to all services.\n"
+ "-l, --local Apply changes to all local services.\n"
+ "-t, --remote Apply changes to all remote services.\n"
+ "-m, --medium-type <type> Apply changes to services of specified type.\n"
+ ), "--all|--remote|--local|--medium-type"
+ , "--all, --remote, --local, --medium-type");
+ break;
+ }
case ZypperCommand::LIST_SERVICES_e:
{
@@ -1221,9 +1261,7 @@
{"disable", no_argument, 0, 'd'},
{"enable", no_argument, 0, 'e'},
{"refresh", no_argument, 0, 'r'},
- {"enable-autorefresh", no_argument, 0, 0 }, // backward compatibility
{"no-refresh", no_argument, 0, 'R'},
- {"disable-autorefresh", no_argument, 0, 0 }, // backward compatibility
{"name", required_argument, 0, 'n'},
{"priority", required_argument, 0, 'p'},
{"keep-packages", no_argument, 0, 'k'},
@@ -2213,6 +2251,63 @@
break;
}
+ case ZypperCommand::MODIFY_SERVICE_e:
+ {
+ if (runningHelp()) { out().info(_command_help, Out::QUIET); return; }
+
+ // check root user
+ if (geteuid() != 0 && !globalOpts().changedRoot)
+ {
+ out().error(
+ _("Root privileges are required for modifying services."));
+ setExitCode(ZYPPER_EXIT_ERR_PRIVILEGES);
+ return;
+ }
+
+ bool non_alias = copts.count("all") || copts.count("local") ||
+ copts.count("remote") || copts.count("medium-type");
+
+ if (_arguments.size() < 1 && !non_alias)
+ {
+ // translators: aggregate option is e.g. "--all". This message will be
+ // followed by ms command help text which will explain it
+ out().error(_("Alias or an aggregate option is required."));
+ ERR << "No alias argument given." << endl;
+ out().info(_command_help);
+ setExitCode(ZYPPER_EXIT_ERR_INVALID_ARGS);
+ return;
+ }
+ // too many arguments
+ if (_arguments.size() > 1
+ || (_arguments.size() > 0 && non_alias))
+ {
+ report_too_many_arguments(_command_help);
+ setExitCode(ZYPPER_EXIT_ERR_INVALID_ARGS);
+ return;
+ }
+
+ if (non_alias)
+ {
+ modify_services_by_option(*this);
+ }
+ else
+ {
+ repo::RepoInfoBase_Ptr srv;
+ if (match_service(*this, _arguments[0], srv))
+ {
+ modify_service(*this, srv->alias());
+ }
+ else
+ {
+ out().error(
+ boost::str(format(_("Service '%s' not found.")) % _arguments[0]));
+ ERR << "Service " << _arguments[0] << " not found" << endl;
+ }
+ }
+
+ break;
+ }
+
// --------------------------( repo list )----------------------------------
case ZypperCommand::LIST_REPOS_e:
Modified: trunk/zypper/src/repos.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/repos.cc?rev=11055&r1=11054&r2=11055&view=diff
==============================================================================
--- trunk/zypper/src/repos.cc (original)
+++ trunk/zypper/src/repos.cc Fri Sep 12 18:23:54 2008
@@ -1490,6 +1490,7 @@
}
// ----------------------------------------------------------------------------
+
void modify_repos_by_option( Zypper & zypper )
{
RepoManager manager(zypper.globalOpts().rm_options);
@@ -1561,69 +1562,23 @@
}
}
+
+// ----------------------------------------------------------------------------
+
void modify_repo(Zypper & zypper, const string & alias)
{
- // tell whether currenlty processed options are contradicting each other
- // bool contradiction = false;
- string msg_contradition =
- // translators: speaking of two mutually contradicting command line options
- _("%s used together with %s, which contradict each other."
- " This property will be left unchanged.");
-
// enable/disable repo
- tribool enable = indeterminate;
- if (copts.count("enable"))
- enable = true;
- if (copts.count("disable"))
- {
- if (enable)
- {
- zypper.out().warning(boost::str(format(msg_contradition)
- % "--enable" % "--disable"), Out::QUIET);
-
- enable = indeterminate;
- }
- else
- enable = false;
- }
+ tribool enable = get_boolean_option(zypper, "enable", "disable");
DBG << "enable = " << enable << endl;
// autorefresh
- tribool autoref = indeterminate;
- if (copts.count("refresh"))
- autoref = true;
- if (copts.count("no-refresh"))
- {
- if (autoref)
- {
- zypper.out().warning(boost::str(format(msg_contradition)
- % "--refresh" % "--no-refresh"));
-
- autoref = indeterminate;
- }
- else
- autoref = false;
- }
+ tribool autoref = get_boolean_option(zypper, "refresh", "no-refresh");
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;
- }
+ tribool keepPackages = get_boolean_option(
+ zypper, "keep-packages", "no-keep-packages");
DBG << "keepPackages = " << keepPackages << endl;
-
try
{
RepoManager manager(zypper.globalOpts().rm_options);
@@ -2416,6 +2371,168 @@
// ---------------------------------------------------------------------------
+void modify_service(Zypper & zypper, const string & alias)
+{
+ // enable/disable repo
+ tribool enable = get_boolean_option(zypper,"enable", "disable");
+ DBG << "enable = " << enable << endl;
+
+ // autorefresh
+ tribool autoref = get_boolean_option(zypper,"refresh", "no-refresh");
+ DBG << "autoref = " << autoref << endl;
+
+ try
+ {
+ RepoManager manager(zypper.globalOpts().rm_options);
+ ServiceInfo srv(manager.getService(alias));
+ bool chnaged_enabled = false;
+ bool changed_autoref = false;
+
+ if (!indeterminate(enable))
+ {
+ if (enable != srv.enabled())
+ chnaged_enabled = true;
+ srv.setEnabled(enable);
+ }
+
+ if (!indeterminate(autoref))
+ {
+ if (autoref != srv.autorefresh())
+ changed_autoref = true;
+ srv.setAutorefresh(autoref);
+ }
+
+ string name;
+ parsed_opts::const_iterator tmp1;
+ if ((tmp1 = zypper.cOpts().find("name")) != zypper.cOpts().end())
+ {
+ name = *tmp1->second.begin();
+ if (!name.empty())
+ srv.setName(name);
+ }
+
+ if (chnaged_enabled || changed_autoref | !name.empty())
+ {
+ manager.modifyService(alias, srv);
+
+ if (chnaged_enabled)
+ {
+ if (srv.enabled())
+ zypper.out().info(boost::str(format(
+ _("Service '%s' has been sucessfully enabled.")) % alias));
+ else
+ zypper.out().info(boost::str(format(
+ _("Service '%s' has been sucessfully disabled.")) % alias));
+ }
+
+ if (changed_autoref)
+ {
+ if (srv.autorefresh())
+ zypper.out().info(boost::str(format(
+ _("Autorefresh has been enabled for service '%s'.")) % alias));
+ else
+ zypper.out().info(boost::str(format(
+ _("Autorefresh has been disabled for service '%s'.")) % alias));
+ }
+
+ if (!name.empty())
+ {
+ zypper.out().info(boost::str(format(
+ _("Name of service '%s' has been set to '%s'.")) % alias % name));
+ }
+ }
+ else
+ {
+ zypper.out().info(boost::str(format(
+ _("Nothing to change for service '%s'.")) % alias));
+ MIL << format("Nothing to modify in '%s':") % alias << srv << endl;
+ }
+ }
+ catch (const Exception & ex)
+ {
+ zypper.out().error(ex,
+ _("Error while modifying the service:"),
+ boost::str(format(_("Leaving service %s unchanged.")) % alias));
+
+ ERR << "Error while modifying the service:" << ex.asUserString() << endl;
+ }
+}
+
+// ---------------------------------------------------------------------------
+
+void modify_services_by_option( Zypper & zypper )
+{
+ RepoManager manager(zypper.globalOpts().rm_options);
+ ServiceList known = get_all_services(zypper);
+ set<string> repos_to_modify;
+ set<string> services_to_modify;
+
+ ServiceInfo_Ptr sptr;
+ RepoInfo_Ptr rptr;
+
+ if ( copts.count("all") )
+ {
+ for_(it, known.begin(), known.end())
+ {
+ if (sptr = dynamic_pointer_cast<ServiceInfo>(*it))
+ modify_service( zypper, sptr->alias() );
+ else
+ modify_repo( zypper, (*it)->alias() );
+ }
+ return;
+ }
+
+ bool local = copts.count("local");
+ bool remote = copts.count("remote");
+ list<string> pars = copts["medium-type"];
+ set<string> schemes(pars.begin(), pars.end());
+
+ for_(it, known.begin(), known.end())
+ {
+ Url url;
+ if (sptr = dynamic_pointer_cast<ServiceInfo>(*it))
+ url = sptr->url();
+ else
+ {
+ rptr = dynamic_pointer_cast<RepoInfo>(*it);
+ if (!rptr->baseUrlsEmpty())
+ url = *rptr->baseUrlsBegin();
+ }
+
+ if (url.isValid())
+ {
+ bool modify = false;
+ if (local && !MediaAccess::downloads( url ) )
+ modify = true;
+
+ if (!modify && remote && MediaAccess::downloads( url ) )
+ modify = true;
+
+ if (!modify && schemes.find(url.getScheme()) != schemes.end())
+ modify = true;
+
+ if (modify)
+ {
+ string alias = (*it)->alias();
+ if (sptr)
+ services_to_modify.insert( alias );
+ else
+ repos_to_modify.insert( alias );
+ }
+ }
+ else
+ WAR << "got invalid url: " << url.asString() << endl;
+ }
+
+ for_(it, services_to_modify.begin(), services_to_modify.end())
+ modify_service( zypper, *it );
+
+ for_(it, repos_to_modify.begin(), repos_to_modify.end())
+ modify_repo( zypper, *it );
+}
+
+// ---------------------------------------------------------------------------
+
// ---------------------------------------------------------------------------
void load_resolvables(Zypper & zypper)
Modified: trunk/zypper/src/repos.h
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/repos.h?rev=11055&r1=11054&r2=11055&view=diff
==============================================================================
--- trunk/zypper/src/repos.h (original)
+++ trunk/zypper/src/repos.h Fri Sep 12 18:23:54 2008
@@ -148,12 +148,16 @@
void remove_service(Zypper & zypper, const zypp::ServiceInfo & service);
+void modify_service(Zypper & zypper, const std::string & alias);
+
void refresh_services(Zypper & zypper);
bool match_service(Zypper & zypper,
std::string str,
zypp::repo::RepoInfoBase_Ptr & service_ptr);
+void modify_services_by_option( Zypper & zypper );
+
/**
* Initialize rpm database on target, if not already initialized.
*/
Modified: trunk/zypper/src/utils/getopt.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/utils/getopt.cc?rev=11055&r1=11054&r2=11055&view=diff
==============================================================================
--- trunk/zypper/src/utils/getopt.cc (original)
+++ trunk/zypper/src/utils/getopt.cc Fri Sep 12 18:23:54 2008
@@ -2,6 +2,7 @@
#include "main.h"
#include <iostream>
#include "zypp/base/String.h"
+#include "Zypper.h"
using namespace std;
@@ -73,6 +74,39 @@
return result;
}
+using boost::tribool;
+using boost::indeterminate;
+
+tribool get_boolean_option(
+ Zypper & zypper,
+ const string & pname,
+ const string & nname )
+{
+ static string msg_contradition =
+ // translators: speaking of two mutually contradicting command line options
+ _("%s used together with %s, which contradict each other."
+ " This property will be left unchanged.");
+
+ tribool result = indeterminate;
+ if (copts.count(pname))
+ result = true;
+ if (copts.count(nname))
+ {
+ if (result)
+ {
+ string po = "--" + pname;
+ string no = "--" + nname;
+ // report contradition
+ zypper.out().warning(zypp::str::form(
+ msg_contradition.c_str(), po.c_str(), no.c_str()), Out::QUIET);
+
+ result = indeterminate;
+ }
+ else
+ result = false;
+ }
+ return result;
+}
Args::Args (const std::string& s)
: _argv(NULL) {
Modified: trunk/zypper/src/utils/getopt.h
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/utils/getopt.h?rev=11055&r1=11054&r2=11055&view=diff
==============================================================================
--- trunk/zypper/src/utils/getopt.h (original)
+++ trunk/zypper/src/utils/getopt.h Fri Sep 12 18:23:54 2008
@@ -8,12 +8,24 @@
#include
+#include //! \todo replace with zypp's tribool
+
typedef std::mapstd::string > parsed_opts;
// longopts.flag must be NULL
parsed_opts parse_options (int argc, char **argv,
const struct option *longopts);
+class Zypper;
+
+/**
+ *
+ */
+boost::tribool get_boolean_option(
+ Zypper & zypper,
+ const std::string & pname,
+ const std::string & nname );
+
//! Parse a single string to an array of char* usable for getopt_long.
class Args {
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org