From patchwork Fri Dec 2 09:19:13 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mattias Backman X-Patchwork-Id: 5429 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 0C0E923E0F for ; Fri, 2 Dec 2011 09:19:17 +0000 (UTC) Received: from mail-lpp01m010-f52.google.com (mail-lpp01m010-f52.google.com [209.85.215.52]) by fiordland.canonical.com (Postfix) with ESMTP id CC822A1850D for ; Fri, 2 Dec 2011 09:19:16 +0000 (UTC) Received: by laah2 with SMTP id h2so1570737laa.11 for ; Fri, 02 Dec 2011 01:19:16 -0800 (PST) Received: by 10.152.123.9 with SMTP id lw9mr4230229lab.12.1322817556448; Fri, 02 Dec 2011 01:19:16 -0800 (PST) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.152.41.198 with SMTP id h6cs104448lal; Fri, 2 Dec 2011 01:19:16 -0800 (PST) Received: by 10.180.104.35 with SMTP id gb3mr8696313wib.11.1322817554261; Fri, 02 Dec 2011 01:19:14 -0800 (PST) Received: from indium.canonical.com (indium.canonical.com. [91.189.90.7]) by mx.google.com with ESMTPS id eo3si3578415wbb.124.2011.12.02.01.19.14 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 02 Dec 2011 01:19:14 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) client-ip=91.189.90.7; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) smtp.mail=bounces@canonical.com Received: from ackee.canonical.com ([91.189.89.26]) by indium.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1RWPGz-0001ec-RZ for ; Fri, 02 Dec 2011 09:19:13 +0000 Received: from ackee.canonical.com (localhost [127.0.0.1]) by ackee.canonical.com (Postfix) with ESMTP id C4FABE0100 for ; Fri, 2 Dec 2011 09:19:13 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: linaro-image-tools X-Launchpad-Branch: ~linaro-image-tools/linaro-image-tools/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 471 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [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> Date: Fri, 02 Dec 2011 09:19:13 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="14414"; Instance="launchpad-lazr.conf" X-Launchpad-Hash: 6dc350c216478cc2cf6194d22bb4d5af1917a897 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 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 === 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 . +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')