mirror of
https://github.com/samba-team/samba.git
synced 2025-07-30 19:42:05 +03:00
Use CommandError exception to deal with problems during net commands.
This commit is contained in:
committed by
Jelmer Vernooij
parent
eaf4a9afb2
commit
732a7630e9
@ -2,17 +2,17 @@
|
|||||||
|
|
||||||
# Samba-specific bits for optparse
|
# Samba-specific bits for optparse
|
||||||
# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
|
# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify
|
# This program is free software; you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
# the Free Software Foundation; either version 3 of the License, or
|
# the Free Software Foundation; either version 3 of the License, or
|
||||||
# (at your option) any later version.
|
# (at your option) any later version.
|
||||||
#
|
#
|
||||||
# This program is distributed in the hope that it will be useful,
|
# This program is distributed in the hope that it will be useful,
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
# GNU General Public License for more details.
|
# GNU General Public License for more details.
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
@ -30,7 +30,7 @@ class SambaOptions(optparse.OptionGroup):
|
|||||||
def __init__(self, parser):
|
def __init__(self, parser):
|
||||||
optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
|
optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
|
||||||
self.add_option("-s", "--configfile", action="callback",
|
self.add_option("-s", "--configfile", action="callback",
|
||||||
type=str, metavar="FILE", help="Configuration file",
|
type=str, metavar="FILE", help="Configuration file",
|
||||||
callback=self._load_configfile)
|
callback=self._load_configfile)
|
||||||
self._configfile = None
|
self._configfile = None
|
||||||
|
|
||||||
@ -73,15 +73,15 @@ class CredentialsOptions(optparse.OptionGroup):
|
|||||||
help="DN to use for a simple bind")
|
help="DN to use for a simple bind")
|
||||||
self.add_option("--password", metavar="PASSWORD", action="callback",
|
self.add_option("--password", metavar="PASSWORD", action="callback",
|
||||||
help="Password", type=str, callback=self._set_password)
|
help="Password", type=str, callback=self._set_password)
|
||||||
self.add_option("-U", "--username", metavar="USERNAME",
|
self.add_option("-U", "--username", metavar="USERNAME",
|
||||||
action="callback", type=str,
|
action="callback", type=str,
|
||||||
help="Username", callback=self._parse_username)
|
help="Username", callback=self._parse_username)
|
||||||
self.add_option("-W", "--workgroup", metavar="WORKGROUP",
|
self.add_option("-W", "--workgroup", metavar="WORKGROUP",
|
||||||
action="callback", type=str,
|
action="callback", type=str,
|
||||||
help="Workgroup", callback=self._parse_workgroup)
|
help="Workgroup", callback=self._parse_workgroup)
|
||||||
self.add_option("-N", "--no-pass", action="store_true",
|
self.add_option("-N", "--no-pass", action="store_true",
|
||||||
help="Don't ask for a password")
|
help="Don't ask for a password")
|
||||||
self.add_option("-k", "--kerberos", metavar="KERBEROS",
|
self.add_option("-k", "--kerberos", metavar="KERBEROS",
|
||||||
action="callback", type=str,
|
action="callback", type=str,
|
||||||
help="Use Kerberos", callback=self._set_kerberos)
|
help="Use Kerberos", callback=self._set_kerberos)
|
||||||
self.creds = Credentials()
|
self.creds = Credentials()
|
||||||
|
@ -29,7 +29,7 @@ class Command(object):
|
|||||||
"""A net command."""
|
"""A net command."""
|
||||||
|
|
||||||
def _get_description(self):
|
def _get_description(self):
|
||||||
return self.__doc__.splitlines()[-1].rstrip("\n")
|
return self.__doc__.splitlines()[0].rstrip("\n")
|
||||||
|
|
||||||
def _get_name(self):
|
def _get_name(self):
|
||||||
name = self.__class__.__name__
|
name = self.__class__.__name__
|
||||||
@ -44,15 +44,26 @@ class Command(object):
|
|||||||
|
|
||||||
description = property(_get_description)
|
description = property(_get_description)
|
||||||
|
|
||||||
|
def _get_synopsis(self):
|
||||||
|
ret = self.name
|
||||||
|
if self.takes_args:
|
||||||
|
ret += " " + " ".join(self.takes_args)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
synopsis = property(_get_synopsis)
|
||||||
|
|
||||||
takes_args = []
|
takes_args = []
|
||||||
takes_options = []
|
takes_options = []
|
||||||
|
takes_optiongroups = []
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
synopsis = self.name
|
self.parser = optparse.OptionParser(self.synopsis)
|
||||||
if self.takes_args:
|
|
||||||
synopsis += " " + " ".join(self.takes_args)
|
|
||||||
self.parser = optparse.OptionParser(synopsis)
|
|
||||||
self.parser.add_options(self.takes_options)
|
self.parser.add_options(self.takes_options)
|
||||||
|
for optiongroup in self.takes_optiongroups:
|
||||||
|
self.parser.add_option_group(optiongroup(self.parser))
|
||||||
|
|
||||||
|
def message(self, text):
|
||||||
|
print text
|
||||||
|
|
||||||
def _run(self, *argv):
|
def _run(self, *argv):
|
||||||
opts, args = self.parser.parse_args(list(argv))
|
opts, args = self.parser.parse_args(list(argv))
|
||||||
@ -70,8 +81,12 @@ class SuperCommand(Command):
|
|||||||
|
|
||||||
def run(self, subcommand, *args, **kwargs):
|
def run(self, subcommand, *args, **kwargs):
|
||||||
if not subcommand in subcommands:
|
if not subcommand in subcommands:
|
||||||
print >>sys.stderr, "No such subcommand '%s'" % subcommand
|
print >>sys.stderr, "ERROR: No such subcommand '%s'" % subcommand
|
||||||
return subcommands[subcommand].run(*args, **kwargs)
|
try:
|
||||||
|
return subcommands[subcommand].run(*args, **kwargs)
|
||||||
|
except CommandError, e:
|
||||||
|
print >>sys.stderr, "ERROR: %s" % e.message
|
||||||
|
return -1
|
||||||
|
|
||||||
def usage(self, subcommand=None, *args, **kwargs):
|
def usage(self, subcommand=None, *args, **kwargs):
|
||||||
if subcommand is None:
|
if subcommand is None:
|
||||||
@ -81,15 +96,14 @@ class SuperCommand(Command):
|
|||||||
return 0
|
return 0
|
||||||
else:
|
else:
|
||||||
if not subcommand in subcommands:
|
if not subcommand in subcommands:
|
||||||
print >>sys.stderr, "No such subcommand '%s'" % subcommand
|
print >>sys.stderr, "ERROR: No such subcommand '%s'" % subcommand
|
||||||
return subcommands[subcommand].usage(*args, **kwargs)
|
return subcommands[subcommand].usage(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class FooCommand(Command):
|
class CommandError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
def run(self, bar):
|
|
||||||
print "LALALA" + bar
|
|
||||||
return 0
|
|
||||||
|
|
||||||
commands = {}
|
commands = {}
|
||||||
commands["foo"] = FooCommand()
|
from samba.netcmd.pwsettings import cmd_pwsettings
|
||||||
|
commands["pwsettings"] = cmd_pwsettings()
|
||||||
|
Reference in New Issue
Block a user