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/>.
21 from samba
import getopt
as options
, Ldb
25 class Option(optparse
.Option
):
29 class Command(object):
32 def _get_description(self
):
33 return self
.__doc
__.splitlines()[0].rstrip("\n")
36 name
= self
.__class
__.__name
__
37 if name
.startswith("cmd_"):
41 name
= property(_get_name
)
43 def usage(self
, args
):
44 parser
, _
= self
._create
_parser
()
47 description
= property(_get_description
)
49 def _get_synopsis(self
):
52 ret
+= " " + " ".join(self
.takes_args
)
55 synopsis
= property(_get_synopsis
)
59 takes_optiongroups
= {}
61 def _create_parser(self
):
62 parser
= optparse
.OptionParser(self
.synopsis
)
64 parser
.add_options(self
.takes_options
)
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
):
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
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 for i
, arg
in enumerate(self
.takes_args
):
89 if len(args
) > len(self
.takes_args
):
93 return self
.run(*args
, **kwargs
)
94 except CommandError
, e
:
95 print >>sys
.stderr
, "ERROR: %s" % e
99 """Run the command. This should be overriden by all subclasses."""
100 raise NotImplementedError(self
.run
)
103 class SuperCommand(Command
):
104 """A command with subcommands."""
108 def run(self
, subcommand
, *args
, **kwargs
):
109 if not subcommand
in subcommands
:
110 print >>sys
.stderr
, "ERROR: No such subcommand '%s'" % subcommand
111 return subcommands
[subcommand
].run(*args
, **kwargs
)
113 def usage(self
, subcommand
=None, *args
, **kwargs
):
114 if subcommand
is None:
115 print "Available subcommands"
116 for subcommand
in subcommands
:
117 print "\t%s" % subcommand
120 if not subcommand
in subcommands
:
121 print >>sys
.stderr
, "ERROR: No such subcommand '%s'" % subcommand
122 return subcommands
[subcommand
].usage(*args
, **kwargs
)
125 class CommandError(Exception):
130 from samba
.netcmd
.pwsettings
import cmd_pwsettings
131 commands
["pwsettings"] = cmd_pwsettings()
132 from samba
.netcmd
.domainlevel
import cmd_domainlevel
133 commands
["domainlevel"] = cmd_domainlevel()
134 from samba
.netcmd
.setpassword
import cmd_setpassword
135 commands
["setpassword"] = cmd_setpassword()
136 from samba
.netcmd
.setexpiry
import cmd_setexpiry
137 commands
["setexpiry"] = cmd_setexpiry()
138 from samba
.netcmd
.enableaccount
import cmd_enableaccount
139 commands
["enableaccount"] = cmd_enableaccount()