# Unix SMB/CIFS implementation.
# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009
# Copyright (C) Theresa Halloran <theresahalloran@gmail.com> 2011
+# Copyright (C) Giampaolo Lauria <lauria2@yahoo.com> 2011
#
# 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
pass
+
class Command(object):
- """A net command."""
+ """A samba-tool command."""
def _get_description(self):
return self.__doc__.splitlines()[0].rstrip("\n")
description = property(_get_description)
- def _get_synopsis(self):
- ret = self.name
- if self.takes_args:
- ret += " " + " ".join([x.upper() for x in self.takes_args])
- return ret
-
def show_command_error(self, e):
'''display a command error'''
if isinstance(e, CommandError):
if force_traceback or samba.get_debug_level() >= 3:
traceback.print_tb(etraceback)
-
-
- synopsis = property(_get_synopsis)
+ sys.exit(1)
outf = sys.stdout
+ # synopsis must be defined in all subclasses in order to provide the command usage
+ synopsis = ""
takes_args = []
takes_options = []
- takes_optiongroups = {}
+ takes_optiongroups = {
+ "sambaopts": options.SambaOptions,
+ "credopts": options.CredentialsOptions,
+ "versionopts": options.VersionOptions,
+ }
def _create_parser(self):
parser = optparse.OptionParser(self.synopsis)
- parser.prog = "samba-tool"
parser.add_options(self.takes_options)
optiongroups = {}
for name, optiongroup in self.takes_optiongroups.iteritems():
if option.dest is not None:
del kwargs[option.dest]
kwargs.update(optiongroups)
+
+ # Check for a min a max number of allowed arguments, whenever possible
+ # The suffix "?" means zero or one occurence
+ # The suffix "+" means at least one occurence
min_args = 0
max_args = 0
+ undetermined_max_args = False
for i, arg in enumerate(self.takes_args):
- if arg[-1] not in ("?", "*"):
- min_args += 1
- max_args += 1
- if arg[-1] == "*":
- max_args = -1
- if len(args) < min_args or (max_args != -1 and len(args) > max_args):
- self.usage(*args)
+ if arg[-1] != "?":
+ min_args += 1
+ if arg[-1] == "+":
+ undetermined_max_args = True
+ else:
+ max_args += 1
+ if (len(args) < min_args) or (undetermined_max_args == False and len(args) > max_args):
+ parser.print_usage()
return -1
+
try:
return self.run(*args, **kwargs)
except Exception, e:
raise NotImplementedError(self.run)
+
class SuperCommand(Command):
- """A command with subcommands."""
+ """A samba-tool command with subcommands."""
subcommands = {}
def _run(self, myname, subcommand=None, *args):
if subcommand in self.subcommands:
return self.subcommands[subcommand]._run(subcommand, *args)
+ print "Usage: samba-tool %s <subcommand> [options]" % myname
print "Available subcommands:"
- for cmd in self.subcommands:
- print "\t%-20s - %s" % (cmd, self.subcommands[cmd].description)
- if subcommand in [None, 'help', '-h', '--help' ]:
+ subcmds = self.subcommands.keys()
+ subcmds.sort()
+ for cmd in subcmds:
+ print " %-20s - %s" % (cmd, self.subcommands[cmd].description)
+ if subcommand in [None]:
+ raise CommandError("You must specify a subcommand")
+ if subcommand in ['help', '-h', '--help']:
+ print "For more help on a specific subcommand, please type: samba-tool %s <subcommand> (-h|--help)" % myname
return 0
raise CommandError("No such subcommand '%s'" % subcommand)
def usage(self, myname, subcommand=None, *args):
if subcommand is None or not subcommand in self.subcommands:
- print "Usage: %s (%s) [options]" % (myname,
+ print "Usage: samba-tool %s (%s) [options]" % (myname,
" | ".join(self.subcommands.keys()))
else:
return self.subcommands[subcommand].usage(*args)
+
class CommandError(Exception):
- '''an exception class for netcmd errors'''
+ '''an exception class for samba-tool cmd errors'''
def __init__(self, message, inner_exception=None):
self.message = message
self.inner_exception = inner_exception
self.exception_info = sys.exc_info()
+
commands = {}
-from samba.netcmd.newuser import cmd_newuser
-commands["newuser"] = cmd_newuser()
from samba.netcmd.netacl import cmd_acl
commands["acl"] = cmd_acl()
from samba.netcmd.fsmo import cmd_fsmo