[PATCH 13/13] Always use testtools/subunit pair both from either the system or bundled, never a combination.

Jelmer Vernooij jelmer at samba.org
Sat Nov 15 12:17:08 MST 2014


Change-Id: I5384c0aeff6a5e1ef36ad8e90633cf05e77d1fa0
Signed-off-by: Jelmer Vernooij <jelmer at samba.org>
---
 python/samba/__init__.py | 13 +++++--------
 python/samba/subunit.py  | 37 ++++++++++++++++++++++++++-----------
 2 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/python/samba/__init__.py b/python/samba/__init__.py
index 47174ab..dcbbbc8 100644
--- a/python/samba/__init__.py
+++ b/python/samba/__init__.py
@@ -314,8 +314,9 @@ def valid_netbios_name(name):
     return True
 
 
-def import_bundled_package(modulename, location, source_tree_container,
-                           namespace):
+def import_bundled_package(modulename, location,
+                           source_tree_container="third_party",
+                           namespace="samba.third_party"):
     """Import the bundled version of a package.
 
     :note: This should only be called if the system version of the package
@@ -353,14 +354,10 @@ def ensure_third_party_module(modulename, location, check=lambda mod: True):
     try:
         mod = __import__(modulename)
     except ImportError:
-        mod = import_bundled_package(modulename, location,
-            source_tree_container="third_party",
-            namespace="samba.third_party")
+        mod = import_bundled_package(modulename, location)
     else:
         if not check(mod):
-            mod = import_bundled_package(modulename, location,
-                source_tree_container="third_party",
-                namespace="samba.third_party")
+            mod = import_bundled_package(modulename, location)
     if not check(mod):
         raise ImportError("Unable to find checked system or bundled %s" %
                           modulename)
diff --git a/python/samba/subunit.py b/python/samba/subunit.py
index 5c13299..61954ac 100644
--- a/python/samba/subunit.py
+++ b/python/samba/subunit.py
@@ -20,14 +20,29 @@
 __docformat__ = "restructuredText"
 
 import samba
-samba.ensure_third_party_module("mimeparse", "mimeparse")
-samba.ensure_third_party_module("extras", "python-extras")
-samba.ensure_third_party_module("testtools", "testtools")
-def check_subunit(mod):
-    try:
-        __import__("subunit.run.TestProgram")
-    except ImportError:
-        return False
-    else:
-        return True
-samba.ensure_third_party_module("subunit", "subunit/python", check_subunit)
+import sys
+
+# testtools and subunit are fairly tightly coupled, so either use the system
+# versions of both *or* the bundled versions. That way we always use a
+# combination that was chosen by the distribution or by us (when updating
+# the bundled copies in third_party/).
+
+try:
+    import subunit.run.TestProgram
+    import subunit.v2
+except ImportError:
+    # No system subunit we can use, fall back to bundled.
+
+    # Cleanup any half-imported system modules:
+    for m in sys.modules.keys():
+        if m.startswith("testtools.") or m.startswith("subunit."):
+            del sys.modules[m]
+
+    # For mimeparse/extras, using the system versions with
+    # bundled testtools/subunit is fine.
+    samba.ensure_third_party_module("mimeparse", "mimeparse")
+    samba.ensure_third_party_module("extras", "python-extras")
+
+    # Explicitly use the bundled copies of testtools and subunit.
+    samba.import_bundled_package("testtools", "testtools")
+    samba.import_bundled_package("subunit", "subunit/python")
-- 
2.1.3



More information about the samba-technical mailing list