~lefteris-nikoltsios/+junk/samba-lp1016895

« back to all changes in this revision

Viewing changes to lib/testtools/testtools/helpers.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2011-12-21 13:18:04 UTC
  • mfrom: (0.39.21 sid)
  • Revision ID: package-import@ubuntu.com-20111221131804-xtlr39wx6njehxxr
Tags: 2:3.6.1-3ubuntu1
* Merge from Debian testing.  Remaining changes:
  + debian/patches/VERSION.patch:
    - set SAMBA_VERSION_SUFFIX to Ubuntu.
  + debian/patches/error-trans.fix-276472:
    - Add the translation of Unix Error code -ENOTSUP to NT Error Code
    - NT_STATUS_NOT_SUPPORTED to prevent the Permission denied error.
  + debian/smb.conf:
    - add "(Samba, Ubuntu)" to server string.
    - comment out the default [homes] share, and add a comment about
      "valid users = %S" to show users how to restrict access to
      \\server\username to only username.
    - Set 'usershare allow guests', so that usershare admins are 
      allowed to create public shares in addition to authenticated
      ones.
    - add map to guest = Bad user, maps bad username to guest access.
  + debian/samba-common.config:
    - Do not change priority to high if dhclient3 is installed.
    - Use priority medium instead of high for the workgroup question.
  + debian/control:
    - Don't build against or suggest ctdb.
    - Add dependency on samba-common-bin to samba.
  + Add ufw integration:
    - Created debian/samba.ufw.profile
    - debian/rules, debian/samba.dirs, debian/samba.files: install
      profile
    - debian/control: have samba suggest ufw
  + Add apport hook:
    - Created debian/source_samba.py.
    - debian/rules, debian/samba.dirs, debian/samba-common-bin.files: install
  + Switch to upstart:
    - Add debian/samba.{nmbd,smbd}.upstart.
  + debian/samba.logrotate, debian/samba-common.dhcp, debian/samba.if-up:
    - Make them upstart compatible
  + debian/samba.postinst: 
    - Avoid scary pdbedit warnings on first import.
  + debian/samba-common.postinst: Add more informative error message for
    the case where smb.conf was manually deleted
  + debian/patches/fix-debuglevel-name-conflict.patch: don't use 'debug_level'
    as a global variable name in an NSS module 
  + Dropped:
    - debian/patches/error-trans.fix-276472
    - debian/patches/fix-debuglevel-name-conflict.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2010 Jonathan M. Lange. See LICENSE for details.
 
2
 
 
3
__all__ = [
 
4
    'try_import',
 
5
    'try_imports',
 
6
    ]
 
7
 
 
8
 
 
9
def try_import(name, alternative=None):
 
10
    """Attempt to import `name`.  If it fails, return `alternative`.
 
11
 
 
12
    When supporting multiple versions of Python or optional dependencies, it
 
13
    is useful to be able to try to import a module.
 
14
 
 
15
    :param name: The name of the object to import, e.g. 'os.path' or
 
16
        'os.path.join'.
 
17
    :param alternative: The value to return if no module can be imported.
 
18
        Defaults to None.
 
19
    """
 
20
    module_segments = name.split('.')
 
21
    while module_segments:
 
22
        module_name = '.'.join(module_segments)
 
23
        try:
 
24
            module = __import__(module_name)
 
25
        except ImportError:
 
26
            module_segments.pop()
 
27
            continue
 
28
        else:
 
29
            break
 
30
    else:
 
31
        return alternative
 
32
    nonexistent = object()
 
33
    for segment in name.split('.')[1:]:
 
34
        module = getattr(module, segment, nonexistent)
 
35
        if module is nonexistent:
 
36
            return alternative
 
37
    return module
 
38
 
 
39
 
 
40
_RAISE_EXCEPTION = object()
 
41
def try_imports(module_names, alternative=_RAISE_EXCEPTION):
 
42
    """Attempt to import modules.
 
43
 
 
44
    Tries to import the first module in `module_names`.  If it can be
 
45
    imported, we return it.  If not, we go on to the second module and try
 
46
    that.  The process continues until we run out of modules to try.  If none
 
47
    of the modules can be imported, either raise an exception or return the
 
48
    provided `alternative` value.
 
49
 
 
50
    :param module_names: A sequence of module names to try to import.
 
51
    :param alternative: The value to return if no module can be imported.
 
52
        If unspecified, we raise an ImportError.
 
53
    :raises ImportError: If none of the modules can be imported and no
 
54
        alternative value was specified.
 
55
    """
 
56
    module_names = list(module_names)
 
57
    for module_name in module_names:
 
58
        module = try_import(module_name)
 
59
        if module:
 
60
            return module
 
61
    if alternative is _RAISE_EXCEPTION:
 
62
        raise ImportError(
 
63
            "Could not import any of: %s" % ', '.join(module_names))
 
64
    return alternative