Add docstrings to samba3 and getopt modules.
[samba.git] / source4 / scripting / python / samba / getopt.py
1 #!/usr/bin/python
2
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.
10 #   
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.
15 #   
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/>.
18 #
19
20 """Support for parsing Samba-related command-line options."""
21
22 import optparse
23 from credentials import Credentials, AUTO_USE_KERBEROS, DONT_USE_KERBEROS, MUST_USE_KERBEROS
24
25 __docformat__ = "restructuredText"
26
27 class SambaOptions(optparse.OptionGroup):
28     """General Samba-related command line options."""
29     def __init__(self, parser):
30         optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
31         self.add_option("-s", "--configfile", action="callback",
32                         type=str, metavar="FILE", help="Configuration file", 
33                         callback=self._load_configfile)
34         self._configfile = None
35
36     def get_loadparm_path(self):
37         """Return the path to the smb.conf file specified on the command line.  """
38         return self._configfile
39
40     def _load_configfile(self, option, opt_str, arg, parser):
41         self._configfile = arg
42
43     def get_loadparm(self):
44         """Return a loadparm object with data specified on the command line.  """
45         import os, param
46         lp = param.LoadParm()
47         if self._configfile is not None:
48             lp.load(self._configfile)
49         elif os.getenv("SMB_CONF_PATH") is not None:
50             lp.load(os.getenv("SMB_CONF_PATH"))
51         else:
52             lp.load_default()
53         return lp
54
55
56 class VersionOptions(optparse.OptionGroup):
57     """Command line option for printing Samba version."""
58     def __init__(self, parser):
59         optparse.OptionGroup.__init__(self, parser, "Version Options")
60
61
62 class CredentialsOptions(optparse.OptionGroup):
63     """Command line options for specifying credentials."""
64     def __init__(self, parser):
65         self.no_pass = False
66         optparse.OptionGroup.__init__(self, parser, "Credentials Options")
67         self.add_option("--simple-bind-dn", metavar="DN", action="callback",
68                         callback=self._set_simple_bind_dn, type=str,
69                         help="DN to use for a simple bind")
70         self.add_option("--password", metavar="PASSWORD", action="callback",
71                         help="Password", type=str, callback=self._set_password)
72         self.add_option("-U", "--username", metavar="USERNAME", 
73                         action="callback", type=str,
74                         help="Username", callback=self._parse_username)
75         self.add_option("-W", "--workgroup", metavar="WORKGROUP", 
76                         action="callback", type=str,
77                         help="Workgroup", callback=self._parse_workgroup)
78         self.add_option("-N", "--no-pass", action="store_true",
79                         help="Don't ask for a password")
80         self.add_option("-k", "--kerberos", metavar="KERBEROS", 
81                         action="callback", type=str,
82                         help="Use Kerberos", callback=self._set_kerberos)
83         self.creds = Credentials()
84
85     def _parse_username(self, option, opt_str, arg, parser):
86         self.creds.parse_string(arg)
87
88     def _parse_workgroup(self, option, opt_str, arg, parser):
89         self.creds.set_domain(arg)
90
91     def _set_password(self, option, opt_str, arg, parser):
92         self.creds.set_password(arg)
93
94     def _set_kerberos(self, option, opt_str, arg, parser):
95         if bool(arg) or arg.lower() == "yes":
96             self.creds.set_kerberos_state(MUST_USE_KERBEROS)
97         else:
98             self.creds.set_kerberos_state(DONT_USE_KERBEROS)
99
100     def _set_simple_bind_dn(self, option, opt_str, arg, parser):
101         self.creds.set_bind_dn(arg)
102
103     def get_credentials(self, lp):
104         """Obtain the credentials set on the command-line.
105
106         :param lp: Loadparm object to use.
107         :return: Credentials object
108         """
109         self.creds.guess(lp)
110         if not self.no_pass:
111             self.creds.set_cmdline_callbacks()
112         return self.creds