s4/net: Add domainlevel subcommand.
[Samba/eduardoll.git] / source4 / scripting / python / samba / netcmd / __init__.py
blob5c18d29fc3ae9e9fb024e08745d660c55d346cc6
1 #!/usr/bin/python
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 import optparse
21 from samba import getopt as options, Ldb
24 class Option(optparse.Option):
25 pass
28 class Command(object):
29 """A net command."""
31 def _get_description(self):
32 return self.__doc__.splitlines()[0].rstrip("\n")
34 def _get_name(self):
35 name = self.__class__.__name__
36 if name.startswith("cmd_"):
37 return name[4:]
38 return name
40 name = property(_get_name)
42 def usage(self, args):
43 parser, _ = self._create_parser()
44 parser.print_usage()
46 description = property(_get_description)
48 def _get_synopsis(self):
49 ret = self.name
50 if self.takes_args:
51 ret += " " + " ".join(self.takes_args)
52 return ret
54 synopsis = property(_get_synopsis)
56 takes_args = []
57 takes_options = []
58 takes_optiongroups = {}
60 def _create_parser(self):
61 parser = optparse.OptionParser(self.synopsis)
62 parser.prog = "net"
63 parser.add_options(self.takes_options)
64 optiongroups = {}
65 for name, optiongroup in self.takes_optiongroups.iteritems():
66 optiongroups[name] = optiongroup(parser)
67 parser.add_option_group(optiongroups[name])
68 return parser, optiongroups
70 def message(self, text):
71 print text
73 def _run(self, *argv):
74 parser, optiongroups = self._create_parser()
75 opts, args = parser.parse_args(list(argv))
76 # Filter out options from option groups
77 kwargs = dict(opts.__dict__)
78 for option_group in parser.option_groups:
79 for option in option_group.option_list:
80 del kwargs[option.dest]
81 kwargs.update(optiongroups)
82 if len(args) < len(self.takes_args):
83 self.usage(args)
84 return -1
85 return self.run(*args, **kwargs)
87 def run(self):
88 """Run the command. This should be overriden by all subclasses."""
89 raise NotImplementedError(self.run)
92 class SuperCommand(Command):
93 """A command with subcommands."""
95 subcommands = {}
97 def run(self, subcommand, *args, **kwargs):
98 if not subcommand in subcommands:
99 print >>sys.stderr, "ERROR: No such subcommand '%s'" % subcommand
100 try:
101 return subcommands[subcommand].run(*args, **kwargs)
102 except CommandError, e:
103 print >>sys.stderr, "ERROR: %s" % e.message
104 return -1
106 def usage(self, subcommand=None, *args, **kwargs):
107 if subcommand is None:
108 print "Available subcommands"
109 for subcommand in subcommands:
110 print "\t%s" % subcommand
111 return 0
112 else:
113 if not subcommand in subcommands:
114 print >>sys.stderr, "ERROR: No such subcommand '%s'" % subcommand
115 return subcommands[subcommand].usage(*args, **kwargs)
118 class CommandError(Exception):
119 pass
122 commands = {}
123 from samba.netcmd.pwsettings import cmd_pwsettings
124 commands["pwsettings"] = cmd_pwsettings()
125 from samba.netcmd.domainlevel import cmd_domainlevel
126 commands["domainlevel"] = cmd_domainlevel()