Example #1
0
 def file_from_module_name(self, modname, contextfile):
     try:
         value = self._mod_file_cache[(modname, contextfile)]
     except KeyError:
         try:
             value = modutils.file_info_from_modpath(modname.split("."), context_file=contextfile)
         except ImportError as ex:
             msg = "Unable to load module %s (%s)" % (modname, ex)
             value = AstroidBuildingException(msg)
         self._mod_file_cache[(modname, contextfile)] = value
     if isinstance(value, AstroidBuildingException):
         raise value
     return value
Example #2
0
 def file_from_module_name(self, modname, contextfile):
     try:
         value = self._mod_file_cache[(modname, contextfile)]
     except KeyError:
         try:
             value = modutils.file_info_from_modpath(
                 modname.split('.'), context_file=contextfile)
         except ImportError as ex:
             msg = 'Unable to load module %s (%s)' % (modname, ex)
             value = AstroidBuildingException(msg)
         self._mod_file_cache[(modname, contextfile)] = value
     if isinstance(value, AstroidBuildingException):
         raise value
     return value
Example #3
0
 def file_from_module_name(self, modname, contextfile):
     # pylint: disable=redefined-variable-type
     try:
         value = self._mod_file_cache[(modname, contextfile)]
     except KeyError:
         try:
             value = modutils.file_info_from_modpath(
                 modname.split('.'), context_file=contextfile)
         except ImportError as ex:
             msg = 'Unable to load module %s (%s)' % (modname, ex)
             value = exceptions.AstroidBuildingException(msg)
         self._mod_file_cache[(modname, contextfile)] = value
     if isinstance(value, exceptions.AstroidBuildingException):
         raise value
     return value
Example #4
0
 def file_from_module_name(self, modname, contextfile):
     try:
         value = self._mod_file_cache[(modname, contextfile)]
     except KeyError:
         try:
             value = modutils.file_info_from_modpath(
                 modname.split('.'), context_file=contextfile)
         except ImportError as ex:
             value = exceptions.AstroidImportError(
                 'Failed to import module {modname} with error:\n{error}.',
                 modname=modname, error=ex)
         self._mod_file_cache[(modname, contextfile)] = value
     if isinstance(value, exceptions.AstroidBuildingError):
         raise value
     return value
Example #5
0
 def file_from_module_name(self, modname, contextfile):
     try:
         value = self._mod_file_cache[(modname, contextfile)]
     except KeyError:
         try:
             value = file_info_from_modpath(modname.split("."),
                                            context_file=contextfile)
         except ImportError as e:
             value = AstroidImportError(
                 "Failed to import module {modname} with error:\n{error}.",
                 modname=modname,
                 # we remove the traceback here to save on memory usage (since these exceptions are cached)
                 error=e.with_traceback(None),
             )
         self._mod_file_cache[(modname, contextfile)] = value
     if isinstance(value, AstroidBuildingError):
         # we remove the traceback here to save on memory usage (since these exceptions are cached)
         raise value.with_traceback(None)
     return value
Example #6
0
def expand_modules(files_or_modules, black_list, black_list_re):
    """take a list of files/modules/packages and return the list of tuple
    (file, module name) which have to be actually checked
    """
    result = []
    errors = []
    for something in files_or_modules:
        if exists(something):
            # this is a file or a directory
            try:
                modname = '.'.join(modutils.modpath_from_file(something))
            except ImportError:
                modname = splitext(basename(something))[0]
            if isdir(something):
                filepath = join(something, '__init__.py')
            else:
                filepath = something
        else:
            # suppose it's a module or package
            modname = something
            try:
                filepath = modutils.file_from_modpath(modname.split('.'))
                if filepath is None:
                    continue
            except (ImportError, SyntaxError) as ex:
                # FIXME p3k : the SyntaxError is a Python bug and should be
                # removed as soon as possible http://bugs.python.org/issue10588
                errors.append({'key': 'fatal', 'mod': modname, 'ex': ex})
                continue

        filepath = normpath(filepath)
        modparts = (modname or something).split('.')

        try:
            spec = modutils.file_info_from_modpath(modparts, path=sys.path)
        except ImportError:
            # Might not be acceptable, don't crash.
            is_namespace = False
            is_directory = isdir(something)
        else:
            is_namespace = spec.type == modutils.ModuleType.PY_NAMESPACE
            is_directory = spec.type == modutils.ModuleType.PKG_DIRECTORY

        if not is_namespace:
            result.append({
                'path': filepath,
                'name': modname,
                'isarg': True,
                'basepath': filepath,
                'basename': modname
            })

        has_init = (
            not (modname.endswith('.__init__') or modname == '__init__')
            and '__init__.py' in filepath)

        if has_init or is_namespace or is_directory:
            for subfilepath in modutils.get_module_files(
                    dirname(filepath), black_list, list_all=is_namespace):
                if filepath == subfilepath:
                    continue
                if _basename_in_blacklist_re(basename(subfilepath),
                                             black_list_re):
                    continue

                modpath = _modpath_from_file(subfilepath, is_namespace)
                submodname = '.'.join(modpath)
                result.append({
                    'path': subfilepath,
                    'name': submodname,
                    'isarg': False,
                    'basepath': filepath,
                    'basename': modname
                })
    return result, errors
Example #7
0
def expand_modules(files_or_modules, black_list, black_list_re):
    """take a list of files/modules/packages and return the list of tuple
    (file, module name) which have to be actually checked
    """
    result = []
    errors = []
    path = sys.path.copy()

    for something in files_or_modules:
        if os.path.basename(something) in black_list:
            continue
        if _basename_in_blacklist_re(os.path.basename(something),
                                     black_list_re):
            continue

        module_path = get_python_path(something)
        additional_search_path = [".", module_path] + path
        if os.path.exists(something):
            # this is a file or a directory
            try:
                modname = ".".join(
                    modutils.modpath_from_file(something,
                                               path=additional_search_path))
            except ImportError:
                modname = os.path.splitext(os.path.basename(something))[0]
            if os.path.isdir(something):
                filepath = os.path.join(something, "__init__.py")
            else:
                filepath = something
        else:
            # suppose it's a module or package
            modname = something
            try:
                filepath = modutils.file_from_modpath(
                    modname.split("."), path=additional_search_path)
                if filepath is None:
                    continue
            except (ImportError, SyntaxError) as ex:
                # The SyntaxError is a Python bug and should be
                # removed once we move away from imp.find_module: https://bugs.python.org/issue10588
                errors.append({"key": "fatal", "mod": modname, "ex": ex})
                continue

        filepath = os.path.normpath(filepath)
        modparts = (modname or something).split(".")

        try:
            spec = modutils.file_info_from_modpath(modparts,
                                                   path=additional_search_path)
        except ImportError:
            # Might not be acceptable, don't crash.
            is_namespace = False
            is_directory = os.path.isdir(something)
        else:
            is_namespace = modutils.is_namespace(spec)
            is_directory = modutils.is_directory(spec)

        if not is_namespace:
            result.append({
                "path": filepath,
                "name": modname,
                "isarg": True,
                "basepath": filepath,
                "basename": modname,
            })

        has_init = (
            not (modname.endswith(".__init__") or modname == "__init__")
            and os.path.basename(filepath) == "__init__.py")
        if has_init or is_namespace or is_directory:
            for subfilepath in modutils.get_module_files(
                    os.path.dirname(filepath), black_list,
                    list_all=is_namespace):
                if filepath == subfilepath:
                    continue
                if _basename_in_blacklist_re(os.path.basename(subfilepath),
                                             black_list_re):
                    continue

                modpath = _modpath_from_file(subfilepath,
                                             is_namespace,
                                             path=additional_search_path)
                submodname = ".".join(modpath)
                result.append({
                    "path": subfilepath,
                    "name": submodname,
                    "isarg": False,
                    "basepath": filepath,
                    "basename": modname,
                })
    return result, errors
Example #8
0
def expand_modules(files_or_modules, black_list, black_list_re):
    """take a list of files/modules/packages and return the list of tuple
    (file, module name) which have to be actually checked
    """
    result = []
    errors = []
    for something in files_or_modules:
        if exists(something):
            # this is a file or a directory
            try:
                modname = '.'.join(modutils.modpath_from_file(something))
            except ImportError:
                modname = splitext(basename(something))[0]
            if isdir(something):
                filepath = join(something, '__init__.py')
            else:
                filepath = something
        else:
            # suppose it's a module or package
            modname = something
            try:
                filepath = modutils.file_from_modpath(modname.split('.'))
                if filepath is None:
                    continue
            except (ImportError, SyntaxError) as ex:
                # FIXME p3k : the SyntaxError is a Python bug and should be
                # removed as soon as possible http://bugs.python.org/issue10588
                errors.append({'key': 'fatal', 'mod': modname, 'ex': ex})
                continue

        filepath = normpath(filepath)
        modparts = (modname or something).split('.')

        try:
            spec = modutils.file_info_from_modpath(modparts, path=sys.path)
        except ImportError:
            # Might not be acceptable, don't crash.
            is_namespace = False
            is_directory = isdir(something)
        else:
            is_namespace = spec.type == modutils.ModuleType.PY_NAMESPACE
            is_directory = spec.type == modutils.ModuleType.PKG_DIRECTORY

        if not is_namespace:
            result.append({'path': filepath, 'name': modname, 'isarg': True,
                           'basepath': filepath, 'basename': modname})

        has_init = (not (modname.endswith('.__init__') or modname == '__init__')
                    and '__init__.py' in filepath)

        if has_init or is_namespace or is_directory:
            for subfilepath in modutils.get_module_files(dirname(filepath), black_list,
                                                         list_all=is_namespace):
                if filepath == subfilepath:
                    continue
                if _basename_in_blacklist_re(basename(subfilepath), black_list_re):
                    continue

                modpath = _modpath_from_file(subfilepath, is_namespace)
                submodname = '.'.join(modpath)
                result.append({'path': subfilepath, 'name': submodname,
                               'isarg': False,
                               'basepath': filepath, 'basename': modname})
    return result, errors
Example #9
0
def expand_modules(files_or_modules, black_list, black_list_re):
    """take a list of files/modules/packages and return the list of tuple
    (file, module name) which have to be actually checked
    """
    result = []
    errors = []
    for something in files_or_modules:
        if basename(something) in black_list:
            continue
        if _basename_in_blacklist_re(basename(something), black_list_re):
            continue
        if exists(something):
            # this is a file or a directory
            try:
                modname = ".".join(modutils.modpath_from_file(something))
            except ImportError:
                modname = splitext(basename(something))[0]
            if isdir(something):
                filepath = join(something, "__init__.py")
            else:
                filepath = something
        else:
            # suppose it's a module or package
            modname = something
            try:
                filepath = modutils.file_from_modpath(modname.split("."))
                if filepath is None:
                    continue
            except (ImportError, SyntaxError) as ex:
                # FIXME p3k : the SyntaxError is a Python bug and should be
                # removed as soon as possible http://bugs.python.org/issue10588
                errors.append({"key": "fatal", "mod": modname, "ex": ex})
                continue

        filepath = normpath(filepath)
        modparts = (modname or something).split(".")

        try:
            spec = modutils.file_info_from_modpath(modparts, path=sys.path)
        except ImportError:
            # Might not be acceptable, don't crash.
            is_namespace = False
            is_directory = isdir(something)
        else:
            is_namespace = modutils.is_namespace(spec)
            is_directory = modutils.is_directory(spec)

        if not is_namespace:
            result.append(
                {
                    "path": filepath,
                    "name": modname,
                    "isarg": True,
                    "basepath": filepath,
                    "basename": modname,
                }
            )

        has_init = (
            not (modname.endswith(".__init__") or modname == "__init__")
            and basename(filepath) == "__init__.py"
        )

        if has_init or is_namespace or is_directory:
            for subfilepath in modutils.get_module_files(
                dirname(filepath), black_list, list_all=is_namespace
            ):
                if filepath == subfilepath:
                    continue
                if _basename_in_blacklist_re(basename(subfilepath), black_list_re):
                    continue

                modpath = _modpath_from_file(subfilepath, is_namespace)
                submodname = ".".join(modpath)
                result.append(
                    {
                        "path": subfilepath,
                        "name": submodname,
                        "isarg": False,
                        "basepath": filepath,
                        "basename": modname,
                    }
                )
    return result, errors