s4-provision Add an invalid names check for 'domain == netbiosname'
[nivanova/samba-autobuild/.git] / source4 / scripting / python / samba / __init__.py
index a6532b7f0d619ec65d77a693295a65faefe70a47..bd7628993ebe85931f7b7eddae99d6da4ddd25ae 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python
 
 # Unix SMB/CIFS implementation.
 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
@@ -27,13 +27,13 @@ __docformat__ = "restructuredText"
 import os
 import sys
 
-def _in_source_tree():
+def in_source_tree():
     """Check whether the script is being run from the source dir. """
     return os.path.exists("%s/../../../selftest/skip" % os.path.dirname(__file__))
 
 
 # When running, in-tree, make sure bin/python is in the PYTHONPATH
-if _in_source_tree():
+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
@@ -42,7 +42,6 @@ else:
 
 
 import ldb
-import _glue
 from samba._ldb import Ldb as _Ldb
 
 class Ldb(_Ldb):
@@ -53,6 +52,7 @@ class Ldb(_Ldb):
     not necessarily the Sam database. For Sam-specific helper
     functions see samdb.py.
     """
+
     def __init__(self, url=None, lp=None, modules_dir=None, session_info=None,
                  credentials=None, flags=0, options=None):
         """Opens a Samba Ldb file.
@@ -92,7 +92,7 @@ class Ldb(_Ldb):
         self.register_samba_handlers()
 
         # TODO set debug
-        def msg(l,text):
+        def msg(l, text):
             print text
         #self.set_debug(msg)
 
@@ -128,7 +128,7 @@ class Ldb(_Ldb):
 
     def erase_users_computers(self, dn):
         """Erases user and computer objects from our AD.
-        
+
         This is needed since the 'samldb' module denies the deletion of primary
         groups. Therefore all groups shouldn't be primary somewhere anymore.
         """
@@ -153,7 +153,7 @@ class Ldb(_Ldb):
 
     def erase_except_schema_controlled(self):
         """Erase this ldb.
-        
+
         :note: Removes all records, except those that are controlled by
             Samba4's schema.
         """
@@ -166,7 +166,7 @@ class Ldb(_Ldb):
         # Delete the 'visible' records, and the invisble 'deleted' records (if this DB supports it)
         for msg in self.search(basedn, ldb.SCOPE_SUBTREE,
                        "(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))",
-                       [], controls=["show_deleted:0"]):
+                       [], controls=["show_deleted:0", "show_recycled:0"]):
             try:
                 self.delete(msg.dn, ["relax:0"])
             except ldb.LdbError, (errno, _):
@@ -175,7 +175,7 @@ class Ldb(_Ldb):
                     raise
 
         res = self.search(basedn, ldb.SCOPE_SUBTREE,
-            "(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))", [], controls=["show_deleted:0"])
+            "(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))", [], controls=["show_deleted:0", "show_recycled:0"])
         assert len(res) == 0
 
         # delete the specials
@@ -201,39 +201,6 @@ class Ldb(_Ldb):
                     # Ignore missing dn errors
                     raise
 
-    def erase_partitions(self):
-        """Erase an ldb, removing all records."""
-
-        def erase_recursive(self, dn):
-            try:
-                res = self.search(base=dn, scope=ldb.SCOPE_ONELEVEL, attrs=[],
-                                  controls=["show_deleted:0"])
-            except ldb.LdbError, (errno, _):
-                if errno == ldb.ERR_NO_SUCH_OBJECT:
-                    # Ignore no such object errors
-                    return
-
-            for msg in res:
-                erase_recursive(self, msg.dn)
-
-            try:
-                self.delete(dn, ["relax:0"])
-            except ldb.LdbError, (errno, _):
-                if errno != ldb.ERR_NO_SUCH_OBJECT:
-                    # Ignore no such object errors
-                    raise
-
-        res = self.search("", ldb.SCOPE_BASE, "(objectClass=*)",
-                         ["namingContexts"])
-        assert len(res) == 1
-        if not "namingContexts" in res[0]:
-            return
-        for basedn in res[0]["namingContexts"]:
-            # Try to delete user/computer accounts to allow deletion of groups
-            self.erase_users_computers(basedn)
-            # Try and erase from the bottom-up in the tree
-            erase_recursive(self, basedn)
-
     def load_ldif_file_add(self, ldif_path):
         """Load a LDIF file.
 
@@ -248,7 +215,7 @@ class Ldb(_Ldb):
         """
         for changetype, msg in self.parse_ldif(ldif):
             assert changetype == ldb.CHANGETYPE_NONE
-            self.add(msg,controls)
+            self.add(msg, controls)
 
     def modify_ldif(self, ldif, controls=None):
         """Modify database based on a LDIF string.
@@ -264,7 +231,7 @@ class Ldb(_Ldb):
 
 def substitute_var(text, values):
     """Substitute strings of the form ${NAME} in str, replacing
-    with substitutions from subobj.
+    with substitutions from values.
 
     :param text: Text in which to subsitute.
     :param values: Dictionary with keys and values.
@@ -279,7 +246,8 @@ def substitute_var(text, values):
 
 
 def check_all_substituted(text):
-    """Make sure that all substitution variables in a string have been replaced.
+    """Check that all substitution variables in a string have been replaced.
+
     If not, raise an exception.
 
     :param text: The text to search for substitution variables
@@ -290,16 +258,17 @@ def check_all_substituted(text):
     var_start = text.find("${")
     var_end = text.find("}", var_start)
 
-    raise Exception("Not all variables substituted: %s" % text[var_start:var_end+1])
+    raise Exception("Not all variables substituted: %s" %
+        text[var_start:var_end+1])
 
 
-def read_and_sub_file(file, subst_vars):
+def read_and_sub_file(file_name, subst_vars):
     """Read a file and sub in variables found in it
 
-    :param file: File to be read (typically from setup directory)
+    :param file_name: File to be read (typically from setup directory)
      param subst_vars: Optional variables to subsitute in the file.
     """
-    data = open(file, 'r').read()
+    data = open(file_name, 'r').read()
     if subst_vars is not None:
         data = substitute_var(data, subst_vars)
         check_all_substituted(data)
@@ -339,15 +308,14 @@ def ensure_external_module(modulename, location):
     """Add a location to sys.path if an external dependency can't be found.
 
     :param modulename: Module name to import
-    :param location: Location to add to sys.path (can be relative to 
-        ${srcdir}/lib
+    :param location: Location to add to sys.path (can be relative to
+        ${srcdir}/lib)
     """
     try:
         __import__(modulename)
     except ImportError:
-        import sys
-        if _in_source_tree():
-            sys.path.insert(0, 
+        if in_source_tree():
+            sys.path.insert(0,
                 os.path.join(os.path.dirname(__file__),
                              "../../../../lib", location))
             __import__(modulename)
@@ -355,8 +323,13 @@ def ensure_external_module(modulename, location):
             sys.modules[modulename] = __import__(
                 "samba.external.%s" % modulename, fromlist=["samba.external"])
 
+from samba import _glue
 version = _glue.version
 interface_ips = _glue.interface_ips
 set_debug_level = _glue.set_debug_level
+get_debug_level = _glue.get_debug_level
+unix2nttime = _glue.unix2nttime
+nttime2string = _glue.nttime2string
+nttime2unix = _glue.nttime2unix
 unix2nttime = _glue.unix2nttime
 generate_random_password = _glue.generate_random_password