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
24 class Option(optparse
.Option
):
28 class Command(object):
31 def _get_description(self
):
32 return self
.__doc
__.splitlines()[0].rstrip("\n")
35 name
= self
.__class
__.__name
__
36 if name
.startswith("cmd_"):
40 name
= property(_get_name
)
42 def usage(self
, args
):
43 parser
, _
= self
._create
_parser
()
46 description
= property(_get_description
)
48 def _get_synopsis(self
):
51 ret
+= " " + " ".join(self
.takes_args
)
54 synopsis
= property(_get_synopsis
)
58 takes_optiongroups
= {}
60 def _create_parser(self
):
61 parser
= optparse
.OptionParser(self
.synopsis
)
63 parser
.add_options(self
.takes_options
)
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
):
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
):
85 return self
.run(*args
, **kwargs
)
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."""
97 def run(self
, subcommand
, *args
, **kwargs
):
98 if not subcommand
in subcommands
:
99 print >>sys
.stderr
, "ERROR: No such subcommand '%s'" % subcommand
101 return subcommands
[subcommand
].run(*args
, **kwargs
)
102 except CommandError
, e
:
103 print >>sys
.stderr
, "ERROR: %s" % e
.message
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
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):
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()