r26375: Move provision-independent utility function to main samba python module.
[ira/wip.git] / source / scripting / python / samba / __init__.py
1 #!/usr/bin/python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Andrew Tridgell <tridge@samba.org> 2005
5 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
6 #   
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #   
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #   
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 import os
22 from misc import ldb_set_credentials
23
24 def Ldb(url, session_info=None, credentials=None, modules_dir=None):
25     """Open a Samba Ldb file. 
26
27     This is different from a regular Ldb file in that the Samba-specific
28     modules-dir is used by default and that credentials and session_info 
29     can be passed through (required by some modules).
30     """
31     import ldb
32     ret = ldb.Ldb()
33     if modules_dir is None:
34         modules_dir = os.path.join(os.getcwd(), "bin", "modules", "ldb")
35     ret.set_modules_dir(modules_dir)
36     def samba_debug(level,text):
37         print "%d %s" % (level, text)
38     ldb_set_opaque("credentials", credentials)
39     ret.set_opaque("sessionInfo", session_info)
40     #ret.set_debug(samba_debug)
41     ret.connect(url)
42     return ret
43
44
45 def substitute_var(text, values):
46     """substitute strings of the form ${NAME} in str, replacing
47     with substitutions from subobj.
48     
49     :param text: Text in which to subsitute.
50     :param values: Dictionary with keys and values.
51     """
52
53     for (name, value) in values.items():
54         text = text.replace("${%s}" % name, value)
55
56     return text
57
58
59 def valid_netbios_name(name):
60     """Check whether a name is valid as a NetBIOS name. """
61     # FIXME: There are probably more constraints here. 
62     # crh has a paragraph on this in his book (1.4.1.1)
63     if len(name) > 13:
64         return False
65     return True
66