s4:provision Simplify the module list
[Samba/ekacnet.git] / source4 / scripting / python / samba / getopt.py
blob8b756b2d6fcf26225bd28e78aff721e12d16da67
1 #!/usr/bin/python
3 # Samba-specific bits for optparse
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """Support for parsing Samba-related command-line options."""
22 import optparse
23 from credentials import Credentials, DONT_USE_KERBEROS, MUST_USE_KERBEROS
24 from hostconfig import Hostconfig
26 __docformat__ = "restructuredText"
28 class SambaOptions(optparse.OptionGroup):
29 """General Samba-related command line options."""
30 def __init__(self, parser):
31 optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
32 self.add_option("-s", "--configfile", action="callback",
33 type=str, metavar="FILE", help="Configuration file",
34 callback=self._load_configfile)
35 self._configfile = None
37 def get_loadparm_path(self):
38 """Return the path to the smb.conf file specified on the command line. """
39 return self._configfile
41 def _load_configfile(self, option, opt_str, arg, parser):
42 self._configfile = arg
44 def get_loadparm(self):
45 """Return a loadparm object with data specified on the command line. """
46 import os, param
47 lp = param.LoadParm()
48 if self._configfile is not None:
49 lp.load(self._configfile)
50 elif os.getenv("SMB_CONF_PATH") is not None:
51 lp.load(os.getenv("SMB_CONF_PATH"))
52 else:
53 lp.load_default()
54 return lp
56 def get_hostconfig(self):
57 return Hostconfig(self.get_loadparm())
60 class VersionOptions(optparse.OptionGroup):
61 """Command line option for printing Samba version."""
62 def __init__(self, parser):
63 optparse.OptionGroup.__init__(self, parser, "Version Options")
66 class CredentialsOptions(optparse.OptionGroup):
67 """Command line options for specifying credentials."""
68 def __init__(self, parser):
69 self.no_pass = False
70 optparse.OptionGroup.__init__(self, parser, "Credentials Options")
71 self.add_option("--simple-bind-dn", metavar="DN", action="callback",
72 callback=self._set_simple_bind_dn, type=str,
73 help="DN to use for a simple bind")
74 self.add_option("--password", metavar="PASSWORD", action="callback",
75 help="Password", type=str, callback=self._set_password)
76 self.add_option("-U", "--username", metavar="USERNAME",
77 action="callback", type=str,
78 help="Username", callback=self._parse_username)
79 self.add_option("-W", "--workgroup", metavar="WORKGROUP",
80 action="callback", type=str,
81 help="Workgroup", callback=self._parse_workgroup)
82 self.add_option("-N", "--no-pass", action="store_true",
83 help="Don't ask for a password")
84 self.add_option("-k", "--kerberos", metavar="KERBEROS",
85 action="callback", type=str,
86 help="Use Kerberos", callback=self._set_kerberos)
87 self.creds = Credentials()
89 def _parse_username(self, option, opt_str, arg, parser):
90 self.creds.parse_string(arg)
92 def _parse_workgroup(self, option, opt_str, arg, parser):
93 self.creds.set_domain(arg)
95 def _set_password(self, option, opt_str, arg, parser):
96 self.creds.set_password(arg)
98 def _set_kerberos(self, option, opt_str, arg, parser):
99 if bool(arg) or arg.lower() == "yes":
100 self.creds.set_kerberos_state(MUST_USE_KERBEROS)
101 else:
102 self.creds.set_kerberos_state(DONT_USE_KERBEROS)
104 def _set_simple_bind_dn(self, option, opt_str, arg, parser):
105 self.creds.set_bind_dn(arg)
107 def get_credentials(self, lp):
108 """Obtain the credentials set on the command-line.
110 :param lp: Loadparm object to use.
111 :return: Credentials object
113 self.creds.guess(lp)
114 if not self.no_pass:
115 self.creds.set_cmdline_callbacks()
116 return self.creds