Add samba.ensure_third_party_module() function, loading external python modules from...
authorJelmer Vernooij <jelmer@samba.org>
Fri, 17 Oct 2014 07:48:20 +0000 (00:48 -0700)
committerJeremy Allison <jra@samba.org>
Wed, 12 Nov 2014 19:21:09 +0000 (20:21 +0100)
Signed-off-by: Jelmer Vernooij <jelmer@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
python/samba/__init__.py
third_party/wscript_build [new file with mode: 0644]
wscript_build

index cd2a309fc0aea445139081ec4e2117fd1b95bea0..0cbdec7800c316db273f5c994a18694a0e751c2c 100644 (file)
@@ -314,7 +314,8 @@ def valid_netbios_name(name):
     return True
 
 
-def import_bundled_package(modulename, location):
+def import_bundled_package(modulename, location, source_tree_container,
+                           namespace):
     """Import the bundled version of a package.
 
     :note: This should only be called if the system version of the package
@@ -322,14 +323,35 @@ def import_bundled_package(modulename, location):
 
     :param modulename: Module name to import
     :param location: Location to add to sys.path (can be relative to
-        ${srcdir}/lib)
+        ${srcdir}/${source_tree_container})
+    :param source_tree_container: Directory under source root that
+        contains the bundled third party modules.
+    :param namespace: Namespace to import module from, when not in source tree
     """
     if in_source_tree():
-        sys.path.insert(0, os.path.join(source_tree_topdir(), "lib", location))
+        extra_path = os.path.join(source_tree_topdir(), source_tree_container,
+            location)
+        if not extra_path in sys.path:
+            sys.path.insert(0, extra_path)
         sys.modules[modulename] = __import__(modulename)
     else:
         sys.modules[modulename] = __import__(
-            "samba.external.%s" % modulename, fromlist=["samba.external"])
+            "%s.%s" % (namespace, modulename), fromlist=[namespace])
+
+
+def ensure_third_party_module(modulename, location):
+    """Add a location to sys.path if a third party dependency can't be found.
+
+    :param modulename: Module name to import
+    :param location: Location to add to sys.path (can be relative to
+        ${srcdir}/third_party)
+    """
+    try:
+        __import__(modulename)
+    except ImportError:
+        import_bundled_package(modulename, location,
+            source_tree_container="third_party",
+            namespace="samba.third_party")
 
 
 def ensure_external_module(modulename, location):
@@ -339,10 +361,13 @@ def ensure_external_module(modulename, location):
     :param location: Location to add to sys.path (can be relative to
         ${srcdir}/lib)
     """
+    # This is deprecated - please use ensure_third_party_module for
+    # new modules instead, and put them in third_party/.
     try:
         __import__(modulename)
     except ImportError:
-        import_bundled_package(modulename, location)
+        import_bundled_package(modulename, location,
+            source_tree_container="lib", namespace="samba.external")
 
 
 def dn_from_dns_name(dnsdomain):
diff --git a/third_party/wscript_build b/third_party/wscript_build
new file mode 100644 (file)
index 0000000..d8b9aae
--- /dev/null
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+
+import os
+
+# work out what python external libraries we need to install
+external_libs = {
+    }
+
+list = []
+
+for module, package in external_libs.items():
+    try:
+        __import__(module)
+    except ImportError:
+        list.append(package)
+
+for e in list:
+    bld.INSTALL_WILDCARD('${PYTHONARCHDIR}/samba/third_party', e + '/**/*', flat=False,
+                         exclude='*.pyc', trim_path=os.path.dirname(e))
+
+bld.SAMBA_GENERATOR('third_party_init_py',
+                    rule='touch ${TGT}',
+                    target='empty_file')
+
+bld.INSTALL_FILES('${PYTHONARCHDIR}/samba/third_party', 'empty_file', destname='__init__.py')
+bld.RECURSE('zlib')
+bld.RECURSE('popt')
index d7dea5466eada2c977da1ab43bbf4bd8cde23911..e74841e96a1a9b1db9dad6b8d54321cfb42739c9 100644 (file)
@@ -74,8 +74,7 @@ bld.RECURSE('lib/socket_wrapper')
 bld.RECURSE('lib/nss_wrapper')
 bld.RECURSE('lib/uid_wrapper')
 if bld.CHECK_FOR_THIRD_PARTY():
-    bld.RECURSE('third_party/zlib')
-    bld.RECURSE('third_party/popt')
+    bld.RECURSE('third_party')
 bld.RECURSE('source4/lib/stream')
 bld.RECURSE('lib/afs')
 bld.RECURSE('lib/util')