Пример #1
0
    def leave_module(self, node):
        """leave module: check globals
        """
        assert len(self._to_consume) == 1
        not_consumed = self._to_consume.pop()[0]
        # attempt to check for __all__ if defined
        if '__all__' in node.locals:
            assigned = next(node.igetattr('__all__'))
            if assigned is not astroid.YES:
                for elt in getattr(assigned, 'elts', ()):
                    try:
                        elt_name = next(elt.infer())
                    except astroid.InferenceError:
                        continue

                    if not isinstance(elt_name, astroid.Const) \
                             or not isinstance(elt_name.value, six.string_types):
                        self.add_message('invalid-all-object',
                                         args=elt.as_string(), node=elt)
                        continue
                    elt_name = elt_name.value
                    # If elt is in not_consumed, remove it from not_consumed
                    if elt_name in not_consumed:
                        del not_consumed[elt_name]
                        continue
                    if elt_name not in node.locals:
                        if not node.package:
                            self.add_message('undefined-all-variable',
                                             args=elt_name,
                                             node=elt)
                        else:
                            basename = os.path.splitext(node.file)[0]
                            if os.path.basename(basename) == '__init__':
                                name = node.name + "." + elt_name
                                try:
                                    modutils.file_from_modpath(name.split("."))
                                except ImportError:
                                    self.add_message('undefined-all-variable',
                                                     args=elt_name,
                                                     node=elt)
                                except SyntaxError:
                                    # don't yield an syntax-error warning,
                                    # because it will be later yielded
                                    # when the file will be checked
                                    pass
        # don't check unused imports in __init__ files
        if not self.config.init_import and node.package:
            return

        self._check_imports(not_consumed)
Пример #2
0
 def test_std_lib(self):
     from os import path
     self.assertEqual(
         os.path.realpath(
             modutils.file_from_modpath(['os',
                                         'path']).replace('.pyc', '.py')),
         os.path.realpath(path.__file__.replace('.pyc', '.py')))
Пример #3
0
def project_from_files(
    files, func_wrapper=_astroid_wrapper, project_name="no name", black_list=("CVS",)
):
    """return a Project from a list of files or modules"""
    # build the project representation
    astroid_manager = manager.AstroidManager()
    project = Project(project_name)
    for something in files:
        if not os.path.exists(something):
            fpath = modutils.file_from_modpath(something.split("."))
        elif os.path.isdir(something):
            fpath = os.path.join(something, "__init__.py")
        else:
            fpath = something
        ast = func_wrapper(astroid_manager.ast_from_file, fpath)
        if ast is None:
            continue
        project.path = project.path or ast.file
        project.add_module(ast)
        base_name = ast.name
        # recurse in package except if __init__ was explicitly given
        if ast.package and something.find("__init__") == -1:
            # recurse on others packages / modules if this is a package
            for fpath in modutils.get_module_files(
                os.path.dirname(ast.file), black_list
            ):
                ast = func_wrapper(astroid_manager.ast_from_file, fpath)
                if ast is None or ast.name == base_name:
                    continue
                project.add_module(ast)
    return project
Пример #4
0
 def project_from_files(self,
                        files,
                        func_wrapper=astroid_wrapper,
                        project_name=None,
                        black_list=None):
     """return a Project from a list of files or modules"""
     # build the project representation
     project_name = project_name or self.config.project
     black_list = black_list or self.config.black_list
     project = Project(project_name)
     for something in files:
         if not exists(something):
             fpath = modutils.file_from_modpath(something.split('.'))
         elif isdir(something):
             fpath = join(something, '__init__.py')
         else:
             fpath = something
         astroid = func_wrapper(self.ast_from_file, fpath)
         if astroid is None:
             continue
         # XXX why is first file defining the project.path ?
         project.path = project.path or astroid.file
         project.add_module(astroid)
         base_name = astroid.name
         # recurse in package except if __init__ was explicitly given
         if astroid.package and something.find('__init__') == -1:
             # recurse on others packages / modules if this is a package
             for fpath in modutils.get_module_files(dirname(astroid.file),
                                                    black_list):
                 astroid = func_wrapper(self.ast_from_file, fpath)
                 if astroid is None or astroid.name == base_name:
                     continue
                 project.add_module(astroid)
     return project
Пример #5
0
 def project_from_files(self, files, func_wrapper=astroid_wrapper,
                        project_name=None, black_list=None):
     """return a Project from a list of files or modules"""
     # build the project representation
     project_name = project_name or self.config.project
     black_list = black_list or self.config.black_list
     project = Project(project_name)
     for something in files:
         if not exists(something):
             fpath = file_from_modpath(something.split('.'))
         elif isdir(something):
             fpath = join(something, '__init__.py')
         else:
             fpath = something
         astroid = func_wrapper(self.ast_from_file, fpath)
         if astroid is None:
             continue
         # XXX why is first file defining the project.path ?
         project.path = project.path or astroid.file
         project.add_module(astroid)
         base_name = astroid.name
         # recurse in package except if __init__ was explicitly given
         if astroid.package and something.find('__init__') == -1:
             # recurse on others packages / modules if this is a package
             for fpath in get_module_files(dirname(astroid.file),
                                           black_list):
                 astroid = func_wrapper(self.ast_from_file, fpath)
                 if astroid is None or astroid.name == base_name:
                     continue
                 project.add_module(astroid)
     return project
Пример #6
0
 def test_std_lib(self):
     path = modutils.file_from_modpath(["os",
                                        "path"]).replace(".pyc", ".py")
     self.assertEqual(
         os.path.realpath(path),
         os.path.realpath(os.path.__file__.replace(".pyc", ".py")),
     )
Пример #7
0
def project_from_files(files, func_wrapper=_astroid_wrapper,
                       project_name="no name",
                       black_list=('CVS',)):
    """return a Project from a list of files or modules"""
    # build the project representation
    astroid_manager = manager.AstroidManager()
    project = Project(project_name)
    for something in files:
        if not os.path.exists(something):
            fpath = modutils.file_from_modpath(something.split('.'))
        elif os.path.isdir(something):
            fpath = os.path.join(something, '__init__.py')
        else:
            fpath = something
        ast = func_wrapper(astroid_manager.ast_from_file, fpath)
        if ast is None:
            continue
        # XXX why is first file defining the project.path ?
        project.path = project.path or ast.file
        project.add_module(ast)
        base_name = ast.name
        # recurse in package except if __init__ was explicitly given
        if ast.package and something.find('__init__') == -1:
            # recurse on others packages / modules if this is a package
            for fpath in modutils.get_module_files(os.path.dirname(ast.file),
                                                   black_list):
                ast = func_wrapper(astroid_manager.ast_from_file, fpath)
                if ast is None or ast.name == base_name:
                    continue
                project.add_module(ast)
    return project
Пример #8
0
 def test_xmlplus(self):
     try:
         # don't fail if pyxml isn't installed
         from xml.dom import ext
     except ImportError:
         pass
     else:
         self.assertEqual(path.realpath(modutils.file_from_modpath(['xml', 'dom', 'ext']).replace('.pyc', '.py')),
                          path.realpath(ext.__file__.replace('.pyc', '.py')))
Пример #9
0
    def load_module(self):
        if os.path.isdir(self.file):
            fpath = os.path.join(self.file, "__init__.py")
        elif os.path.exists(self.file):
            fpath = self.file
        else:
            fpath = modutils.file_from_modpath(self.file.split("."))

        return self._manager.ast_from_file(fpath)
Пример #10
0
def expand_modules(files_or_modules, black_list):
    """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(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 = file_from_modpath(modname.split('.'))
                if filepath is None:
                    errors.append({
                        'key': 'ignored-builtin-module',
                        'mod': modname
                    })
                    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)
        result.append({
            'path': filepath,
            'name': modname,
            'isarg': True,
            'basepath': filepath,
            'basename': modname
        })
        if not (modname.endswith('.__init__') or modname == '__init__') \
                and '__init__.py' in filepath:
            for subfilepath in get_module_files(dirname(filepath), black_list):
                if filepath == subfilepath:
                    continue
                submodname = '.'.join(modpath_from_file(subfilepath))
                result.append({
                    'path': subfilepath,
                    'name': submodname,
                    'isarg': False,
                    'basepath': filepath,
                    'basename': modname
                })
    return result, errors
Пример #11
0
 def file_from_module_name(self, modname, contextfile):
     try:
         value = self._mod_file_cache[(modname, contextfile)]
     except KeyError:
         try:
             value = file_from_modpath(modname.split('.'),
                                       context_file=contextfile)
         except ImportError, ex:
             msg = 'Unable to load module %s (%s)' % (modname, ex)
             value = AstroidBuildingException(msg)
         self._mod_file_cache[(modname, contextfile)] = value
Пример #12
0
 def test_xmlplus(self):
     try:
         # don't fail if pyxml isn't installed
         from xml.dom import ext
     except ImportError:
         pass
     else:
         self.assertEqual(
             os.path.realpath(
                 modutils.file_from_modpath(['xml', 'dom',
                                             'ext']).replace('.pyc',
                                                             '.py')),
             os.path.realpath(ext.__file__.replace('.pyc', '.py')))
def is_standard_module(linter, modname, std_path=None):
    standard = linter._old_is_standard_module(modname, std_path=std_path)
    if standard:
        try:
            filename = modutils.file_from_modpath([modname])
        except ImportError:
            # Import failed, so we can suppose that the module is not standard.
            return False

        if filename is not None and '/site-packages/' in filename:
            return False

    return standard
Пример #14
0
    def _check_imports_order(self, node):
        """Checks imports of module `node` are grouped by category

        Imports must follow this order: standard, 3rd party, local
        """
        extern_imports = []
        local_imports = []
        std_imports = []
        stdlib_paths = [sys.prefix, self.ext_lib_dir]
        real_prefix = getattr(sys, "real_prefix", None)
        if real_prefix is not None:
            stdlib_paths.append(real_prefix)

        for node, modname in self._imports_stack:
            package = modname.split(".")[0]
            if is_standard_module(modname):
                std_imports.append((node, package))
                wrong_import = extern_imports or local_imports
                if not wrong_import:
                    continue
                self.add_message(
                    "wrong-import-order",
                    node=node,
                    args=('standard import "%s"' % node.as_string(), '"%s"' % wrong_import[0][0].as_string()),
                )
            else:
                try:
                    filename = file_from_modpath([package])
                except ImportError:
                    extern_imports.append((node, package))
                    continue
                if not filename:
                    extern_imports.append((node, package))
                    continue
                filename = os.path.normcase(os.path.abspath(filename))
                if not any(filename.startswith(path) for path in stdlib_paths):
                    local_imports.append((node, package))
                    continue
                extern_imports.append((node, package))
                if not local_imports:
                    continue
                self.add_message(
                    "wrong-import-order",
                    node=node,
                    args=('external import "%s"' % node.as_string(), '"%s"' % local_imports[0][0].as_string()),
                )
        return std_imports, extern_imports, local_imports
Пример #15
0
    def _check_imports_order(self, node):
        """Checks imports of module `node` are grouped by category

        Imports must follow this order: standard, 3rd party, local
        """
        extern_imports = []
        local_imports = []
        std_imports = []
        stdlib_paths = [sys.prefix, self.ext_lib_dir]
        real_prefix = getattr(sys, 'real_prefix', None)
        if real_prefix is not None:
            stdlib_paths.append(real_prefix)

        for node, modname in self._imports_stack:
            package = modname.split('.')[0]
            if is_standard_module(modname):
                std_imports.append((node, package))
                wrong_import = extern_imports or local_imports
                if not wrong_import:
                    continue
                self.add_message(
                    'wrong-import-order',
                    node=node,
                    args=('standard import "%s"' % node.as_string(),
                          '"%s"' % wrong_import[0][0].as_string()))
            else:
                try:
                    filename = file_from_modpath([package])
                except ImportError:
                    extern_imports.append((node, package))
                    continue
                if not filename:
                    extern_imports.append((node, package))
                    continue
                filename = os.path.normcase(os.path.abspath(filename))
                if not any(filename.startswith(path) for path in stdlib_paths):
                    local_imports.append((node, package))
                    continue
                extern_imports.append((node, package))
                if not local_imports:
                    continue
                self.add_message(
                    'wrong-import-order',
                    node=node,
                    args=('external import "%s"' % node.as_string(),
                          '"%s"' % local_imports[0][0].as_string()))
        return std_imports, extern_imports, local_imports
Пример #16
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(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 = 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)
        result.append({'path': filepath, 'name': modname, 'isarg': True,
                       'basepath': filepath, 'basename': modname})
        if not (modname.endswith('.__init__') or modname == '__init__') \
                and '__init__.py' in filepath:
            for subfilepath in get_module_files(dirname(filepath), black_list):
                if filepath == subfilepath:
                    continue
                if _basename_in_blacklist_re(basename(subfilepath), black_list_re):
                    continue
                submodname = '.'.join(modpath_from_file(subfilepath))
                result.append({'path': subfilepath, 'name': submodname,
                               'isarg': False,
                               'basepath': filepath, 'basename': modname})
    return result, errors
Пример #17
0
def _get_valid_files_to_check(reporter, module_name):
    """A generator for all valid files to check. Uses a reporter to output
    messages when an input cannot be checked.
    """
    # Allow call to check with empty args
    if module_name == '':
        m = sys.modules['__main__']
        spec = importlib.util.spec_from_file_location(m.__name__, m.__file__)
        module_name = [spec.origin]
    # Enforce API to expect 1 file or directory if type is list
    elif isinstance(module_name, str):
        module_name = [module_name]
    # Otherwise, enforce API to expect `module_name` type as list
    elif not isinstance(module_name, list):
        print(
            'No checks run. Input to check, `{}`, has invalid type, must be a list of strings.'
            .format(module_name))
        return

    # Filter valid files to check
    for item in module_name:
        if not isinstance(item, str):  # Issue errors for invalid types
            print(reporter.filename_to_display(item))
            print(
                'No check run on file `{}`, with invalid type. Must be type: str.\n'
                .format(item))
        elif os.path.isdir(item):
            yield item
        elif not os.path.exists(os.path.expanduser(item)):
            try:
                # For files with dot notation, e.g., `examples.<filename>`
                filepath = modutils.file_from_modpath(item.split('.'))
                if os.path.exists(filepath):
                    yield filepath
                else:
                    print(reporter.filename_to_display(item))
                    print(
                        'Could not find the file called, `{}`\n'.format(item))
            except ImportError:
                print(reporter.filename_to_display(item))
                print('Could not find the file called, `{}`\n'.format(item))
        else:
            yield item  # Check other valid files.
Пример #18
0
    def _check_imports_order(self, node):
        """Checks imports of module `node` are grouped by category

        Imports must follow this order: standard, 3rd party, local
        """
        extern_imports = []
        local_imports = []
        std_imports = []
        for node, modname in self._imports_stack:
            package = modname.split('.')[0]
            if is_standard_module(modname):
                std_imports.append((node, package))
                wrong_import = extern_imports or local_imports
                if not wrong_import:
                    continue
                if self._is_fallback_import(node, wrong_import):
                    continue
                self.add_message('wrong-import-order', node=node,
                                 args=('standard import "%s"' % node.as_string(),
                                       '"%s"' % wrong_import[0][0].as_string()))
            else:
                try:
                    filename = file_from_modpath([package])
                except ImportError:
                    continue
                if not filename:
                    continue

                filename = os.path.normcase(os.path.abspath(filename))
                if not any(filename.startswith(path) for path in self._site_packages):
                    local_imports.append((node, package))
                    continue
                extern_imports.append((node, package))
                if not local_imports:
                    continue
                self.add_message('wrong-import-order', node=node,
                                 args=('external import "%s"' % node.as_string(),
                                       '"%s"' % local_imports[0][0].as_string()))
        return std_imports, extern_imports, local_imports
Пример #19
0
def _get_valid_files_to_check(reporter, module_name):
    """A generator for all valid files to check. Uses a reporter to output
    messages when an input cannot be checked.
    """
    # Allow call to check with empty args
    if module_name == '':
        m = sys.modules['__main__']
        spec = importlib.util.spec_from_file_location(m.__name__, m.__file__)
        module_name = [spec.origin]
    # Enforce API to expect 1 file or directory if type is list
    elif isinstance(module_name, str):
        module_name = [module_name]
    # Otherwise, enforce API to expect `module_name` type as list
    elif not isinstance(module_name, list):
        print('No checks run. Input to check, `{}`, has invalid type, must be a list of strings.'.format(module_name))
        return

    # Filter valid files to check
    for item in module_name:
        if not isinstance(item, str):  # Issue errors for invalid types
            print(reporter.filename_to_display(item))
            print('No check run on file `{}`, with invalid type. Must be type: str.\n'.format(item))
        elif os.path.isdir(item):
            yield item
        elif not os.path.exists(os.path.expanduser(item)):
            try:
                # For files with dot notation, e.g., `examples.<filename>`
                filepath = modutils.file_from_modpath(item.split('.'))
                if os.path.exists(filepath):
                    yield filepath
                else:
                    print(reporter.filename_to_display(item))
                    print('Could not find the file called, `{}`\n'.format(item))
            except ImportError:
                print(reporter.filename_to_display(item))
                print('Could not find the file called, `{}`\n'.format(item))
        else:
            yield item  # Check other valid files.
Пример #20
0
 def test_site_packages(self):
     self.assertEqual(path.realpath(modutils.file_from_modpath(['astroid', 'modutils'])),
                      path.realpath(modutils.__file__.replace('.pyc', '.py')))
 def test_builtin(self):
     self.assertIsNone(modutils.file_from_modpath(['sys']))
 def test_unicode_in_package_init(self):
     # file_from_modpath should not crash when reading an __init__
     # file with unicode characters.
     modutils.file_from_modpath(["data", "unicode_package", "core"])
 def test_site_packages(self):
     filename = _get_file_from_object(modutils)
     result = modutils.file_from_modpath(['astroid', 'modutils'])
     self.assertEqual(os.path.realpath(result), os.path.realpath(filename))
 def test_std_lib(self):
     path = modutils.file_from_modpath(['os', 'path']).replace('.pyc', '.py')
     self.assertEqual(os.path.realpath(path),
                      os.path.realpath(os.path.__file__.replace('.pyc', '.py')))
Пример #25
0
 def test_builtin(self):
     self.assertEqual(modutils.file_from_modpath(['sys']),
                      None)
Пример #26
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
Пример #27
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
Пример #28
0
 def test_site_packages(self):
     filename = _get_file_from_object(modutils)
     result = modutils.file_from_modpath(['astroid', 'modutils'])
     self.assertEqual(os.path.realpath(result), os.path.realpath(filename))
Пример #29
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
Пример #30
0
 def test_builtin(self):
     self.assertEqual(modutils.file_from_modpath(['sys']), None)
Пример #31
0
 def test_unicode_in_package_init(self) -> None:
     # file_from_modpath should not crash when reading an __init__
     # file with unicode characters.
     modutils.file_from_modpath(["data", "unicode_package", "core"])
Пример #32
0
 def test_builtin(self) -> None:
     self.assertIsNone(modutils.file_from_modpath(["sys"]))
Пример #33
0
 def test_site_packages(self) -> None:
     filename = _get_file_from_object(modutils)
     result = modutils.file_from_modpath(["astroid", "modutils"])
     self.assertEqual(os.path.realpath(result), os.path.realpath(filename))
Пример #34
0
 def test_std_lib(self):
     from os import path
     self.assertEqual(path.realpath(modutils.file_from_modpath(['os', 'path']).replace('.pyc', '.py')),
                      path.realpath(path.__file__.replace('.pyc', '.py')))
Пример #35
0
 def test_site_packages(self):
     self.assertEqual(
         os.path.realpath(
             modutils.file_from_modpath(['astroid', 'modutils'])),
         os.path.realpath(modutils.__file__.replace('.pyc', '.py')))
Пример #36
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
Пример #37
0
 def test_builtin(self):
     self.assertIsNone(modutils.file_from_modpath(['sys']))