Implemented net fsmo command for transferring fsmo roles
[Samba/fernandojvsilva.git] / source4 / scripting / python / samba / netcmd / __init__.py
blob09c8cc3f0879880d3881961e4fb4b0df7bb45c9e
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
22 import sys
25 class Option(optparse.Option):
26 pass
29 class Command(object):
30 """A net command."""
32 def _get_description(self):
33 return self.__doc__.splitlines()[0].rstrip("\n")
35 def _get_name(self):
36 name = self.__class__.__name__
37 if name.startswith("cmd_"):
38 return name[4:]
39 return name
41 name = property(_get_name)
43 def usage(self, *args):
44 parser, _ = self._create_parser()
45 parser.print_usage()
47 description = property(_get_description)
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
55 synopsis = property(_get_synopsis)
57 takes_args = []
58 takes_options = []
59 takes_optiongroups = {}
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
71 def message(self, text):
72 print text
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 args = args[1:]
79 kwargs = dict(opts.__dict__)
80 for option_group in parser.option_groups:
81 for option in option_group.option_list:
82 del kwargs[option.dest]
83 kwargs.update(optiongroups)
84 min_args = 0
85 max_args = 0
86 for i, arg in enumerate(self.takes_args):
87 if arg[-1] not in ("?", "*"):
88 min_args += 1
89 max_args += 1
90 if arg[-1] == "*":
91 max_args = -1
92 if len(args) < min_args or (max_args != -1 and len(args) > max_args):
93 self.usage(*args)
94 return -1
95 try:
96 return self.run(*args, **kwargs)
97 except CommandError, e:
98 print >>sys.stderr, "ERROR: %s" % e
99 return -1
101 def run(self):
102 """Run the command. This should be overriden by all subclasses."""
103 raise NotImplementedError(self.run)
106 class SuperCommand(Command):
107 """A command with subcommands."""
109 subcommands = {}
111 def _run(self, myname, subcommand=None, *args):
112 if subcommand is None:
113 print "Available subcommands:"
114 for subcommand in self.subcommands:
115 print "\t%s" % subcommand
116 return 0
117 if not subcommand in self.subcommands:
118 raise CommandError("No such subcommand '%s'" % subcommand)
119 return self.subcommands[subcommand]._run(subcommand, *args)
121 def usage(self, myname, subcommand=None, *args):
122 if subcommand is None or not subcommand in self.subcommands:
123 print "Usage: %s (%s) [options]" % (myname,
124 " | ".join(self.subcommands.keys()))
125 else:
126 return self.subcommands[subcommand].usage(*args)
129 class CommandError(Exception):
130 pass
133 commands = {}
134 from samba.netcmd.pwsettings import cmd_pwsettings
135 commands["pwsettings"] = cmd_pwsettings()
136 from samba.netcmd.domainlevel import cmd_domainlevel
137 commands["domainlevel"] = cmd_domainlevel()
138 from samba.netcmd.setpassword import cmd_setpassword
139 commands["setpassword"] = cmd_setpassword()
140 from samba.netcmd.setexpiry import cmd_setexpiry
141 commands["setexpiry"] = cmd_setexpiry()
142 from samba.netcmd.enableaccount import cmd_enableaccount
143 commands["enableaccount"] = cmd_enableaccount()
144 from samba.netcmd.newuser import cmd_newuser
145 commands["newuser"] = cmd_newuser()
146 from samba.netcmd.ntacl import cmd_acl
147 commands["acl"] = cmd_acl()
148 from samba.netcmd.fsmo import cmd_fsmo
149 commands["fsmo"] = cmd_fsmo()