summaryrefslogtreecommitdiff
path: root/migrate/versioning/util/importpath.py
blob: 529be89c2ff6b92c783d5db1c61ac5673e580f6f (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
import os
import sys

PY33 = sys.version_info >= (3, 3)

if PY33:
    from importlib import machinery
else:
    from six.moves import reload_module as reload


def import_path(fullpath):
    """ Import a file with full path specification. Allows one to
        import from anywhere, something __import__ does not do.
    """
    if PY33:
        name = os.path.splitext(os.path.basename(fullpath))[0]
        return machinery.SourceFileLoader(
            name, fullpath).load_module(name)
    else:
        # http://zephyrfalcon.org/weblog/arch_d7_2002_08_31.html
        path, filename = os.path.split(fullpath)
        filename, ext = os.path.splitext(filename)
        sys.path.append(path)
        try:
            module = __import__(filename)
            reload(module)  # Might be out of date during tests
            return module
        finally:
            del sys.path[-1]