python: Add convenience function for getting command line loadparm context
authorJelmer Vernooij <jelmer@samba.org>
Wed, 23 Jan 2008 22:33:36 +0000 (23:33 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Wed, 23 Jan 2008 22:38:57 +0000 (23:38 +0100)
and default to using system smb.conf.

source/lib/ldb/tests/python/ldap.py
source/scripting/bin/winreg.py
source/scripting/python/samba/getopt.py
source/setup/provision.py
source/setup/upgrade.py
source/setup/vampire.py

index 01b66a389041f5eabe59a14cdaef45042650e179..d35f7767dfdc0a77789be38e6ae9139d9f6999d5 100755 (executable)
@@ -19,7 +19,8 @@ from samba import Ldb
 import param
 
 parser = optparse.OptionParser("ldap [options] <host>")
-parser.add_option_group(options.SambaOptions(parser))
+sambaopts = options.SambaOptions(parser)
+parser.add_option_group(sambaopts)
 parser.add_option_group(options.VersionOptions(parser))
 # use command line creds if available
 credopts = options.CredentialsOptions(parser)
@@ -33,9 +34,7 @@ if len(args) < 1:
 
 host = args[0]
 
-lp = param.LoadParm()
-if opts.configfile:
-    lp.load(opts.configfile)
+lp = sambaopts.get_loadparm()
 
 def delete_force(ldb, dn):
     try:
index f68f2d12f25731b9c0970951f1f999fe693a1ec7..1e39ee8f78e41a25f0432e48e304993806d3afea 100755 (executable)
@@ -12,7 +12,8 @@ import optparse
 import samba.getopt as options
 
 parser = optparse.OptionParser("%s <BINDING> [path]" % sys.argv[0])
-parser.add_option_group(options.SambaOptions(parser))
+sambaopts = options.SambaOptions(parser)
+parser.add_option_group(sambaopts)
 parser.add_option("--createkey", type="string", metavar="KEYNAME", 
         help="create a key")
 
@@ -25,7 +26,7 @@ if len(args) < 1:
 binding = args[0]
 
 print "Connecting to " + binding
-conn = winreg.winreg(binding, opts.configfile)
+conn = winreg.winreg(binding, sambaopts.get_loadparm())
 
 def list_values(key):
     (num_values, max_valnamelen, max_valbufsize) = conn.QueryInfoKey(key, winreg.String())[4:8]
index a087974a6986a97424d2aa7d97f16227a831fdc0..dfcf2c457e8bc278ea8343a0f3b75517e6592523 100644 (file)
@@ -23,9 +23,25 @@ from credentials import Credentials
 class SambaOptions(optparse.OptionGroup):
     def __init__(self, parser):
         optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
-        self.add_option("-s", "--configfile", type="string", metavar="FILE",
-                        help="Configuration file")
+        self.add_option("-s", "--configfile", action="callback",
+                        type=str, metavar="FILE", help="Configuration file", 
+                        callback=self._load_configfile)
+        self._configfile = None
 
+    def get_loadparm_path(self):
+        return self._configfile
+
+    def _load_configfile(self, option, opt_str, arg, parser):
+        self._configfile = arg
+
+    def get_loadparm(self):
+        import param
+        lp = param.LoadParm()
+        if self._configfile is None:
+            lp.load_default()
+        else:
+            lp.load(self._configfile)
+        return lp
 
 class VersionOptions(optparse.OptionGroup):
     def __init__(self, parser):
index 88015ce0a314f9ef0945a39b4f0bd59d27a462f6..b9a11bdd78d1e929f85cff98659be169fd4e8cab 100755 (executable)
@@ -37,7 +37,8 @@ from samba.provision import (provision,
                              provision_paths_from_lp)
 
 parser = optparse.OptionParser("provision [options]")
-parser.add_option_group(options.SambaOptions(parser))
+sambaopts = options.SambaOptions(parser)
+parser.add_option_group(sambaopts)
 parser.add_option_group(options.VersionOptions(parser))
 credopts = options.CredentialsOptions(parser)
 parser.add_option_group(credopts)
@@ -111,9 +112,7 @@ if opts.realm is None or opts.domain is None:
        sys.exit(1)
 
 # cope with an initially blank smb.conf 
-lp = param.LoadParm()
-if opts.configfile:
-    lp.load(opts.configfile)
+lp = sambaopts.get_loadparm()
 if opts.targetdir is not None:
     if not os.path.exists(opts.targetdir):
         os.mkdir(opts.targetdir)
@@ -127,7 +126,7 @@ if opts.aci is not None:
        print "set ACI: %s" % opts.aci
 
 paths = provision_paths_from_lp(lp, opts.realm.lower())
-paths.smbconf = opts.configfile
+paths.smbconf = sambaopts.get_loadparm_path()
 
 if opts.ldap_backend:
        if opts.ldap_backend == "ldapi":
index ea6f83d7de678aa0f55cbdca11934298747a876e..4cf9641ef2aca3f0164b08639fa2b41c45a1b330 100755 (executable)
@@ -14,7 +14,8 @@ import samba.getopt as options
 from auth import system_session
 
 parser = optparse.OptionParser("upgrade [options] <libdir> <smbconf>")
-parser.add_option_group(options.SambaOptions(parser))
+sambaopts = options.SambaOptions(parser)
+parser.add_option_group(sambaopts)
 parser.add_option_group(options.VersionOptions(parser))
 credopts = options.CredentialsOptions(parser)
 parser.add_option_group(credopts)
@@ -59,15 +60,13 @@ if setup_dir is None:
        setup_dir = "setup"
 
 creds = credopts.get_credentials()
-lp = param.LoadParm()
-if opts.configfile:
-    lp.load(opts.configfile)
+lp = sambaopts.get_loadparm()
 if opts.targetdir is not None:
     if not os.path.exists(opts.targetdir):
         os.mkdir(opts.targetdir)
     lp.set("private dir", os.path.abspath(opts.targetdir))
     lp.set("lock dir", os.path.abspath(opts.targetdir))
 paths = provision_paths_from_lp(lp, "")
-paths.smbconf = opts.configfile
+paths.smbconf = sambaopts.get_loadparm_path()
 upgrade_provision(samba3, setup_dir, message, credentials=creds, session_info=system_session(), 
                   lp=lp, paths=paths)
index 392cd2d4fb22a17b11287431b394134a99ba6d75..728c53146aff4d6633e512263f26ece13eb4d252 100755 (executable)
@@ -26,7 +26,8 @@ from auth import system_session
 import sys
 
 parser = optparse.OptionParser("vampire [options] <domain>")
-parser.add_option_group(options.SambaOptions(parser))
+sambaopts = options.SambaOptions(parser)
+parser.add_option_group(sambaopts)
 parser.add_option_group(options.VersionOptions(parser))
 credopts = options.CredentialsOptions(parser)
 parser.add_option_group(credopts)
@@ -47,8 +48,6 @@ def vampire(domain, session_info, credentials, lp):
     ctx.samsync_ldb(vampire_ctx, machine_creds=machine_creds, 
                     session_info=session_info)
 
-lp = param.LoadParm()
-if opts.configfile:
-    lp.load(opts.configfile)
+lp = sambaopts.get_loadparm()
 vampire(args[0], session_info=system_session(), 
         credentials=credopts.get_credentials(), lp=lp)