Merge lp:~mabac/linaro-image-tools/testtools-dependency into lp:linaro-image-tools/11.11

Proposed by Mattias Backman
Status: Merged
Approved by: Данило Шеган
Approved revision: 471
Merged at revision: 471
Proposed branch: lp:~mabac/linaro-image-tools/testtools-dependency
Merge into: lp:linaro-image-tools/11.11
Diff against target: 58 lines (+45/-2)
1 file modified
linaro_image_tools/utils.py (+45/-2)
To merge this branch: bzr merge lp:~mabac/linaro-image-tools/testtools-dependency
Reviewer Review Type Date Requested Status
Данило Шеган (community) Approve
Review via email: mp+83597@code.launchpad.net

Description of the change

Hi,

This branch adds the try_import function from testtools to avoid having that project as a production dependency.

Thanks,

Mattias

To post a comment you must log in.
Revision history for this message
Данило Шеган (danilo) wrote :

 merge approve

review: Approve
Revision history for this message
Guilherme Salgado (salgado) wrote :

I'm guessing this is because it turned out to be a problem to have
testtools as a runtime dependency, but I'm curious to know what the
problem was.

Revision history for this message
Mattias Backman (mabac) wrote :

On Tue, Nov 29, 2011 at 1:21 PM, Guilherme Salgado
<email address hidden> wrote:
> I'm guessing this is because it turned out to be a problem to have
> testtools as a runtime dependency, but I'm curious to know what the
> problem was.

In the "fetch_image_settings.yaml on releases.l.o" email thread, fabo
reminded me that lool wanted the dependency removed. I must admit that
I missed that request and had to search irclogs for it. Some people
had problems with testtools missing or corrupt which may be the reason
to remove it. I'm not sure if there was a bigger problem than that
though.

>
> --
> https://code.launchpad.net/~mabac/linaro-image-tools/testtools-dependency/+merge/83597
> You are the owner of lp:~mabac/linaro-image-tools/testtools-dependency.

Revision history for this message
Loïc Minier (lool) wrote :

On Tue, Nov 29, 2011, Mattias Backman wrote:
> In the "fetch_image_settings.yaml on releases.l.o" email thread, fabo
> reminded me that lool wanted the dependency removed. I must admit that
> I missed that request and had to search irclogs for it. Some people
> had problems with testtools missing or corrupt which may be the reason
> to remove it. I'm not sure if there was a bigger problem than that
> though.

 I had brought it up on IRC because we had people from Linaro and
 outside of Linaro filing bugs or stating that the latest
 linaro-image-tools didn't work because they hadn't installed
 python-testtools. Given that we were only using a single relatively
 small function from it, it seemed best to avoid the dependency and
 issue entirely. Some people were confused by the fact that this
 sounded like testsuite stuff, and as such shouldn't be a runtime
 dependency, only a development or build-time/testsuite dependency.

 Another small factor is that python-testtools was originally developed
 by Canonical folks and put into Debian/Ubuntu, but might not be
 widespread into .rpm distros. However linaro-image-tools has similar
 issues with other dependencies I'm afraid (e.g. distutils-extra).

--
Loïc Minier

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'linaro_image_tools/utils.py'
2--- linaro_image_tools/utils.py 2011-11-21 13:48:20 +0000
3+++ linaro_image_tools/utils.py 2011-11-28 12:53:31 +0000
4@@ -24,11 +24,54 @@
5 import logging
6 import tempfile
7 import tarfile
8-
9-from testtools import try_import
10+import sys
11
12 from linaro_image_tools import cmd_runner
13
14+
15+# try_import was copied from python-testtools 0.9.12 and was originally
16+# licensed under a MIT-style license but relicensed under the GPL in Linaro
17+# Image Tools.
18+# Copyright (c) 2011 Jonathan M. Lange <jml@mumak.net>.
19+def try_import(name, alternative=None, error_callback=None):
20+ """Attempt to import ``name``. If it fails, return ``alternative``.
21+
22+ When supporting multiple versions of Python or optional dependencies, it
23+ is useful to be able to try to import a module.
24+
25+ :param name: The name of the object to import, e.g. ``os.path`` or
26+ ``os.path.join``.
27+ :param alternative: The value to return if no module can be imported.
28+ Defaults to None.
29+ :param error_callback: If non-None, a callable that is passed the ImportError
30+ when the module cannot be loaded.
31+ """
32+ module_segments = name.split('.')
33+ last_error = None
34+ while module_segments:
35+ module_name = '.'.join(module_segments)
36+ try:
37+ module = __import__(module_name)
38+ except ImportError:
39+ last_error = sys.exc_info()[1]
40+ module_segments.pop()
41+ continue
42+ else:
43+ break
44+ else:
45+ if last_error is not None and error_callback is not None:
46+ error_callback(last_error)
47+ return alternative
48+ nonexistent = object()
49+ for segment in name.split('.')[1:]:
50+ module = getattr(module, segment, nonexistent)
51+ if module is nonexistent:
52+ if last_error is not None and error_callback is not None:
53+ error_callback(last_error)
54+ return alternative
55+ return module
56+
57+
58 CommandNotFound = try_import('CommandNotFound.CommandNotFound')
59
60

Subscribers

People subscribed via source and target branches