s4-python: Simplify mechanism for finding included Python modules.
[kai/samba-autobuild/.git] / source4 / scripting / python / samba / __init__.py
index 24bb45a003fe1ff2ea1a9cb36b48fae07d5f81d9..87753b5a1dd9cfa8ba5e87a22985f7dbc6c06afc 100644 (file)
@@ -392,6 +392,21 @@ def valid_netbios_name(name):
     return True
 
 
+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
+    """
+    try:
+        __import__(modulename)
+    except ImportError:
+        sys.path.insert(0, 
+            os.path.join(os.path.dirname(__file__),
+                         "../../../../lib", location))
+        __import__(modulename)
+
 version = glue.version
 
 # "userAccountControl" flags