29d322e2fc4a1f46b6a70f320e11fbf45484e5aa
[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
23 def _in_source_tree():
24     return os.path.exists("%s/../../../samba4-skip" % os.path.dirname(__file__))
25
26 # When running, in-tree, make sure bin/python is in the PYTHONPATH
27 if _in_source_tree():
28     import sys
29     dir = os.path.dirname(__file__)
30     sys.path.append("%s/../../../bin/python" % os.path.dirname(__file__))
31
32 import misc
33 import ldb
34 ldb.ldb.set_credentials = misc.ldb_set_credentials
35
36 def Ldb(url, session_info=None, credentials=None, modules_dir=None):
37     """Open a Samba Ldb file. 
38
39     This is different from a regular Ldb file in that the Samba-specific
40     modules-dir is used by default and that credentials and session_info 
41     can be passed through (required by some modules).
42     """
43     import ldb
44     ret = ldb.Ldb()
45     if modules_dir is None:
46         modules_dir = os.path.join(os.getcwd(), "bin", "modules", "ldb")
47     ret.set_modules_dir(modules_dir)
48     def samba_debug(level,text):
49         print "%d %s" % (level, text)
50     ldb_set_opaque("credentials", credentials)
51     ret.set_opaque("sessionInfo", session_info)
52     #ret.set_debug(samba_debug)
53     ret.connect(url)
54     return ret
55
56
57 def substitute_var(text, values):
58     """substitute strings of the form ${NAME} in str, replacing
59     with substitutions from subobj.
60     
61     :param text: Text in which to subsitute.
62     :param values: Dictionary with keys and values.
63     """
64
65     for (name, value) in values.items():
66         text = text.replace("${%s}" % name, value)
67
68     return text
69
70
71 def valid_netbios_name(name):
72     """Check whether a name is valid as a NetBIOS name. """
73     # FIXME: There are probably more constraints here. 
74     # crh has a paragraph on this in his book (1.4.1.1)
75     if len(name) > 13:
76         return False
77     return True
78