2010-06-24 08:33:58 +04:00
#!/usr/bin/env python
2009-12-28 03:04:33 +03:00
# 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
2010-03-01 06:39:53 +03:00
from samba import getopt as options
2009-12-28 22:37:48 +03:00
import sys
2009-12-28 15:53:18 +03:00
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-30 23:06:21 +03:00
def usage ( self , * args ) :
2009-12-28 18:48:07 +03:00
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 :
2009-12-30 23:06:21 +03:00
ret + = " " + " " . join ( [ x . upper ( ) for x in self . takes_args ] )
2009-12-28 18:05:04 +03:00
return ret
synopsis = property ( _get_synopsis )
2010-04-12 04:55:27 +04:00
outf = sys . stdout
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
2009-12-28 23:07:25 +03:00
args = args [ 1 : ]
2009-12-28 18:48:07 +03:00
kwargs = dict ( opts . __dict__ )
for option_group in parser . option_groups :
for option in option_group . option_list :
2010-04-09 04:37:20 +04:00
if option . dest is not None :
del kwargs [ option . dest ]
2009-12-28 18:48:07 +03:00
kwargs . update ( optiongroups )
2009-12-30 22:40:11 +03:00
min_args = 0
max_args = 0
2009-12-30 21:53:05 +03:00
for i , arg in enumerate ( self . takes_args ) :
2009-12-30 22:40:11 +03:00
if arg [ - 1 ] not in ( " ? " , " * " ) :
min_args + = 1
max_args + = 1
if arg [ - 1 ] == " * " :
max_args = - 1
if len ( args ) < min_args or ( max_args != - 1 and len ( args ) > max_args ) :
2009-12-30 23:06:21 +03:00
self . usage ( * args )
2009-12-28 18:48:07 +03:00
return - 1
2009-12-28 22:37:48 +03:00
try :
return self . run ( * args , * * kwargs )
except CommandError , e :
print >> sys . stderr , " ERROR: %s " % e
return - 1
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 = { }
2009-12-30 23:06:21 +03:00
def _run ( self , myname , subcommand = None , * args ) :
2009-12-28 15:53:18 +03:00
if subcommand is None :
2009-12-30 23:06:21 +03:00
print " Available subcommands: "
for subcommand in self . subcommands :
2009-12-28 15:53:18 +03:00
print " \t %s " % subcommand
return 0
2009-12-30 23:06:21 +03:00
if not subcommand in self . subcommands :
raise CommandError ( " No such subcommand ' %s ' " % subcommand )
return self . subcommands [ subcommand ] . _run ( subcommand , * args )
def usage ( self , myname , subcommand = None , * args ) :
if subcommand is None or not subcommand in self . subcommands :
print " Usage: %s ( %s ) [options] " % ( myname ,
" | " . join ( self . subcommands . keys ( ) ) )
2009-12-28 15:53:18 +03:00
else :
2009-12-30 23:06:21 +03:00
return self . subcommands [ subcommand ] . usage ( * args )
2009-12-28 15:53:18 +03:00
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 ( )
2009-12-30 21:53:05 +03:00
from samba . netcmd . setpassword import cmd_setpassword
commands [ " setpassword " ] = cmd_setpassword ( )
2009-12-30 22:00:12 +03:00
from samba . netcmd . setexpiry import cmd_setexpiry
commands [ " setexpiry " ] = cmd_setexpiry ( )
2009-12-30 22:10:34 +03:00
from samba . netcmd . enableaccount import cmd_enableaccount
commands [ " enableaccount " ] = cmd_enableaccount ( )
2009-12-30 22:40:11 +03:00
from samba . netcmd . newuser import cmd_newuser
commands [ " newuser " ] = cmd_newuser ( )
2010-03-16 14:06:08 +03:00
from samba . netcmd . netacl import cmd_acl
2009-10-24 15:34:31 +04:00
commands [ " acl " ] = cmd_acl ( )
2010-01-27 18:57:37 +03:00
from samba . netcmd . fsmo import cmd_fsmo
commands [ " fsmo " ] = cmd_fsmo ( )
2010-03-01 22:43:19 +03:00
from samba . netcmd . export import cmd_export
commands [ " export " ] = cmd_export ( )
2010-04-09 01:41:08 +04:00
from samba . netcmd . time import cmd_time
commands [ " time " ] = cmd_time ( )
2010-03-02 01:03:41 +03:00
from samba . netcmd . user import cmd_user
commands [ " user " ] = cmd_user ( )
2010-04-09 01:53:19 +04:00
from samba . netcmd . vampire import cmd_vampire
commands [ " vampire " ] = cmd_vampire ( )
2010-04-09 01:58:15 +04:00
from samba . netcmd . machinepw import cmd_machinepw
commands [ " machinepw " ] = cmd_machinepw ( )
2010-07-02 23:52:33 +04:00
from samba . netcmd . spn import cmd_spn
commands [ " spn " ] = cmd_spn ( )
2010-06-07 20:10:28 +04:00
from samba . netcmd . group import cmd_group
commands [ " group " ] = cmd_group ( )
2010-08-23 02:52:19 +04:00
from samba . netcmd . join import cmd_join
commands [ " join " ] = cmd_join ( )