summaryrefslogtreecommitdiff
path: root/paste/util/thirdparty.py
blob: 08833724b1fab8a11eed4747900f81d3314347f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
import os

third_party = os.path.join(
    os.path.dirname(os.path.dirname(__file__)),
    '3rd-party')

new_python_path = os.path.join(third_party, 'new_python')

def load_new_module(module_name, python_version):
    """
    Modules in the standard library that have been improved can be
    loaded with this command.  python_version is a sys.version_info
    tuple, and if you need a newer version then we'll look in
    ../3rd-party/new_python/python/module.py; otherwise it'll return
    the normal module.  E.g.:

        doctest = load_new_module('doctest', (2, 4))
    """
    if python_version > sys.version_info:
        if new_python_path not in sys.path:
            sys.path.append(new_python_path)
        try:
            exec "import python.%s as generic_module" % module_name
        except ImportError, e:
            raise ImportError(
                "Cannot load backported (from python version %s) "
                "stdlib module %s; expected to find it in %s"
                % ('.'.join(map(str, python_version)),
                   module_name,
                   os.path.join(new_python_path, 'python')))
    else:
        exec "import %s as generic_module" % module_name
    return generic_module

def add_package(package_name):
    """
    If package_name has not been installed, we add the appropriate
    path from ../3rd-party/package_name-files

    *After* calling this function you can import the package on your
    own, either from the package the user installed, or from the
    package we distribute.
    """
    try:
        exec "import %s" % package_name
    except ImportError:
        path = os.path.join(third_party, '%s-files' % package_name)
        if os.path.exists(path):
            sys.path.append(path)
        else:
            raise ImportError(
                "Cannot load the package %s; expected to find it in "
                "%s.  Have you run build-pkg.py?"
                % (package_name, path))