コード例 #1
0
def load_module(fname):
    if sys.version_info.major > 2:  # python 3+
        fname = os.path.normpath(fname)

        if os.path.isabs(fname) and platform.system() == 'Windows':
            rootdir, m = os.path.splitdrive(fname)
            rootdir = '{}\\'.format(rootdir)
            m = m[1:]
        elif os.path.isabs(fname) and platform.system() != 'Windows':
            rootdir, m = '/', fname[1:]
        else:
            rootdir, m = None, fname

        if fname.endswith('.py'):
            rootdir, m = os.path.split(fname[:-3])
        else:
            # mapping ``my/path/sa`` to ``my.path.sa``
            m = '.'.join(m.split(os.path.sep))

        # well, seems we need to hack sys.path
        if rootdir:
            with cd(rootdir):
                sys.path.append(os.getcwd())
                sa = importlib.import_module(m)
        else:
            sys.path.append(os.getcwd())
            sa = importlib.import_module(m)

        sys.path.pop()

        return sa
    else:  # in case of python 2, only single file is supported
        if os.path.isdir(fname):
            raise RuntimeError(
                "Only single file loading is supported in Python 2")

        class App(object):
            def __init__(self, d):
                self.__dict__ = d

        d = {}
        with open(fname) as f:
            exec(f, d)

        return App(d)
コード例 #2
0
def load_module(fname):
    if sys.version_info.major > 2:  # python 3+
        if fname.endswith('.py'):
            # https://stackoverflow.com/a/67692
            if sys.version_info >= (3, 5):
                spec = importlib.util.spec_from_file_location('ida', fname)
                ida = importlib.util.module_from_spec(spec)
                spec.loader.exec_module(ida)
            else:  # case of python 3.4
                # this import only for python 3.4-
                from importlib.machinery import SourceFileLoader
                ida = SourceFileLoader('ida', fname).load_module()
        else:
            fname = os.path.normpath(fname)
            m = fname[1:] if fname.startswith('/') else fname

            # mapping ``my/path/ida`` to ``my.path.ida``
            m = '.'.join(m.split(os.path.sep))

            # well, seems we need to hack sys.path
            if fname.startswith('/'):
                with cd('/'):
                    sys.path.append(os.getcwd())
                    ida = importlib.import_module(m, )
            else:
                sys.path.append(os.getcwd())
                ida = importlib.import_module(m)

            sys.path.pop()

        return ida
    else:  # in case of python 2, only single file is supported
        if os.path.isdir(fname):
            raise RuntimeError(
                "Only single file loading is supported in Python 2")

        class App(object):
            def __init__(self, d):
                self.__dict__ = d

        d = {}
        with open(fname) as f:
            exec(f, d)

        return App(d)