[PATCH 01/13] ensure_external_module: Support check function.

Jelmer Vernooij jelmer at samba.org
Sat Nov 15 12:16:56 MST 2014


Change-Id: I4e11f1f088db88fa75584bb93927b56454cf5a41
Signed-Off-By: Jelmer Vernooij <jelmer at samba.org>
---
 python/samba/__init__.py | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)

diff --git a/python/samba/__init__.py b/python/samba/__init__.py
index 0cbdec7..aa6bd3c 100644
--- a/python/samba/__init__.py
+++ b/python/samba/__init__.py
@@ -327,6 +327,7 @@ def import_bundled_package(modulename, location, 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
+    :return: The module object
     """
     if in_source_tree():
         extra_path = os.path.join(source_tree_topdir(), source_tree_container,
@@ -337,37 +338,59 @@ def import_bundled_package(modulename, location, source_tree_container,
     else:
         sys.modules[modulename] = __import__(
             "%s.%s" % (namespace, modulename), fromlist=[namespace])
+    return sys.modules[modulename]
 
 
-def ensure_third_party_module(modulename, location):
+def ensure_third_party_module(modulename, location, check=lambda mod: True):
     """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)
+    :param check: Optional callback to check the module
+    :return: The module object
     """
     try:
         __import__(modulename)
     except ImportError:
-        import_bundled_package(modulename, location,
+        mod = import_bundled_package(modulename, location,
             source_tree_container="third_party",
             namespace="samba.third_party")
+    else:
+        if not check(mod):
+            mod = import_bundled_package(modulename, location,
+                source_tree_container="third_party",
+                namespace="samba.third_party")
+    if not check(mod):
+        raise ImportError("Unable to find checked system or bundled %s" %
+                          modulename)
+    return mod
 
 
-def ensure_external_module(modulename, location):
+def ensure_external_module(modulename, location, check=lambda mod: True):
     """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 check: Optional callback to check the module
+    :return: The module object
     """
     # This is deprecated - please use ensure_third_party_module for
     # new modules instead, and put them in third_party/.
     try:
-        __import__(modulename)
+        mod = __import__(modulename)
     except ImportError:
-        import_bundled_package(modulename, location,
+        mod = import_bundled_package(modulename, location,
             source_tree_container="lib", namespace="samba.external")
+    else:
+        if not check(mod):
+            mod = import_bundled_package(modulename, location,
+                source_tree_container="lib", namespace="samba.external")
+    if not check(mod):
+        raise ImportError("Unable to find checked system or bundled %s" %
+                          modulename)
+    return mod
 
 
 def dn_from_dns_name(dnsdomain):
-- 
2.1.3



More information about the samba-technical mailing list