cb8fa01fe1f7f0f4e69b98eb9d8839b8e1d813fe
[kai/samba-autobuild/.git] / source4 / scripting / python / samba / netcmd / __init__.py
1 #!/usr/bin/python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009
5 #
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.
10 #
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.
15 #
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/>.
18 #
19
20 import optparse
21 from samba import getopt as options, Ldb
22 import sys
23
24
25 class Option(optparse.Option):
26     pass
27
28
29 class Command(object):
30     """A net command."""
31
32     def _get_description(self):
33         return self.__doc__.splitlines()[0].rstrip("\n")
34
35     def _get_name(self):
36         name = self.__class__.__name__
37         if name.startswith("cmd_"):
38             return name[4:]
39         return name
40
41     name = property(_get_name)
42
43     def usage(self, args):
44         parser, _ = self._create_parser()
45         parser.print_usage()
46
47     description = property(_get_description)
48
49     def _get_synopsis(self):
50         ret = self.name
51         if self.takes_args:
52             ret += " " + " ".join(self.takes_args)
53         return ret
54
55     synopsis = property(_get_synopsis)
56
57     takes_args = []
58     takes_options = []
59     takes_optiongroups = {}
60
61     def _create_parser(self):
62         parser = optparse.OptionParser(self.synopsis)
63         parser.prog = "net"
64         parser.add_options(self.takes_options)
65         optiongroups = {}
66         for name, optiongroup in self.takes_optiongroups.iteritems():
67             optiongroups[name] = optiongroup(parser)
68             parser.add_option_group(optiongroups[name])
69         return parser, optiongroups
70
71     def message(self, text):
72         print text
73
74     def _run(self, *argv):
75         parser, optiongroups = self._create_parser()
76         opts, args = parser.parse_args(list(argv))
77         # Filter out options from option groups
78         kwargs = dict(opts.__dict__)
79         for option_group in parser.option_groups:
80             for option in option_group.option_list:
81                 del kwargs[option.dest]
82         kwargs.update(optiongroups)
83         if len(args) < len(self.takes_args):
84             self.usage(args)
85             return -1
86         try:
87             return self.run(*args, **kwargs)
88         except CommandError, e:
89             print >>sys.stderr, "ERROR: %s" % e
90             return -1
91
92     def run(self):
93         """Run the command. This should be overriden by all subclasses."""
94         raise NotImplementedError(self.run)
95
96
97 class SuperCommand(Command):
98     """A command with subcommands."""
99
100     subcommands = {}
101
102     def run(self, subcommand, *args, **kwargs):
103         if not subcommand in subcommands:
104             print >>sys.stderr, "ERROR: No such subcommand '%s'" % subcommand
105             return subcommands[subcommand].run(*args, **kwargs)
106
107     def usage(self, subcommand=None, *args, **kwargs):
108         if subcommand is None:
109             print "Available subcommands"
110             for subcommand in subcommands:
111                 print "\t%s" % subcommand
112             return 0
113         else:
114             if not subcommand in subcommands:
115                 print >>sys.stderr, "ERROR: No such subcommand '%s'" % subcommand
116             return subcommands[subcommand].usage(*args, **kwargs)
117
118
119 class CommandError(Exception):
120     pass
121
122
123 commands = {}
124 from samba.netcmd.pwsettings import cmd_pwsettings
125 commands["pwsettings"] = cmd_pwsettings()
126 from samba.netcmd.domainlevel import cmd_domainlevel
127 commands["domainlevel"] = cmd_domainlevel()