s4-provision: cope with top level directory for provision
authorAndrew Tridgell <tridge@samba.org>
Thu, 3 Feb 2011 02:49:29 +0000 (13:49 +1100)
committerAndrew Tridgell <tridge@samba.org>
Mon, 7 Feb 2011 02:22:00 +0000 (13:22 +1100)
to allow for top level 'make test' we need to cope with two in-tree
directory layouts

Pair-Programmed-With: Andrew Bartlett <abartlet@samba.org>

source4/scripting/python/samba/__init__.py
source4/scripting/python/samba/provision/__init__.py

index 5faeef05cfb1c0e3067846a458b6084d3fc749ff..2a54f47d2bbbdb660dc88dd2fc5d147411195a72 100644 (file)
@@ -27,17 +27,23 @@ __docformat__ = "restructuredText"
 import os
 import sys
 
-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__))
+def source_tree_topdir():
+    '''return the top level directory (the one containing the source4 directory)'''
+    paths = [ "../../..", "../../../.." ]
+    for p in paths:
+        topdir = os.path.normpath(os.path.join(os.path.dirname(__file__), p))
+        if os.path.exists(os.path.join(topdir, 'source4')):
+            return topdir
+    raise RuntimeError("unable to find top level source directory")
 
+def in_source_tree():
+    '''return True if we are running from within the samba source tree'''
+    try:
+        topdir = source_tree_topdir()
+    except RuntimeError:
+        return False
+    return True
 
-# When running, in-tree, make sure ldb modules can be found
-if in_source_tree():
-    srcdir = "%s/../../.." % os.path.dirname(__file__)
-    default_ldb_modules_dir = "%s/bin/modules/ldb" % srcdir
-else:
-    default_ldb_modules_dir = None
 
 
 import ldb
@@ -71,8 +77,6 @@ class Ldb(_Ldb):
 
         if modules_dir is not None:
             self.set_modules_dir(modules_dir)
-        elif default_ldb_modules_dir is not None:
-            self.set_modules_dir(default_ldb_modules_dir)
         elif lp is not None:
             self.set_modules_dir(os.path.join(lp.get("modules dir"), "ldb"))
 
@@ -314,9 +318,7 @@ def import_bundled_package(modulename, location):
         ${srcdir}/lib)
     """
     if in_source_tree():
-        sys.path.insert(0,
-            os.path.join(os.path.dirname(__file__),
-                         "../../../../lib", location))
+        sys.path.insert(0, os.path.join(source_tree_topdir(), "lib", location))
         sys.modules[modulename] = __import__(modulename)
     else:
         sys.modules[modulename] = __import__(
index e200083a333f1ab74f071cef6f1a2a62a31e519d..341f822cf29dffc710d0153a354394f1776d798d 100644 (file)
@@ -47,6 +47,7 @@ from samba import (
     Ldb,
     check_all_substituted,
     in_source_tree,
+    source_tree_topdir,
     read_and_sub_file,
     setup_file,
     substitute_var,
@@ -89,8 +90,7 @@ def find_setup_dir():
     """Find the setup directory used by provision."""
     if in_source_tree():
         # In source tree
-        dirname = os.path.dirname(__file__)
-        return os.path.normpath(os.path.join(dirname, "../../../../setup"))
+        return os.path.join(source_tree_topdir(), "source4/setup")
     else:
         import sys
         for prefix in [sys.prefix,