コード例 #1
0
ファイル: testing.py プロジェクト: ghisvail/imageio
def _test_style(filename, ignore):
    """ Test style for a certain file.
    """
    if isinstance(ignore, (list, tuple)):
        ignore = ','.join(ignore)
    
    orig_dir = os.getcwd()
    orig_argv = sys.argv
    
    os.chdir(ROOT_DIR)
    sys.argv[1:] = [filename]
    sys.argv.append('--ignore=' + ignore)
    nerrors = 1
    try:
        import flake8  # noqa
        from flake8.main.application import Application
        app = Application()
        app.run()
        nerrors = app.result_count
        app.exit()
    except SystemExit as ex:
        return nerrors
    finally:
        os.chdir(orig_dir)
        sys.argv[:] = orig_argv
コード例 #2
0
ファイル: test.py プロジェクト: zoofIO/flexx
def test_style(rel_path='.'):
    # Ensure we have flake8
    try:
        import flake8  # noqa
        from flake8.main.application import Application
    except ImportError as err:
        sys.exit('Cannot do style test: ' + str(err))
    # Prepare
    os.chdir(ROOT_DIR)
    if rel_path in ('', '.'):
        sys.argv[1:] = ['flexx', 'flexxamples']
    else:
        sys.argv[1:] = ['flexx/' + rel_path]
    # Do test
    print('Running flake8 tests ...')
    app = Application()
    app.run()
    # Report
    nerrors = app.result_count
    if nerrors:
        print('Arg! Found %i style errors.' % nerrors)
    else:
        print('Hooray, no style errors found!')
    # Exit (will exit(1) if errors)
    app.exit()
コード例 #3
0
def check_flake8_passes():
    output = io.StringIO()
    with contextlib.redirect_stdout(output), contextlib.redirect_stderr(
            output):
        app = Application()
        app.run(["ice_validator"])
    output.seek(0)
    lines = [f"   {l}" for l in output.readlines()]
    return ["flake8 errors detected:"] + lines if lines else []
コード例 #4
0
ファイル: magic_marker.py プロジェクト: rcbops/magic-marker
 def find_options(self, config):
     """Use flake8's library to find a valid config for flake8"""
     flk8 = Application()
     args = []
     if config:
         args.append("--config={}".format(config))
     flk8.initialize(args)
     opts = {}
     for key, value in list(vars(flk8.options).items()):
         if re.match(r'pytest_mark.*', key):
             for option in value:
                 try:
                     opts[key]
                 except KeyError:
                     opts[key] = {}
                 val = option.split('=')
                 opts[key][val[0]] = val[1]
     self.options = opts
コード例 #5
0
ファイル: testing.py プロジェクト: fanjinfei/imageio
def _test_style(filename, ignore):
    """ Test style for a certain file.
    """
    if isinstance(ignore, (list, tuple)):
        ignore = ','.join(ignore)

    orig_dir = os.getcwd()
    orig_argv = sys.argv

    os.chdir(ROOT_DIR)
    sys.argv[1:] = [filename]
    sys.argv.append('--ignore=' + ignore)
    nerrors = 1
    try:
        import flake8  # noqa
        from flake8.main.application import Application
        app = Application()
        app.run()
        nerrors = app.result_count
        app.exit()
    except SystemExit as ex:
        return nerrors
    finally:
        os.chdir(orig_dir)
        sys.argv[:] = orig_argv
コード例 #6
0
def _test_style(filename, ignore):
    """ Test style for a certain file.
    """
    if isinstance(ignore, (list, tuple)):
        ignore = ','.join(ignore)

    orig_dir = os.getcwd()
    orig_argv = sys.argv

    os.chdir(ROOT_DIR)
    sys.argv[1:] = [filename]
    sys.argv.append('--ignore=' + ignore)
    try:
        import flake8  # noqa
        from flake8.main.application import Application
        app = Application()
        app.run()
        app.exit()
    except SystemExit as ex:
        if ex.code in (None, 0):
            return False
        else:
            return True
    finally:
        os.chdir(orig_dir)
        sys.argv[:] = orig_argv
コード例 #7
0
ファイル: test.py プロジェクト: zxjlm/flexx
def test_style(rel_path='.'):
    # Ensure we have flake8
    try:
        import flake8  # noqa
        from flake8.main.application import Application
    except ImportError as err:
        sys.exit('Cannot do style test: ' + str(err))
    # Prepare
    os.chdir(ROOT_DIR)
    if rel_path in ('', '.'):
        sys.argv[1:] = ['flexx', 'flexxamples']
    else:
        sys.argv[1:] = ['flexx/' + rel_path]
    # Do test
    print('Running flake8 tests ...')
    app = Application()
    app.run()
    # Report
    nerrors = app.result_count
    if nerrors:
        print('Arg! Found %i style errors.' % nerrors)
    else:
        print('Hooray, no style errors found!')
    # Exit (will exit(1) if errors)
    app.exit()
コード例 #8
0
ファイル: tabwidget.py プロジェクト: mgbin088/pyminer-1
 def run(self):
     """
     代码检测工作函数
     """
     while 1:
         if not self._running:
             logger.info('code checker quit')
             break
         if not self.background_checking:
             QThread.msleep(500)
             continue
         if self._queue.qsize() == 0:
             QThread.msleep(500)
             continue
         try:
             widget, code = self._queue.get(False, 0.5)
             # 创建临时文件
             file = QTemporaryFile(self)
             file.setAutoRemove(True)
             if file.open():
                 with open(file.fileName(), 'wb') as fp:
                     fp.write(code.encode())
                 file.close()
                 # 使用flake8检测代码
                 results = []
                 with StringIO() as out, redirect_stdout(out):
                     app = Application()
                     app.initialize(
                         ['flake8', '--exit-zero', '--config',
                          os.path.join(os.path.dirname(__file__), 'config', '.flake8')])
                     app.run_checks([file.fileName()])
                     app.report()
                     results = out.getvalue().split('\n')
                 results = [ret for ret in results if re.search(r'\d+:\d+:[EFW]\d+:.*?', ret)]
                 # if results:
                 self.checked.emit(widget, results)  # 如果为空,也应该这样做。将空列表传入可以清空所有的标记。
             file.deleteLater()
             del file
         except Exception as e:
             logger.warning(str(e))
コード例 #9
0
ファイル: flake8.py プロジェクト: autofix-bot/gecko-dev
def lint(paths, config, **lintargs):
    from flake8.main.application import Application

    log = lintargs['log']
    root = lintargs['root']
    config_path = os.path.join(root, '.flake8')

    if lintargs.get('fix'):
        fix_cmd = [
            os.path.join(bindir, 'autopep8'),
            '--global-config',
            config_path,
            '--in-place',
            '--recursive',
        ]

        if config.get('exclude'):
            fix_cmd.extend(['--exclude', ','.join(config['exclude'])])

        subprocess.call(fix_cmd + paths)

    # Run flake8.
    app = Application()
    log.debug("flake8 version={}".format(app.version))

    output_file = mozfile.NamedTemporaryFile(mode='r')
    flake8_cmd = [
        '--config',
        config_path,
        '--output-file',
        output_file.name,
        '--format',
        '{"path":"%(path)s","lineno":%(row)s,'
        '"column":%(col)s,"rule":"%(code)s","message":"%(text)s"}',
        '--filename',
        ','.join(['*.{}'.format(e) for e in config['extensions']]),
    ]
    log.debug("Command: {}".format(' '.join(flake8_cmd)))

    orig_make_file_checker_manager = app.make_file_checker_manager

    def wrap_make_file_checker_manager(self):
        """Flake8 is very inefficient when it comes to applying exclusion
        rules, using `expand_exclusions` to turn directories into a list of
        relevant python files is an order of magnitude faster.

        Hooking into flake8 here also gives us a convenient place to merge the
        `exclude` rules specified in the root .flake8 with the ones added by
        tools/lint/mach_commands.py.
        """
        # Ignore exclude rules if `--no-filter` was passed in.
        config.setdefault('exclude', [])
        if lintargs.get('use_filters', True):
            config['exclude'].extend(self.options.exclude)

        # Since we use the root .flake8 file to store exclusions, we haven't
        # properly filtered the paths through mozlint's `filterpaths` function
        # yet. This mimics that though there could be other edge cases that are
        # different. Maybe we should call `filterpaths` directly, though for
        # now that doesn't appear to be necessary.
        filtered = [
            p for p in paths
            if not any(p.startswith(e) for e in config['exclude'])
        ]

        self.args = self.args + list(expand_exclusions(filtered, config, root))

        if not self.args:
            raise NothingToLint
        return orig_make_file_checker_manager()

    app.make_file_checker_manager = wrap_make_file_checker_manager.__get__(
        app, Application)

    # Make sure to run from repository root so exclusions are joined to the
    # repository root and not the current working directory.
    oldcwd = os.getcwd()
    os.chdir(root)
    try:
        app.run(flake8_cmd)
    except NothingToLint:
        pass
    finally:
        os.chdir(oldcwd)

    results = []

    def process_line(line):
        # Escape slashes otherwise JSON conversion will not work
        line = line.replace('\\', '\\\\')
        try:
            res = json.loads(line)
        except ValueError:
            print('Non JSON output from linter, will not be processed: {}'.
                  format(line))
            return

        if res.get('code') in LINE_OFFSETS:
            res['lineoffset'] = LINE_OFFSETS[res['code']]

        results.append(result.from_config(config, **res))

    list(map(process_line, output_file.readlines()))
    return results
コード例 #10
0
ファイル: flake8.py プロジェクト: SimonAayani/gecko-dev
def lint(paths, config, **lintargs):
    from flake8.main.application import Application

    root = lintargs['root']
    config_path = os.path.join(root, '.flake8')

    if lintargs.get('fix'):
        fix_cmd = [
            os.path.join(bindir, 'autopep8'),
            '--global-config',
            config_path,
            '--in-place',
            '--recursive',
        ]

        if config.get('exclude'):
            fix_cmd.extend(['--exclude', ','.join(config['exclude'])])

        subprocess.call(fix_cmd + paths)

    # Run flake8.
    app = Application()

    output_file = mozfile.NamedTemporaryFile(mode='r')
    flake8_cmd = [
        '--config',
        config_path,
        '--output-file',
        output_file.name,
        '--format',
        '{"path":"%(path)s","lineno":%(row)s,'
        '"column":%(col)s,"rule":"%(code)s","message":"%(text)s"}',
        '--filename',
        ','.join(['*.{}'.format(e) for e in config['extensions']]),
    ]

    orig_make_file_checker_manager = app.make_file_checker_manager

    def wrap_make_file_checker_manager(self):
        """Flake8 is very inefficient when it comes to applying exclusion
        rules, using `expand_exclusions` to turn directories into a list of
        relevant python files is an order of magnitude faster.

        Hooking into flake8 here also gives us a convenient place to merge the
        `exclude` rules specified in the root .flake8 with the ones added by
        tools/lint/mach_commands.py.
        """
        config.setdefault('exclude', []).extend(self.options.exclude)
        self.options.exclude = None
        self.args = self.args + list(expand_exclusions(paths, config, root))

        if not self.args:
            raise NothingToLint
        return orig_make_file_checker_manager()

    app.make_file_checker_manager = wrap_make_file_checker_manager.__get__(
        app, Application)

    # Make sure to run from repository root so exclusions are joined to the
    # repository root and not the current working directory.
    oldcwd = os.getcwd()
    os.chdir(root)
    try:
        app.run(flake8_cmd)
    except NothingToLint:
        pass
    finally:
        os.chdir(oldcwd)

    results = []

    def process_line(line):
        # Escape slashes otherwise JSON conversion will not work
        line = line.replace('\\', '\\\\')
        try:
            res = json.loads(line)
        except ValueError:
            print('Non JSON output from linter, will not be processed: {}'.
                  format(line))
            return

        if res.get('code') in LINE_OFFSETS:
            res['lineoffset'] = LINE_OFFSETS[res['code']]

        results.append(result.from_config(config, **res))

    list(map(process_line, output_file.readlines()))
    return results
コード例 #11
0
ファイル: test_checker.py プロジェクト: vpodk/flake8-author
def make_linter(code, path='example.py', argv=None):
    app = Application()
    app.initialize(argv)
    Checker.parse_options(app.options)
    tree = ast.parse(code, path)
    return Checker(tree, path)
コード例 #12
0
from flake8.main.application import Application
import pytest

if __name__ == "__main__":
    # Lint code
    flake8 = Application()
    flake8.run([
        'assignment', '--max-line-length', '100', '--banned-modules',
        'skimage.exposure = The scikit-image exposure module is not allowed for this assignment'
    ])
    if flake8.result_count > 0:
        print('-- flake8 found code style issues --')
        flake8.exit()

    # Run tests
    pytest.main([
        '-v', '--basetemp', 'processing_results', '-W',
        'ignore::DeprecationWarning'
    ])
コード例 #13
0
ファイル: test_style.py プロジェクト: kevinpowell/balancer
def test_flake8(dir):
    src_dir = os.path.abspath(os.path.join(__file__, '..', '..', dir))
    app = Application()
    app.run([src_dir])
    assert app.result_count == 0
コード例 #14
0
ファイル: test_checker.py プロジェクト: jparise/flake8-author
def make_linter(code, path='example.py', argv=None):
    app = Application()
    app.initialize(argv)
    Checker.parse_options(app.options)
    tree = ast.parse(code, path)
    return Checker(tree, path)
コード例 #15
0
ファイル: test_flake8.py プロジェクト: stagg54/vcstool
def get_style_guide(argv=None):
    # this is a fork of flake8.api.legacy.get_style_guide
    # to allow passing command line argument
    application = Application()
    if hasattr(application, 'parse_preliminary_options'):
        prelim_opts, remaining_args = application.parse_preliminary_options(
            argv)
        from flake8 import configure_logging
        configure_logging(prelim_opts.verbose, prelim_opts.output_file)
        from flake8.options import config
        config_finder = config.ConfigFileFinder(
            application.program,
            prelim_opts.append_config,
            config_file=prelim_opts.config,
            ignore_config_files=prelim_opts.isolated)
        application.find_plugins(config_finder)
        application.register_plugin_options()
        application.parse_configuration_and_cli(config_finder, remaining_args)
    else:
        application.parse_preliminary_options_and_args([])
        application.make_config_finder()
        application.find_plugins()
        application.register_plugin_options()
        application.parse_configuration_and_cli(argv)
    application.make_formatter()
    application.make_guide()
    application.make_file_checker_manager()
    return StyleGuide(application)
コード例 #16
0
ファイル: test_source.py プロジェクト: hroncok/breezy
    def test_flake8(self):
        self.requireFeature(features.flake8)
        # Older versions of flake8 don't support the 'paths'
        # variable
        new_path = list(sys.path)
        new_path.insert(
            0, os.path.join(os.path.dirname(__file__), '..', '..', 'tools'))
        self.overrideAttr(sys, 'path', new_path)
        from flake8.main.application import Application
        from flake8.formatting.base import BaseFormatter
        app = Application()
        app.config = u'setup.cfg'
        app.jobs = 1

        class Formatter(BaseFormatter):
            def __init__(self):
                self.errors = []

            def start(self):
                pass

            def stop(self):
                app.file_checker_manager.report()

            def handle(self, error):
                self.errors.append(error)

        app.formatter = Formatter()
        app.initialize([])
        app.run_checks()
        app.report()
        self.assertEqual(app.formatter.errors, [])
コード例 #17
0
def get_style_guide(argv=None):
    # this is a fork of flake8.api.legacy.get_style_guide
    # to allow passing command line argument
    application = Application()
    application.parse_preliminary_options_and_args(argv)
    configure_logging(
        application.prelim_opts.verbose, application.prelim_opts.output_file)
    application.make_config_finder()
    application.find_plugins()
    application.register_plugin_options()
    application.parse_configuration_and_cli(argv)
    application.make_formatter()
    application.make_guide()
    application.make_file_checker_manager()
    return StyleGuide(application)
コード例 #18
0
def get_style_guide(argv=None):
    # this is a fork of flake8.api.legacy.get_style_guide
    # to allow passing command line argument
    application = Application()
    application.parse_preliminary_options_and_args(argv)
    application.make_config_finder()
    application.find_plugins()
    application.register_plugin_options()
    application.parse_configuration_and_cli(argv)
    application.make_formatter()
    application.make_notifier()
    application.make_guide()
    application.make_file_checker_manager()
    return StyleGuide(application)
コード例 #19
0
import sys

from flake8.main.application import Application
from importlinter.cli import lint_imports, EXIT_STATUS_SUCCESS
import pytest

if __name__ == "__main__":
    # Lint code
    flake8 = Application()
    flake8.run([
        'assignment',
        '--max-line-length', '100',
    ])

    if flake8.result_count > 0:
        print('-- flake8 found code style issues --')
        flake8.exit()

    if (exit_code := lint_imports()) != EXIT_STATUS_SUCCESS:
        print('-- import-linter found invalid packages --')
        sys.exit(exit_code)

    # Run tests
    pytest.main(['-v', '--basetemp', 'processing_results', '-W', 'ignore::DeprecationWarning'])
コード例 #20
0
ファイル: test_flake8.py プロジェクト: ruffsl/ros_buildfarm
def get_style_guide(argv=None):
    # this is a fork of flake8.api.legacy.get_style_guide
    # to allow passing command line argument
    application = Application()
    application.find_plugins()
    application.register_plugin_options()
    application.parse_configuration_and_cli(argv)
    application.make_formatter()
    application.make_notifier()
    application.make_guide()
    application.make_file_checker_manager()
    return StyleGuide(application)