Support --version in python scripts.
[nivanova/samba-autobuild/.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, DONT_USE_KERBEROS, MUST_USE_KERBEROS
24 from hostconfig import Hostconfig
25 import samba
26
27 __docformat__ = "restructuredText"
28
29 class SambaOptions(optparse.OptionGroup):
30     """General Samba-related command line options."""
31     def __init__(self, parser):
32         optparse.OptionGroup.__init__(self, parser, "Samba Common Options")
33         self.add_option("-s", "--configfile", action="callback",
34                         type=str, metavar="FILE", help="Configuration file",
35                         callback=self._load_configfile)
36         self.add_option("-d", "--debuglevel", action="callback",
37                         type=int, metavar="DEBUGLEVEL", help="debug level",
38                         callback=self._set_debuglevel)
39         self._configfile = None
40         self._debuglevel = None
41
42     def get_loadparm_path(self):
43         """Return the path to the smb.conf file specified on the command line.  """
44         return self._configfile
45
46     def _load_configfile(self, option, opt_str, arg, parser):
47         self._configfile = arg
48
49     def _set_debuglevel(self, option, opt_str, arg, parser):
50         self._debuglevel = arg
51
52     def get_loadparm(self):
53         """Return a loadparm object with data specified on the command line.  """
54         import os, param
55         lp = param.LoadParm()
56         if self._configfile is not None:
57             lp.load(self._configfile)
58         elif os.getenv("SMB_CONF_PATH") is not None:
59             lp.load(os.getenv("SMB_CONF_PATH"))
60         else:
61             lp.load_default()
62         if self._debuglevel:
63             samba.set_debug_level(self._debuglevel)
64         return lp
65
66     def get_hostconfig(self):
67         return Hostconfig(self.get_loadparm())
68
69
70 class VersionOptions(optparse.OptionGroup):
71     """Command line option for printing Samba version."""
72     def __init__(self, parser):
73         optparse.OptionGroup.__init__(self, parser, "Version Options")
74         self.add_option("--version", action="callback",
75                 callback=self._display_version, 
76                 help="Display version number")
77
78     def _display_version(self, option, opt_str, arg, parser):
79         import samba, sys
80         print samba.version
81         sys.exit(0)
82
83
84 class CredentialsOptions(optparse.OptionGroup):
85     """Command line options for specifying credentials."""
86     def __init__(self, parser):
87         self.no_pass = True
88         optparse.OptionGroup.__init__(self, parser, "Credentials Options")
89         self.add_option("--simple-bind-dn", metavar="DN", action="callback",
90                         callback=self._set_simple_bind_dn, type=str,
91                         help="DN to use for a simple bind")
92         self.add_option("--password", metavar="PASSWORD", action="callback",
93                         help="Password", type=str, callback=self._set_password)
94         self.add_option("-U", "--username", metavar="USERNAME",
95                         action="callback", type=str,
96                         help="Username", callback=self._parse_username)
97         self.add_option("-W", "--workgroup", metavar="WORKGROUP",
98                         action="callback", type=str,
99                         help="Workgroup", callback=self._parse_workgroup)
100         self.add_option("-N", "--no-pass", action="store_true",
101                         help="Don't ask for a password")
102         self.add_option("-k", "--kerberos", metavar="KERBEROS",
103                         action="callback", type=str,
104                         help="Use Kerberos", callback=self._set_kerberos)
105         self.creds = Credentials()
106
107     def _parse_username(self, option, opt_str, arg, parser):
108         self.creds.parse_string(arg)
109
110     def _parse_workgroup(self, option, opt_str, arg, parser):
111         self.creds.set_domain(arg)
112
113     def _set_password(self, option, opt_str, arg, parser):
114         self.creds.set_password(arg)
115         self.no_pass = False
116
117     def _set_kerberos(self, option, opt_str, arg, parser):
118         if bool(arg) or arg.lower() == "yes":
119             self.creds.set_kerberos_state(MUST_USE_KERBEROS)
120         else:
121             self.creds.set_kerberos_state(DONT_USE_KERBEROS)
122
123     def _set_simple_bind_dn(self, option, opt_str, arg, parser):
124         self.creds.set_bind_dn(arg)
125
126     def get_credentials(self, lp):
127         """Obtain the credentials set on the command-line.
128
129         :param lp: Loadparm object to use.
130         :return: Credentials object
131         """
132         self.creds.guess(lp)
133         if self.no_pass:
134             self.creds.set_cmdline_callbacks()
135         return self.creds
136
137 class CredentialsOptionsDouble(CredentialsOptions):
138     """Command line options for specifying credentials of two servers."""
139     def __init__(self, parser):
140         CredentialsOptions.__init__(self, parser)
141         self.no_pass2 = True
142         self.add_option("--simple-bind-dn2", metavar="DN2", action="callback",
143                         callback=self._set_simple_bind_dn2, type=str,
144                         help="DN to use for a simple bind")
145         self.add_option("--password2", metavar="PASSWORD2", action="callback",
146                         help="Password", type=str, callback=self._set_password2)
147         self.add_option("--username2", metavar="USERNAME2",
148                         action="callback", type=str,
149                         help="Username for second server", callback=self._parse_username2)
150         self.add_option("--workgroup2", metavar="WORKGROUP2",
151                         action="callback", type=str,
152                         help="Workgroup for second server", callback=self._parse_workgroup2)
153         self.add_option("--no-pass2", action="store_true",
154                         help="Don't ask for a password for the second server")
155         self.add_option("--kerberos2", metavar="KERBEROS2",
156                         action="callback", type=str,
157                         help="Use Kerberos", callback=self._set_kerberos2)
158         self.creds2 = Credentials()
159
160     def _parse_username2(self, option, opt_str, arg, parser):
161         self.creds2.parse_string(arg)
162
163     def _parse_workgroup2(self, option, opt_str, arg, parser):
164         self.creds2.set_domain(arg)
165
166     def _set_password2(self, option, opt_str, arg, parser):
167         self.creds2.set_password(arg)
168         self.no_pass2 = False
169
170     def _set_kerberos2(self, option, opt_str, arg, parser):
171         if bool(arg) or arg.lower() == "yes":
172             self.creds2.set_kerberos_state(MUST_USE_KERBEROS)
173         else:
174             self.creds2.set_kerberos_state(DONT_USE_KERBEROS)
175
176     def _set_simple_bind_dn2(self, option, opt_str, arg, parser):
177         self.creds2.set_bind_dn(arg)
178
179     def get_credentials2(self, lp):
180         """Obtain the credentials set on the command-line.
181
182         :param lp: Loadparm object to use.
183         :return: Credentials object
184         """
185         self.creds2.guess(lp)
186         if self.no_pass2:
187             self.creds2.set_cmdline_callbacks()
188         return self.creds2