2009-12-28 03:04:33 +03:00
#!/usr/bin/python
# Unix SMB/CIFS implementation.
# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
2009-12-28 15:53:18 +03:00
import optparse
from samba import getopt as options , Ldb
class Option ( optparse . Option ) :
pass
2009-12-28 03:04:33 +03:00
class Command ( object ) :
""" A net command. """
def _get_description ( self ) :
2009-12-28 18:05:04 +03:00
return self . __doc__ . splitlines ( ) [ 0 ] . rstrip ( " \n " )
2009-12-28 15:53:18 +03:00
def _get_name ( self ) :
name = self . __class__ . __name__
if name . startswith ( " cmd_ " ) :
return name [ 4 : ]
return name
name = property ( _get_name )
2009-12-28 03:04:33 +03:00
2009-12-28 18:48:07 +03:00
def usage ( self , args ) :
parser , _ = self . _create_parser ( )
parser . print_usage ( )
2009-12-28 03:21:27 +03:00
2009-12-28 03:04:33 +03:00
description = property ( _get_description )
2009-12-28 18:05:04 +03:00
def _get_synopsis ( self ) :
ret = self . name
if self . takes_args :
ret + = " " + " " . join ( self . takes_args )
return ret
synopsis = property ( _get_synopsis )
2009-12-28 15:53:18 +03:00
takes_args = [ ]
takes_options = [ ]
2009-12-28 18:48:07 +03:00
takes_optiongroups = { }
def _create_parser ( self ) :
parser = optparse . OptionParser ( self . synopsis )
parser . prog = " net "
parser . add_options ( self . takes_options )
optiongroups = { }
for name , optiongroup in self . takes_optiongroups . iteritems ( ) :
optiongroups [ name ] = optiongroup ( parser )
parser . add_option_group ( optiongroups [ name ] )
return parser , optiongroups
2009-12-28 18:05:04 +03:00
def message ( self , text ) :
print text
2009-12-28 15:53:18 +03:00
def _run ( self , * argv ) :
2009-12-28 18:48:07 +03:00
parser , optiongroups = self . _create_parser ( )
opts , args = parser . parse_args ( list ( argv ) )
# Filter out options from option groups
kwargs = dict ( opts . __dict__ )
for option_group in parser . option_groups :
for option in option_group . option_list :
del kwargs [ option . dest ]
kwargs . update ( optiongroups )
if len ( args ) < len ( self . takes_args ) :
self . usage ( args )
return - 1
return self . run ( * args , * * kwargs )
2009-12-28 15:53:18 +03:00
2009-12-28 03:04:33 +03:00
def run ( self ) :
""" Run the command. This should be overriden by all subclasses. """
raise NotImplementedError ( self . run )
2009-12-28 15:53:18 +03:00
class SuperCommand ( Command ) :
""" A command with subcommands. """
subcommands = { }
def run ( self , subcommand , * args , * * kwargs ) :
if not subcommand in subcommands :
2009-12-28 18:05:04 +03:00
print >> sys . stderr , " ERROR: No such subcommand ' %s ' " % subcommand
try :
return subcommands [ subcommand ] . run ( * args , * * kwargs )
except CommandError , e :
print >> sys . stderr , " ERROR: %s " % e . message
return - 1
2009-12-28 15:53:18 +03:00
def usage ( self , subcommand = None , * args , * * kwargs ) :
if subcommand is None :
print " Available subcommands "
for subcommand in subcommands :
print " \t %s " % subcommand
return 0
else :
if not subcommand in subcommands :
2009-12-28 18:05:04 +03:00
print >> sys . stderr , " ERROR: No such subcommand ' %s ' " % subcommand
2009-12-28 15:53:18 +03:00
return subcommands [ subcommand ] . usage ( * args , * * kwargs )
2009-12-28 18:05:04 +03:00
class CommandError ( Exception ) :
pass
2009-12-28 15:53:18 +03:00
2009-12-28 03:04:33 +03:00
commands = { }
2009-12-28 18:05:04 +03:00
from samba . netcmd . pwsettings import cmd_pwsettings
commands [ " pwsettings " ] = cmd_pwsettings ( )
2009-12-28 18:48:07 +03:00
from samba . netcmd . domainlevel import cmd_domainlevel
commands [ " domainlevel " ] = cmd_domainlevel ( )