s4-python: Add convenience function for forcibly importing bundled
authorJelmer Vernooij <jelmer@samba.org>
Thu, 9 Dec 2010 23:47:33 +0000 (00:47 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Fri, 10 Dec 2010 02:04:06 +0000 (03:04 +0100)
package.

source4/scripting/bin/subunitrun
source4/scripting/python/samba/__init__.py

index 9717c9e345ad8b3ce78cc4194783f9df0fe09135..21243a73ec5aca4d0a7fcbf7d7f165118b4f50b2 100755 (executable)
@@ -29,10 +29,8 @@ samba.ensure_external_module("subunit", "subunit/python")
 try:
     from subunit.run import SubunitTestRunner, TestProgram
 except ImportError:
-    del sys.modules["subunit"]
-    if "subunit.run" in sys.modules:
-        del sys.modules["subunit.run"]
-    samba.import_bundled_package("subunit", "subunit/python")
+    samba.force_bundled_package("testtools", "testtools")
+    samba.force_bundled_package("subunit", "subunit/python")
     from subunit.run import SubunitTestRunner, TestProgram
 import samba.getopt as options
 import samba.tests
index b6c53f4b2c1bbe0408fcbebd2b7c755cde627362..6fcac16691ee85af90722701faa0c31abd14ee22 100644 (file)
@@ -324,6 +324,21 @@ def import_bundled_package(modulename, location):
             "samba.external.%s" % modulename, fromlist=["samba.external"])
 
 
+def force_bundled_package(packagename, location):
+    """Forcibly use the bundled package.
+
+    This will first unload the system module and then load the bundled one.
+
+    :param packagename: The package name
+    :param location: Location to add to sys.path (can be relative to
+        ${srcdir}/lib)
+    """
+    for m in sys.modules.keys():
+        if m.startswith("%s." % packagename):
+            del sys.modules[m]
+    import_bundled_package(packagename, location)
+
+
 def ensure_external_module(modulename, location):
     """Add a location to sys.path if an external dependency can't be found.