s4 net: Add spn module to list/add/remove spn on objects
[mat/samba.git] / source4 / scripting / python / samba / netcmd / __init__.py
1 #!/usr/bin/env 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
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([x.upper() for x in self.takes_args])
53         return ret
54
55     synopsis = property(_get_synopsis)
56
57     outf = sys.stdout
58
59     takes_args = []
60     takes_options = []
61     takes_optiongroups = {}
62
63     def _create_parser(self):
64         parser = optparse.OptionParser(self.synopsis)
65         parser.prog = "net"
66         parser.add_options(self.takes_options)
67         optiongroups = {}
68         for name, optiongroup in self.takes_optiongroups.iteritems():
69             optiongroups[name] = optiongroup(parser)
70             parser.add_option_group(optiongroups[name])
71         return parser, optiongroups
72
73     def message(self, text):
74         print text
75
76     def _run(self, *argv):
77         parser, optiongroups = self._create_parser()
78         opts, args = parser.parse_args(list(argv))
79         # Filter out options from option groups
80         args = args[1:]
81         kwargs = dict(opts.__dict__)
82         for option_group in parser.option_groups:
83             for option in option_group.option_list:
84                 if option.dest is not None:
85                     del kwargs[option.dest]
86         kwargs.update(optiongroups)
87         min_args = 0
88         max_args = 0
89         for i, arg in enumerate(self.takes_args):
90             if arg[-1] not in ("?", "*"):
91                 min_args += 1
92             max_args += 1
93             if arg[-1] == "*":
94                 max_args = -1
95         if len(args) < min_args or (max_args != -1 and len(args) > max_args):
96             self.usage(*args)
97             return -1
98         try:
99             return self.run(*args, **kwargs)
100         except CommandError, e:
101             print >>sys.stderr, "ERROR: %s" % e
102             return -1
103
104     def run(self):
105         """Run the command. This should be overriden by all subclasses."""
106         raise NotImplementedError(self.run)
107
108
109 class SuperCommand(Command):
110     """A command with subcommands."""
111
112     subcommands = {}
113
114     def _run(self, myname, subcommand=None, *args):
115         if subcommand is None:
116             print "Available subcommands:"
117             for subcommand in self.subcommands:
118                 print "\t%s" % subcommand
119             return 0
120         if not subcommand in self.subcommands:
121             raise CommandError("No such subcommand '%s'" % subcommand)
122         return self.subcommands[subcommand]._run(subcommand, *args)
123
124     def usage(self, myname, subcommand=None, *args):
125         if subcommand is None or not subcommand in self.subcommands:
126             print "Usage: %s (%s) [options]" % (myname,
127                 " | ".join(self.subcommands.keys()))
128         else:
129             return self.subcommands[subcommand].usage(*args)
130
131
132 class CommandError(Exception):
133     pass
134
135
136 commands = {}
137 from samba.netcmd.pwsettings import cmd_pwsettings
138 commands["pwsettings"] = cmd_pwsettings()
139 from samba.netcmd.domainlevel import cmd_domainlevel
140 commands["domainlevel"] = cmd_domainlevel()
141 from samba.netcmd.setpassword import cmd_setpassword
142 commands["setpassword"] = cmd_setpassword()
143 from samba.netcmd.setexpiry import cmd_setexpiry
144 commands["setexpiry"] = cmd_setexpiry()
145 from samba.netcmd.enableaccount import cmd_enableaccount
146 commands["enableaccount"] = cmd_enableaccount()
147 from samba.netcmd.newuser import cmd_newuser
148 commands["newuser"] = cmd_newuser()
149 from samba.netcmd.netacl import cmd_acl
150 commands["acl"] = cmd_acl()
151 from samba.netcmd.fsmo import cmd_fsmo
152 commands["fsmo"] = cmd_fsmo()
153 from samba.netcmd.export import cmd_export
154 commands["export"] = cmd_export()
155 from samba.netcmd.time import cmd_time
156 commands["time"] = cmd_time()
157 from samba.netcmd.user import cmd_user
158 commands["user"] = cmd_user()
159 from samba.netcmd.vampire import cmd_vampire
160 commands["vampire"] = cmd_vampire()
161 from samba.netcmd.machinepw import cmd_machinepw
162 commands["machinepw"] = cmd_machinepw()
163 from samba.netcmd.spn import cmd_spn
164 commands["spn"] = cmd_spn()
165 from samba.netcmd.group import cmd_group
166 commands["group"] = cmd_group()