diff mbox

[Branch,~linaro-image-tools/linaro-image-tools/trunk] Rev 471: Remove runtime dependency for testtools by copying the try_import function from testtools.

Message ID 20111202091913.18322.2171.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

Mattias Backman Dec. 2, 2011, 9:19 a.m. UTC
Merge authors:
  Mattias Backman (mabac)
Related merge proposals:
  https://code.launchpad.net/~mabac/linaro-image-tools/testtools-dependency/+merge/83597
  proposed by: Mattias Backman (mabac)
  review: Approve - Данило Шеган (danilo)
------------------------------------------------------------
revno: 471 [merge]
committer: Mattias Backman <mattias.backman@linaro.org>
branch nick: linaro-image-tools
timestamp: Fri 2011-12-02 10:17:14 +0100
message:
  Remove runtime dependency for testtools by copying the try_import function from testtools.
modified:
  README
  linaro_image_tools/utils.py


--
lp:linaro-image-tools
https://code.launchpad.net/~linaro-image-tools/linaro-image-tools/trunk

You are subscribed to branch lp:linaro-image-tools.
To unsubscribe from this branch go to https://code.launchpad.net/~linaro-image-tools/linaro-image-tools/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'README'
--- README	2011-11-29 12:30:53 +0000
+++ README	2011-12-02 09:17:14 +0000
@@ -16,8 +16,6 @@ 
   - qemu-user-static >= 0.13.0 (only if you're running on x86)
   - btrfs-tools
   - command-not-found
-  - python-testtools >= 0.9.8
-     (available at https://launchpad.net/~linaro-maintainers/+archive/tools)
   - python-yaml
 
 = Running tests =

=== modified file 'linaro_image_tools/utils.py'
--- linaro_image_tools/utils.py	2011-11-21 13:48:20 +0000
+++ linaro_image_tools/utils.py	2011-11-28 12:49:14 +0000
@@ -24,11 +24,54 @@ 
 import logging
 import tempfile
 import tarfile
-
-from testtools import try_import
+import sys
 
 from linaro_image_tools import cmd_runner
 
+
+# try_import was copied from python-testtools 0.9.12 and was originally
+# licensed under a MIT-style license but relicensed under the GPL in Linaro
+# Image Tools.
+# Copyright (c) 2011 Jonathan M. Lange <jml@mumak.net>.
+def try_import(name, alternative=None, error_callback=None):
+    """Attempt to import ``name``.  If it fails, return ``alternative``.
+
+    When supporting multiple versions of Python or optional dependencies, it
+    is useful to be able to try to import a module.
+
+    :param name: The name of the object to import, e.g. ``os.path`` or
+        ``os.path.join``.
+    :param alternative: The value to return if no module can be imported.
+        Defaults to None.
+    :param error_callback: If non-None, a callable that is passed the ImportError
+        when the module cannot be loaded.
+    """
+    module_segments = name.split('.')
+    last_error = None
+    while module_segments:
+        module_name = '.'.join(module_segments)
+        try:
+            module = __import__(module_name)
+        except ImportError:
+            last_error = sys.exc_info()[1]
+            module_segments.pop()
+            continue
+        else:
+            break
+    else:
+        if last_error is not None and error_callback is not None:
+            error_callback(last_error)
+        return alternative
+    nonexistent = object()
+    for segment in name.split('.')[1:]:
+        module = getattr(module, segment, nonexistent)
+        if module is nonexistent:
+            if last_error is not None and error_callback is not None:
+                error_callback(last_error)
+            return alternative
+    return module
+
+
 CommandNotFound = try_import('CommandNotFound.CommandNotFound')