s4-samba-tool: switched over to python version of samba-tool drs
[idra/samba.git] / source4 / scripting / python / samba / netcmd / __init__.py
index cb8fa01fe1f7f0f4e69b98eb9d8839b8e1d813fe..dafdd77c87f98b6d0af516535bcae61018ed127a 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python
 
 # Unix SMB/CIFS implementation.
 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009
@@ -18,7 +18,7 @@
 #
 
 import optparse
-from samba import getopt as options, Ldb
+from samba import getopt as options
 import sys
 
 
@@ -40,7 +40,7 @@ class Command(object):
 
     name = property(_get_name)
 
-    def usage(self, args):
+    def usage(self, *args):
         parser, _ = self._create_parser()
         parser.print_usage()
 
@@ -49,11 +49,13 @@ class Command(object):
     def _get_synopsis(self):
         ret = self.name
         if self.takes_args:
-            ret += " " + " ".join(self.takes_args)
+            ret += " " + " ".join([x.upper() for x in self.takes_args])
         return ret
 
     synopsis = property(_get_synopsis)
 
+    outf = sys.stdout
+
     takes_args = []
     takes_options = []
     takes_optiongroups = {}
@@ -75,13 +77,23 @@ class Command(object):
         parser, optiongroups = self._create_parser()
         opts, args = parser.parse_args(list(argv))
         # Filter out options from option groups
+        args = args[1:]
         kwargs = dict(opts.__dict__)
         for option_group in parser.option_groups:
             for option in option_group.option_list:
-                del kwargs[option.dest]
+                if option.dest is not None:
+                    del kwargs[option.dest]
         kwargs.update(optiongroups)
-        if len(args) < len(self.takes_args):
-            self.usage(args)
+        min_args = 0
+        max_args = 0
+        for i, arg in enumerate(self.takes_args):
+            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):
+            self.usage(*args)
             return -1
         try:
             return self.run(*args, **kwargs)
@@ -99,21 +111,22 @@ class SuperCommand(Command):
 
     subcommands = {}
 
-    def run(self, subcommand, *args, **kwargs):
-        if not subcommand in subcommands:
-            print >>sys.stderr, "ERROR: No such subcommand '%s'" % subcommand
-            return subcommands[subcommand].run(*args, **kwargs)
-
-    def usage(self, subcommand=None, *args, **kwargs):
+    def _run(self, myname, subcommand=None, *args):
         if subcommand is None:
-            print "Available subcommands"
-            for subcommand in subcommands:
+            print "Available subcommands:"
+            for subcommand in self.subcommands:
                 print "\t%s" % subcommand
             return 0
+        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()))
         else:
-            if not subcommand in subcommands:
-                print >>sys.stderr, "ERROR: No such subcommand '%s'" % subcommand
-            return subcommands[subcommand].usage(*args, **kwargs)
+            return self.subcommands[subcommand].usage(*args)
 
 
 class CommandError(Exception):
@@ -125,3 +138,35 @@ from samba.netcmd.pwsettings import cmd_pwsettings
 commands["pwsettings"] = cmd_pwsettings()
 from samba.netcmd.domainlevel import cmd_domainlevel
 commands["domainlevel"] = cmd_domainlevel()
+from samba.netcmd.setpassword import cmd_setpassword
+commands["setpassword"] = cmd_setpassword()
+from samba.netcmd.setexpiry import cmd_setexpiry
+commands["setexpiry"] = cmd_setexpiry()
+from samba.netcmd.enableaccount import cmd_enableaccount
+commands["enableaccount"] = cmd_enableaccount()
+from samba.netcmd.newuser import cmd_newuser
+commands["newuser"] = cmd_newuser()
+from samba.netcmd.netacl import cmd_acl
+commands["acl"] = cmd_acl()
+from samba.netcmd.fsmo import cmd_fsmo
+commands["fsmo"] = cmd_fsmo()
+from samba.netcmd.export import cmd_export
+commands["export"] = cmd_export()
+from samba.netcmd.time import cmd_time
+commands["time"] = cmd_time()
+from samba.netcmd.user import cmd_user
+commands["user"] = cmd_user()
+from samba.netcmd.vampire import cmd_vampire
+commands["vampire"] = cmd_vampire()
+from samba.netcmd.machinepw import cmd_machinepw
+commands["machinepw"] = cmd_machinepw()
+from samba.netcmd.spn import cmd_spn
+commands["spn"] = cmd_spn()
+from samba.netcmd.group import cmd_group
+commands["group"] = cmd_group()
+from samba.netcmd.join import cmd_join
+commands["join"] = cmd_join()
+from samba.netcmd.rodc import cmd_rodc
+commands["rodc"] = cmd_rodc()
+from samba.netcmd.drs import cmd_drs
+commands["drs"] = cmd_drs()