Example #1
0
def test_flake8():
    style_guide = get_style_guide(
        ignore=['D100', 'D104'],
        show_source=True,
    )
    style_guide_tests = get_style_guide(
        ignore=['D100', 'D101', 'D102', 'D103', 'D104', 'D105', 'D107'],
        show_source=True,
    )

    stdout = sys.stdout
    sys.stdout = sys.stderr
    # implicitly calls report_errors()
    report = style_guide.check_files([
        str(Path(__file__).parents[1] / 'colcon_cmake'),
    ])
    report_tests = style_guide_tests.check_files([
        str(Path(__file__).parents[1] / 'test'),
    ])
    sys.stdout = stdout

    total_errors = report.total_errors + report_tests.total_errors
    if total_errors:  # pragma: no cover
        # output summary with per-category counts
        print()
        report._application.formatter.show_statistics(report._stats)
        print('flake8 reported {total_errors} errors'.format_map(locals()),
              file=sys.stderr)

    assert not report.total_errors, \
        'flake8 reported {total_errors} errors'.format_map(locals())
Example #2
0
def test_flake8():
    style_guide = get_style_guide(
        extend_ignore=['D100', 'D104', 'W503'],
        show_source=True,
    )
    style_guide_tests = get_style_guide(
        extend_ignore=['D100', 'D101', 'D102', 'D103', 'D104', 'D105', 'D107'],
        show_source=True,
    )

    report = style_guide.check_files([
        os.path.join(os.path.dirname(__file__), '..', 'rosdoc2'),
    ])
    report_tests = style_guide_tests.check_files([
        os.path.join(os.path.dirname(__file__), '..', 'test'),
    ])

    total_errors = report.total_errors + report_tests.total_errors
    if total_errors:
        # output summary with per-category counts
        print()
        if report.total_errors:
            report._application.formatter.show_statistics(report._stats)
        if report_tests.total_errors:
            report_tests._application.formatter.show_statistics(
                report_tests._stats)
        print('flake8 reported {total_errors} errors'.format_map(locals()),
              file=sys.stderr)

    assert not total_errors, \
        'flake8 reported {total_errors} errors'.format_map(locals())
def get_flake8_errors():
    # chrdir so reported paths are relative to it (and not absolute)
    with chdir(str(REPO_ROOT)):
        output = StringIO()

        with redirect_stdout(output):
            get_style_guide().check_files(['.'])

        return list(filter(None, output.getvalue().split('\n')))
Example #4
0
def test_flake8():
    style_guide = legacy.get_style_guide()
    report = style_guide.check_files(['ksoftapi'])
    statistics = report.get_statistics('E')
    failed = bool(statistics)

    return failed
Example #5
0
def test_js101_eq(eq_file_path):
    style_guide = flake8.get_style_guide(select=['JS101'], )

    p = os.path.abspath(eq_file_path)
    r = style_guide.check_files([p])

    assert 0 == r.total_errors
Example #6
0
def test_js102_multiple_opening(multiple_opening_file_path):
    style_guide = flake8.get_style_guide(select=['JS102'], )

    p = os.path.abspath(multiple_opening_file_path)
    r = style_guide.check_files([p])

    assert 1 == r.total_errors
Example #7
0
def test_flake8():
    test_dir = os.path.dirname(os.path.abspath(inspect.getfile(
        inspect.currentframe())))

    basedir = os.path.dirname(test_dir)

    # Possibility to ignore some files and paths.
    ignore_paths = [
        os.path.join(basedir, "doc"),
        os.path.join(basedir, ".git"),
        os.path.join(basedir, "scripts")]
    files = []

    for dirpath, _, filenames in os.walk(basedir):
        ignore = False
        for path in ignore_paths:
            if dirpath.startswith(path):
                ignore = True
                break
        if ignore:
            continue
        filenames = [_i for _i in filenames if
                     os.path.splitext(_i)[-1] == os.path.extsep + "py"]
        if not filenames:
            continue
        for py_file in filenames:
            full_path = os.path.join(dirpath, py_file)
            files.append(full_path)

    style_guide = flake8.get_style_guide(ignore=['E24', 'W503', 'E226'])
    report = style_guide.check_files(files)
    assert report.get_statistics('E') == [], 'Flake8 found violations'
    assert report.total_errors == 0
Example #8
0
 def test_flake8(self):
     python_filepaths = self._get_python_filepaths(FLAKE8_ROOTS)
     style_guide = get_style_guide(paths=FLAKE8_OPTIONS)
     fake_stdout = io.StringIO()
     with patch('sys.stdout', fake_stdout):
         report = style_guide.check_files(python_filepaths)
     self.assertEqual(report.total_errors, 0, "There are issues!\n" + fake_stdout.getvalue())
Example #9
0
    def run(self):
        from flake8.api.legacy import get_style_guide

        flake8_style = get_style_guide(config_file="setup.cfg")
        paths = self.distribution_files()
        report = flake8_style.check_files(paths)
        raise SystemExit(report.total_errors > 0)
async def test_all_options(tmpdir, test_client, loop, template_engine, session,
                           database, example):
    StartProject(
        path=str(tmpdir),
        name='foobar',
        template_engine=template_engine,
        session=session,
        database=database,
        example=example,
    )
    assert 'app' in {p.basename for p in tmpdir.listdir()}
    style_guide = flake8.get_style_guide()
    report = style_guide.check_files([str(tmpdir)])
    assert report.total_errors == 0
    if database != 'none':
        return
    config = Config(app_path='app/main.py',
                    root_path=str(tmpdir),
                    static_path='.')

    app_factory = config.import_app_factory()
    app = app_factory()
    modify_main_app(app, config)
    cli = await test_client(app)
    r = await cli.get('/')
    assert r.status == 200
    text = await r.text()
    assert '<title>foobar</title>' in text
Example #11
0
def flake8(line, cell):
    """flake8 cell magic"""

    logger.setLevel(logging.INFO)
    with tempfile.NamedTemporaryFile(mode='r+', delete=False) as f:
        # save to file
        f.write(cell)
        # make sure it's written
        f.flush()
        f.close()

    flake = flake8_module.get_style_guide(
        extend_ignore=['W292', 'W391', 'F401', 'F821'])
    # flake_result = flake.check_files([f.name])

    # # split lines
    # stdout = sys.stdout.getvalue().splitlines()
    # for line in stdout:
    # logger.info(line)
    # # sys.stdout = old_stdout

    with io.StringIO() as buf, redirect_stdout(buf):
        _ = flake.check_files([f.name])
        for line in buf.getvalue().splitlines():
            # on windows drive path also contains :
            temp_file, line, col, error = line.split(':')[-4:]
            # add + 1 for line as first line is %%flake8, inc pre py3.6 string
            logger.info('{}:{}:{}'.format(int(line) + 1, col, error))

    # try:
    # os.remove(f.name)
    # except OSError as e:  # if failed, report it back to the user
    # logger.error("Error: %s - %s." % (e.filename, e.strerror))
    return
Example #12
0
    def test_pep8(self):
        # verify all files are nicely styled
        python_filepaths = get_python_filepaths()
        style_guide = get_style_guide()
        fake_stdout = io.StringIO()
        with patch('sys.stdout', fake_stdout):
            report = style_guide.check_files(python_filepaths)

        # if flake8 didnt' report anything, we're done
        if report.total_errors == 0:
            return

        # grab on which files we have issues
        flake8_issues = fake_stdout.getvalue().split('\n')
        broken_filepaths = {
            item.split(':')[0]
            for item in flake8_issues if item
        }

        # give hints to the developer on how files' style could be improved
        options = autopep8.parse_args([''])
        options.aggressive = 1
        options.diff = True
        options.max_line_length = 99

        issues = []
        for filepath in broken_filepaths:
            diff = autopep8.fix_file(filepath, options=options)
            if diff:
                issues.append(diff)

        report = ["Please fix files as suggested by autopep8:"] + issues
        report += ["\n-- Original flake8 reports:"] + flake8_issues
        self.fail("\n".join(report))
Example #13
0
async def test_db_creation(tmpdir, test_client, loop):
    StartProject(
        path=str(tmpdir),
        name='foobar postgres test',
        template_engine=TemplateChoice.JINJA,
        session=SessionChoices.NONE,
        database=DatabaseChoice.PG_SA,
        example=ExampleChoice.MESSAGE_BOARD,
    )
    assert 'app' in {p.basename for p in tmpdir.listdir()}
    style_guide = flake8.get_style_guide()
    report = style_guide.check_files([str(tmpdir)])
    assert report.total_errors == 0
    db_password = os.getenv('APP_DB_PASSWORD', '')
    env = {
        'APP_DB_PASSWORD': db_password,
        'PATH': os.getenv('PATH', ''),
    }
    p = subprocess.run(['make', 'reset-database'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                       cwd=str(tmpdir), env=env, universal_newlines=True)
    assert p.returncode == 0, p.stdout
    assert 'creating database "foobar"...'
    assert 'creating tables from model definition...'

    os.environ['APP_DB_PASSWORD'] = db_password
    config = Config(app_path='app/main.py', root_path=str(tmpdir))

    app = config.app_factory(loop=loop)
    modify_main_app(app, config)
    cli = await test_client(app)
    r = await cli.get('/')
    assert r.status == 200
    text = await r.text()
    assert '<title>foobar postgres test</title>' in text
Example #14
0
    def check_syntax(self):
        """
        From flake8
        """
        packages = settings.PACKAGES_TO_TEST

        # Prepare
        config_options = getattr(yak_settings, 'FLAKE8_CONFIG', {})
        flake8_style = get_style_guide(**config_options)

        # Save to file for later printing instead of printing now
        old_stdout = sys.stdout
        sys.stdout = out = open('syntax_output', 'w+')

        # Run the checkers
        report = flake8_style.check_files(paths=packages)

        sys.stdout = old_stdout

        # Print the final report
        if report.total_errors:
            out.close()
            with open("syntax_output") as f:
                self.fail("{0} Syntax warnings!\n\n{1}".format(
                    report.total_errors, f.read()))
Example #15
0
def flake8_on_files(files, commit):
    """ Runs flake8 on the files to report style guide violations.
    """
    style = get_style_guide(config_file=None, quiet=False)

    # We need to redirect stdout while generating the JSON to avoid spilling
    # messages to the user.
    old_stdout = sys.stdout
    sys.stdout = open("/dev/null", "w")
    review = {}
    for file in filter_files(files, (".py", )):
        report = style.check_files((file, ))
        if report.total_errors:
            if not "comments" in review:
                review["comments"] = defaultdict(list)
            for line_number, offset, code, text, doc in report._deferred_print:
                if not line_part_of_commit(file, line_number, commit): continue
                review["comments"][file].append({
                "path": file,
                "line": line_number,
                "message": "[{0}] {1}".format(code, text)
            })
    if "comments" in review and len(review["comments"]):
        review["message"] = "[FLAKE8] Some issues found."
    else:
        review["message"] = "[FLAKE8] No issues found. OK"
    sys.stdout = old_stdout
    return json.dumps(review)
def test_flake8():
    test_dir = os.path.dirname(
        os.path.abspath(inspect.getfile(inspect.currentframe())))

    basedir = os.path.dirname(test_dir)

    # Possibility to ignore some files and paths.
    ignore_paths = [
        os.path.join(basedir, "doc"),
        os.path.join(basedir, ".git"),
        os.path.join(basedir, "scripts")
    ]
    files = []

    for dirpath, _, filenames in os.walk(basedir):
        ignore = False
        for path in ignore_paths:
            if dirpath.startswith(path):
                ignore = True
                break
        if ignore:
            continue
        filenames = [
            _i for _i in filenames
            if os.path.splitext(_i)[-1] == os.path.extsep + "py"
        ]
        if not filenames:
            continue
        for py_file in filenames:
            full_path = os.path.join(dirpath, py_file)
            files.append(full_path)

    style_guide = flake8.get_style_guide()
    report = style_guide.check_files(files)
    assert report.total_errors == 0
    def __check_for_undefined_names(self):
        """
        Checks the plugin module for undefined names. This catches
        missing imports, references to nonexistent variables, etc.

        ..note::
            We are using the legacy flake8 api, because there is currently
            no public, stable api for flake8 >= 3.0.0

            For more info, see
            https://flake8.pycqa.org/en/latest/user/python-api.html
        """
        warnings = defaultdict(list)
        src_dir = self.__plugin_config_content['srcDir']
        exclude_dir = os.path.sep.join([src_dir, CODEGEN_PACKAGE])
        style_guide = flake8.get_style_guide(select=["F821"],
                                             exclude=[exclude_dir],
                                             quiet=1)
        style_guide.check_files(paths=[src_dir])
        file_checkers = style_guide._application.file_checker_manager.checkers

        for checker in file_checkers:
            for result in checker.results:
                # From the api code, result is a tuple defined as: error =
                # (error_code, line_number, column, text, physical_line)
                if result[0] == 'F821':
                    msg = "{} on line {} in {}".format(result[3], result[1],
                                                       checker.filename)
                    warnings['exception'].append(exceptions.UserError(msg))

        if warnings and len(warnings) > 0:
            raise exceptions.ValidationFailedError(warnings)
def test_flake8():
    test_dir = os.path.dirname(
        os.path.abspath(inspect.getfile(inspect.currentframe())))
    package_dir = os.path.dirname(test_dir)

    # Possibility to ignore some files and paths.
    ignore_paths = [
        os.path.join(package_dir, "doc"),
        os.path.join(package_dir, ".git")
    ]
    files = []
    for dirpath, _, filenames in os.walk(package_dir):
        ignore = False
        for path in ignore_paths:
            if dirpath.startswith(path):
                ignore = True
                break
        if ignore:
            continue
        filenames = [
            _i for _i in filenames
            if os.path.splitext(_i)[-1] == os.path.extsep + "py"
        ]
        if not filenames:
            continue
        for py_file in filenames:
            full_path = os.path.join(dirpath, py_file)
            files.append(full_path)

    # Get the style checker with the default style.
    style_guide = flake8.get_style_guide(ignore=['E24', 'W503', 'E226'])
    report = style_guide.check_files(files)
    assert report.get_statistics('E') == [], 'Flake8 found violations'
    assert report.total_errors == 0
Example #19
0
def test_js102_dict(dict_file_path):
    style_guide = flake8.get_style_guide(select=['JS102'], )

    p = os.path.abspath(dict_file_path)
    r = style_guide.check_files([p])

    assert 1 == r.total_errors
 def test_flake8_conformance(self):
     with CaptureStdout() as stdout:
         flake8_style = flake8.get_style_guide(
             exclude=['*/env/*', '*/node_modules/*', '*/migrations/*'])
         report = flake8_style.check_files([settings.BASE_DIR])
     if report.total_errors > 0:
         self.fail('Got some flake8 errors:\n{0}'.format(stdout), )
Example #21
0
def check_flake8():
    if not HAS_FLAKE8:
        raise Exception('flake8 is required to check code formatting')

    # pyflakes autodetection of PY2 does not work with the future library.
    # Therefore, overwrite the pyflakes autodetection manually
    if PY2:
        import pyflakes.checker  # @UnusedImport
        pyflakes.checker.PY2 = True

    test_dir = os.path.abspath(inspect.getfile(inspect.currentframe()))
    obspy_dir = os.path.dirname(os.path.dirname(os.path.dirname(test_dir)))
    untracked_files = get_untracked_files_from_git() or []
    files = []
    for dirpath, _, filenames in os.walk(obspy_dir):
        filenames = [
            _i for _i in filenames
            if os.path.splitext(_i)[-1] == os.path.extsep + "py"
        ]
        if not filenames:
            continue
        for py_file in filenames:
            py_file = os.path.join(dirpath, py_file)
            # ignore untracked files
            if os.path.abspath(py_file) in untracked_files:
                continue

            # exclude *.py files in obspy/lib
            try:
                tmp_dir, _ = os.path.split(py_file)
                _, tmp_dir = os.path.split(tmp_dir)
                if tmp_dir == "lib":
                    continue
            except:
                pass
            # Check files that do not match any exclusion pattern
            for exclude_pattern in FLAKE8_EXCLUDE_FILES:
                if fnmatch.fnmatch(py_file, exclude_pattern):
                    break
            else:
                files.append(py_file)

    if flake8_version >= LooseVersion('3.0.0'):
        from flake8.api.legacy import get_style_guide
    else:
        from flake8.engine import get_style_guide
    flake8_kwargs = {'parse_argv': False}
    if flake8_version < LooseVersion('2.5.5'):
        import flake8.main
        flake8_kwargs['config_file'] = flake8.main.DEFAULT_CONFIG

    flake8_style = get_style_guide(**flake8_kwargs)
    flake8_style.options.ignore = tuple(
        set(flake8_style.options.ignore).union(set(FLAKE8_IGNORE_CODES)))

    with CatchOutput() as out:
        files = [native_str(f) for f in files]
        report = flake8_style.check_files(files)

    return report, out.stdout
Example #22
0
def test_js102_list_comprehension(list_file_path):
    style_guide = flake8.get_style_guide(select=['JS102'], )

    p = os.path.abspath(list_file_path)
    r = style_guide.check_files([p])

    assert 0 == r.total_errors
Example #23
0
def flake8_on_files(files, commit):
    """ Runs flake8 on the files to report style guide violations.
    """
    style = get_style_guide(config_file=None, quiet=False)

    # We need to redirect stdout while generating the JSON to avoid spilling
    # messages to the user.
    old_stdout = sys.stdout
    sys.stdout = open("/dev/null", "w")
    review = {}
    for file in filter_files(files, (".py", )):
        report = style.check_files((file, ))
        if report.total_errors:
            if not "comments" in review:
                review["comments"] = defaultdict(list)
            for line_number, offset, code, text, doc in report._deferred_print:
                if not line_part_of_commit(file, line_number, commit): continue
                review["comments"][file].append({
                    "path":
                    file,
                    "line":
                    line_number,
                    "message":
                    "[{0}] {1}".format(code, text)
                })
    if "comments" in review and len(review["comments"]):
        review["message"] = "[FLAKE8] Some issues found."
    else:
        review["message"] = "[FLAKE8] No issues found. OK"
    sys.stdout = old_stdout
    return json.dumps(review)
Example #24
0
def test_get_style_guide():
    """Verify the methods called on our internal Application."""
    prelim_opts = argparse.Namespace(
        append_config=[],
        config=None,
        isolated=False,
        output_file=None,
        verbose=0,
    )
    mockedapp = mock.Mock()
    mockedapp.parse_preliminary_options.return_value = (prelim_opts, [])
    mockedapp.program = "flake8"
    with mock.patch(
        "flake8.api.legacy.config.ConfigFileFinder"
    ) as mock_config_finder:  # noqa: E501
        config_finder = ConfigFileFinder(mockedapp.program)
        mock_config_finder.return_value = config_finder

        with mock.patch("flake8.main.application.Application") as application:
            application.return_value = mockedapp
            style_guide = api.get_style_guide()

    application.assert_called_once_with()
    mockedapp.parse_preliminary_options.assert_called_once_with([])
    mockedapp.find_plugins.assert_called_once_with(config_finder)
    mockedapp.register_plugin_options.assert_called_once_with()
    mockedapp.parse_configuration_and_cli.assert_called_once_with(
        config_finder, []
    )
    mockedapp.make_formatter.assert_called_once_with()
    mockedapp.make_guide.assert_called_once_with()
    mockedapp.make_file_checker_manager.assert_called_once_with()
    assert isinstance(style_guide, api.StyleGuide)
Example #25
0
def test_get_style_guide():
    """Verify the methods called on our internal Application."""
    prelim_opts = argparse.Namespace(
        append_config=[],
        config=None,
        isolated=False,
        output_file=None,
        verbose=0,
    )
    mockedapp = mock.Mock()
    mockedapp.parse_preliminary_options_and_args.return_value = (
        prelim_opts,
        [],
    )
    with mock.patch('flake8.main.application.Application') as application:
        application.return_value = mockedapp
        style_guide = api.get_style_guide()

    application.assert_called_once_with()
    mockedapp.parse_preliminary_options_and_args.assert_called_once_with([])
    mockedapp.make_config_finder.assert_called_once_with([], [])
    mockedapp.find_plugins.assert_called_once_with(None, False)
    mockedapp.register_plugin_options.assert_called_once_with()
    mockedapp.parse_configuration_and_cli.assert_called_once_with([])
    mockedapp.make_formatter.assert_called_once_with()
    mockedapp.make_guide.assert_called_once_with()
    mockedapp.make_file_checker_manager.assert_called_once_with()
    assert isinstance(style_guide, api.StyleGuide)
Example #26
0
def flake8_scan_file(commit_sha, owner, repo, parent_sha=None):
    """Runs flake8 scan on all changed files and returns array of warnings"""
    if parent_sha is None:
        parent_sha = get_commit_parent(commit_sha, owner, repo)
    diff_url = GIT_COMPARE_URL.format(base=parent_sha,
                                      head=commit_sha,
                                      owner=owner,
                                      repo=repo,
                                      host=host_api)
    diff_info = get(diff_url, auth=auth).json()
    diff_content = get(diff_url,
                       auth=auth,
                       headers={
                           "Accept": "application/vnd.github.v3.diff"
                       }).content.decode('utf8')
    patch_set = PatchSet(diff_content)
    comments_per_file = {}
    for file in diff_info['files']:
        content = get(file['contents_url'], auth=auth).json()
        file_content = get(content['download_url']).content
        with open("flake8_tmp_file.py", 'wb') as test_file:
            test_file.write(file_content)
        style_guide = flake8.get_style_guide(ignore=['E24', 'W503'])
        style_guide.input_file('./flake8_tmp_file.py', )
        results = style_guide._application.file_checker_manager.checkers[
            0].results
        comments_per_line = {}
        for code, line_n, offset, text, src in results:
            if changed_in_diff(get_file_by_name(patch_set, file['filename']),
                               line_n):
                comments = comments_per_line.get(line_n, [])
                comments.append((file['filename'], line_n, offset, code, text))
                comments_per_line[line_n] = comments
        comments_per_file[file['filename']] = comments_per_line
    return comments_per_file
def test_js102_pound(pound_file_path):
    """Pound signs in strings shouldn't be considered the start of comments."""
    style_guide = flake8.get_style_guide(select=['JS102'], )

    p = os.path.abspath(pound_file_path)
    r = style_guide.check_files([p])

    assert 0 == r.total_errors
Example #28
0
    def test_check(self):
        """Performing checks by flake8

        """
        style_guide = flake8.get_style_guide(max_line_length=120,
                                             ignore=['W504', 'N802'])
        report = style_guide.check_files(self.list_of_files)
        self.assertEqual(report.total_errors, 0)
def test_js102_function_calls_ignored(function_calls_file_path):
    """Function calls should not trigger JS102."""
    style_guide = flake8.get_style_guide(select=['JS102'], )

    p = os.path.abspath(function_calls_file_path)
    r = style_guide.check_files([p])

    assert 0 == r.total_errors
 def use_flake8(self, files, ignore):
     style_guide = flake8.get_style_guide(ignore=ignore)
     output = io.StringIO()
     with redirect_stdout(output):
         style_guide.check_files(files)
     report = output.getvalue().split('\n')
     final_information = ''
     return self._parse_flake8(report), final_information
Example #31
0
def py_lint(file_list, parseable=False):
    from flake8.api.legacy import get_style_guide

    file_list = get_python_files(file_list)
    flake8_style = get_style_guide(parse_argv=True)
    report = flake8_style.check_files(file_list)

    return report.total_errors != 0
Example #32
0
def test_flake8_pytest(mocker):
    python_filepaths = get_python_filepaths(FLAKE8_ROOTS)
    style_guide = get_style_guide(paths=FLAKE8_OPTIONS)
    fake_stdout = io.StringIO()
    mocker.patch('sys.stdout', fake_stdout)
    report = style_guide.check_files(python_filepaths)
    assert report.total_errors == 0, "There are issues!\n" + fake_stdout.getvalue(
    )
Example #33
0
def run_flake8():
    formatted_message("Running flake8...", "yellow")
    from flake8.api import legacy as flake8
    style_guide = flake8.get_style_guide(ignore=['H101','H301','H306','H401',
    'E402','H403','H404','H405']
                                         )
    report = style_guide.check_files(["."])
    assert report.total_errors == 0, '\033[31;1mERROR\033[31;1;1m Flake8 found errors\033[0m' 
Example #34
0
def check_flake8():
    if not HAS_FLAKE8:
        raise Exception('flake8 is required to check code formatting')

    # pyflakes autodetection of PY2 does not work with the future library.
    # Therefore, overwrite the pyflakes autodetection manually
    if PY2:
        import pyflakes.checker  # @UnusedImport
        pyflakes.checker.PY2 = True

    test_dir = os.path.abspath(inspect.getfile(inspect.currentframe()))
    obspy_dir = os.path.dirname(os.path.dirname(os.path.dirname(test_dir)))
    untracked_files = get_untracked_files_from_git() or []
    files = []
    for dirpath, _, filenames in os.walk(obspy_dir):
        filenames = [_i for _i in filenames if
                     os.path.splitext(_i)[-1] == os.path.extsep + "py"]
        if not filenames:
            continue
        for py_file in filenames:
            py_file = os.path.join(dirpath, py_file)
            # ignore untracked files
            if os.path.abspath(py_file) in untracked_files:
                continue

            # exclude *.py files in obspy/lib
            try:
                tmp_dir, _ = os.path.split(py_file)
                _, tmp_dir = os.path.split(tmp_dir)
                if tmp_dir == "lib":
                    continue
            except:
                pass
            # Check files that do not match any exclusion pattern
            for exclude_pattern in FLAKE8_EXCLUDE_FILES:
                if fnmatch.fnmatch(py_file, exclude_pattern):
                    break
            else:
                files.append(py_file)

    if flake8_version >= LooseVersion('3.0.0'):
        from flake8.api.legacy import get_style_guide
    else:
        from flake8.engine import get_style_guide
    flake8_kwargs = {'parse_argv': False}
    if flake8_version < LooseVersion('2.5.5'):
        import flake8.main
        flake8_kwargs['config_file'] = flake8.main.DEFAULT_CONFIG

    flake8_style = get_style_guide(**flake8_kwargs)
    flake8_style.options.ignore = tuple(set(
        flake8_style.options.ignore).union(set(FLAKE8_IGNORE_CODES)))

    with CatchOutput() as out:
        files = [native_str(f) for f in files]
        report = flake8_style.check_files(files)

    return report, out.stdout
Example #35
0
def test_legacy_api(tmpdir):
    """A basic end-to-end test for the legacy api reporting errors."""
    with tmpdir.as_cwd():
        t_py = tmpdir.join('t.py')
        t_py.write('import os  # unused import\n')

        style_guide = legacy.get_style_guide()
        report = style_guide.check_files([t_py.strpath])
        assert report.total_errors == 1
Example #36
0
def assert_conforms_to_style(python_files):
    checker = flake8.get_style_guide(max_complexity=MAX_COMPLEXITY)

    checker.options.jobs = 1
    checker.options.verbose = True
    report = checker.check_files(python_files)

    warnings = report.get_statistics("W")
    errors = report.get_statistics("E")

    assert not (warnings or errors), "\n" + "\n".join(["Warnings:", "\n".join(warnings), "Errors:", "\n".join(errors)])
Example #37
0
def test_cython_files():

    style_guide = flake8.get_style_guide(
        filename=['*.pyx', '*.px'],
        exclude=['doc', '.eggs', '*.egg', 'build', 'setup.py'],
        select=['E', 'W', 'F'],
        ignore=['E225'],
        max_line_length=88
    )

    report = style_guide.check_files()

    assert report.get_statistics('E') == []
    assert report.get_statistics('W') == []
    assert report.get_statistics('F') == []
Example #38
0
def test_regular_files():

    style_guide = flake8.get_style_guide(
        filename=['*.py'],
        exclude=['doc', '.eggs', '*.egg', 'build', 'benchmark.py',
                 'run_all_examples.py'],
        select=['E', 'W', 'F'],
        max_line_length=88
    )

    report = style_guide.check_files()

    assert report.get_statistics('E') == []
    assert report.get_statistics('W') == []
    assert report.get_statistics('F') == []
Example #39
0
def test_get_style_guide():
    """Verify the methods called on our internal Application."""
    mockedapp = mock.Mock()
    with mock.patch('flake8.main.application.Application') as Application:
        Application.return_value = mockedapp
        style_guide = api.get_style_guide()

    Application.assert_called_once_with()
    mockedapp.find_plugins.assert_called_once_with()
    mockedapp.register_plugin_options.assert_called_once_with()
    mockedapp.parse_configuration_and_cli.assert_called_once_with([])
    mockedapp.make_formatter.assert_called_once_with()
    mockedapp.make_notifier.assert_called_once_with()
    mockedapp.make_guide.assert_called_once_with()
    mockedapp.make_file_checker_manager.assert_called_once_with()
    assert isinstance(style_guide, api.StyleGuide)
Example #40
0
def check_flake8(filename):
    style_guide = flake8.get_style_guide()
    report = style_guide.check_files([filename])
    score = 0
    if report.get_statistics('E') == []:
        score += 4
        if report.get_statistics('W') == []:
            score += 3
        else:
            logger.info(report.get_statistics('W'))
        if report.get_statistics('F') == []:
            score += 3
        else:
            logger.info(report.get_statistics('F'))
    else:
        logger.info(report.get_statistics('E'))
    return score
Example #41
0
def test_get_style_guide():
    """Verify the methods called on our internal Application."""
    mockedapp = mock.Mock()
    mockedapp.prelim_opts.verbose = 0
    mockedapp.prelim_opts.output_file = None
    with mock.patch('flake8.main.application.Application') as application:
        application.return_value = mockedapp
        style_guide = api.get_style_guide()

    application.assert_called_once_with()
    mockedapp.parse_preliminary_options_and_args.assert_called_once_with([])
    mockedapp.make_config_finder.assert_called_once_with()
    mockedapp.find_plugins.assert_called_once_with()
    mockedapp.register_plugin_options.assert_called_once_with()
    mockedapp.parse_configuration_and_cli.assert_called_once_with([])
    mockedapp.make_formatter.assert_called_once_with()
    mockedapp.make_guide.assert_called_once_with()
    mockedapp.make_file_checker_manager.assert_called_once_with()
    assert isinstance(style_guide, api.StyleGuide)
Example #42
0
def test_flake8():
    if get_style_guide is None:
        # skip test on Python 2.6 and older
        return

    style_guide = get_style_guide(
        exclude=['conf.py'],
        ignore=[
            'C402',  # ignore presence of unnecessary generators
            'C405',  # ignore presence of unnecessary literals
            'C407',  # ignore presence of unnecessary comprehensions
            'C408',  # ignore presence of unnecessary tuple/list/dict
            'D',  # ignore documentation related warnings
            'F401',  # ignore presence of unused imports
            'F841',  # ignore presence of unused variables
            'I',  # ignore import order related warnings
            'N802',  # ignore presence of upper case in function names
        ],
        max_line_length=200,
        show_source=True,
    )

    stdout = sys.stdout
    sys.stdout = sys.stderr
    # implicitly calls report_errors()
    report = style_guide.check_files([
        os.path.dirname(os.path.dirname(__file__)),
    ])
    sys.stdout = stdout

    if report.total_errors:
        # output summary with per-category counts
        print()
        report._application.formatter.show_statistics(report._stats)
        print(
            'flake8 reported {report.total_errors} errors'
            .format_map(locals()), file=sys.stderr)

    assert not report.total_errors, \
        'flake8 reported {report.total_errors} errors'.format_map(locals())
Example #43
0
    def run(self, apps_locations, **options):
        output = open(os.path.join(options['output_dir'], 'flake8.report'), 'w')

        pep8_options = {}

        config_file = self.get_config_path(options)
        if config_file is not None:
            pep8_options['config_file'] = config_file

        set_option(pep8_options, 'exclude', options['pep8-exclude'], config_file,
                   default=pep8.DEFAULT_EXCLUDE + ",south_migrations", split=',')

        set_option(pep8_options, 'select', options['pep8-select'], config_file, split=',')

        set_option(pep8_options, 'ignore', options['pep8-ignore'], config_file, split=',')

        set_option(pep8_options, 'max_line_length', options['pep8-max-line-length'], config_file,
                   default=pep8.MAX_LINE_LENGTH)

        set_option(pep8_options, 'max_complexity', options['flake8-max-complexity'], config_file,
                   default=-1)

        old_stdout, flake8_output = sys.stdout, StringIO()
        sys.stdout = flake8_output

        pep8style = get_style_guide(
            parse_argv=False,
            jobs='1',
            **pep8_options)

        try:
            for location in apps_locations:
                pep8style.input_file(os.path.relpath(location))
        finally:
            sys.stdout = old_stdout

        flake8_output.seek(0)
        output.write(flake8_output.read())
        output.close()
Example #44
0
def test_flake8():
    if get_style_guide is None:
        # skip test on Python 2.6 and older
        return

    style_guide = get_style_guide(
        exclude=[],
        ignore=[
            'E731',  # ignore assign lambda warning
            'E226',  # ignore whitespace around arithmetic operators
            'E305',  # ignore whitespace before/after functions rule
            'D',  # ignore documentation related warnings
            'I',  # ignore import order related warnings
            'N802',  # ignore presence of upper case in function names
        ],
        max_line_length=100,
        max_complexity=10,
        show_source=True,
    )

    stdout = sys.stdout
    sys.stdout = sys.stderr
    # implicitly calls report_errors()
    report = style_guide.check_files([
        os.path.dirname(os.path.dirname(__file__)),
    ])
    sys.stdout = stdout

    if report.total_errors:
        # output summary with per-category counts
        print()
        report._application.formatter.show_statistics(report._stats)
        print(
            'flake8 reported {report.total_errors} errors'
            .format_map(locals()), file=sys.stderr)

    assert not report.total_errors, \
        'flake8 reported {report.total_errors} errors'.format(**locals())
Example #45
0
def check_style(notebook_code):
    """
    The notebook PEP 8 test ignore:
    W292: blank line at end of file (always present when using nbconvert)
    E226: missing whitespace around arithmetic operator (not enforced by PEP 8)
    E402: module level import not at top of file (for notebooks it is clearer
    to import at the top of the cell of its first use.)

    """
    lines = notebook_code.split('\n')
    notebook_code = '\n'.join(
        [line for line in lines if not line.startswith('get_ipython')]
        )
    fid, fname = mkstemp(suffix='.py', prefix='temp_script_')
    with open(fname, 'w') as f:
        f.write(notebook_code.strip())
    style_guide = flake8.get_style_guide(
        ignore=['W292', 'E226', 'E402'],
        max_line_length=100
        )
    report = style_guide.input_file(fname)
    os.close(fid)
    return report
Example #46
0
def test_flake8():
    # Configure flake8 using the .flake8 file in the root of this repository.
    style_guide = get_style_guide()

    style_guide.options.exclude += ['*/doc/_build']

    stdout = sys.stdout
    sys.stdout = sys.stderr
    # implicitly calls report_errors()
    report = style_guide.check_files([
        os.path.dirname(os.path.dirname(__file__)),
    ])
    sys.stdout = stdout

    if report.total_errors:
        # output summary with per-category counts
        print()
        report._application.formatter.show_statistics(report._stats)
        print(
            'flake8 reported %d errors' % report.total_errors,
            file=sys.stderr)

    assert not report.total_errors, \
        'flake8 reported %d errors' % report.total_errors
Example #47
0
    def check_syntax(self):
        """
        From flake8
        """
        packages = settings.PACKAGES_TO_TEST

        # Prepare
        config_options = getattr(yak_settings, 'FLAKE8_CONFIG', {})
        flake8_style = get_style_guide(**config_options)

        # Save to file for later printing instead of printing now
        old_stdout = sys.stdout
        sys.stdout = out = open('syntax_output', 'w+')

        # Run the checkers
        report = flake8_style.check_files(paths=packages)

        sys.stdout = old_stdout

        # Print the final report
        if report.total_errors:
            out.close()
            with open("syntax_output") as f:
                self.fail("{0} Syntax warnings!\n\n{1}".format(report.total_errors, f.read()))
def flake8_examine(file_location):
    style_guide = f8.get_style_guide()
    result = style_guide.check_files([file_location])
    num_of_errors = result.total_errors
    return num_of_errors
Example #49
0
File: lint.py Project: Bartzi/1327
	def handle(self, *args, **options):
		os.chdir(os.path.join(os.path.join(BASE_DIR, '..')))
		style = flake8.get_style_guide(config_file='.flake8')
		report = style.check_files(['_1327'])
		if report.total_errors > 0:
			sys.exit(1)
Example #50
0
style_guide = flake8.get_style_guide(
    ignore=(
        'E129',  # visually indented line with same indent (in gui.py)
        'E501',  # line too long
        'E126',  # continuation line over-indented for hanging indent
        'E128',  # continuation line under-indented for visual indent
        # 'E221',  # multiple spaces before operator
        # 'E222',  # multiple spaces after operator
        'E722',  # do not use bare except
        'F403',  # 'import *' used; unable to detect undefined names
        'F405',  # ... may be undefined, or defined from star imports: ...
    ),
    report=None,
    exclude=[
        # old files:
        # 'f-engrave-140.py',
        # 'py2exe_setup.py',

        # new files:
        # '__init__.py'
        # 'gui.py',
        # 'job.py',
        # 'settings.py',
        # 'tooltip.py',

        # 'bitmap_settings.py',
        # 'general_settings.py',
        # 'vcarve_settings.py',

        # geometry:
        # 'boundingbox.py'
        # 'coords.py',
        # 'engrave.py'
        # 'font.py',
        # 'linearcfitter.py',
        # 'pathsorter.py',

        # readers:
        # 'bspline.py',
        # 'dxf_class.py',
        # 'elements.py',
        # 'nurbs.py',

        # 'cxf.py',
        # 'font.py',
        # 'image.py'

        # writers:
        # 'dxf.py',
        # 'gcode.py',
        # 'svg.py',

        # util:
        # 'externals.py'
        # 'icon.py'
    ]
)
Example #51
0
def test_flake8():
    # Configure flake8 using the .flake8 file in the root of this repository.
    style = get_style_guide()
    results = style.check_files()
    assert results.total_errors == 0, 'Found code style errors / warnings'