expand tabs in python file, consistent with the rest of the file.
[amitay/samba.git] / source4 / scripting / python / samba / __init__.py
index 5b345341330954695e62af143598e5afc41625dc..a49e6e1eadb7460476b7cc2bb5838465574c473f 100644 (file)
@@ -1,8 +1,10 @@
 #!/usr/bin/python
 
 # Unix SMB/CIFS implementation.
+# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
+# 
+# Based on the original in EJS:
 # Copyright (C) Andrew Tridgell <tridge@samba.org> 2005
-# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
 #   
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+"""Samba 4."""
+
+__docformat__ = "restructuredText"
+
 import os
 
 def _in_source_tree():
@@ -31,11 +37,13 @@ if _in_source_tree():
     srcdir = "%s/../../.." % os.path.dirname(__file__)
     sys.path.append("%s/bin/python" % srcdir)
     default_ldb_modules_dir = "%s/bin/modules/ldb" % srcdir
+else:
+    default_ldb_modules_dir = None
 
 
 import ldb
 import credentials
-import misc
+import glue
 
 class Ldb(ldb.Ldb):
     """Simple Samba-specific LDB subclass that takes care 
@@ -67,15 +75,15 @@ class Ldb(ldb.Ldb):
             self.set_modules_dir(default_ldb_modules_dir)
 
         if credentials is not None:
-            self.set_credentials(self, credentials)
+            self.set_credentials(credentials)
 
         if session_info is not None:
-            self.set_session_info(self, session_info)
+            self.set_session_info(session_info)
 
-        assert misc.ldb_register_samba_handlers(self) == 0
+        glue.ldb_register_samba_handlers(self)
 
         if lp is not None:
-            self.set_loadparm(self, lp)
+            self.set_loadparm(lp)
 
         def msg(l,text):
             print text
@@ -84,12 +92,16 @@ class Ldb(ldb.Ldb):
         if url is not None:
             self.connect(url)
 
+    def set_credentials(self, credentials):
+        glue.ldb_set_credentials(self, credentials)
 
-    set_credentials = misc.ldb_set_credentials
-    set_session_info = misc.ldb_set_session_info
-    set_loadparm = misc.ldb_set_loadparm
+    def set_session_info(self, session_info):
+        glue.ldb_set_session_info(self, session_info)
 
-    def searchone(self, basedn, attribute, expression=None, 
+    def set_loadparm(self, lp_ctx):
+        glue.ldb_set_loadparm(self, lp_ctx)
+
+    def searchone(self, attribute, basedn=None, expression=None, 
                   scope=ldb.SCOPE_BASE):
         """Search for one attribute as a string.
         
@@ -104,7 +116,7 @@ class Ldb(ldb.Ldb):
             return None
         values = set(res[0][attribute])
         assert len(values) == 1
-        return values.pop()
+        return self.schema_format_value(attribute, values.pop())
 
     def erase(self):
         """Erase this ldb, removing all records."""
@@ -112,28 +124,28 @@ class Ldb(ldb.Ldb):
         for attr in ["@INDEXLIST", "@ATTRIBUTES", "@SUBCLASSES", "@MODULES", 
                      "@OPTIONS", "@PARTITION", "@KLUDGEACL"]:
             try:
-                self.delete(ldb.Dn(self, attr))
+                self.delete(attr)
             except ldb.LdbError, (LDB_ERR_NO_SUCH_OBJECT, _):
                 # Ignore missing dn errors
                 pass
 
-        basedn = ldb.Dn(self, "")
+        basedn = ""
         # and the rest
         for msg in self.search(basedn, ldb.SCOPE_SUBTREE, 
-                "(&(|(objectclass=*)(dn=*))(!(dn=@BASEINFO)))", 
-                ["dn"]):
+                "(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))", 
+                ["distinguishedName"]):
             try:
                 self.delete(msg.dn)
             except ldb.LdbError, (LDB_ERR_NO_SUCH_OBJECT, _):
-                # Ignor eno such object errors
+                # Ignorno such object errors
                 pass
 
-        res = self.search(basedn, ldb.SCOPE_SUBTREE, "(&(|(objectclass=*)(dn=*))(!(dn=@BASEINFO)))", ["dn"])
+        res = self.search(basedn, ldb.SCOPE_SUBTREE, "(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))", ["distinguishedName"])
         assert len(res) == 0
 
     def erase_partitions(self):
         """Erase an ldb, removing all records."""
-        res = self.search(ldb.Dn(self, ""), ldb.SCOPE_BASE, "(objectClass=*)", 
+        res = self.search("", ldb.SCOPE_BASE, "(objectClass=*)", 
                          ["namingContexts"])
         assert len(res) == 1
         if not "namingContexts" in res[0]:
@@ -145,11 +157,23 @@ class Ldb(ldb.Ldb):
             k = 0
             while ++k < 10 and (previous_remaining != current_remaining):
                 # and the rest
-                res2 = self.search(ldb.Dn(self, basedn), ldb.SCOPE_SUBTREE, "(|(objectclass=*)(dn=*))", ["dn"])
+                try:
+                    res2 = self.search(basedn, ldb.SCOPE_SUBTREE, "(|(objectclass=*)(distinguishedName=*))", ["distinguishedName"])
+                except ldb.LdbError, (LDB_ERR_NO_SUCH_OBJECT, _):
+                    # Ignore missing dn errors
+                    return
+
                 previous_remaining = current_remaining
                 current_remaining = len(res2)
                 for msg in res2:
-                    self.delete(msg.dn)
+                    try:
+                        self.delete(msg.dn)
+                    # Ignore no such object errors
+                    except ldb.LdbError, (LDB_ERR_NO_SUCH_OBJECT, _):
+                        pass
+                    # Ignore not allowed on non leaf errors
+                    except ldb.LdbError, (LDB_ERR_NOT_ALLOWED_ON_NON_LEAF, _):
+                        pass
 
     def load_ldif_file_add(self, ldif_path):
         """Load a LDIF file.
@@ -159,14 +183,21 @@ class Ldb(ldb.Ldb):
         self.add_ldif(open(ldif_path, 'r').read())
 
     def add_ldif(self, ldif):
+        """Add data based on a LDIF string.
+
+        :param ldif: LDIF text.
+        """
         for changetype, msg in self.parse_ldif(ldif):
             assert changetype == ldb.CHANGETYPE_NONE
             self.add(msg)
 
     def modify_ldif(self, ldif):
-        for (changetype, msg) in ldb.parse_ldif(data):
-            assert changetype == CHANGETYPE_MODIFY
-            ldb.modify(msg)
+        """Modify database based on a LDIF string.
+
+        :param ldif: LDIF text.
+        """
+        for changetype, msg in self.parse_ldif(ldif):
+            self.modify(msg)
 
 
 def substitute_var(text, values):
@@ -185,12 +216,27 @@ def substitute_var(text, values):
     return text
 
 
+def check_all_substituted(text):
+    """Make sure that all substitution variables in a string have been replaced.
+    If not, raise an exception.
+    
+    :param text: The text to search for substitution variables
+    """
+    if not "${" in text:
+        return
+    
+    var_start = text.find("${")
+    var_end = text.find("}", var_start)
+    
+    raise Exception("Not all variables substituted: %s" % text[var_start:var_end+1])
+
+
 def valid_netbios_name(name):
     """Check whether a name is valid as a NetBIOS name. """
     # FIXME: There are probably more constraints here. 
     # crh has a paragraph on this in his book (1.4.1.1)
-    if len(name) > 13:
+    if len(name) > 15:
         return False
     return True
 
-version = misc.version
+version = glue.version