1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009-2012
3 # Copyright (C) Theresa Halloran <theresahalloran@gmail.com> 2011
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import optparse, samba
20 from samba import getopt as options
21 from samba import colour
22 from ldb import LdbError
27 class Option(optparse.Option):
30 # This help formatter does text wrapping and preserves newlines
33 class PlainHelpFormatter(optparse.IndentedHelpFormatter):
34 def format_description(self, description=""):
35 desc_width = self.width - self.current_indent
36 indent = " " * self.current_indent
37 paragraphs = description.split('\n')
38 wrapped_paragraphs = [
41 initial_indent=indent,
42 subsequent_indent=indent)
44 result = "\n".join(wrapped_paragraphs) + "\n"
47 def format_epilog(self, epilog):
49 return "\n" + epilog + "\n"
54 class Command(object):
55 """A samba-tool command."""
57 def _get_short_description(self):
58 return self.__doc__.splitlines()[0].rstrip("\n")
60 short_description = property(_get_short_description)
62 def _get_full_description(self):
63 lines = self.__doc__.split("\n")
64 return lines[0] + "\n" + textwrap.dedent("\n".join(lines[1:]))
66 full_description = property(_get_full_description)
69 name = self.__class__.__name__
70 if name.startswith("cmd_"):
74 name = property(_get_name)
76 # synopsis must be defined in all subclasses in order to provide the
81 takes_optiongroups = {}
89 def __init__(self, outf=sys.stdout, errf=sys.stderr):
93 def usage(self, prog, *args):
94 parser, _ = self._create_parser(prog)
97 def show_command_error(self, e):
98 '''display a command error'''
99 if isinstance(e, CommandError):
100 (etype, evalue, etraceback) = e.exception_info
101 inner_exception = e.inner_exception
103 force_traceback = False
105 (etype, evalue, etraceback) = sys.exc_info()
107 message = "uncaught exception"
108 force_traceback = True
110 if isinstance(inner_exception, LdbError):
111 (ldb_ecode, ldb_emsg) = inner_exception.args
112 self.errf.write("ERROR(ldb): %s - %s\n" % (message, ldb_emsg))
113 elif isinstance(inner_exception, AssertionError):
114 self.errf.write("ERROR(assert): %s\n" % message)
115 force_traceback = True
116 elif isinstance(inner_exception, RuntimeError):
117 self.errf.write("ERROR(runtime): %s - %s\n" % (message, evalue))
118 elif type(inner_exception) is Exception:
119 self.errf.write("ERROR(exception): %s - %s\n" % (message, evalue))
120 force_traceback = True
121 elif inner_exception is None:
122 self.errf.write("ERROR: %s\n" % (message))
124 self.errf.write("ERROR(%s): %s - %s\n" % (str(etype), message, evalue))
125 force_traceback = True
127 if force_traceback or samba.get_debug_level() >= 3:
128 traceback.print_tb(etraceback, file=self.errf)
130 def _create_parser(self, prog, epilog=None):
131 parser = optparse.OptionParser(
133 description=self.full_description,
134 formatter=PlainHelpFormatter(),
135 prog=prog, epilog=epilog)
136 parser.add_options(self.takes_options)
138 for name, optiongroup in self.takes_optiongroups.items():
139 optiongroups[name] = optiongroup(parser)
140 parser.add_option_group(optiongroups[name])
141 return parser, optiongroups
143 def message(self, text):
144 self.outf.write(text + "\n")
146 def _run(self, *argv):
147 parser, optiongroups = self._create_parser(argv[0])
148 opts, args = parser.parse_args(list(argv))
149 # Filter out options from option groups
151 kwargs = dict(opts.__dict__)
152 for option_group in parser.option_groups:
153 for option in option_group.option_list:
154 if option.dest is not None:
155 del kwargs[option.dest]
156 kwargs.update(optiongroups)
158 # Check for a min a max number of allowed arguments, whenever possible
159 # The suffix "?" means zero or one occurence
160 # The suffix "+" means at least one occurence
161 # The suffix "*" means zero or more occurences
164 undetermined_max_args = False
165 for i, arg in enumerate(self.takes_args):
166 if arg[-1] != "?" and arg[-1] != "*":
168 if arg[-1] == "+" or arg[-1] == "*":
169 undetermined_max_args = True
172 if (len(args) < min_args) or (not undetermined_max_args and len(args) > max_args):
176 self.raw_argv = list(argv)
178 self.raw_kwargs = kwargs
181 return self.run(*args, **kwargs)
182 except Exception as e:
183 self.show_command_error(e)
187 """Run the command. This should be overridden by all subclasses."""
188 raise NotImplementedError(self.run)
190 def get_logger(self, name="netcmd"):
191 """Get a logger object."""
193 logger = logging.getLogger(name)
194 logger.addHandler(logging.StreamHandler(self.errf))
197 def apply_colour_choice(self, requested):
198 """Heuristics to work out whether the user wants colour output, from a
199 --color=yes|no|auto option. This alters the ANSI 16 bit colour
200 "constants" in the colour module to be either real colours or empty
203 requested = requested.lower()
204 if requested == 'no':
205 colour.switch_colour_off()
207 elif requested == 'yes':
208 colour.switch_colour_on()
210 elif requested == 'auto':
211 if (hasattr(self.outf, 'isatty') and self.outf.isatty()):
212 colour.switch_colour_on()
214 colour.switch_colour_off()
217 raise CommandError("Unknown --color option: %s "
218 "please choose from yes, no, auto")
221 class SuperCommand(Command):
222 """A samba-tool command with subcommands."""
224 synopsis = "%prog <subcommand>"
228 def _run(self, myname, subcommand=None, *args):
229 if subcommand in self.subcommands:
230 return self.subcommands[subcommand]._run(
231 "%s %s" % (myname, subcommand), *args)
233 if subcommand == 'help':
234 # pass the request down
236 sub = self.subcommands.get(args[0])
237 if isinstance(sub, SuperCommand):
238 return sub._run("%s %s" % (myname, args[0]), 'help',
240 elif sub is not None:
241 return sub._run("%s %s" % (myname, args[0]), '--help',
244 subcommand = '--help'
246 epilog = "\nAvailable subcommands:\n"
247 subcmds = self.subcommands.keys()
249 max_length = max([len(c) for c in subcmds])
250 for cmd_name in subcmds:
251 cmd = self.subcommands[cmd_name]
253 epilog += " %*s - %s\n" % (
254 -max_length, cmd_name, cmd.short_description)
255 epilog += "For more help on a specific subcommand, please type: %s <subcommand> (-h|--help)\n" % myname
257 parser, optiongroups = self._create_parser(myname, epilog=epilog)
258 args_list = list(args)
260 args_list.insert(0, subcommand)
261 opts, args = parser.parse_args(args_list)
267 class CommandError(Exception):
268 """An exception class for samba-tool Command errors."""
270 def __init__(self, message, inner_exception=None):
271 self.message = message
272 self.inner_exception = inner_exception
273 self.exception_info = sys.exc_info()
276 return "CommandError(%s)" % self.message