Python imp.find_module() Examples

The following are 30 code examples of imp.find_module(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module imp , or try the search function .
Example #1
Source File: sandbox_test.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def test_load_with_path_hook_cant_find(self):

    class DummyPathHook(object):

      def __init__(self, path):
        if path != 'dummy/path':
          raise ImportError

      def find_module(self, unused_fullname):
        return None

      def load_module(self, fullname):
        raise ImportError

    self.test_policies['distutils.util'] = sandbox.ModuleOverridePolicy(
        None, [], {}, default_pass_through=True)
    sys.path_hooks = [DummyPathHook]
    util = self.hook.load_module('distutils.util')
    self.assertEqual('distutils.util', util.__name__) 
Example #2
Source File: coverage.py    From oscrypto with MIT License 6 votes vote down vote up
def _load_package_tests(name):
    """
    Load the test classes from another modularcrypto package

    :param name:
        A unicode string of the other package name

    :return:
        A list of unittest.TestCase classes of the tests for the package
    """

    package_dir = os.path.join('..', name)
    if not os.path.exists(package_dir):
        return []

    tests_module_info = imp.find_module('tests', [package_dir])
    tests_module = imp.load_module('%s.tests' % name, *tests_module_info)
    return tests_module.test_classes() 
Example #3
Source File: CTPlugin.py    From CapTipper with GNU General Public License v3.0 6 votes vote down vote up
def init_plugins():
    p_files = glob.glob(CTCore.plugins_folder + "*.py")
    for p in p_files:
        p_full = os.path.join(os.path.dirname(os.path.realpath(__file__)),p)
        (path, name) = os.path.split(p_full)
        (name, ext) = os.path.splitext(name)

        (p_file, filename, data) = imp.find_module(name, [path])
        mod = imp.load_module(name, p_file, filename, data)

        for name, value in inspect.getmembers(mod):
            if inspect.isclass(value):
                if issubclass(value, ConsolePlugin) and value is not ConsolePlugin:
                    p_num = len(CTCore.plugins)
                    CTCore.plugins.append(namedtuple('Plugin', ['id', 'name','module', 'description']))
                    CTCore.plugins[p_num].id = p_num
                    CTCore.plugins[p_num].name = name
                    CTCore.plugins[p_num].module = value
                    CTCore.plugins[p_num].description = value.description 
Example #4
Source File: module_dependency.py    From pywren-ibm-cloud with Apache License 2.0 6 votes vote down vote up
def _is_relative_import(module_name, path):
        """Checks if import is relative. Returns True if relative, False if
        absolute, and None if import could not be found."""
        try:
            # Check within the restricted path of a (sub-)package
            imp.find_module(module_name, [path])
        except ImportError:
            pass
        else:
            return True

        try:
            # Check across all of sys.path
            imp.find_module(module_name)
        except ImportError:
            pass
        else:
            return False

        # Module could not be found on system due to:
        # 1. Import that doesn't exist. "Bad import".
        # 2. Since we're only scanning the AST, there's a good chance the
        #    import's inclusion is conditional, and would never be triggered.
        #    For example, an import specific to an OS.
        return None 
Example #5
Source File: _compat.py    From jbox with MIT License 6 votes vote down vote up
def _check_if_pyc(fname):
    """Return True if the extension is .pyc, False if .py
    and None if otherwise"""
    from imp import find_module
    from os.path import realpath, dirname, basename, splitext

    # Normalize the file-path for the find_module()
    filepath = realpath(fname)
    dirpath = dirname(filepath)
    module_name = splitext(basename(filepath))[0]

    # Validate and fetch
    try:
        fileobj, fullpath, (_, _, pytype) = find_module(module_name, [dirpath])
    except ImportError:
        raise IOError("Cannot find config file. "
                      "Path maybe incorrect! : {0}".format(filepath))
    return pytype, fileobj, fullpath 
Example #6
Source File: __init__.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def find_module(self, fullname, path=None):
        logger.debug('Running find_module: {0}...'.format(fullname))
        if '.' in fullname:
            parent, child = fullname.rsplit('.', 1)
            if path is None:
                loader = self.find_module(parent, path)
                mod = loader.load_module(parent)
                path = mod.__path__
            fullname = child

        # Perhaps we should try using the new importlib functionality in Python
        # 3.3: something like this?
        # thing = importlib.machinery.PathFinder.find_module(fullname, path)
        try:
            self.found = imp.find_module(fullname, path)
        except Exception as e:
            logger.debug('Py2Fixer could not find {0}')
            logger.debug('Exception was: {0})'.format(fullname, e))
            return None
        self.kind = self.found[-1][-1]
        if self.kind == imp.PKG_DIRECTORY:
            self.pathname = os.path.join(self.found[1], '__init__.py')
        elif self.kind == imp.PY_SOURCE:
            self.pathname = self.found[1]
        return self 
Example #7
Source File: depends.py    From jbox with MIT License 6 votes vote down vote up
def find_module(module, paths=None):
    """Just like 'imp.find_module()', but with package support"""

    parts = module.split('.')

    while parts:
        part = parts.pop(0)
        f, path, (suffix,mode,kind) = info = imp.find_module(part, paths)

        if kind==PKG_DIRECTORY:
            parts = parts or ['__init__']
            paths = [path]

        elif parts:
            raise ImportError("Can't find %r in %s" % (parts,module))

    return info 
Example #8
Source File: __init__.py    From verge3d-blender-addon with GNU General Public License v3.0 6 votes vote down vote up
def _find_and_load_module(self, name, path=None):
        """
        Finds and loads it. But if there's a . in the name, handles it
        properly.
        """
        bits = name.split('.')
        while len(bits) > 1:
            # Treat the first bit as a package
            packagename = bits.pop(0)
            package = self._find_and_load_module(packagename, path)
            try:
                path = package.__path__
            except AttributeError:
                # This could be e.g. moves.
                flog.debug('Package {0} has no __path__.'.format(package))
                if name in sys.modules:
                    return sys.modules[name]
                flog.debug('What to do here?')

        name = bits[0]
        module_info = imp.find_module(name, path)
        return imp.load_module(name, *module_info) 
Example #9
Source File: __init__.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _find_and_load_module(self, name, path=None):
        """
        Finds and loads it. But if there's a . in the name, handles it
        properly.
        """
        bits = name.split('.')
        while len(bits) > 1:
            # Treat the first bit as a package
            packagename = bits.pop(0)
            package = self._find_and_load_module(packagename, path)
            try:
                path = package.__path__
            except AttributeError:
                # This could be e.g. moves.
                flog.debug('Package {0} has no __path__.'.format(package))
                if name in sys.modules:
                    return sys.modules[name]
                flog.debug('What to do here?')

        name = bits[0]
        module_info = imp.find_module(name, path)
        return imp.load_module(name, *module_info) 
Example #10
Source File: spec.py    From linter-pylama with MIT License 6 votes vote down vote up
def find_module(self, modname, module_parts, processed, submodule_path):
        """Find the given module

        Each finder is responsible for each protocol of finding, as long as
        they all return a ModuleSpec.

        :param str modname: The module which needs to be searched.
        :param list module_parts: It should be a list of strings,
                                  where each part contributes to the module's
                                  namespace.
        :param list processed: What parts from the module parts were processed
                               so far.
        :param list submodule_path: A list of paths where the module
                                    can be looked into.
        :returns: A ModuleSpec, describing how and where the module was found,
                  None, otherwise.
        """ 
Example #11
Source File: dev_appserver_import_hook.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def SetAllowedModule(name):
    """Allow the use of a module based on where it is located.

    Meant to be used by use_library() so that it has a link back into the
    trusted part of the interpreter.

    Args:
      name: Name of the module to allow.
    """
    stream, pathname, description = imp.find_module(name)
    pathname = os.path.normcase(os.path.abspath(pathname))
    if stream:
      stream.close()
      FakeFile.ALLOWED_FILES.add(pathname)
      FakeFile.ALLOWED_FILES.add(os.path.realpath(pathname))
    else:
      assert description[2] == imp.PKG_DIRECTORY
      if pathname.startswith(SITE_PACKAGES):
        FakeFile.ALLOWED_SITE_PACKAGE_DIRS.add(pathname)
        FakeFile.ALLOWED_SITE_PACKAGE_DIRS.add(os.path.realpath(pathname))
      else:
        FakeFile.ALLOWED_DIRS.add(pathname)
        FakeFile.ALLOWED_DIRS.add(os.path.realpath(pathname)) 
Example #12
Source File: check_MySQLdb_module.py    From autopython with GNU General Public License v3.0 6 votes vote down vote up
def check_mysqldb_modules():
# 检查MySQLdb模块是否存在
# check MySQLdb module exists
	try:
		imp.find_module('MySQLdb')
		found = 1
	except ImportError:
		found = 0 
	if found == 0:
		os.system('yum install -y MySQL-python')
		# 如果MySQLdb不存在,则使用yum安装MySQL-python
		# If MySQLdb doesn`t exist, then use Yum to install MySQL-python
	else:
		pass
		# 如果MySLQdb存在,则什么都不用做
		# If MySLQdb exists, there's nothing to do 
Example #13
Source File: sandbox.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def find_module(self, fullname, path=None):
    if fullname in _WHITE_LIST_C_MODULES:
      return None
    if any(regex.match(fullname) for regex in self._enabled_regexes):
      return None
    _, _, submodule_name = fullname.rpartition('.')
    try:
      result = imp.find_module(submodule_name, path)
    except ImportError:
      return None
    f, _, description = result
    _, _, file_type = description
    if isinstance(f, file):
      f.close()
    if file_type == imp.C_EXTENSION:
      return self
    return None 
Example #14
Source File: sandbox.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def find_module(self, fullname, path=None):
    if any(regex.match(fullname) for regex in self._enabled_regexes):
      return None
    _, _, submodule_name = fullname.rpartition('.')
    try:
      f, filename, description = imp.find_module(submodule_name, path)
    except ImportError:
      return None
    if f:
      f.close()
    _, _, file_type = description
    if (file_type in self._EXCLUDED_TYPES or
        stubs.FakeFile.is_file_accessible(filename) or
        (filename.endswith('.pyc') and
         os.path.exists(filename.replace('.pyc', '.py')))):
      return None
    return self 
Example #15
Source File: __init__.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def find_module(self, fullname, path=None):
        logger.debug('Running find_module: {0}...'.format(fullname))
        if '.' in fullname:
            parent, child = fullname.rsplit('.', 1)
            if path is None:
                loader = self.find_module(parent, path)
                mod = loader.load_module(parent)
                path = mod.__path__
            fullname = child

        # Perhaps we should try using the new importlib functionality in Python
        # 3.3: something like this?
        # thing = importlib.machinery.PathFinder.find_module(fullname, path)
        try:
            self.found = imp.find_module(fullname, path)
        except Exception as e:
            logger.debug('Py2Fixer could not find {0}')
            logger.debug('Exception was: {0})'.format(fullname, e))
            return None
        self.kind = self.found[-1][-1]
        if self.kind == imp.PKG_DIRECTORY:
            self.pathname = os.path.join(self.found[1], '__init__.py')
        elif self.kind == imp.PY_SOURCE:
            self.pathname = self.found[1]
        return self 
Example #16
Source File: sandbox_test.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def test_load_with_path_hook(self):

    class DummyPathHook(object):

      def __init__(self, path):
        if path != 'dummy/path':
          raise ImportError

      def find_module(self, unused_fullname):
        return self

      def load_module(self, fullname):
        return imp.new_module('fake name: %s' % fullname)

    self.test_policies['distutils.util'] = sandbox.ModuleOverridePolicy(
        None, [], {}, default_pass_through=True)
    sys.path_hooks = [DummyPathHook]
    util = self.hook.load_module('distutils.util')
    self.assertEqual('fake name: distutils.util', util.__name__) 
Example #17
Source File: __init__.py    From ALF with Apache License 2.0 6 votes vote down vote up
def import_helper():
    from os.path import dirname
    import imp
    possible_libs = ["_alf_grammar.win32",
                     "_alf_grammar.ntoarm",
                     "_alf_grammar.ntox86",
                     "_alf_grammar.linux"]
    found_lib = False
    for i in possible_libs:
        fp = None
        try:
            fp, pathname, description = imp.find_module(i, [dirname(__file__)])
            _mod = imp.load_module("_alf_grammar", fp, pathname, description)
            found_lib = True
            break
        except ImportError:
            pass
        finally:
            if fp:
                fp.close()
    if not found_lib:
        raise ImportError("Failed to load _alf_grammar module")
    return _mod 
Example #18
Source File: sandbox_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def test_c_module_accessible(self):
    imp.find_module('bar', ['foo']).AndReturn((None, 'foo/bar.so',
                                               (None, None, imp.C_EXTENSION)))
    stubs.FakeFile.is_file_accessible('foo/bar.so').AndReturn(True)
    self.mox.ReplayAll()
    self.assertIsNone(self.hook.find_module('foo.bar', ['foo'])) 
Example #19
Source File: sandbox_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def test_module_not_installed(self):
    hook = sandbox.PathOverrideImportHook(['foo'])
    self.assertFalse(hook.find_module('foo'))
    self.assertFalse(hook.extra_accessible_paths)
    self.assertFalse(hook.extra_sys_paths) 
Example #20
Source File: modutils.py    From python-netsurv with MIT License 5 votes vote down vote up
def is_relative(modname, from_file):
    """return true if the given module name is relative to the given
    file name

    :type modname: str
    :param modname: name of the module we are interested in

    :type from_file: str
    :param from_file:
      path of the module from which modname has been imported

    :rtype: bool
    :return:
      true if the module has been imported relatively to `from_file`
    """
    if not os.path.isdir(from_file):
        from_file = os.path.dirname(from_file)
    if from_file in sys.path:
        return False
    try:
        stream, _, _ = imp.find_module(modname.split(".")[0], [from_file])

        # Close the stream to avoid ResourceWarnings.
        if stream:
            stream.close()
        return True
    except ImportError:
        return False


# internal only functions ##################################################### 
Example #21
Source File: sandbox_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    self.mox = mox.Mox()
    self.mox.StubOutWithMock(imp, 'find_module')
    self.mox.StubOutWithMock(stubs.FakeFile, 'is_file_accessible')
    self.hook = sandbox.PathRestrictingImportHook([re.compile(r'lxml(\..*)?$')]) 
Example #22
Source File: sandbox_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def test_module_success(self):
    hook = sandbox.PathOverrideImportHook(['urllib'])
    self.assertEqual(hook, hook.find_module('urllib'))
    del sys.modules['urllib']
    hooked_urllib = hook.load_module('urllib')
    self.assertEqual(hooked_urllib.__file__.replace('.pyc', '.py'),
                     urllib.__file__.replace('.pyc', '.py'))
    self.assertEqual(hooked_urllib.__loader__, hook)
    self.assertNotIn('__path__', hooked_urllib.__dict__)
    self.assertFalse(hook.extra_accessible_paths)
    self.assertFalse(hook.extra_sys_paths) 
Example #23
Source File: sandbox_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def test_package_success_pil_in_sys_path(self):
    hook = sandbox.PathOverrideImportHook(['PIL'])
    self.assertEqual(hook, hook.find_module('PIL'))
    del sys.modules['PIL']
    hooked_pil = hook.load_module('PIL')
    self.assertEqual(hooked_pil.__file__, PIL.__file__)
    self.assertEqual(hooked_pil.__path__, PIL.__path__)
    self.assertEqual(hooked_pil.__loader__, hook)
    self.assertFalse(hook.extra_accessible_paths)
    self.assertEqual([os.path.dirname(self.saved_pil.__file__)],
                     hook.extra_sys_paths) 
Example #24
Source File: sandbox_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def test_find_module_not_builtin(self):
    hook = sandbox.BuiltinImportHook()
    self.assertIsNone(hook.find_module('httplib')) 
Example #25
Source File: sandbox_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def test_disabled_modules(self):
    hook = sandbox.PathOverrideImportHook(['lxml'])
    self.assertFalse(hook.find_module('lxml.foo'))
    self.assertFalse(hook.find_module('numpy'))
    self.assertFalse(hook.find_module('os')) 
Example #26
Source File: sandbox_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def test_not_accessible(self):
    imp.find_module('bar', ['foo']).AndReturn((None, 'foo/bar.py',
                                               (None, None, imp.PY_SOURCE)))
    stubs.FakeFile.is_file_accessible('foo/bar.py').AndReturn(False)
    self.mox.ReplayAll()
    self.assertEqual(self.hook, self.hook.find_module('foo.bar', ['foo'])) 
Example #27
Source File: sandbox_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def test_c_module_not_accessible(self):
    imp.find_module('bar', ['foo']).AndReturn((None, 'foo/bar.so',
                                               (None, None, imp.C_EXTENSION)))
    stubs.FakeFile.is_file_accessible('foo/bar.so').AndReturn(False)
    self.mox.ReplayAll()
    self.assertEqual(self.hook, self.hook.find_module('foo.bar', ['foo'])) 
Example #28
Source File: sandbox_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def test_compiled_python_accessible(self):
    imp.find_module('bar', ['foo']).AndReturn((None, 'foo/bar.pyc',
                                               (None, None, imp.PY_COMPILED)))
    stubs.FakeFile.is_file_accessible('foo/bar.pyc').AndReturn(True)
    self.mox.ReplayAll()
    self.assertIsNone(self.hook.find_module('foo.bar', ['foo'])) 
Example #29
Source File: sandbox_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def test_enabled_c_library(self):
    imp.find_module('lxmla', ['foo']).AndReturn((None, 'lxmla.py',
                                                 (None, None, imp.PY_SOURCE)))
    stubs.FakeFile.is_file_accessible('lxmla.py').AndReturn(False)
    self.mox.ReplayAll()
    self.assertEqual(self.hook, self.hook.find_module('lxmla', ['foo']))
    self.assertIsNone(self.hook.find_module('lxml', None))
    self.assertIsNone(self.hook.find_module('lxml.html', None)) 
Example #30
Source File: plugin_loader.py    From cascade-server with Apache License 2.0 5 votes vote down vote up
def enumerate_plugins(self):
        # Get list of plugins
        if os.path.isdir(self.plugin_path):
            for top_dir_name in os.listdir(self.plugin_path):
                top_path = os.path.join(self.plugin_path, top_dir_name)
                if os.path.isdir(top_path):
                    for sub_file in os.listdir(top_path):
                        if sub_file.endswith('_main.py'):
                            # self.plugins[sub_file.rstrip('_main.py')] = os.path.join(top_path, sub_file)
                            self.plugins[sub_file.rstrip('_main.py')] = imp.find_module(sub_file.rstrip('.py'), [top_path])
        else:
            logger.warning("Couldn't find plugin directory at ./{} (will not load plugins)".format(self.plugin_path))